├── .gitattributes ├── .gitignore ├── Original.md ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pom.xml ├── settings.gradle └── src ├── main ├── c │ ├── 10055.c │ ├── 10055.cc │ ├── 10055.java │ ├── 10055.pascal │ ├── 8-queens.c │ ├── Makefile │ ├── README │ ├── annealing.c │ ├── annealing.h │ ├── backtrack.c │ ├── backtrack.h │ ├── bfs-demo.c │ ├── bfs-dfs.c │ ├── biconnected.c │ ├── bignum.c │ ├── binomial.c │ ├── bipartite.c │ ├── bool.h │ ├── cgtest.c │ ├── component-graphs │ │ ├── ANSWERS │ │ ├── convert.m │ │ ├── g-1 │ │ ├── g-2 │ │ ├── g-3 │ │ ├── g-4 │ │ ├── g-5 │ │ ├── run │ │ └── strong-clr │ ├── connected.c │ ├── convex-hull.c │ ├── datafiles │ │ ├── 10055-in │ │ ├── 10055-out │ │ ├── 8-queens-out │ │ ├── VDError-uniq.dat │ │ ├── VDError-uniq.dat-out │ │ ├── VDError.dat │ │ ├── art3 │ │ ├── baase │ │ ├── biconnected-art3 │ │ ├── biconnected-baase │ │ ├── biconnected-clr-graph │ │ ├── biconnected-grid │ │ ├── biconnected-tree │ │ ├── bignum-in │ │ ├── bignum-in.v1 │ │ ├── bignum-out │ │ ├── bipartite-art3 │ │ ├── bipartite-grid │ │ ├── bipartite-tree │ │ ├── cgtest-in │ │ ├── cgtest-out │ │ ├── chin │ │ ├── chin-out │ │ ├── chin1 │ │ ├── chin1-out │ │ ├── clr-graph │ │ ├── connected-in │ │ ├── connected-out │ │ ├── convex-bad.10 │ │ ├── convex-bad.10-out │ │ ├── distance-out │ │ ├── editbrute-in │ │ ├── editbrute-out │ │ ├── elevator-in │ │ ├── elevator-out │ │ ├── fib-out │ │ ├── g-1 │ │ ├── g-2 │ │ ├── g-3 │ │ ├── g-4 │ │ ├── g-5 │ │ ├── gcd-in │ │ ├── gcd-out │ │ ├── geotest-in │ │ ├── geotest-out │ │ ├── grid │ │ ├── grid-bfs-demo-out │ │ ├── grid-connected-out │ │ ├── grid-dfs-demo-out │ │ ├── grid-topsort-out │ │ ├── grid-topsort1-out │ │ ├── grid4 │ │ ├── gridw │ │ ├── i.10 │ │ ├── i.10-out │ │ ├── i.19 │ │ ├── i.19-out │ │ ├── i.2 │ │ ├── i.2-out │ │ ├── i.4 │ │ ├── i.4-out │ │ ├── i.9 │ │ ├── i.9-out │ │ ├── lcs-out │ │ ├── list-in │ │ ├── list-out │ │ ├── matrix-data1 │ │ ├── matrix-data1-out │ │ ├── matrix-data2 │ │ ├── matrix-data2-out │ │ ├── name-in │ │ ├── name-out │ │ ├── netflow1-in │ │ ├── netflow1-out │ │ ├── netflow2-in │ │ ├── netflow2-out │ │ ├── netflow3-in │ │ ├── netflow3-out │ │ ├── order-out │ │ ├── outgraph.m │ │ ├── partition-data1 │ │ ├── partition-data1-out │ │ ├── partition-data2 │ │ ├── partition-data2-out │ │ ├── paths-graph │ │ ├── paths-graph-out │ │ ├── permutations-out │ │ ├── plates1-in │ │ ├── plates1-out │ │ ├── plates2-in │ │ ├── plates2-out │ │ ├── plates3-in │ │ ├── plates3-out │ │ ├── polly-in │ │ ├── polly-out │ │ ├── primes-in │ │ ├── primes-out │ │ ├── puzzle │ │ ├── puzzle-out │ │ ├── sorting-out │ │ ├── spbug-in │ │ ├── stringedit-in │ │ ├── stringedit-out │ │ ├── strong-clr │ │ ├── strong-clr-out │ │ ├── strong-g-1 │ │ ├── strong-g-2 │ │ ├── strong-g-3 │ │ ├── strong-g-4 │ │ ├── strong-g-5 │ │ ├── subsets-out │ │ ├── substringedit-in │ │ ├── substringedit-out │ │ ├── super.m │ │ ├── superin1 │ │ ├── superin1-out │ │ ├── superin2 │ │ ├── superin2-out │ │ ├── superin2~ │ │ ├── superin3 │ │ ├── superin3-out │ │ ├── superin3~ │ │ ├── superin4 │ │ ├── superin4-out │ │ ├── t.in │ │ ├── t1.in │ │ ├── t1.out │ │ ├── test │ │ ├── tree │ │ ├── tree2 │ │ ├── tree3 │ │ ├── treelist-out │ │ ├── treeplus │ │ ├── tri1 │ │ ├── tri1-out │ │ ├── tri2 │ │ ├── tri2-out │ │ ├── tri3 │ │ ├── tri3-out │ │ ├── tsp48-in │ │ ├── tsp48-out │ │ ├── war-in │ │ ├── war-out │ │ ├── war-out-old │ │ ├── wgrid │ │ ├── wgrid-dijkstra-out │ │ ├── wgrid-disconnected1 │ │ ├── wgrid-disconnected2 │ │ ├── wgrid-floyd-out │ │ ├── wgrid-prim-out │ │ └── wgrid.m │ ├── dfs-demo.c │ ├── dijkstra.c │ ├── distance.c │ ├── editbrute.c │ ├── editdistance.c │ ├── editdistance.h │ ├── elevator.c │ ├── fib.c │ ├── findcycle.c │ ├── floyd.c │ ├── gcd.c │ ├── geometry.c │ ├── geometry.h │ ├── geotest.c │ ├── graph.c │ ├── graph.h │ ├── item.h │ ├── kruskal.c │ ├── lcs.c │ ├── list-demo.c │ ├── list.h │ ├── matrix.c │ ├── mwt.c │ ├── my-bi │ ├── name.c │ ├── netflow.c │ ├── obsolete │ │ ├── backtrack.v1 │ │ ├── bfs-dfs.v1 │ │ ├── bfs-dfs.v2 │ │ ├── biconnected.v1 │ │ ├── biconnected.v2 │ │ ├── convex-hull.v1 │ │ ├── dfs-demo.v1 │ │ ├── diff │ │ ├── list-demo.v1 │ │ ├── list-demo.v2 │ │ ├── list-demo.v3 │ │ ├── netflow.v1 │ │ ├── sorting.v1 │ │ ├── sorting.v2 │ │ ├── sorting.v3 │ │ ├── topsort1 │ │ ├── tree-demo.v1 │ │ ├── tree-demo.v2 │ │ ├── tree-demo.v3 │ │ ├── tree-demo.v4 │ │ ├── tree.v1 │ │ └── tsp.v1 │ ├── order.c │ ├── original │ │ ├── 10055.c │ │ ├── 8-queens.c │ │ ├── annealing.c │ │ ├── annealing.h │ │ ├── backtrack.c │ │ ├── backtrack.h │ │ ├── bfs-demo.c │ │ ├── bfs-dfs.c │ │ ├── bignum.c │ │ ├── binomial.c │ │ ├── bool.h │ │ ├── cgtest.c │ │ ├── connected.c │ │ ├── convex-hull.c │ │ ├── dfs-demo.c │ │ ├── dijkstra.c │ │ ├── distance.c │ │ ├── editbrute.c │ │ ├── editdistance.c │ │ ├── editdistance.h │ │ ├── elevator.c │ │ ├── fib.c │ │ ├── findcycle.c │ │ ├── floyd.c │ │ ├── gcd.c │ │ ├── geometry.c │ │ ├── geometry.h │ │ ├── geotest.c │ │ ├── graph.c │ │ ├── graph.h │ │ ├── graphp.c │ │ ├── graphp.h │ │ ├── lcs.c │ │ ├── mwt.c │ │ ├── name.c │ │ ├── netflow.c │ │ ├── order.c │ │ ├── partition.c │ │ ├── paths.c │ │ ├── permutations.c │ │ ├── plates.c │ │ ├── polly.c │ │ ├── prim.c │ │ ├── primes.c │ │ ├── queue.c │ │ ├── queue.h │ │ ├── random.c │ │ ├── sentinel.c │ │ ├── sorting.c │ │ ├── stringedit.c │ │ ├── subsets.c │ │ ├── substringedit.c │ │ ├── sudoku.c │ │ ├── superman.c │ │ ├── topsort.c │ │ ├── triangulate.c │ │ ├── tsp.c │ │ ├── tsp.h │ │ ├── war.c │ │ ├── wgraph.c │ │ └── wgraph.h │ ├── partition.c │ ├── paths.c │ ├── permutations.c │ ├── plates.c │ ├── polly.c │ ├── prim.c │ ├── primes.c │ ├── priority_queue.c │ ├── priority_queue.h │ ├── queue.c │ ├── queue.h │ ├── random.c │ ├── sentinel.c │ ├── set_union.c │ ├── set_union.h │ ├── sorting.c │ ├── stack.c │ ├── stack.h │ ├── stringedit.c │ ├── strong.c │ ├── subsets.c │ ├── substringedit.c │ ├── sudoku-examples │ │ ├── champ │ │ ├── evil0 │ │ ├── evil0-out │ │ ├── evil1 │ │ ├── evil18 │ │ ├── evil2 │ │ ├── minimum1 │ │ ├── minimum2 │ │ ├── minimum3 │ │ ├── puzzle │ │ ├── puzzle0 │ │ └── sudoku.v1 │ ├── sudoku.c │ ├── superman.c │ ├── test-script │ ├── topsort.c │ ├── topsort1.c │ ├── tree-demo.c │ ├── tree.h │ ├── triangulate.c │ ├── tsp-examples │ │ ├── tsp-48 │ │ ├── tsp-48sol │ │ ├── tsp48 │ │ ├── tsp48sarun │ │ └── tsp5 │ ├── tsp.c │ ├── tsp.h │ ├── war.c │ ├── wgraph.c │ └── wgraph.h └── java │ └── com │ └── algorist │ ├── backtrack │ ├── Annealing.java │ ├── Backtrack.java │ ├── BacktrackCallback.java │ ├── NQueens.java │ ├── Paths.java │ ├── Permutations.java │ ├── Subsets.java │ ├── Sudoku.java │ └── TSP.java │ ├── datastructure │ ├── ArrayUtils.java │ ├── List.java │ ├── PriorityQueue.java │ ├── Queue.java │ ├── SetUnion.java │ ├── Stack.java │ ├── Tree.java │ └── War.java │ ├── dp │ ├── Binomial.java │ ├── EditBrute.java │ ├── EditDistance.java │ ├── Elevator.java │ ├── Fib.java │ ├── LCS.java │ ├── Partition.java │ ├── StringEdit.java │ └── SubStringEdit.java │ ├── geometry │ ├── ConvexHull.java │ ├── Distance.java │ ├── Geometry.java │ ├── Order.java │ ├── Plates.java │ ├── SuperMan.java │ └── Triangulate.java │ ├── graph │ ├── AbstractGraphSearch.java │ ├── BFS.java │ ├── Biconnected.java │ ├── Bipartite.java │ ├── Connected.java │ ├── DFS.java │ ├── DefaultGraphSearchCallback.java │ ├── EdgeNode.java │ ├── FindCycle.java │ ├── Graph.java │ ├── GraphReader.java │ ├── GraphSearch.java │ ├── GraphSearchCallback.java │ ├── GraphSearchUtils.java │ ├── Strong.java │ ├── TopSort.java │ ├── TopSort1.java │ ├── UnweightedEdgeNode.java │ └── UnweightedGraphReader.java │ ├── numerical │ ├── BigNum.java │ ├── GCD.java │ ├── Matrix.java │ ├── Primes.java │ └── Random.java │ ├── sort │ └── Sorting.java │ ├── string │ └── Name.java │ ├── utils │ └── IterableUtils.java │ └── wgraph │ ├── AdjacencyMatrix.java │ ├── Dijkstra.java │ ├── FlowEdgeNode.java │ ├── FlowGraph.java │ ├── FlowGraphReader.java │ ├── Floyd.java │ ├── Kruskal.java │ ├── Netflow.java │ ├── Prim.java │ ├── WeightedEdgeNode.java │ └── WeightedGraphReader.java └── test └── java └── com └── algorist ├── backtrack ├── NQueensTest.java ├── PathsTest.java ├── PermutationsTest.java ├── SubsetsTest.java ├── SudokuTest.java └── TSPTest.java ├── datastructure ├── ListDemoTest.java ├── PriorityQueueTest.java ├── QueueTest.java ├── SetUnionTest.java ├── StackTest.java ├── TreeDemoTest.java └── WarTest.java ├── dp ├── ElevatorTest.java ├── FibTest.java ├── LCSTest.java ├── PartitionTest.java ├── StringEditTest.java └── SubStringEditTest.java ├── geometry ├── CgTest.java ├── ConvexHullTest.java ├── DistanceTest.java ├── GeoTest.java ├── OrderTest.java ├── PlatesTest.java ├── SuperManTest.java └── TriangulateTest.java ├── graph ├── BFSDemoTest.java ├── BiconnectedTest.java ├── BipartiteTest.java ├── ConnectedTest.java ├── DFSDemoTest.java ├── FindCycleTest.java ├── StrongTest.java ├── TopSort1Test.java └── TopSortTest.java ├── numerical ├── BigNumTest.java ├── GCDTest.java ├── MatrixTest.java └── PrimesTest.java ├── sort ├── PollyTest.java ├── SentinelsTest.java └── SortingTest.java ├── string └── NameTest.java ├── test ├── TestCaseWithInput.java ├── TestCaseWithoutInput.java └── TestEngine.java └── wgraph ├── DijkstraTest.java ├── FloydTest.java ├── NetflowTest.java └── PrimTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | *.java linguist-detectable=true 2 | *.c linguist-detectable=false 3 | *.h linguist-detectable=false 4 | Makefile linguist-detectable=false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | build 3 | out 4 | .vscode 5 | .idea 6 | *.o 7 | 8-queens 8 | permutations 9 | subsets 10 | war 11 | sorting 12 | bfs-demo 13 | dfs-demo 14 | connected 15 | topsort 16 | prim 17 | dijkstra 18 | floyd 19 | findcycle 20 | stringedit 21 | lcs 22 | substringedit 23 | superman 24 | convex-hull 25 | triangulate 26 | 10055 27 | distance 28 | name 29 | polly 30 | gcd 31 | primes 32 | bignum 33 | editbrute 34 | elevator 35 | order 36 | plates 37 | geotest 38 | cgtest 39 | netflow 40 | paths 41 | sudoku 42 | tsp 43 | fib 44 | partition 45 | biconnected 46 | strong 47 | topsort1 48 | bipartite 49 | kruskal 50 | matrix 51 | list-demo 52 | tree-demo 53 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven' 3 | 4 | group = 'com.algorist' 5 | version = '1.0-SNAPSHOT' 6 | description = """algorithm-design-manual-java""" 7 | 8 | sourceCompatibility = 1.8 9 | targetCompatibility = 1.8 10 | tasks.withType(JavaCompile) { 11 | options.encoding = 'UTF-8' 12 | } 13 | 14 | repositories { 15 | maven { url "https://repo.maven.apache.org/maven2" } 16 | } 17 | dependencies { 18 | testCompile group: 'junit', name: 'junit', version: '4.12' 19 | } 20 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/csong2022/algorithm-design-manual-java/185c84012a61f4a232a282dbd03be43dd9df7059/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip 6 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.algorist 8 | algorithm-design-manual-java 9 | 1.0-SNAPSHOT 10 | 11 | algorithm-design-manual-java 12 | https://www3.cs.stonybrook.edu/~skiena/algorist/book/programs/ 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 1.8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 4.12 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'algorithm-design-manual-java' 2 | -------------------------------------------------------------------------------- /src/main/c/10055.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | int main() { 5 | long p,q,r; 6 | 7 | while (scanf("%ld %ld",&p,&q) 8 | !=EOF) { 9 | if (q>p) r=q-p; 10 | else r=p-q; 11 | 12 | printf("%ld\n",r); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /src/main/c/10055.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | void main() 5 | { 6 | long long a,b,c; 7 | 8 | while (cin>>a>>b) { 9 | if (b>a) 10 | c=b-a; 11 | else 12 | c=a-b; 13 | cout << c << endl; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/main/c/10055.java: -------------------------------------------------------------------------------- 1 | 2 | /* @JUDGE_ID: 15451CF 10055 Java "Esse e ridiculo" */ 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class Main { 8 | 9 | public static void main(String[] args) { 10 | Main myWork = new Main(); 11 | myWork.begin(); 12 | } 13 | 14 | void begin () { 15 | String s; 16 | StringTokenizer st; 17 | long a, b; 18 | 19 | while ((s = Main.readLn(255)) != null) { 20 | st = new StringTokenizer(s); 21 | a = Long.parseLong(st.nextToken()); 22 | b = Long.parseLong(st.nextToken()); 23 | System.out.println(Math.abs(a - b)); 24 | } 25 | } 26 | 27 | static String readLn (int maxLg) { 28 | byte lin[] = new byte [maxLg]; 29 | int lg = 0, car = -1; 30 | String line = ""; 31 | 32 | try { 33 | while (lg < maxLg) { 34 | car = System.in.read(); 35 | if ((car < 0) || (car == '\n')) break; 36 | lin [lg++] += car; 37 | } 38 | } 39 | catch (IOException e) { return (null); } 40 | 41 | if ((car < 0) && (lg == 0)) return (null); 42 | return (new String (lin, 0, lg)); 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/main/c/10055.pascal: -------------------------------------------------------------------------------- 1 | 2 | {$N+} 3 | program acm; 4 | var 5 | a, b, c : integer; 6 | begin 7 | while not eof do 8 | begin 9 | readln(a, b); 10 | if b > a then 11 | begin 12 | c := b; 13 | b := a; 14 | a := c 15 | end; 16 | writeln(a - b); 17 | end 18 | end. 19 | 20 | -------------------------------------------------------------------------------- /src/main/c/backtrack.c: -------------------------------------------------------------------------------- 1 | /* 2 | backtrack.c 3 | A generic backtracking implementation 4 | 5 | begun: March 27, 2002 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | 30 | #include "backtrack.h" 31 | #include "bool.h" 32 | 33 | bool finished = FALSE; /* found all solutions yet? */ 34 | 35 | backtrack(int a[], int k, data input) 36 | { 37 | int c[MAXCANDIDATES]; /* candidates for next position */ 38 | int ncandidates; /* next position candidate count */ 39 | int i; /* counter */ 40 | 41 | if (is_a_solution(a,k,input)) 42 | process_solution(a,k,input); 43 | else { 44 | k = k+1; 45 | construct_candidates(a,k,input,c,&ncandidates); 46 | for (i=0; invertices; i++) 67 | if (discovered[i] == FALSE) { 68 | c = c+1; 69 | printf("Component %d:",c); 70 | bfs(g,i); 71 | printf("\n"); 72 | } 73 | } 74 | 75 | main() 76 | { 77 | graph g; 78 | 79 | read_graph(&g,FALSE); 80 | print_graph(&g); 81 | 82 | connected_components(&g); 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/main/c/datafiles/10055-in: -------------------------------------------------------------------------------- 1 | 1 2 2 | 3 2 3 | 0 0 4 | 9999999 11111 5 | -19 20 6 | -1 1 7 | 10 -10 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/10055-out: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 0 4 | 9988888 5 | 39 6 | 2 7 | 20 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/8-queens-out: -------------------------------------------------------------------------------- 1 | n=1 solution_count=1 2 | n=2 solution_count=0 3 | n=3 solution_count=0 4 | n=4 solution_count=2 5 | n=5 solution_count=10 6 | n=6 solution_count=4 7 | n=7 solution_count=40 8 | n=8 solution_count=92 9 | n=9 solution_count=352 10 | n=10 solution_count=724 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/VDError-uniq.dat-out: -------------------------------------------------------------------------------- 1 | (50.280000,225.890000) 2 | (50.290000,70.380000) 3 | (70.360000,50.310000) 4 | (134.140000,50.230000) 5 | (189.130000,50.230000) 6 | (269.340000,50.290000) 7 | (271.060000,50.300000) 8 | (291.130000,70.380000) 9 | (291.190000,240.350000) 10 | (291.130000,249.920000) 11 | (291.120000,251.010000) 12 | (271.060000,271.080000) 13 | (164.400000,271.110000) 14 | (74.360000,271.090000) 15 | (70.360000,271.080000) 16 | (50.280000,251.010000) 17 | -------------------------------------------------------------------------------- /src/main/c/datafiles/art3: -------------------------------------------------------------------------------- 1 | 10 13 2 | 1 2 3 | 1 4 4 | 2 3 5 | 3 4 6 | 3 5 7 | 3 6 8 | 5 7 9 | 5 8 10 | 6 8 11 | 7 9 12 | 7 10 13 | 8 9 14 | 9 10 15 | -------------------------------------------------------------------------------- /src/main/c/datafiles/baase: -------------------------------------------------------------------------------- 1 | 10 14 2 | 1 4 3 | 1 6 4 | 1 8 5 | 2 3 6 | 2 5 7 | 2 7 8 | 2 9 9 | 3 5 10 | 4 6 11 | 4 10 12 | 5 6 13 | 6 8 14 | 6 10 15 | 7 9 16 | -------------------------------------------------------------------------------- /src/main/c/datafiles/biconnected-art3: -------------------------------------------------------------------------------- 1 | 1: 4 2 2 | 2: 3 1 3 | 3: 6 5 4 2 4 | 4: 3 1 5 | 5: 8 7 3 6 | 6: 8 3 7 | 7: 10 9 5 8 | 8: 9 6 5 9 | 9: 10 8 7 10 | 10: 9 7 11 | parent articulation vertex: 3 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/biconnected-baase: -------------------------------------------------------------------------------- 1 | 1: 8 6 4 2 | 2: 9 7 5 3 3 | 3: 5 2 4 | 4: 10 6 1 5 | 5: 6 3 2 6 | 6: 10 8 5 4 1 7 | 7: 9 2 8 | 8: 6 1 9 | 9: 7 2 10 | 10: 6 4 11 | parent articulation vertex: 2 12 | parent articulation vertex: 5 13 | bridge articulation vertex: 6 14 | bridge articulation vertex: 5 15 | -------------------------------------------------------------------------------- /src/main/c/datafiles/biconnected-clr-graph: -------------------------------------------------------------------------------- 1 | 1: 4 3 2 2 | 2: 4 3 1 3 | 3: 4 2 1 4 | 4: 7 3 2 1 5 | 5: 7 6 6 | 6: 7 5 7 | 7: 11 9 8 6 5 4 8 | 8: 10 9 7 9 | 9: 8 7 10 | 10: 8 11 | 11: 13 12 7 12 | 12: 14 11 13 | 13: 14 11 14 | 14: 15 13 12 15 | 15: 19 18 14 16 | 16: 19 18 17 17 | 17: 18 16 18 | 18: 19 17 16 15 19 | 19: 23 20 18 16 15 20 | 20: 19 21 | 21: 23 22 22 | 22: 23 21 23 | 23: 22 21 19 24 | parent articulation vertex: 23 25 | bridge articulation vertex: 19 26 | bridge articulation vertex: 23 27 | bridge articulation vertex: 19 28 | parent articulation vertex: 15 29 | bridge articulation vertex: 14 30 | bridge articulation vertex: 15 31 | parent articulation vertex: 11 32 | bridge articulation vertex: 7 33 | bridge articulation vertex: 11 34 | bridge articulation vertex: 8 35 | parent articulation vertex: 7 36 | parent articulation vertex: 7 37 | bridge articulation vertex: 4 38 | bridge articulation vertex: 7 39 | -------------------------------------------------------------------------------- /src/main/c/datafiles/biconnected-grid: -------------------------------------------------------------------------------- 1 | 1: 6 2 2 | 2: 7 3 1 3 | 3: 8 4 2 4 | 4: 9 5 3 5 | 5: 10 4 6 | 6: 11 1 7 7 | 7: 12 2 8 6 8 | 8: 13 3 9 7 9 | 9: 14 4 10 8 10 | 10: 15 5 9 11 | 11: 16 6 12 12 | 12: 17 7 13 11 13 | 13: 18 8 14 12 14 | 14: 19 9 15 13 15 | 15: 20 10 14 16 | 16: 21 11 17 17 | 17: 22 12 18 16 18 | 18: 23 13 19 17 19 | 19: 24 14 20 18 20 | 20: 25 15 19 21 | 21: 16 22 22 | 22: 17 23 21 23 | 23: 18 24 22 24 | 24: 19 25 23 25 | 25: 20 24 26 | -------------------------------------------------------------------------------- /src/main/c/datafiles/biconnected-tree: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 5 4 1 3 | 3: 1 4 | 4: 2 5 | 5: 2 6 | bridge articulation vertex: 1 7 | bridge articulation vertex: 2 8 | bridge articulation vertex: 2 9 | bridge articulation vertex: 1 10 | bridge articulation vertex: 2 11 | root articulation vertex: 1 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/bignum-in: -------------------------------------------------------------------------------- 1 | 23423 32432 2 | 32432 23423 3 | 0 0 4 | 0 1 5 | 1 0 6 | 1 1 7 | 0 -1 8 | -1 0 9 | -1 -1 10 | 9999999 1 11 | 1 9999999 12 | 4378321 3423832 13 | 5 4 14 | -5 4 15 | 5 -4 16 | -5 -4 17 | 4 5 18 | -4 5 19 | 4 -5 20 | -4 -5 21 | 314 783 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/bignum-in.v1: -------------------------------------------------------------------------------- 1 | 23423 32432 2 | 32432 23423 3 | 0 0 4 | 0 1 5 | 1 0 6 | 1 1 7 | 0 -1 8 | -1 0 9 | -1 -1 10 | 9999999999 1 11 | 1 9999999999 12 | 4332478321 3428423832 13 | 5 4 14 | -5 4 15 | 5 -4 16 | -5 -4 17 | 4 5 18 | -4 5 19 | 4 -5 20 | -4 -5 21 | 314 783 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/bipartite-art3: -------------------------------------------------------------------------------- 1 | 1: 4 2 2 | 2: 3 1 3 | 3: 6 5 4 2 4 | 4: 3 1 5 | 5: 8 7 3 6 | 6: 8 3 7 | 7: 10 9 5 8 | 8: 9 6 5 9 | 9: 10 8 7 10 | 10: 9 7 11 | Warning: graph not bipartite, due to (9,10) 12 | 1 2 1 2 2 2 1 1 2 1 13 | -------------------------------------------------------------------------------- /src/main/c/datafiles/bipartite-grid: -------------------------------------------------------------------------------- 1 | 1: 6 2 2 | 2: 7 3 1 3 | 3: 8 4 2 4 | 4: 9 5 3 5 | 5: 10 4 6 | 6: 11 1 7 7 | 7: 12 2 8 6 8 | 8: 13 3 9 7 9 | 9: 14 4 10 8 10 | 10: 15 5 9 11 | 11: 16 6 12 12 | 12: 17 7 13 11 13 | 13: 18 8 14 12 14 | 14: 19 9 15 13 15 | 15: 20 10 14 16 | 16: 21 11 17 17 | 17: 22 12 18 16 18 | 18: 23 13 19 17 19 | 19: 24 14 20 18 20 | 20: 25 15 19 21 | 21: 16 22 22 | 22: 17 23 21 23 | 23: 18 24 22 24 | 24: 19 25 23 25 | 25: 20 24 26 | 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 27 | -------------------------------------------------------------------------------- /src/main/c/datafiles/bipartite-tree: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 5 4 1 3 | 3: 1 4 | 4: 2 5 | 5: 2 6 | 1 2 2 1 1 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/cgtest-in: -------------------------------------------------------------------------------- 1 | 1.0 5.0 2 | 3.0 5.0 3 | 5.0 5.0 4 | 5.0 0.0 5 | 1.0 5.0 6 | 3.0 5.0 7 | 3.0 5.0 8 | 3.0 0.0 9 | 2.3 5.3 10 | 5.7 6.8 11 | 12.1 3.4 12 | 9.8 5.3 13 | 1.0 5.0 14 | 3.0 5.0 15 | 2.0 4.0 16 | -17.0 4.0 17 | 0.0 0.0 18 | 1.0 1.0 19 | 5.0 0.0 20 | 10.0 5.0 21 | 0.0 0.0 22 | 14.3 -4.3 23 | 0.0 0.0 24 | 14.3 -4.3 25 | 2.0 2.0 26 | 4.0 4.0 27 | 2.0 4.0 28 | 4.0 2.0 29 | 0.0 0.0 30 | 5.0 5.0 31 | 2.0 2.0 32 | 3.0 3.0 33 | 0.0 0.0 34 | 5.0 5.0 35 | 2.0 2.0 36 | 6.0 6.0 37 | 0.0 0.0 38 | 2.0 2.0 39 | 5.0 5.0 40 | 6.0 6.0 41 | -------------------------------------------------------------------------------- /src/main/c/datafiles/chin: -------------------------------------------------------------------------------- 1 | 10 2 | 10.3 23.5 3 | 8.5 2.0 4 | 5.1 17.4 5 | 10.31 13.0 6 | 5.5 9.5 7 | 4.8 4.8 8 | 3.0 7.0 9 | 5.5 12.4 10 | 8.7 10.9 11 | 6.7 7.6 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/chin-out: -------------------------------------------------------------------------------- 1 | (3.000000,7.000000) 2 | (4.800000,4.800000) 3 | (8.500000,2.000000) 4 | (10.310000,13.000000) 5 | (10.300000,23.500000) 6 | (5.100000,17.400000) 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/chin1: -------------------------------------------------------------------------------- 1 | 10 2 | 10.3 23.5 3 | 8.5 2.0 4 | 5.1 17.4 5 | 10.3 13.0 6 | 5.5 9.5 7 | 4.8 4.8 8 | 3.0 7.0 9 | 5.5 12.4 10 | 8.7 10.9 11 | 6.7 7.6 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/chin1-out: -------------------------------------------------------------------------------- 1 | (3.000000,7.000000) 2 | (4.800000,4.800000) 3 | (8.500000,2.000000) 4 | (10.300000,13.000000) 5 | (10.300000,23.500000) 6 | (5.100000,17.400000) 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/clr-graph: -------------------------------------------------------------------------------- 1 | 23 32 2 | 1 2 3 | 1 3 4 | 1 4 5 | 2 3 6 | 2 4 7 | 3 4 8 | 4 7 9 | 5 6 10 | 5 7 11 | 6 7 12 | 7 8 13 | 7 9 14 | 7 11 15 | 8 9 16 | 8 10 17 | 11 12 18 | 11 13 19 | 12 14 20 | 13 14 21 | 14 15 22 | 15 18 23 | 15 19 24 | 16 17 25 | 16 18 26 | 16 19 27 | 17 18 28 | 18 19 29 | 19 20 30 | 19 23 31 | 21 22 32 | 21 23 33 | 22 23 34 | -------------------------------------------------------------------------------- /src/main/c/datafiles/connected-in: -------------------------------------------------------------------------------- 1 | 25 20 2 | 1 2 3 | 3 4 4 | 6 7 5 | 8 9 6 | 11 12 7 | 13 14 8 | 16 17 9 | 18 19 10 | 21 22 11 | 23 24 12 | 1 6 13 | 3 8 14 | 5 10 15 | 7 12 16 | 9 14 17 | 11 16 18 | 13 18 19 | 15 20 20 | 17 22 21 | 19 24 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/connected-out: -------------------------------------------------------------------------------- 1 | 1: 6 2 2 | 2: 1 3 | 3: 8 4 4 | 4: 3 5 | 5: 10 6 | 6: 1 7 7 | 7: 12 6 8 | 8: 3 9 9 | 9: 14 8 10 | 10: 5 11 | 11: 16 12 12 | 12: 7 11 13 | 13: 18 14 14 | 14: 9 13 15 | 15: 20 16 | 16: 11 17 17 | 17: 22 16 18 | 18: 13 19 19 | 19: 24 18 20 | 20: 15 21 | 21: 22 22 | 22: 17 21 23 | 23: 24 24 | 24: 19 23 25 | 25: 26 | Component 1: 1 6 2 7 12 11 16 17 22 21 27 | Component 2: 3 8 4 9 14 13 18 19 24 23 28 | Component 3: 5 10 29 | Component 4: 15 20 30 | Component 5: 25 31 | -------------------------------------------------------------------------------- /src/main/c/datafiles/convex-bad.10: -------------------------------------------------------------------------------- 1 | 10 2 | 0 0 3 | 0 1 4 | 0 12 5 | 0 9 6 | 0 -5 7 | 0 4 8 | 0 -4 9 | 0 -12 10 | 0 11 11 | 1 10 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/convex-bad.10-out: -------------------------------------------------------------------------------- 1 | (0.000000,-12.000000) 2 | (1.000000,10.000000) 3 | (0.000000,12.000000) 4 | -------------------------------------------------------------------------------- /src/main/c/datafiles/distance-out: -------------------------------------------------------------------------------- 1 | distance = 1.414214 2 | -------------------------------------------------------------------------------- /src/main/c/datafiles/editbrute-in: -------------------------------------------------------------------------------- 1 | afdsdf 2 | sadfasd 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/editbrute-out: -------------------------------------------------------------------------------- 1 | matching cost = 4 2 | s a d f a s d 3 | : 0 0 0 0 0 0 0 0 4 | a: 0 1 1 2 3 4 5 6 5 | f: 0 2 2 2 2 3 4 5 6 | d: 0 3 3 2 3 3 4 4 7 | s: 0 3 4 3 3 4 3 4 8 | d: 0 4 4 4 4 4 4 3 9 | f: 0 5 5 5 4 5 5 4 10 | 11 | s a d f a s d 12 | : 0 0 0 0 0 0 0 0 13 | a: 0 0 0 0 0 0 0 0 14 | f: 0 0 0 0 0 0 0 0 15 | d: 0 0 0 0 0 0 0 0 16 | s: 0 0 0 0 0 0 0 0 17 | d: 0 0 0 0 0 0 0 0 18 | f: 0 0 0 0 0 0 0 0 19 | 6 7 20 | SMSSSSS 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/elevator-in: -------------------------------------------------------------------------------- 1 | 10 3 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | 10 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/elevator-out: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 6 7 | 7 8 | 8 9 | 9 10 | 10 11 | 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 12 | 55 45 37 30 25 21 19 18 19 21 25 30 35 39 43 46 49 51 53 54 55 55 55 55 55 55 13 | 55 45 36 29 23 18 14 12 11 11 12 15 17 18 18 18 18 18 18 18 18 18 18 18 18 18 14 | 55 45 36 28 22 17 13 10 8 7 8 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 15 | 16 | -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 17 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 | 0 0 1 1 1 2 3 3 3 4 5 5 5 6 7 7 7 7 7 7 7 7 7 7 7 7 19 | 0 0 1 2 2 2 3 4 5 6 6 6 7 8 8 8 8 8 8 8 8 8 8 8 8 8 20 | cost = 7 21 | 3 22 | 6 23 | 9 24 | -------------------------------------------------------------------------------- /src/main/c/datafiles/g-1: -------------------------------------------------------------------------------- 1 | 10 12 2 | 1 3 3 | 3 5 4 | 3 7 5 | 4 3 6 | 4 5 7 | 5 3 8 | 5 4 9 | 6 9 10 | 8 2 11 | 9 5 12 | 9 10 13 | 10 5 14 | -------------------------------------------------------------------------------- /src/main/c/datafiles/g-2: -------------------------------------------------------------------------------- 1 | 10 20 2 | 1 3 3 | 1 8 4 | 2 8 5 | 2 9 6 | 3 5 7 | 3 6 8 | 4 5 9 | 4 6 10 | 5 2 11 | 5 4 12 | 5 7 13 | 6 2 14 | 6 3 15 | 7 4 16 | 7 8 17 | 7 9 18 | 7 10 19 | 9 4 20 | 10 4 21 | 10 5 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/g-3: -------------------------------------------------------------------------------- 1 | 10 19 2 | 1 7 3 | 2 4 4 | 3 9 5 | 3 10 6 | 4 1 7 | 4 3 8 | 4 6 9 | 5 10 10 | 6 4 11 | 6 10 12 | 7 1 13 | 7 9 14 | 8 9 15 | 9 1 16 | 9 3 17 | 9 4 18 | 10 3 19 | 10 5 20 | 10 6 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/g-4: -------------------------------------------------------------------------------- 1 | 10 20 2 | 1 2 3 | 1 5 4 | 1 6 5 | 1 7 6 | 1 9 7 | 2 1 8 | 3 1 9 | 4 2 10 | 4 6 11 | 5 2 12 | 5 8 13 | 5 10 14 | 6 5 15 | 7 1 16 | 7 3 17 | 7 4 18 | 7 5 19 | 8 7 20 | 10 5 21 | 10 9 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/g-5: -------------------------------------------------------------------------------- 1 | 10 13 2 | 1 5 3 | 1 10 4 | 4 6 5 | 5 3 6 | 5 6 7 | 6 2 8 | 6 4 9 | 6 7 10 | 7 5 11 | 8 7 12 | 9 4 13 | 10 5 14 | 10 7 15 | -------------------------------------------------------------------------------- /src/main/c/datafiles/gcd-in: -------------------------------------------------------------------------------- 1 | 1232 572 2 | 1232 573 3 | 123421389 123421389 4 | 324 342 5 | 234234 323 6 | 34232 1 7 | 234243 0 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/gcd-out: -------------------------------------------------------------------------------- 1 | gcd(1232,572) &=& gcd(1232 \mod 572, 572) = gcd(572,88) 2 | gcd(572,88) &=& gcd(572 \mod 88, 88) = gcd(88,44) 3 | gcd(88,44) &=& gcd(88 \mod 44, 44) = gcd(44,0) 4 | gcd of p=1232 and q=572 = 44 5 | 1232*-6 + 572*13 = 44 6 | gcd(1232,573) &=& gcd(1232 \mod 573, 573) = gcd(573,86) 7 | gcd(573,86) &=& gcd(573 \mod 86, 86) = gcd(86,57) 8 | gcd(86,57) &=& gcd(86 \mod 57, 57) = gcd(57,29) 9 | gcd(57,29) &=& gcd(57 \mod 29, 29) = gcd(29,28) 10 | gcd(29,28) &=& gcd(29 \mod 28, 28) = gcd(28,1) 11 | gcd(28,1) &=& gcd(28 \mod 1, 1) = gcd(1,0) 12 | gcd of p=1232 and q=573 = 1 13 | 1232*20 + 573*-43 = 1 14 | gcd(123421389,123421389) &=& gcd(123421389 \mod 123421389, 123421389) = gcd(123421389,0) 15 | gcd of p=123421389 and q=123421389 = 123421389 16 | 123421389*0 + 123421389*1 = 123421389 17 | gcd(342,324) &=& gcd(342 \mod 324, 324) = gcd(324,18) 18 | gcd(324,18) &=& gcd(324 \mod 18, 18) = gcd(18,0) 19 | gcd of p=324 and q=342 = 18 20 | 324*-1 + 342*1 = 18 21 | gcd(234234,323) &=& gcd(234234 \mod 323, 323) = gcd(323,59) 22 | gcd(323,59) &=& gcd(323 \mod 59, 59) = gcd(59,28) 23 | gcd(59,28) &=& gcd(59 \mod 28, 28) = gcd(28,3) 24 | gcd(28,3) &=& gcd(28 \mod 3, 3) = gcd(3,1) 25 | gcd(3,1) &=& gcd(3 \mod 1, 1) = gcd(1,0) 26 | gcd of p=234234 and q=323 = 1 27 | 234234*-104 + 323*75419 = 1 28 | gcd(34232,1) &=& gcd(34232 \mod 1, 1) = gcd(1,0) 29 | gcd of p=34232 and q=1 = 1 30 | 34232*0 + 1*1 = 1 31 | gcd of p=234243 and q=0 = 234243 32 | 234243*1 + 0*0 = 234243 33 | -------------------------------------------------------------------------------- /src/main/c/datafiles/geotest-in: -------------------------------------------------------------------------------- 1 | 1.0 5.0 2 | 3.0 5.0 3 | 5.0 5.0 4 | 5.0 0.0 5 | 1.0 5.0 6 | 3.0 5.0 7 | 3.0 5.0 8 | 3.0 0.0 9 | 2.3 5.3 10 | 5.7 6.8 11 | 12.1 3.4 12 | 9.8 5.3 13 | 1.0 5.0 14 | 3.0 5.0 15 | 2.0 4.0 16 | -17.0 4.0 17 | 0.0 0.0 18 | 1.0 1.0 19 | 5.0 0.0 20 | 10.0 5.0 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/grid: -------------------------------------------------------------------------------- 1 | 25 40 2 | 1 2 3 | 2 3 4 | 3 4 5 | 4 5 6 | 6 7 7 | 7 8 8 | 8 9 9 | 9 10 10 | 11 12 11 | 12 13 12 | 13 14 13 | 14 15 14 | 16 17 15 | 17 18 16 | 18 19 17 | 19 20 18 | 21 22 19 | 22 23 20 | 23 24 21 | 24 25 22 | 1 6 23 | 2 7 24 | 3 8 25 | 4 9 26 | 5 10 27 | 6 11 28 | 7 12 29 | 8 13 30 | 9 14 31 | 10 15 32 | 11 16 33 | 12 17 34 | 13 18 35 | 14 19 36 | 15 20 37 | 16 21 38 | 17 22 39 | 18 23 40 | 19 24 41 | 20 25 42 | -------------------------------------------------------------------------------- /src/main/c/datafiles/grid-connected-out: -------------------------------------------------------------------------------- 1 | 1: 6 2 2 | 2: 7 3 1 3 | 3: 8 4 2 4 | 4: 9 5 3 5 | 5: 10 4 6 | 6: 11 1 7 7 | 7: 12 2 8 6 8 | 8: 13 3 9 7 9 | 9: 14 4 10 8 10 | 10: 15 5 9 11 | 11: 16 6 12 12 | 12: 17 7 13 11 13 | 13: 18 8 14 12 14 | 14: 19 9 15 13 15 | 15: 20 10 14 16 | 16: 21 11 17 17 | 17: 22 12 18 16 18 | 18: 23 13 19 17 19 | 19: 24 14 20 18 20 | 20: 25 15 19 21 | 21: 16 22 22 | 22: 17 23 21 23 | 23: 18 24 22 24 | 24: 19 25 23 25 | 25: 20 24 26 | Component 1: 1 6 2 11 7 3 16 12 8 4 21 17 13 9 5 22 18 14 10 23 19 15 24 20 25 27 | -------------------------------------------------------------------------------- /src/main/c/datafiles/grid-topsort-out: -------------------------------------------------------------------------------- 1 | 1: 6 2 2 | 2: 7 3 3 | 3: 8 4 4 | 4: 9 5 5 | 5: 10 6 | 6: 11 7 7 | 7: 12 8 8 | 8: 13 9 9 | 9: 14 10 10 | 10: 15 11 | 11: 16 12 12 | 12: 17 13 13 | 13: 18 14 14 | 14: 19 15 15 | 15: 20 16 | 16: 21 17 17 | 17: 22 18 18 | 18: 23 19 19 | 19: 24 20 20 | 20: 25 21 | 21: 22 22 | 22: 23 23 | 23: 24 24 | 24: 25 25 | 25: 26 | 1 6 2 11 7 3 16 12 8 4 21 17 13 9 5 22 18 14 10 23 19 15 24 20 25 27 | -------------------------------------------------------------------------------- /src/main/c/datafiles/grid-topsort1-out: -------------------------------------------------------------------------------- 1 | 1: 6 2 2 | 2: 7 3 3 | 3: 8 4 4 | 4: 9 5 5 | 5: 10 6 | 6: 11 7 7 | 7: 12 8 8 | 8: 13 9 9 | 9: 14 10 10 | 10: 15 11 | 11: 16 12 12 | 12: 17 13 13 | 13: 18 14 14 | 14: 19 15 15 | 15: 20 16 | 16: 21 17 17 | 17: 22 18 18 | 18: 23 19 19 | 19: 24 20 20 | 20: 25 21 | 21: 22 22 | 22: 23 23 | 23: 24 24 | 24: 25 25 | 25: 26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 | -------------------------------------------------------------------------------- /src/main/c/datafiles/grid4: -------------------------------------------------------------------------------- 1 | 16 24 2 | 1 2 3 | 1 5 4 | 2 3 5 | 2 6 6 | 3 4 7 | 3 7 8 | 4 8 9 | 5 6 10 | 5 9 11 | 6 7 12 | 6 10 13 | 7 8 14 | 7 11 15 | 8 12 16 | 9 10 17 | 9 13 18 | 10 11 19 | 10 14 20 | 11 12 21 | 11 15 22 | 12 16 23 | 13 14 24 | 14 15 25 | 15 16 26 | -------------------------------------------------------------------------------- /src/main/c/datafiles/gridw: -------------------------------------------------------------------------------- 1 | 25 40 2 | 1 2 10 3 | 2 3 15 4 | 3 4 29 5 | 4 5 17 6 | 6 7 10 7 | 7 8 24 8 | 8 9 13 9 | 9 10 12 10 | 11 12 1 11 | 12 13 16 12 | 13 14 10 13 | 14 15 72 14 | 16 17 23 15 | 17 18 9 16 | 18 19 3 17 | 19 20 15 18 | 21 22 23 19 | 22 23 8 20 | 23 24 12 21 | 24 25 5 22 | 1 6 14 23 | 2 7 7 24 | 3 8 10 25 | 4 9 24 26 | 5 10 4 27 | 6 11 8 28 | 7 12 12 29 | 8 13 25 30 | 9 14 9 31 | 10 15 16 32 | 11 16 58 33 | 12 17 12 34 | 13 18 2 35 | 14 19 1 36 | 15 20 8 37 | 16 21 23 38 | 17 22 56 39 | 18 23 18 40 | 19 24 32 41 | 20 25 12 42 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.10: -------------------------------------------------------------------------------- 1 | 10 2 | 7 4 3 | 6 5 4 | 3 3 5 | 0 5 6 | -2 3 7 | -2 2 8 | -5 1 9 | 0 0 10 | -3 -2 11 | 3 -2 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.10-out: -------------------------------------------------------------------------------- 1 | (-5.000000,1.000000) 2 | (-3.000000,-2.000000) 3 | (3.000000,-2.000000) 4 | (7.000000,4.000000) 5 | (6.000000,5.000000) 6 | (0.000000,5.000000) 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.19: -------------------------------------------------------------------------------- 1 | 19 2 | 3 3 3 | 3 5 4 | 0 1 5 | 2 5 6 | -2 2 7 | -3 2 8 | 6 5 9 | -3 4 10 | -5 2 11 | -5 -1 12 | 1 -2 13 | -3 -2 14 | 4 2 15 | 5 1 16 | -5 1 17 | 3 -2 18 | 0 5 19 | 0 0 20 | 7 4 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.19-out: -------------------------------------------------------------------------------- 1 | (-5.000000,-1.000000) 2 | (-3.000000,-2.000000) 3 | (3.000000,-2.000000) 4 | (7.000000,4.000000) 5 | (6.000000,5.000000) 6 | (0.000000,5.000000) 7 | (-3.000000,4.000000) 8 | (-5.000000,2.000000) 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.2: -------------------------------------------------------------------------------- 1 | 2 2 | 0 1 3 | 1 1 4 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.2-out: -------------------------------------------------------------------------------- 1 | (0.000000,1.000000) 2 | (1.000000,1.000000) 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.4: -------------------------------------------------------------------------------- 1 | 4 2 | 0 0 3 | 0 1 4 | 1 1 5 | 1 0 6 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.4-out: -------------------------------------------------------------------------------- 1 | (0.000000,0.000000) 2 | (1.000000,0.000000) 3 | (1.000000,1.000000) 4 | (0.000000,1.000000) 5 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.9: -------------------------------------------------------------------------------- 1 | 9 2 | 1 1 3 | 1 2 4 | 1 3 5 | 2 2 6 | 2 3 7 | 2 4 8 | 3 3 9 | 3 4 10 | 3 5 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/i.9-out: -------------------------------------------------------------------------------- 1 | (1.000000,1.000000) 2 | (3.000000,3.000000) 3 | (3.000000,5.000000) 4 | (1.000000,3.000000) 5 | -------------------------------------------------------------------------------- /src/main/c/datafiles/lcs-out: -------------------------------------------------------------------------------- 1 | length of longest common subsequence = 10 2 | ou-shl-not 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/list-in: -------------------------------------------------------------------------------- 1 | p 2 | s 1 3 | i 1 4 | s 1 5 | s 2 6 | p 7 | i 2 8 | p 9 | i 3 10 | p 11 | s 1 12 | s 2 13 | s 3 14 | s 4 15 | p 16 | d 1 17 | p 18 | d 3 19 | p 20 | d 2 21 | p 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/list-out: -------------------------------------------------------------------------------- 1 | 2 | item 1 not found 3 | new item: 1 4 | item 1 found 5 | item 2 not found 6 | 1 7 | new item: 2 8 | 2 1 9 | new item: 3 10 | 3 2 1 11 | item 1 found 12 | item 2 found 13 | item 3 found 14 | item 4 not found 15 | 3 2 1 16 | item 1 deleted 17 | 3 2 18 | item 3 deleted 19 | 2 20 | item 2 deleted 21 | 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/matrix-data1: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 2 3 4 3 | 5 6 7 8 4 | 9 10 11 12 5 | 4 4 6 | 1 0 0 0 7 | 0 1 0 0 8 | 0 0 1 0 9 | 0 0 0 1 10 | -------------------------------------------------------------------------------- /src/main/c/datafiles/matrix-data1-out: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | 5 6 7 8 3 | 9 10 11 12 4 | 5 | 1 0 0 0 6 | 0 1 0 0 7 | 0 0 1 0 8 | 0 0 0 1 9 | 10 | 1 2 3 4 11 | 5 6 7 8 12 | 9 10 11 12 13 | 14 | -------------------------------------------------------------------------------- /src/main/c/datafiles/matrix-data2: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 2 3 4 3 | 5 6 7 8 4 | 9 10 11 12 5 | 4 1 6 | 1 7 | 1 8 | 1 9 | 1 10 | -------------------------------------------------------------------------------- /src/main/c/datafiles/matrix-data2-out: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | 5 6 7 8 3 | 9 10 11 12 4 | 5 | 1 6 | 1 7 | 1 8 | 1 9 | 10 | 10 11 | 26 12 | 42 13 | 14 | -------------------------------------------------------------------------------- /src/main/c/datafiles/name-in: -------------------------------------------------------------------------------- 1 | 4 2 | "Anderson Consulting" to "Accenture" 3 | "Enron" to "Dynegy" 4 | "DEC" to "Compaq" 5 | "TWA" to "American" 6 | 5 7 | Anderson Accounting begat Anderson Consulting, which 8 | offered advice to Enron before it DECLARED bankruptcy, 9 | which made Anderson 10 | Consulting quite happy it changed its name 11 | in the first place! 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/name-out: -------------------------------------------------------------------------------- 1 | Anderson Accounting begat Accenture, which 2 | offered advice to Dynegy before it CompaqLARED bankruptcy, 3 | which made Anderson 4 | Consulting quite happy it changed its name 5 | in the first place! 6 | -------------------------------------------------------------------------------- /src/main/c/datafiles/netflow1-in: -------------------------------------------------------------------------------- 1 | 1 6 2 | 6 10 3 | 1 2 16 4 | 1 3 13 5 | 2 3 10 6 | 3 2 4 7 | 4 3 9 8 | 2 4 12 9 | 3 5 14 10 | 5 4 7 11 | 4 6 20 12 | 5 6 4 13 | -------------------------------------------------------------------------------- /src/main/c/datafiles/netflow1-out: -------------------------------------------------------------------------------- 1 | 1: 3(13,11,2) 2(16,12,4) 2 | 2: 1(0,0,12) 4(12,12,0) 3(10,0,10) 3 | 3: 4(0,0,0) 1(0,0,11) 5(14,11,3) 2(4,0,4) 4 | 4: 5(0,0,7) 2(0,0,12) 6(20,19,1) 3(9,0,9) 5 | 5: 3(0,0,11) 6(4,4,0) 4(7,7,0) 6 | 6: 5(0,0,4) 4(0,0,19) 7 | total flow = 23 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/netflow2-in: -------------------------------------------------------------------------------- 1 | 1 25 2 | 25 40 3 | 1 2 2820 4 | 2 3 265 5 | 3 4 8266 6 | 4 5 9646 7 | 6 7 1820 8 | 7 8 5390 9 | 8 9 989 10 | 9 10 5277 11 | 11 12 4280 12 | 12 13 7446 13 | 13 14 6270 14 | 14 15 3369 15 | 16 17 6207 16 | 17 18 6763 17 | 18 19 2338 18 | 19 20 1934 19 | 21 22 8844 20 | 22 23 625 21 | 23 24 8460 22 | 24 25 7422 23 | 1 6 1831 24 | 2 7 3203 25 | 3 8 1753 26 | 4 9 521 27 | 5 10 5375 28 | 6 11 1096 29 | 7 12 7169 30 | 8 13 9923 31 | 9 14 7993 32 | 10 15 2447 33 | 11 16 3858 34 | 12 17 9429 35 | 13 18 418 36 | 14 19 639 37 | 15 20 7173 38 | 16 21 6496 39 | 17 22 8371 40 | 18 23 4016 41 | 19 24 4696 42 | 20 25 7827 43 | -------------------------------------------------------------------------------- /src/main/c/datafiles/netflow2-out: -------------------------------------------------------------------------------- 1 | 1: 6(1831,1831,0) 2(2820,2820,0) 2 | 2: 1(0,0,2820) 7(3203,2820,383) 3(265,0,265) 3 | 3: 2(0,0,0) 8(1753,0,1753) 4(8266,0,8266) 4 | 4: 3(0,0,0) 9(521,0,521) 5(9646,0,9646) 5 | 5: 4(0,0,0) 10(5375,0,5375) 6 | 6: 1(0,0,1831) 11(1096,1096,0) 7(1820,735,1085) 7 | 7: 6(0,0,735) 2(0,0,2820) 12(7169,3555,3614) 8(5390,0,5390) 8 | 8: 7(0,0,0) 3(0,0,0) 13(9923,0,9923) 9(989,0,989) 9 | 9: 8(0,0,0) 4(0,0,0) 14(7993,0,7993) 10(5277,0,5277) 10 | 10: 9(0,0,0) 5(0,0,0) 15(2447,0,2447) 11 | 11: 6(0,0,1096) 16(3858,1096,2762) 12(4280,0,4280) 12 | 12: 11(0,0,0) 7(0,0,3555) 17(9429,3555,5874) 13(7446,0,7446) 13 | 13: 12(0,0,0) 8(0,0,0) 18(418,0,418) 14(6270,0,6270) 14 | 14: 13(0,0,0) 9(0,0,0) 19(639,0,639) 15(3369,0,3369) 15 | 15: 14(0,0,0) 10(0,0,0) 20(7173,0,7173) 16 | 16: 11(0,0,1096) 21(6496,625,5871) 17(6207,471,5736) 17 | 17: 16(0,0,471) 12(0,0,3555) 22(8371,0,8371) 18(6763,4026,2737) 18 | 18: 17(0,0,4026) 13(0,0,0) 23(4016,4016,0) 19(2338,10,2328) 19 | 19: 18(0,0,10) 14(0,0,0) 24(4696,10,4686) 20(1934,0,1934) 20 | 20: 19(0,0,0) 15(0,0,0) 25(7827,0,7827) 21 | 21: 16(0,0,625) 22(8844,625,8219) 22 | 22: 21(0,0,625) 17(0,0,0) 23(625,625,0) 23 | 23: 22(0,0,625) 18(0,0,4016) 24(8460,4641,3819) 24 | 24: 23(0,0,4641) 19(0,0,10) 25(7422,4651,2771) 25 | 25: 24(0,0,4651) 20(0,0,0) 26 | total flow = 4651 27 | -------------------------------------------------------------------------------- /src/main/c/datafiles/order-out: -------------------------------------------------------------------------------- 1 | row_major 2 | (1,1) 3 | (1,2) 4 | (1,3) 5 | (1,4) 6 | (1,5) 7 | (2,1) 8 | (2,2) 9 | (2,3) 10 | (2,4) 11 | (2,5) 12 | (3,1) 13 | (3,2) 14 | (3,3) 15 | (3,4) 16 | (3,5) 17 | (4,1) 18 | (4,2) 19 | (4,3) 20 | (4,4) 21 | (4,5) 22 | (5,1) 23 | (5,2) 24 | (5,3) 25 | (5,4) 26 | (5,5) 27 | 28 | column_major 29 | (1,1) 30 | (2,1) 31 | (3,1) 32 | (1,2) 33 | (2,2) 34 | (3,2) 35 | (1,3) 36 | (2,3) 37 | (3,3) 38 | 39 | snake_order 40 | (1,1) 41 | (1,2) 42 | (1,3) 43 | (1,4) 44 | (1,5) 45 | (2,5) 46 | (2,4) 47 | (2,3) 48 | (2,2) 49 | (2,1) 50 | (3,1) 51 | (3,2) 52 | (3,3) 53 | (3,4) 54 | (3,5) 55 | (4,5) 56 | (4,4) 57 | (4,3) 58 | (4,2) 59 | (4,1) 60 | (5,1) 61 | (5,2) 62 | (5,3) 63 | (5,4) 64 | (5,5) 65 | 66 | diagonal_order 67 | (1,1) 68 | (2,1) 69 | (1,2) 70 | (3,1) 71 | (2,2) 72 | (1,3) 73 | (4,1) 74 | (3,2) 75 | (2,3) 76 | (4,2) 77 | (3,3) 78 | (4,3) 79 | 80 | diagonal_order 81 | (1,1) 82 | (2,1) 83 | (1,2) 84 | (3,1) 85 | (2,2) 86 | (1,3) 87 | (3,2) 88 | (2,3) 89 | (1,4) 90 | (3,3) 91 | (2,4) 92 | (3,4) 93 | -------------------------------------------------------------------------------- /src/main/c/datafiles/partition-data1: -------------------------------------------------------------------------------- 1 | 9 3 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 8 10 | 9 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/partition-data1-out: -------------------------------------------------------------------------------- 1 | { 1 2 3 4 5 } 2 | { 6 7 } 3 | { 8 9 } 4 | -------------------------------------------------------------------------------- /src/main/c/datafiles/partition-data2: -------------------------------------------------------------------------------- 1 | 9 3 2 | 1 3 | 1 4 | 1 5 | 1 6 | 1 7 | 1 8 | 1 9 | 1 10 | 1 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/partition-data2-out: -------------------------------------------------------------------------------- 1 | { 1 1 1 } 2 | { 1 1 1 } 3 | { 1 1 1 } 4 | -------------------------------------------------------------------------------- /src/main/c/datafiles/paths-graph: -------------------------------------------------------------------------------- 1 | 6 9 2 | 1 2 3 | 1 3 4 | 1 4 5 | 1 5 6 | 3 4 7 | 2 6 8 | 3 6 9 | 4 6 10 | 5 6 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/paths-graph-out: -------------------------------------------------------------------------------- 1 | 1: 5 4 3 2 2 | 2: 6 1 3 | 3: 6 4 1 4 | 4: 6 3 1 5 | 5: 6 1 6 | 6: 5 4 3 2 7 | 8 | Paths from 1 to 1: 9 | { 1 } 10 | 11 | Paths from 1 to 2: 12 | { 1 5 6 2 } 13 | { 1 4 6 2 } 14 | { 1 4 3 6 2 } 15 | { 1 3 6 2 } 16 | { 1 3 4 6 2 } 17 | { 1 2 } 18 | 19 | Paths from 1 to 3: 20 | { 1 5 6 4 3 } 21 | { 1 5 6 3 } 22 | { 1 4 6 3 } 23 | { 1 4 3 } 24 | { 1 3 } 25 | { 1 2 6 4 3 } 26 | { 1 2 6 3 } 27 | 28 | Paths from 1 to 4: 29 | { 1 5 6 4 } 30 | { 1 5 6 3 4 } 31 | { 1 4 } 32 | { 1 3 6 4 } 33 | { 1 3 4 } 34 | { 1 2 6 4 } 35 | { 1 2 6 3 4 } 36 | 37 | Paths from 1 to 5: 38 | { 1 5 } 39 | { 1 4 6 5 } 40 | { 1 4 3 6 5 } 41 | { 1 3 6 5 } 42 | { 1 3 4 6 5 } 43 | { 1 2 6 5 } 44 | 45 | Paths from 1 to 6: 46 | { 1 5 6 } 47 | { 1 4 6 } 48 | { 1 4 3 6 } 49 | { 1 3 6 } 50 | { 1 3 4 6 } 51 | { 1 2 6 } 52 | -------------------------------------------------------------------------------- /src/main/c/datafiles/permutations-out: -------------------------------------------------------------------------------- 1 | 2 | 1 3 | 1 2 4 | 2 1 5 | 1 2 3 6 | 1 3 2 7 | 2 1 3 8 | 2 3 1 9 | 3 1 2 10 | 3 2 1 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/plates1-in: -------------------------------------------------------------------------------- 1 | 4.0 4.0 0.5 2 | -------------------------------------------------------------------------------- /src/main/c/datafiles/plates1-out: -------------------------------------------------------------------------------- 1 | input box width, box length, and plate radius: 2 | box width=4.000000, box length=4.000000, and plate radius=0.500000: 3 | dense packing = 14 4 | grid packing = 16 5 | (0,0) has 5 on top. 6 | (0,1) has 8 on top. 7 | (0,2) has 8 on top. 8 | (0,3) has 5 on top. 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/plates2-in: -------------------------------------------------------------------------------- 1 | 10.0 10.0 0.5 2 | -------------------------------------------------------------------------------- /src/main/c/datafiles/plates2-out: -------------------------------------------------------------------------------- 1 | input box width, box length, and plate radius: 2 | box width=10.000000, box length=10.000000, and plate radius=0.500000: 3 | dense packing = 105 4 | grid packing = 100 5 | (0,0) has 35 on top. 6 | (0,1) has 45 on top. 7 | (0,2) has 53 on top. 8 | (0,3) has 59 on top. 9 | (0,4) has 63 on top. 10 | (0,5) has 63 on top. 11 | (0,6) has 59 on top. 12 | (0,7) has 53 on top. 13 | (0,8) has 45 on top. 14 | (0,9) has 35 on top. 15 | -------------------------------------------------------------------------------- /src/main/c/datafiles/plates3-in: -------------------------------------------------------------------------------- 1 | 100.0 100.0 0.5 2 | -------------------------------------------------------------------------------- /src/main/c/datafiles/polly-in: -------------------------------------------------------------------------------- 1 | George Bush 195 110 2 | Harry Truman 180 75 3 | Bill Clinton 180 75 4 | John Kennedy 180 65 5 | Ronald Reagan 165 110 6 | Richard Nixon 170 70 7 | Jimmy Carter 180 77 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/polly-out: -------------------------------------------------------------------------------- 1 | Clinton, Bill 2 | Truman, Harry 3 | Kennedy, John 4 | Carter, Jimmy 5 | Nixon, Richard 6 | Bush, George 7 | Reagan, Ronald 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/primes-in: -------------------------------------------------------------------------------- 1 | 17 2 | 234 3 | 128 4 | 2147483647 5 | 9409 6 | 1 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/primes-out: -------------------------------------------------------------------------------- 1 | prime factorization of p=17 2 | 17 3 | prime factorization of p=234 4 | 2 5 | 3 6 | 3 7 | 13 8 | prime factorization of p=128 9 | 2 10 | 2 11 | 2 12 | 2 13 | 2 14 | 2 15 | 2 16 | prime factorization of p=2147483647 17 | 2147483647 18 | prime factorization of p=9409 19 | 97 20 | 97 21 | prime factorization of p=1 22 | -------------------------------------------------------------------------------- /src/main/c/datafiles/puzzle: -------------------------------------------------------------------------------- 1 | 800000320 2 | 070304968 3 | 300060500 4 | 000831400 5 | 400090002 6 | 001425000 7 | 008050006 8 | 123607050 9 | 057000004 10 | -------------------------------------------------------------------------------- /src/main/c/datafiles/puzzle-out: -------------------------------------------------------------------------------- 1 | 2 | There are 46 free board positions. 3 | 8 | |32 | 4 | 7 |3 4|968| 5 | 3 | 6 |5 | 6 | ----------- 7 | |831|4 | 8 | 4 | 9 | 2| 9 | 1|425| | 10 | ----------- 11 | 8| 5 | 6| 12 | 123|6 7| 5 | 13 | 57| | 4| 14 | ----------- 15 | ---------------------------------- 16 | process solution 17 | 18 | There are 0 free board positions. 19 | 864|579|321| 20 | 572|314|968| 21 | 319|268|547| 22 | ----------- 23 | 296|831|475| 24 | 435|796|182| 25 | 781|425|693| 26 | ----------- 27 | 948|152|736| 28 | 123|647|859| 29 | 657|983|214| 30 | ----------- 31 | It took 47 steps to find this solution for fast=1 smart=1 32 | ---------------------------------- 33 | process solution 34 | 35 | There are 0 free board positions. 36 | 864|579|321| 37 | 572|314|968| 38 | 319|268|547| 39 | ----------- 40 | 296|831|475| 41 | 435|796|182| 42 | 781|425|693| 43 | ----------- 44 | 948|152|736| 45 | 123|647|859| 46 | 657|983|214| 47 | ----------- 48 | It took 47 steps to find this solution for fast=1 smart=0 49 | ---------------------------------- 50 | process solution 51 | 52 | There are 0 free board positions. 53 | 864|579|321| 54 | 572|314|968| 55 | 319|268|547| 56 | ----------- 57 | 296|831|475| 58 | 435|796|182| 59 | 781|425|693| 60 | ----------- 61 | 948|152|736| 62 | 123|647|859| 63 | 657|983|214| 64 | ----------- 65 | It took 56 steps to find this solution for fast=0 smart=1 66 | ---------------------------------- 67 | process solution 68 | 69 | There are 0 free board positions. 70 | 864|579|321| 71 | 572|314|968| 72 | 319|268|547| 73 | ----------- 74 | 296|831|475| 75 | 435|796|182| 76 | 781|425|693| 77 | ----------- 78 | 948|152|736| 79 | 123|647|859| 80 | 657|983|214| 81 | ----------- 82 | It took 1590 steps to find this solution for fast=0 smart=0 83 | -------------------------------------------------------------------------------- /src/main/c/datafiles/sorting-out: -------------------------------------------------------------------------------- 1 | 2 | 3 | Insertion sort: 4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 5 | 6 | Selection sort: 7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 8 | 9 | Quicksort: 10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 11 | 12 | Heapsort sort: 13 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 14 | -------------------------------------------------------------------------------- /src/main/c/datafiles/spbug-in: -------------------------------------------------------------------------------- 1 | 7 7 2 | 1 2 10 3 | 2 3 10 4 | 3 4 10 5 | 4 5 10 6 | 5 6 10 7 | 6 7 10 8 | 7 1 10 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/stringedit-in: -------------------------------------------------------------------------------- 1 | thou-shalt-not 2 | you-should-not 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/stringedit-out: -------------------------------------------------------------------------------- 1 | matching cost = 5 2 | y o u - s h o u l d - n o t 3 | : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 4 | t: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 13 5 | h: 2 2 2 3 4 5 5 6 7 8 9 10 11 12 13 6 | o: 3 3 2 3 4 5 6 5 6 7 8 9 10 11 12 7 | u: 4 4 3 2 3 4 5 6 5 6 7 8 9 10 11 8 | -: 5 5 4 3 2 3 4 5 6 6 7 7 8 9 10 9 | s: 6 6 5 4 3 2 3 4 5 6 7 8 8 9 10 10 | h: 7 7 6 5 4 3 2 3 4 5 6 7 8 9 10 11 | a: 8 8 7 6 5 4 3 3 4 5 6 7 8 9 10 12 | l: 9 9 8 7 6 5 4 4 4 4 5 6 7 8 9 13 | t: 10 10 9 8 7 6 5 5 5 5 5 6 7 8 8 14 | -: 11 11 10 9 8 7 6 6 6 6 6 5 6 7 8 15 | n: 12 12 11 10 9 8 7 7 7 7 7 6 5 6 7 16 | o: 13 13 12 11 10 9 8 7 8 8 8 7 6 5 6 17 | t: 14 14 13 12 11 10 9 8 8 9 9 8 7 6 5 18 | 19 | y o u - s h o u l d - n o t 20 | : -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 21 | t: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 22 | h: 2 0 0 0 0 0 0 1 1 1 1 1 1 1 1 23 | o: 2 0 0 0 0 0 0 0 1 1 1 1 1 0 1 24 | u: 2 0 2 0 1 1 1 1 0 1 1 1 1 1 1 25 | -: 2 0 2 2 0 1 1 1 1 0 0 0 1 1 1 26 | s: 2 0 2 2 2 0 1 1 1 1 0 0 0 0 0 27 | h: 2 0 2 2 2 2 0 1 1 1 1 1 1 0 0 28 | a: 2 0 2 2 2 2 2 0 0 0 0 0 0 0 0 29 | l: 2 0 2 2 2 2 2 0 0 0 1 1 1 1 1 30 | t: 2 0 2 2 2 2 2 0 0 0 0 0 0 0 0 31 | -: 2 0 2 2 0 2 2 0 0 0 0 0 1 1 1 32 | n: 2 0 2 2 2 2 2 0 0 0 0 2 0 1 1 33 | o: 2 0 0 2 2 2 2 0 0 0 0 2 2 0 1 34 | t: 2 0 2 2 2 2 2 2 0 0 0 2 2 2 0 35 | 14 14 36 | DSMMMMMISMSMMMM 37 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-clr: -------------------------------------------------------------------------------- 1 | 8 14 2 | 1 2 3 | 2 3 4 | 2 5 5 | 2 6 6 | 3 4 7 | 3 7 8 | 4 3 9 | 4 8 10 | 5 1 11 | 5 6 12 | 6 7 13 | 7 6 14 | 7 8 15 | 8 8 16 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-clr-out: -------------------------------------------------------------------------------- 1 | 1: 2 2 | 2: 6 5 3 3 | 3: 7 4 4 | 4: 8 3 5 | 5: 6 1 6 | 6: 7 7 | 7: 8 6 8 | 8: 8 9 | 8 is in component 1 10 | 6 is in component 2 11 | 7 is in component 2 with 6 12 | 3 is in component 3 13 | 4 is in component 3 with 3 14 | 1 is in component 4 15 | 5 is in component 4 with 1 16 | 2 is in component 4 with 1 17 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-g-1: -------------------------------------------------------------------------------- 1 | 1: 3 2 | 2: 3 | 3: 7 5 4 | 4: 5 3 5 | 5: 4 3 6 | 6: 9 7 | 7: 8 | 8: 2 9 | 9: 10 5 10 | 10: 5 11 | 7 is in component 1 12 | 3 is in component 2 13 | 4 is in component 2 with 3 14 | 5 is in component 2 with 3 15 | 1 is in component 3 16 | 2 is in component 4 17 | 10 is in component 5 18 | 9 is in component 6 19 | 6 is in component 7 20 | 8 is in component 8 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-g-2: -------------------------------------------------------------------------------- 1 | 1: 8 3 2 | 2: 9 8 3 | 3: 6 5 4 | 4: 6 5 5 | 5: 7 4 2 6 | 6: 3 2 7 | 7: 10 9 8 4 8 | 8: 9 | 9: 4 10 | 10: 5 4 11 | 8 is in component 1 12 | 3 is in component 2 13 | 10 is in component 2 with 3 14 | 7 is in component 2 with 3 15 | 5 is in component 2 with 3 16 | 4 is in component 2 with 3 17 | 9 is in component 2 with 3 18 | 2 is in component 2 with 3 19 | 6 is in component 2 with 3 20 | 1 is in component 3 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-g-3: -------------------------------------------------------------------------------- 1 | 1: 7 2 | 2: 4 3 | 3: 10 9 4 | 4: 6 3 1 5 | 5: 10 6 | 6: 10 4 7 | 7: 9 1 8 | 8: 9 9 | 9: 4 3 1 10 | 10: 6 5 3 11 | 1 is in component 1 12 | 3 is in component 1 with 1 13 | 5 is in component 1 with 1 14 | 10 is in component 1 with 1 15 | 6 is in component 1 with 1 16 | 4 is in component 1 with 1 17 | 9 is in component 1 with 1 18 | 7 is in component 1 with 1 19 | 2 is in component 2 20 | 8 is in component 3 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-g-4: -------------------------------------------------------------------------------- 1 | 1: 9 7 6 5 2 2 | 2: 1 3 | 3: 1 4 | 4: 6 2 5 | 5: 10 8 2 6 | 6: 5 7 | 7: 5 4 3 1 8 | 8: 7 9 | 9: 10 | 10: 9 5 11 | 9 is in component 1 12 | 1 is in component 2 13 | 3 is in component 2 with 1 14 | 6 is in component 2 with 1 15 | 4 is in component 2 with 1 16 | 2 is in component 2 with 1 17 | 8 is in component 2 with 1 18 | 10 is in component 2 with 1 19 | 5 is in component 2 with 1 20 | 7 is in component 2 with 1 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/strong-g-5: -------------------------------------------------------------------------------- 1 | 1: 10 5 2 | 2: 3 | 3: 4 | 4: 6 5 | 5: 6 3 6 | 6: 7 4 2 7 | 7: 5 8 | 8: 7 9 | 9: 4 10 | 10: 7 5 11 | 2 is in component 1 12 | 3 is in component 2 13 | 7 is in component 3 14 | 4 is in component 3 with 7 15 | 6 is in component 3 with 7 16 | 5 is in component 3 with 7 17 | 10 is in component 4 18 | 1 is in component 5 19 | 8 is in component 6 20 | 9 is in component 7 21 | -------------------------------------------------------------------------------- /src/main/c/datafiles/subsets-out: -------------------------------------------------------------------------------- 1 | { 1 2 3 } 2 | { 1 2 } 3 | { 1 3 } 4 | { 1 } 5 | { 2 3 } 6 | { 2 } 7 | { 3 } 8 | { } 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/substringedit-in: -------------------------------------------------------------------------------- 1 | skiena 2 | my-name-is-not-skienna-skena-skina-skieng-etc 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/super.m: -------------------------------------------------------------------------------- 1 | (* Steven Skiena 2 | July 23, 2002 3 | Superman input viewer 4 | 5 | *) 6 | 7 | Superman[f_] := 8 | Module[{s,n,i,p1,p2,c,g}, 9 | s = OpenRead[f]; 10 | p1 = Read[s,{Real,Real}]; 11 | p2 = Read[s,{Real,Real}]; 12 | g = {Line[{p1,p2}]}; 13 | Print[p1, p2]; 14 | n = First[ Round[ Read[s,{Real}] ] ]; 15 | Print[n]; 16 | Table[ 17 | (c = Read[s,{Real,Real,Real}]; 18 | AppendTo[g, Circle[{c[[1]],c[[2]]},c[[3]]]]; 19 | Print[g];), {n} 20 | ]; 21 | Show[ Graphics[g] ]; 22 | ] 23 | 24 | 25 | Superman["superin1"] 26 | Superman["superin2"] 27 | Superman["superin3"] 28 | 29 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin1: -------------------------------------------------------------------------------- 1 | 0.0 0.0 2 | 100.0 100.0 3 | 5 4 | 20.0 21.0 5.0 5 | 20.0 0.0 7.0 6 | 50.0 45.0 12.0 7 | 0.0 50.0 20.0 8 | 95.0 95.0 0.0 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin1-out: -------------------------------------------------------------------------------- 1 | 0.000 0.000 2 | 100.000 100.000 3 | 5 4 | 20.000 21.000 5.000 5 | 20.000 0.000 7.000 6 | 50.000 45.000 12.000 7 | 0.000 50.000 20.000 8 | 95.000 95.000 0.000 9 | circle 1 ( 20.000, 21.000, 5.000) is 0.707 away from l, xray= 9.899 around= 14.289 angle= 1.429. 10 | circle 3 ( 50.000, 45.000, 12.000) is 3.536 away from l, xray= 32.834 around= 44.811 angle= 1.272. 11 | Superman sees through 32.834 units, and flies 153.398 units 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin2: -------------------------------------------------------------------------------- 1 | 0.0 0.0 2 | 0.0 100.0 3 | 5 4 | 20.0 21.0 5.0 5 | 20.0 0.0 7.0 6 | 50.0 45.0 12.0 7 | 0.0 50.0 20.0 8 | 95.0 95.0 0.0 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin2-out: -------------------------------------------------------------------------------- 1 | 0.000 0.000 2 | 0.000 100.000 3 | 5 4 | 20.000 21.000 5.000 5 | 20.000 0.000 7.000 6 | 50.000 45.000 12.000 7 | 0.000 50.000 20.000 8 | 95.000 95.000 0.000 9 | circle 4 ( 0.000, 50.000, 20.000) is 0.000 away from l, xray= 40.000 around= 62.832 angle= 1.571. 10 | Superman sees through 40.000 units, and flies 122.832 units 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin2~: -------------------------------------------------------------------------------- 1 | 0.0 0.0 2 | 100.0 100.0 3 | 5 4 | 20.0 21.0 5.0 5 | 20.0 0.0 7.0 6 | 50.0 45.0 12.0 7 | 0.0 50.0 20.0 8 | 95.0 95.0 0.0 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin3: -------------------------------------------------------------------------------- 1 | 100.0 100.0 2 | 0.0 0.0 3 | 5 4 | 20.0 21.0 25.0 5 | 20.0 0.0 19.0 6 | 50.0 45.0 40.0 7 | 0.0 50.0 20.0 8 | 95.0 95.0 0.0 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin3-out: -------------------------------------------------------------------------------- 1 | 100.000 100.000 2 | 0.000 0.000 3 | 5 4 | 20.000 21.000 25.000 5 | 20.000 0.000 19.000 6 | 50.000 45.000 40.000 7 | 0.000 50.000 20.000 8 | 95.000 95.000 0.000 9 | circle 1 ( 20.000, 21.000, 25.000) is 0.707 away from l, xray= 49.980 around= 77.125 angle= 1.543. 10 | circle 2 ( 20.000, 0.000, 19.000) is 14.142 away from l, xray= 75.357 around=104.914 angle= 0.731. 11 | circle 3 ( 50.000, 45.000, 40.000) is 3.536 away from l, xray=155.044 around=223.497 angle= 1.482. 12 | Superman sees through 155.044 units, and flies 209.875 units 13 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin3~: -------------------------------------------------------------------------------- 1 | 100.0 100.0 2 | 0.0 0.0 3 | 5 4 | 20.0 21.0 5.0 5 | 20.0 0.0 7.0 6 | 50.0 45.0 12.0 7 | 0.0 50.0 20.0 8 | 95.0 95.0 0.0 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin4: -------------------------------------------------------------------------------- 1 | 0.0 0.0 2 | 100.0 100.0 3 | 2 4 | 200.0 200.0 5.0 5 | -20.0 -20.0 10.0 6 | -------------------------------------------------------------------------------- /src/main/c/datafiles/superin4-out: -------------------------------------------------------------------------------- 1 | 0.000 0.000 2 | 100.000 100.000 3 | 2 4 | 200.000 200.000 5.000 5 | -20.000 -20.000 10.000 6 | Superman sees through 0.000 units, and flies 141.421 units 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/t.in: -------------------------------------------------------------------------------- 1 | i 1 2 | i 2 3 | i 0 4 | p 5 | d 0 6 | p 7 | d 1 8 | p 9 | d 2 10 | p 11 | 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/t1.in: -------------------------------------------------------------------------------- 1 | i 6 2 | i 5 3 | i 3 4 | i 4 5 | i 7 6 | i 8 7 | i 9 8 | p 9 | d 7 10 | d 3 11 | d 6 12 | d 5 13 | d 4 14 | d 8 15 | d 9 16 | -------------------------------------------------------------------------------- /src/main/c/datafiles/t1.out: -------------------------------------------------------------------------------- 1 | new item: 6 2 | new item: 5 3 | new item: 3 4 | new item: 4 5 | new item: 7 6 | new item: 8 7 | new item: 9 8 | 3 4 5 6 7 8 9 9 | deleting item 7 10 | 3 4 5 6 8 9 11 | deleting item 3 12 | 4 5 6 8 9 13 | deleting item 6 14 | 4 5 8 9 15 | deleting item 5 16 | 4 8 9 17 | deleting item 4 18 | 8 9 19 | deleting item 8 20 | 9 21 | deleting item 9 22 | 23 | -------------------------------------------------------------------------------- /src/main/c/datafiles/test: -------------------------------------------------------------------------------- 1 | 1 2 2 | 3 2 3 | 0 0 4 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tree: -------------------------------------------------------------------------------- 1 | 5 4 2 | 1 2 3 | 1 3 4 | 2 4 5 | 2 5 6 | 3 6 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tree2: -------------------------------------------------------------------------------- 1 | 6 5 2 | 1 2 3 | 1 3 4 | 1 6 5 | 2 4 6 | 2 5 7 | 3 6 8 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tree3: -------------------------------------------------------------------------------- 1 | 6 5 2 | 1 2 3 | 2 3 4 | 2 4 5 | 3 5 6 | 3 6 7 | -------------------------------------------------------------------------------- /src/main/c/datafiles/treelist-out: -------------------------------------------------------------------------------- 1 | 2 | item 1 not found 3 | new item: 1 4 | item 1 found 5 | item 2 not found 6 | 1 7 | new item: 2 8 | 1 2 9 | new item: 3 10 | 1 2 3 11 | item 1 found 12 | item 2 found 13 | item 3 found 14 | item 4 not found 15 | 1 2 3 16 | deleting item 1 17 | 2 3 18 | 2 3 19 | deleting item 3 20 | 2 21 | 2 22 | deleting item 2 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/c/datafiles/treeplus: -------------------------------------------------------------------------------- 1 | 5 7 2 | 1 2 3 | 1 3 4 | 2 4 5 | 2 5 6 | 3 6 7 | 1 6 8 | 4 5 9 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tri1: -------------------------------------------------------------------------------- 1 | 20 2 | 0.156027 0.0230211 3 | 0.863934 0.0346243 4 | 0.649767 0.172752 5 | 0.850717 0.36679 6 | 0.763686 0.347193 7 | 0.958815 0.490144 8 | 0.565076 0.284126 9 | 0.761634 0.451526 10 | 0.53912 0.347704 11 | 0.632116 0.475199 12 | 0.614403 0.713983 13 | 0.603881 0.703203 14 | 0.411401 0.624278 15 | 0.497935 0.837847 16 | 0.352782 0.528943 17 | 0.298858 0.750498 18 | 0.25534 0.651021 19 | 0.218284 0.417186 20 | 0.0603822 0.25343 21 | 0.0435178 0.0994771 22 | 23 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tri1-out: -------------------------------------------------------------------------------- 1 | area via triangulation = 0.343249 2 | area slick = 0.343249 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tri2: -------------------------------------------------------------------------------- 1 | 10 2 | 0.0978873 0.00764079 3 | 0.805176 0.794081 4 | 0.793407 0.801953 5 | 0.714653 0.763816 6 | 0.804108 0.924603 7 | 0.308935 0.404083 8 | 0.183881 0.739017 9 | 0.0998984 0.304136 10 | 0.0363688 0.86674 11 | 0.0762124 0.171322 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tri2-out: -------------------------------------------------------------------------------- 1 | area via triangulation = 0.170820 2 | area slick = 0.170820 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tri3: -------------------------------------------------------------------------------- 1 | 10 2 | 0.631158 0.0229076 3 | 0.729045 0.0305484 4 | 0.966014 0.223357 5 | 0.916505 0.259091 6 | 0.770122 0.14796 7 | 0.846335 0.319282 8 | 0.946233 0.623419 9 | 0.912926 0.769565 10 | 0.718101 0.563646 11 | 0.255167 0.0275019 12 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tri3-out: -------------------------------------------------------------------------------- 1 | area via triangulation = 0.224567 2 | area slick = 0.224567 3 | -------------------------------------------------------------------------------- /src/main/c/datafiles/tsp48-in: -------------------------------------------------------------------------------- 1 | 48 2 | 1 6734 1453 3 | 2 2233 10 4 | 3 5530 1424 5 | 4 401 841 6 | 5 3082 1644 7 | 6 7608 4458 8 | 7 7573 3716 9 | 8 7265 1268 10 | 9 6898 1885 11 | 10 1112 2049 12 | 11 5468 2606 13 | 12 5989 2873 14 | 13 4706 2674 15 | 14 4612 2035 16 | 15 6347 2683 17 | 16 6107 669 18 | 17 7611 5184 19 | 18 7462 3590 20 | 19 7732 4723 21 | 20 5900 3561 22 | 21 4483 3369 23 | 22 6101 1110 24 | 23 5199 2182 25 | 24 1633 2809 26 | 25 4307 2322 27 | 26 675 1006 28 | 27 7555 4819 29 | 28 7541 3981 30 | 29 3177 756 31 | 30 7352 4506 32 | 31 7545 2801 33 | 32 3245 3305 34 | 33 6426 3173 35 | 34 4608 1198 36 | 35 23 2216 37 | 36 7248 3779 38 | 37 7762 4595 39 | 38 7392 2244 40 | 39 3484 2829 41 | 40 6271 2135 42 | 41 4985 140 43 | 42 1916 1569 44 | 43 7280 4899 45 | 44 7509 3239 46 | 45 10 2676 47 | 46 6807 2993 48 | 47 5185 3258 49 | 48 3023 1942 50 | 48 51 | 1 52 | 8 53 | 38 54 | 31 55 | 44 56 | 18 57 | 7 58 | 28 59 | 6 60 | 37 61 | 19 62 | 27 63 | 17 64 | 43 65 | 30 66 | 36 67 | 46 68 | 33 69 | 20 70 | 47 71 | 21 72 | 32 73 | 39 74 | 48 75 | 5 76 | 42 77 | 24 78 | 10 79 | 45 80 | 35 81 | 4 82 | 26 83 | 2 84 | 29 85 | 34 86 | 41 87 | 16 88 | 22 89 | 3 90 | 23 91 | 14 92 | 25 93 | 13 94 | 11 95 | 12 96 | 15 97 | 40 98 | 9 99 | -------------------------------------------------------------------------------- /src/main/c/datafiles/war-in: -------------------------------------------------------------------------------- 1 | 4c 2 | 6d 3 | 4c 6h 5s 4 | 3h 6d 6s 5 | 2s 3s 4s 5s 6s 7s 6 | 8d 9d Td Jd Qd Kd 7 | Zd 8 | 2f 9 | 4d Ks As 4h Jh 6h Jd Qs Qh Kc 10 | 8d 8c 9c 7c 5d 4c Js Qc 5s 7h 11 | 4d Ks As 4h Jh 6h Jd Qs Qh 6s 6c 2c Kc 4s Ah 3h Qd 2h 7s 9s 3c 8h Kd 7h Th Td 12 | 8d 8c 9c 7c 5d 4c Js Qc 5s Ts Jc Ad 7d Kh Tc 3s 8s 2d 2s 5h 6d Ac 5c 9h 3d 9d 13 | 6d 9d 8c 4s Kc 7c 4d Tc Kd 3s 5h 2h Ks 5c 2s Qh 8d 7d 3d Ah Js Jd 4c Jh 6c Qc 14 | 9h Qd Qs 9s Ac 8h Td Jc 7s 2d 6s As 4h Ts 6h 2c Kh Th 7h 5s 9c 5d Ad 3h 8s 3c 15 | Ah As 4c 3s 7d Jc 5h 8s Qc Kh Td 3h 5c 9h 8c Qs 3d Ks 4d Kd 6c 6s 7h Qh 3c Jd 16 | 2h 8h 7s 2c 5d 7c 2d Tc Jh Ac 9s 9c 5s Qd 4s Js 6d Kc 2s Th 8d 9d 4h Ad 6h Ts 17 | -------------------------------------------------------------------------------- /src/main/c/datafiles/war-out: -------------------------------------------------------------------------------- 1 | b wins in 1 steps 2 | a wins in 3 steps 3 | b wins in 6 steps 4 | Warning: bad input value=Z, suit=d 5 | Warning: bad input value=2, suit=f 6 | a and b tie in 1 steps 7 | a wins in 14 steps 8 | game tied after 100000 steps, |a|=26 |b|=26 9 | game tied after 100000 steps, |a|=26 |b|=26 10 | game tied after 100000 steps, |a|=26 |b|=26 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/war-out-old: -------------------------------------------------------------------------------- 1 | b wins in 1 steps 2 | a wins in 3 steps 3 | b wins in 6 steps 4 | Warning: bad input value=90, suit=100 5 | Warning: bad input value=50, suit=102 6 | game tied after 1 steps, |a|=0 |b|=0 7 | a wins in 14 steps 8 | a and b tie in 100000 steps 9 | a and b tie in 100000 steps 10 | a and b tie in 100000 steps 11 | -------------------------------------------------------------------------------- /src/main/c/datafiles/wgrid: -------------------------------------------------------------------------------- 1 | 25 40 2 | 1 2 1994 3 | 2 3 6909 4 | 3 4 6004 5 | 4 5 859 6 | 6 7 2996 7 | 7 8 9004 8 | 8 9 1208 9 | 9 10 443 10 | 11 12 3750 11 | 12 13 8541 12 | 13 14 4234 13 | 14 15 4360 14 | 16 17 2225 15 | 17 18 335 16 | 18 19 6863 17 | 19 20 6293 18 | 21 22 5588 19 | 22 23 739 20 | 23 24 7670 21 | 24 25 7813 22 | 1 6 6533 23 | 2 7 7350 24 | 3 8 5687 25 | 4 9 5173 26 | 5 10 4007 27 | 6 11 358 28 | 7 12 6976 29 | 8 13 812 30 | 9 14 8145 31 | 10 15 6554 32 | 11 16 3709 33 | 12 17 5610 34 | 13 18 5015 35 | 14 19 4660 36 | 15 20 3387 37 | 16 21 7957 38 | 17 22 2716 39 | 18 23 7394 40 | 19 24 5753 41 | 20 25 9575 42 | -------------------------------------------------------------------------------- /src/main/c/datafiles/wgrid-dijkstra-out: -------------------------------------------------------------------------------- 1 | 2 | 1 3 | 1 2 4 | 1 2 3 5 | 1 2 3 4 6 | 1 2 3 4 5 7 | 1 6 8 | 1 2 7 9 | 1 2 3 8 10 | 1 2 3 8 9 11 | 1 2 3 8 9 10 12 | 1 6 11 13 | 1 6 11 12 14 | 1 2 3 8 13 15 | 1 2 3 8 13 14 16 | 1 2 3 8 9 10 15 17 | 1 6 11 16 18 | 1 6 11 16 17 19 | 1 6 11 16 17 18 20 | 1 6 11 16 17 18 19 21 | 1 2 3 8 9 10 15 20 22 | 1 6 11 16 21 23 | 1 6 11 16 17 22 24 | 1 6 11 16 17 22 23 25 | 1 6 11 16 17 22 23 24 26 | 1 6 11 16 17 22 23 24 25 27 | -------------------------------------------------------------------------------- /src/main/c/datafiles/wgrid-disconnected1: -------------------------------------------------------------------------------- 1 | 30 37 2 | 1 2 1994 3 | 3 4 6004 4 | 4 5 859 5 | 6 7 2996 6 | 7 8 9004 7 | 8 9 1208 8 | 9 10 443 9 | 11 12 3750 10 | 12 13 8541 11 | 13 14 4234 12 | 14 15 4360 13 | 16 17 2225 14 | 17 18 335 15 | 18 19 6863 16 | 19 20 6293 17 | 21 22 5588 18 | 22 23 739 19 | 23 24 7670 20 | 24 25 7813 21 | 3 8 5687 22 | 4 9 5173 23 | 5 10 4007 24 | 6 11 358 25 | 7 12 6976 26 | 8 13 812 27 | 9 14 8145 28 | 10 15 6554 29 | 11 16 3709 30 | 12 17 5610 31 | 13 18 5015 32 | 14 19 4660 33 | 15 20 3387 34 | 16 21 7957 35 | 17 22 2716 36 | 18 23 7394 37 | 19 24 5753 38 | 20 25 9575 39 | -------------------------------------------------------------------------------- /src/main/c/datafiles/wgrid-disconnected2: -------------------------------------------------------------------------------- 1 | 30 34 2 | 1 2 1994 3 | 2 3 6909 4 | 3 4 6004 5 | 4 5 859 6 | 6 7 2996 7 | 7 8 9004 8 | 8 9 1208 9 | 9 10 443 10 | 11 12 3750 11 | 12 13 8541 12 | 13 14 4234 13 | 14 15 4360 14 | 16 17 2225 15 | 17 18 335 16 | 18 19 6863 17 | 21 22 5588 18 | 22 23 739 19 | 23 24 7670 20 | 24 25 7813 21 | 1 6 6533 22 | 2 7 7350 23 | 3 8 5687 24 | 4 9 5173 25 | 5 10 4007 26 | 6 11 358 27 | 7 12 6976 28 | 8 13 812 29 | 9 14 8145 30 | 10 15 6554 31 | 11 16 3709 32 | 12 17 5610 33 | 13 18 5015 34 | 14 19 4660 35 | 20 25 9575 36 | -------------------------------------------------------------------------------- /src/main/c/datafiles/wgrid-prim-out: -------------------------------------------------------------------------------- 1 | Out of Prim 2 | 3 | 1 4 | 1 2 5 | 1 6 11 16 17 18 13 8 3 6 | 1 6 11 16 17 18 13 8 9 10 5 4 7 | 1 6 11 16 17 18 13 8 9 10 5 8 | 1 6 9 | 1 6 7 10 | 1 6 11 16 17 18 13 8 11 | 1 6 11 16 17 18 13 8 9 12 | 1 6 11 16 17 18 13 8 9 10 13 | 1 6 11 14 | 1 6 11 12 15 | 1 6 11 16 17 18 13 16 | 1 6 11 16 17 18 13 14 17 | 1 6 11 16 17 18 13 14 15 18 | 1 6 11 16 19 | 1 6 11 16 17 20 | 1 6 11 16 17 18 21 | 1 6 11 16 17 18 13 14 19 22 | 1 6 11 16 17 18 13 14 15 20 23 | 1 6 11 16 17 22 21 24 | 1 6 11 16 17 22 25 | 1 6 11 16 17 22 23 26 | 1 6 11 16 17 18 13 14 19 24 27 | 1 6 11 16 17 18 13 14 19 24 25 28 | -------------------------------------------------------------------------------- /src/main/c/datafiles/wgrid.m: -------------------------------------------------------------------------------- 1 | Graph[{{{1, 2}, EdgeWeight -> 1994}, {{2, 3}, EdgeWeight -> 6909}, 2 | {{3, 4}, EdgeWeight -> 6004}, {{4, 5}, EdgeWeight -> 859}, 3 | {{6, 7}, EdgeWeight -> 2996}, {{7, 8}, EdgeWeight -> 9004}, 4 | {{8, 9}, EdgeWeight -> 1208}, {{9, 10}, EdgeWeight -> 443}, 5 | {{11, 12}, EdgeWeight -> 3750}, {{12, 13}, EdgeWeight -> 8541}, 6 | {{13, 14}, EdgeWeight -> 4234}, {{14, 15}, EdgeWeight -> 4360}, 7 | {{16, 17}, EdgeWeight -> 2225}, {{17, 18}, EdgeWeight -> 335}, 8 | {{18, 19}, EdgeWeight -> 6863}, {{19, 20}, EdgeWeight -> 6293}, 9 | {{21, 22}, EdgeWeight -> 5588}, {{22, 23}, EdgeWeight -> 739}, 10 | {{23, 24}, EdgeWeight -> 7670}, {{24, 25}, EdgeWeight -> 7813}, 11 | {{1, 6}, EdgeWeight -> 6533}, {{2, 7}, EdgeWeight -> 7350}, 12 | {{3, 8}, EdgeWeight -> 5687}, {{4, 9}, EdgeWeight -> 5173}, 13 | {{5, 10}, EdgeWeight -> 4007}, {{6, 11}, EdgeWeight -> 358}, 14 | {{7, 12}, EdgeWeight -> 6976}, {{8, 13}, EdgeWeight -> 812}, 15 | {{9, 14}, EdgeWeight -> 8145}, {{10, 15}, EdgeWeight -> 6554}, 16 | {{11, 16}, EdgeWeight -> 3709}, {{12, 17}, EdgeWeight -> 5610}, 17 | {{13, 18}, EdgeWeight -> 5015}, {{14, 19}, EdgeWeight -> 4660}, 18 | {{15, 20}, EdgeWeight -> 3387}, {{16, 21}, EdgeWeight -> 7957}, 19 | {{17, 22}, EdgeWeight -> 2716}, {{18, 23}, EdgeWeight -> 7394}, 20 | {{19, 24}, EdgeWeight -> 5753}, {{20, 25}, EdgeWeight -> 9575}}, 21 | {{{1., 1.}}, {{2., 1.}}, {{3., 1.}}, {{4., 1.}}, {{5., 1.}}, {{1., 2.}}, 22 | {{2., 2.}}, {{3., 2.}}, {{4., 2.}}, {{5., 2.}}, {{1., 3.}}, {{2., 3.}}, 23 | {{3., 3.}}, {{4., 3.}}, {{5., 3.}}, {{1., 4.}}, {{2., 4.}}, {{3., 4.}}, 24 | {{4., 4.}}, {{5., 4.}}, {{1., 5.}}, {{2., 5.}}, {{3., 5.}}, {{4., 5.}}, 25 | {{5., 5.}}}] 26 | -------------------------------------------------------------------------------- /src/main/c/distance.c: -------------------------------------------------------------------------------- 1 | /* distance.c 2 | 3 | Compute Euclidian distances 4 | 5 | by: Steven Skiena 6 | */ 7 | 8 | /* 9 | Copyright 2003 by Steven S. Skiena; all rights reserved. 10 | 11 | Permission is granted for use in non-commerical applications 12 | provided this copyright notice remains intact and unchanged. 13 | 14 | This program appears in my book: 15 | 16 | "Programming Challenges: The Programming Contest Training Manual" 17 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 18 | 19 | See our website www.programming-challenges.com for additional information. 20 | 21 | This book can be ordered from Amazon.com at 22 | 23 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 24 | 25 | */ 26 | 27 | 28 | #define DIMENSION 3 29 | #include 30 | 31 | typedef int point[DIMENSION]; 32 | 33 | main(){ 34 | point a={6,2,3}; 35 | point b={6,3,4}; 36 | double distance(); 37 | 38 | printf("distance = %f\n",distance(a,b)); 39 | } 40 | 41 | double distance(point a, point b) 42 | { 43 | int i; 44 | double d=0.0; 45 | 46 | for (i=0; i 3 | 4 | int main() { 5 | long p,q,r; 6 | 7 | while (scanf("%ld %ld",&p,&q) 8 | !=EOF) { 9 | if (q>p) r=q-p; 10 | else r=p-q; 11 | 12 | printf("%ld\n",r); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /src/main/c/original/backtrack.c: -------------------------------------------------------------------------------- 1 | /* 2 | backtrack.c 3 | A generic backtracking implementation 4 | 5 | begun: March 27, 2002 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | 30 | #include "backtrack.h" 31 | #include "bool.h" 32 | 33 | bool finished = FALSE; /* found all solutions yet? */ 34 | 35 | backtrack(int a[], int k, data input) 36 | { 37 | int c[MAXCANDIDATES]; /* candidates for next position */ 38 | int ncandidates; /* next position candidate count */ 39 | int i; /* counter */ 40 | 41 | if (is_a_solution(a,k,input)) 42 | process_solution(a,k,input); 43 | else { 44 | k = k+1; 45 | construct_candidates(a,k,input,c,&ncandidates); 46 | for (i=0; invertices; i++) 67 | if (discovered[i] == FALSE) { 68 | c = c+1; 69 | printf("Component %d:",c); 70 | dfs(g,i); 71 | printf("\n"); 72 | } 73 | } 74 | 75 | main() 76 | { 77 | graph g; 78 | 79 | read_graph(&g,FALSE); 80 | print_graph(&g); 81 | 82 | connected_components(&g); 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/main/c/original/dfs-demo.c: -------------------------------------------------------------------------------- 1 | 2 | /* dfs-demo.c 3 | Driver program demonstrating depth-first search. 4 | 5 | by: Steven Skiena 6 | begun: March 27, 2002 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #include "bool.h" 30 | #include "graph.h" 31 | #include "queue.h" 32 | 33 | extern bool processed[]; /* which vertices have been processed */ 34 | extern bool discovered[]; /* which vertices have been found */ 35 | extern int parent[]; /* discovery relation */ 36 | 37 | 38 | process_vertex_early(int v) 39 | { 40 | printf("processed vertex %d\n",v); 41 | } 42 | 43 | process_vertex_late(int v) 44 | { 45 | } 46 | 47 | process_edge(int x, int y) 48 | { 49 | if (parent[x] == y) 50 | printf("processed tree edge (%d,%d)\n",x,y); 51 | else 52 | printf("processed back edge (%d,%d)\n",x,y); 53 | } 54 | 55 | bool valid_edge(int e) 56 | { 57 | return (TRUE); 58 | } 59 | 60 | 61 | main() 62 | { 63 | graph g; 64 | int i; 65 | 66 | read_graph(&g,FALSE); 67 | print_graph(&g); 68 | 69 | initialize_search(&g); 70 | dfs(&g,1); 71 | 72 | for (i=1; i<=g.nvertices; i++) 73 | find_path(1,i,parent); 74 | printf("\n"); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/c/original/distance.c: -------------------------------------------------------------------------------- 1 | /* distance.c 2 | 3 | Compute Euclidian distances 4 | 5 | by: Steven Skiena 6 | */ 7 | 8 | /* 9 | Copyright 2003 by Steven S. Skiena; all rights reserved. 10 | 11 | Permission is granted for use in non-commerical applications 12 | provided this copyright notice remains intact and unchanged. 13 | 14 | This program appears in my book: 15 | 16 | "Programming Challenges: The Programming Contest Training Manual" 17 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 18 | 19 | See our website www.programming-challenges.com for additional information. 20 | 21 | This book can be ordered from Amazon.com at 22 | 23 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 24 | 25 | */ 26 | 27 | 28 | #define DIMENSION 3 29 | #include 30 | 31 | typedef int point[DIMENSION]; 32 | 33 | main(){ 34 | point a={6,2,3}; 35 | point b={6,3,4}; 36 | double distance(); 37 | 38 | printf("distance = %f\n",distance(a,b)); 39 | } 40 | 41 | double distance(point a, point b) 42 | { 43 | int i; 44 | double d=0.0; 45 | 46 | for (i=0; i 31 | #include 32 | 33 | 34 | prime_factorization(long x) 35 | { 36 | long i; /* counter */ 37 | long c; /* remaining product to factor */ 38 | 39 | c = x; 40 | 41 | while ((c % 2) == 0) { 42 | printf("%ld\n",2); 43 | c = c / 2; 44 | } 45 | 46 | i = 3; 47 | 48 | while (i <= (sqrt(c)+1)) { 49 | if ((c % i) == 0) { 50 | printf("%ld\n",i); 51 | c = c / i; 52 | } 53 | else 54 | i = i + 2; 55 | } 56 | 57 | if (c > 1) printf("%ld\n",c); 58 | } 59 | 60 | 61 | 62 | main() { 63 | long p; 64 | 65 | while (scanf("%ld",&p)!=EOF) { 66 | 67 | printf("prime factorization of p=%ld \n",p); 68 | prime_factorization(p); 69 | 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/main/c/original/queue.h: -------------------------------------------------------------------------------- 1 | 2 | /* queue.h 3 | 4 | Header file for queue implementation 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #define QUEUESIZE 1000 30 | 31 | typedef struct { 32 | int q[QUEUESIZE+1]; /* body of queue */ 33 | int first; /* position of first element */ 34 | int last; /* position of last element */ 35 | int count; /* number of queue elements */ 36 | } queue; 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/c/original/sentinel.c: -------------------------------------------------------------------------------- 1 | /* 2 | sentinel.c 3 | 4 | Example search program using sentinels 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | 10 | /* 11 | Copyright 2003 by Steven S. Skiena; all rights reserved. 12 | 13 | Permission is granted for use in non-commerical applications 14 | provided this copyright notice remains intact and unchanged. 15 | 16 | This program appears in my book: 17 | 18 | "Programming Challenges: The Programming Contest Training Manual" 19 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 20 | 21 | See our website www.programming-challenges.com for additional information. 22 | 23 | This book can be ordered from Amazon.com at 24 | 25 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 26 | 27 | */ 28 | 29 | #define MAXINT 1000000 30 | int a[100]; 31 | 32 | main() { 33 | int i,n, x; 34 | 35 | for (i=1; i<=50; i++) a[i]=i; 36 | n=50; 37 | x = -1; 38 | 39 | i = n; 40 | while ((a[i]>=x) && (i>=1)) { 41 | a[i] = a[i-1]; 42 | i=i-1; 43 | } 44 | a[i+1] = x; 45 | 46 | printf("without sentinel, i=%d a[1]=%d\n",i,a[1]); 47 | for (i=1; i<=10; i++) printf("%d ",a[i]); 48 | x = -2; 49 | 50 | i = n; 51 | a[0] = - MAXINT; 52 | while (a[i] >= x) { 53 | a[i] = a[i-1]; 54 | i=i-1; 55 | } 56 | a[i+1] = x; 57 | 58 | printf("\nwith sentinel, i=%d a[1]=%d\n",i,a[1]); 59 | for (i=1; i<=10; i++) printf("%d ",a[i]); 60 | 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /src/main/c/original/subsets.c: -------------------------------------------------------------------------------- 1 | /* subsets.c 2 | 3 | Construct all subsets via backtracking. 4 | 5 | by: Steven Skiena 6 | begun: March 27, 2002 7 | */ 8 | 9 | 10 | /* 11 | Copyright 2003 by Steven S. Skiena; all rights reserved. 12 | 13 | Permission is granted for use in non-commerical applications 14 | provided this copyright notice remains intact and unchanged. 15 | 16 | This program appears in my book: 17 | 18 | "Programming Challenges: The Programming Contest Training Manual" 19 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 20 | 21 | See our website www.programming-challenges.com for additional information. 22 | 23 | This book can be ordered from Amazon.com at 24 | 25 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 26 | 27 | */ 28 | 29 | 30 | #include "bool.h" 31 | #include "backtrack.h" 32 | 33 | 34 | process_solution(int a[], int k) 35 | { 36 | int i; /* counter */ 37 | 38 | printf("{"); 39 | for (i=1; i<=k; i++) 40 | if (a[i] == TRUE) printf(" %d",i); 41 | 42 | printf(" }\n"); 43 | } 44 | 45 | is_a_solution(int a[], int k, int n) 46 | { 47 | return (k == n); 48 | } 49 | 50 | make_move(int a[], int k, int n) 51 | { 52 | } 53 | 54 | unmake_move(int a[], int k, int n) 55 | { 56 | } 57 | 58 | 59 | 60 | /* What are possible elements of the next slot in the permutation? */ 61 | 62 | construct_candidates(int a[], int k, int n, int c[], int *ncandidates) 63 | { 64 | c[0] = TRUE; 65 | c[1] = FALSE; 66 | *ncandidates = 2; 67 | } 68 | 69 | 70 | 71 | main() 72 | { 73 | int a[NMAX]; /* solution vector */ 74 | 75 | backtrack(a,0,3); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/main/c/original/tsp.h: -------------------------------------------------------------------------------- 1 | 2 | #define NMAX 1000 /* maximum number of points */ 3 | 4 | typedef struct { 5 | int x, y; /* x and y coordinates of point */ 6 | } point; 7 | 8 | typedef struct { 9 | int n; /* how many points in problem? */ 10 | point p[NMAX+1]; /* array of points */ 11 | } tsp_instance; 12 | 13 | typedef struct { 14 | int n; /* how many elements in permutation? */ 15 | int p[NMAX+1]; /* array if indices */ 16 | } tsp_solution; 17 | 18 | -------------------------------------------------------------------------------- /src/main/c/original/wgraph.h: -------------------------------------------------------------------------------- 1 | 2 | /* wgraph.h 3 | 4 | Header file for weighted graph type 5 | 6 | by Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | #define MAXV 100 /* maximum number of vertices */ 29 | #define MAXDEGREE 50 /* maximum outdegree of a vertex */ 30 | 31 | typedef struct { 32 | int v; /* neighboring vertex */ 33 | int weight; /* edge weight */ 34 | } edge; 35 | 36 | typedef struct { 37 | edge edges[MAXV+1][MAXDEGREE]; /* adjacency info */ 38 | int degree[MAXV+1]; /* outdegree of each vertex */ 39 | int nvertices; /* number of vertices in the graph */ 40 | int nedges; /* number of edges in the graph */ 41 | } graph; 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/c/permutations.c: -------------------------------------------------------------------------------- 1 | /* permutations.c 2 | 3 | Construct all permutations via backtracking. 4 | 5 | by: Steven Skiena 6 | begun: March 27, 2002 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #include "bool.h" 30 | #include "backtrack.h" 31 | 32 | 33 | process_solution(int a[], int k) 34 | { 35 | int i; /* counter */ 36 | 37 | for (i=1; i<=k; i++) printf(" %d",a[i]); 38 | 39 | printf("\n"); 40 | } 41 | 42 | is_a_solution(int a[], int k, int n) 43 | { 44 | return (k == n); 45 | } 46 | 47 | make_move(int a[], int k, int n) 48 | { 49 | } 50 | 51 | unmake_move(int a[], int k, int n) 52 | { 53 | } 54 | 55 | 56 | /* What are possible elements of the next slot in the permutation? */ 57 | 58 | construct_candidates(int a[], int k, int n, int c[], int *ncandidates) 59 | { 60 | int i; /* counter */ 61 | bool in_perm[NMAX]; /* what is now in the permutation? */ 62 | 63 | for (i=1; i 31 | #include 32 | 33 | 34 | prime_factorization(long x) 35 | { 36 | long i; /* counter */ 37 | long c; /* remaining product to factor */ 38 | 39 | c = x; 40 | 41 | while ((c % 2) == 0) { 42 | printf("%ld\n",2); 43 | c = c / 2; 44 | } 45 | 46 | i = 3; 47 | 48 | while (i <= (sqrt(c)+1)) { 49 | if ((c % i) == 0) { 50 | printf("%ld\n",i); 51 | c = c / i; 52 | } 53 | else 54 | i = i + 2; 55 | } 56 | 57 | if (c > 1) printf("%ld\n",c); 58 | } 59 | 60 | 61 | 62 | main() { 63 | long p; 64 | 65 | while (scanf("%ld",&p)!=EOF) { 66 | 67 | printf("prime factorization of p=%ld \n",p); 68 | prime_factorization(p); 69 | 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/main/c/priority_queue.h: -------------------------------------------------------------------------------- 1 | 2 | /* priority_queue.h 3 | 4 | Header file for queue implementation 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #include "item.h" 30 | 31 | #define PQ_SIZE 1000 32 | 33 | typedef struct { 34 | item_type q[PQ_SIZE+1]; /* body of queue */ 35 | int n; /* number of queue elements */ 36 | } priority_queue; 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/c/queue.h: -------------------------------------------------------------------------------- 1 | 2 | /* queue.h 3 | 4 | Header file for queue implementation 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #define QUEUESIZE 1000 30 | 31 | /* 32 | #include "item.h" 33 | */ 34 | 35 | typedef struct { 36 | int /*item_type*/ q[QUEUESIZE+1]; /* body of queue */ 37 | int first; /* position of first element */ 38 | int last; /* position of last element */ 39 | int count; /* number of queue elements */ 40 | } queue; 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/c/sentinel.c: -------------------------------------------------------------------------------- 1 | /* 2 | sentinel.c 3 | 4 | Example search program using sentinels 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | 10 | /* 11 | Copyright 2003 by Steven S. Skiena; all rights reserved. 12 | 13 | Permission is granted for use in non-commerical applications 14 | provided this copyright notice remains intact and unchanged. 15 | 16 | This program appears in my book: 17 | 18 | "Programming Challenges: The Programming Contest Training Manual" 19 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 20 | 21 | See our website www.programming-challenges.com for additional information. 22 | 23 | This book can be ordered from Amazon.com at 24 | 25 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 26 | 27 | */ 28 | 29 | #define MAXINT 1000000 30 | int a[100]; 31 | 32 | main() { 33 | int i,n, x; 34 | 35 | for (i=1; i<=20; i++) a[i]=i; 36 | n=20; 37 | x = -1; 38 | 39 | i = n; 40 | while ((a[i]>=x) && (i>=1)) { 41 | a[i+1] = a[i]; 42 | i=i-1; 43 | } 44 | a[i+1] = x; 45 | 46 | printf("without sentinel, i=%d a[1]=%d\n",i,a[1]); 47 | for (i=1; i<=25; i++) printf("%d ",a[i]); 48 | x = -2; 49 | 50 | n = 21; 51 | i = n; 52 | a[0] = - MAXINT; 53 | while (a[i] >= x) { 54 | a[i+1] = a[i]; 55 | i=i-1; 56 | } 57 | a[i+1] = x; 58 | 59 | printf("\nwith sentinel, i=%d a[1]=%d\n",i,a[1]); 60 | for (i=1; i<=25; i++) printf("%d ",a[i]); 61 | 62 | 63 | } 64 | 65 | -------------------------------------------------------------------------------- /src/main/c/set_union.h: -------------------------------------------------------------------------------- 1 | 2 | /* set_union.h 3 | 4 | Header file for union-find data structure implementation 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #define SET_SIZE 1000 30 | 31 | typedef struct { 32 | int p[SET_SIZE+1]; /* parent element */ 33 | int size[SET_SIZE+1]; /* number of elements in subtree i */ 34 | int n; /* number of elements in set */ 35 | } set_union; 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/main/c/stack.c: -------------------------------------------------------------------------------- 1 | 2 | /* stack.c 3 | 4 | Implementation of a LIFO stack abstract data type. 5 | 6 | by: Steven Skiena 7 | begun: March 27, 2002 8 | */ 9 | 10 | 11 | /* 12 | Copyright 2003 by Steven S. Skiena; all rights reserved. 13 | 14 | Permission is granted for use in non-commerical applications 15 | provided this copyright notice remains intact and unchanged. 16 | 17 | This program appears in my book: 18 | 19 | "Programming Challenges: The Programming Contest Training Manual" 20 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 21 | 22 | See our website www.programming-challenges.com for additional information. 23 | 24 | This book can be ordered from Amazon.com at 25 | 26 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 27 | 28 | */ 29 | 30 | 31 | #include "stack.h" 32 | #include "bool.h" 33 | 34 | 35 | init_stack(stack *s) 36 | { 37 | s->top = -1; 38 | s->count = 0; 39 | } 40 | 41 | 42 | push(stack *s, int x) 43 | { 44 | if (s->count >= STACKSIZE) 45 | printf("Warning: stack overflow push x=%d\n",x); 46 | else { 47 | s->top = s->top + 1; 48 | s->s[ s->top ] = x; 49 | s->count = s->count + 1; 50 | } 51 | } 52 | 53 | int pop(stack *s) 54 | { 55 | int x; 56 | 57 | if (s->count <= 0) printf("Warning: empty stack pop.\n"); 58 | else { 59 | x = s->s[ s->top ]; 60 | s->top = s->top - 1; 61 | s->count = s->count - 1; 62 | } 63 | 64 | return(x); 65 | } 66 | 67 | int empty_stack(stack *s) 68 | { 69 | if (s->count <= 0) return (TRUE); 70 | else return (FALSE); 71 | } 72 | 73 | print_stack(stack *s) 74 | { 75 | int i; /* counter */ 76 | 77 | for (i=(s->count-1); i>=0; i--) 78 | printf("%d ",s->s[i]); 79 | 80 | printf("\n"); 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /src/main/c/stack.h: -------------------------------------------------------------------------------- 1 | 2 | /* stack.h 3 | 4 | Header file for queue implementation 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #define STACKSIZE 1000 30 | 31 | typedef struct { 32 | int s[STACKSIZE+1]; /* body of queue */ 33 | int top; /* position of top element */ 34 | int count; /* number of stack elements */ 35 | } stack; 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/c/subsets.c: -------------------------------------------------------------------------------- 1 | /* subsets.c 2 | 3 | Construct all subsets via backtracking. 4 | 5 | by: Steven Skiena 6 | begun: March 27, 2002 7 | */ 8 | 9 | 10 | /* 11 | Copyright 2003 by Steven S. Skiena; all rights reserved. 12 | 13 | Permission is granted for use in non-commerical applications 14 | provided this copyright notice remains intact and unchanged. 15 | 16 | This program appears in my book: 17 | 18 | "Programming Challenges: The Programming Contest Training Manual" 19 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 20 | 21 | See our website www.programming-challenges.com for additional information. 22 | 23 | This book can be ordered from Amazon.com at 24 | 25 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 26 | 27 | */ 28 | 29 | 30 | #include "bool.h" 31 | #include "backtrack.h" 32 | 33 | 34 | process_solution(int a[], int k) 35 | { 36 | int i; /* counter */ 37 | 38 | printf("{"); 39 | for (i=1; i<=k; i++) 40 | if (a[i] == TRUE) printf(" %d",i); 41 | 42 | printf(" }\n"); 43 | } 44 | 45 | is_a_solution(int a[], int k, int n) 46 | { 47 | return (k == n); 48 | } 49 | 50 | make_move(int a[], int k, int n) 51 | { 52 | } 53 | 54 | unmake_move(int a[], int k, int n) 55 | { 56 | } 57 | 58 | 59 | 60 | /* What are possible elements of the next slot in the permutation? */ 61 | 62 | construct_candidates(int a[], int k, int n, int c[], int *ncandidates) 63 | { 64 | c[0] = TRUE; 65 | c[1] = FALSE; 66 | *ncandidates = 2; 67 | } 68 | 69 | 70 | 71 | main() 72 | { 73 | int a[NMAX]; /* solution vector */ 74 | 75 | backtrack(a,0,3); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/champ: -------------------------------------------------------------------------------- 1 | 800750003 2 | 030048020 3 | 100000006 4 | 340070008 5 | 790480031 6 | 208000074 7 | 500814007 8 | 080327040 9 | 400569002 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/evil0: -------------------------------------------------------------------------------- 1 | 300600000 2 | 500007000 3 | 870090400 4 | 080050000 5 | 064000790 6 | 000020030 7 | 001040078 8 | 000300002 9 | 000005004 10 | 11 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/evil1: -------------------------------------------------------------------------------- 1 | 000000000 2 | 000000000 3 | 000000000 4 | 000000000 5 | 000000000 6 | 000000000 7 | 000000000 8 | 000000000 9 | 000000000 10 | 11 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/evil18: -------------------------------------------------------------------------------- 1 | 070000009 2 | 000700500 3 | 032050080 4 | 007630054 5 | 000080000 6 | 360095200 7 | 020010730 8 | 004300000 9 | 100000090 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/evil2: -------------------------------------------------------------------------------- 1 | 100000000 2 | 005406100 3 | 080000090 4 | 004010500 5 | 070090020 6 | 006080300 7 | 020100070 8 | 000503600 9 | 000000000 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/minimum1: -------------------------------------------------------------------------------- 1 | 000000010 2 | 400000000 3 | 020000000 4 | 000050407 5 | 008000300 6 | 001090000 7 | 300400200 8 | 050100000 9 | 000806000 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/minimum2: -------------------------------------------------------------------------------- 1 | 000000010 2 | 400000000 3 | 020000000 4 | 000050604 5 | 008000300 6 | 001090000 7 | 300400200 8 | 050100000 9 | 000807000 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/minimum3: -------------------------------------------------------------------------------- 1 | 000000012 2 | 000035000 3 | 000600070 4 | 700000300 5 | 000400800 6 | 100000000 7 | 000120000 8 | 080000040 9 | 050000600 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/puzzle: -------------------------------------------------------------------------------- 1 | 800000320 2 | 070304968 3 | 300060500 4 | 000831400 5 | 400090002 6 | 001425000 7 | 008050006 8 | 123607050 9 | 057000004 10 | -------------------------------------------------------------------------------- /src/main/c/sudoku-examples/puzzle0: -------------------------------------------------------------------------------- 1 | 070020000 2 | 420501003 3 | 509307400 4 | 760000004 5 | 008609300 6 | 900000012 7 | 004702106 8 | 600803079 9 | 000090040 10 | -------------------------------------------------------------------------------- /src/main/c/tree.h: -------------------------------------------------------------------------------- 1 | 2 | /* tree.h 3 | 4 | Header file for binary search tree implementation 5 | 6 | by: Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | /*typedef int item_type;*/ 29 | 30 | #include "item.h" 31 | 32 | typedef struct tree { 33 | item_type item; /* data item */ 34 | struct tree *parent; /* pointer to parent */ 35 | struct tree *left; /* pointer to left child */ 36 | struct tree *right; /* pointer to right child */ 37 | } tree; 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/main/c/tsp-examples/tsp-48: -------------------------------------------------------------------------------- 1 | 48 2 | 1 6734 1453 3 | 2 2233 10 4 | 3 5530 1424 5 | 4 401 841 6 | 5 3082 1644 7 | 6 7608 4458 8 | 7 7573 3716 9 | 8 7265 1268 10 | 9 6898 1885 11 | 10 1112 2049 12 | 11 5468 2606 13 | 12 5989 2873 14 | 13 4706 2674 15 | 14 4612 2035 16 | 15 6347 2683 17 | 16 6107 669 18 | 17 7611 5184 19 | 18 7462 3590 20 | 19 7732 4723 21 | 20 5900 3561 22 | 21 4483 3369 23 | 22 6101 1110 24 | 23 5199 2182 25 | 24 1633 2809 26 | 25 4307 2322 27 | 26 675 1006 28 | 27 7555 4819 29 | 28 7541 3981 30 | 29 3177 756 31 | 30 7352 4506 32 | 31 7545 2801 33 | 32 3245 3305 34 | 33 6426 3173 35 | 34 4608 1198 36 | 35 23 2216 37 | 36 7248 3779 38 | 37 7762 4595 39 | 38 7392 2244 40 | 39 3484 2829 41 | 40 6271 2135 42 | 41 4985 140 43 | 42 1916 1569 44 | 43 7280 4899 45 | 44 7509 3239 46 | 45 10 2676 47 | 46 6807 2993 48 | 47 5185 3258 49 | 48 3023 1942 50 | -------------------------------------------------------------------------------- /src/main/c/tsp-examples/tsp-48sol: -------------------------------------------------------------------------------- 1 | 48 2 | 1 3 | 8 4 | 38 5 | 31 6 | 44 7 | 18 8 | 7 9 | 28 10 | 6 11 | 37 12 | 19 13 | 27 14 | 17 15 | 43 16 | 30 17 | 36 18 | 46 19 | 33 20 | 20 21 | 47 22 | 21 23 | 32 24 | 39 25 | 48 26 | 5 27 | 42 28 | 24 29 | 10 30 | 45 31 | 35 32 | 4 33 | 26 34 | 2 35 | 29 36 | 34 37 | 41 38 | 16 39 | 22 40 | 3 41 | 23 42 | 14 43 | 25 44 | 13 45 | 11 46 | 12 47 | 15 48 | 40 49 | 9 50 | -------------------------------------------------------------------------------- /src/main/c/tsp-examples/tsp48: -------------------------------------------------------------------------------- 1 | 48 2 | 1 6734 1453 3 | 2 2233 10 4 | 3 5530 1424 5 | 4 401 841 6 | 5 3082 1644 7 | 6 7608 4458 8 | 7 7573 3716 9 | 8 7265 1268 10 | 9 6898 1885 11 | 10 1112 2049 12 | 11 5468 2606 13 | 12 5989 2873 14 | 13 4706 2674 15 | 14 4612 2035 16 | 15 6347 2683 17 | 16 6107 669 18 | 17 7611 5184 19 | 18 7462 3590 20 | 19 7732 4723 21 | 20 5900 3561 22 | 21 4483 3369 23 | 22 6101 1110 24 | 23 5199 2182 25 | 24 1633 2809 26 | 25 4307 2322 27 | 26 675 1006 28 | 27 7555 4819 29 | 28 7541 3981 30 | 29 3177 756 31 | 30 7352 4506 32 | 31 7545 2801 33 | 32 3245 3305 34 | 33 6426 3173 35 | 34 4608 1198 36 | 35 23 2216 37 | 36 7248 3779 38 | 37 7762 4595 39 | 38 7392 2244 40 | 39 3484 2829 41 | 40 6271 2135 42 | 41 4985 140 43 | 42 1916 1569 44 | 43 7280 4899 45 | 44 7509 3239 46 | 45 10 2676 47 | 46 6807 2993 48 | 47 5185 3258 49 | 48 3023 1942 50 | 48 51 | 1 52 | 8 53 | 38 54 | 31 55 | 44 56 | 18 57 | 7 58 | 28 59 | 6 60 | 37 61 | 19 62 | 27 63 | 17 64 | 43 65 | 30 66 | 36 67 | 46 68 | 33 69 | 20 70 | 47 71 | 21 72 | 32 73 | 39 74 | 48 75 | 5 76 | 42 77 | 24 78 | 10 79 | 45 80 | 35 81 | 4 82 | 26 83 | 2 84 | 29 85 | 34 86 | 41 87 | 16 88 | 22 89 | 3 90 | 23 91 | 14 92 | 25 93 | 13 94 | 11 95 | 12 96 | 15 97 | 40 98 | 9 99 | -------------------------------------------------------------------------------- /src/main/c/tsp-examples/tsp5: -------------------------------------------------------------------------------- 1 | 5 2 | 1 0 0 3 | 2 0 1 4 | 3 0 2 5 | 4 0 3 6 | 5 0 4 7 | -------------------------------------------------------------------------------- /src/main/c/tsp.h: -------------------------------------------------------------------------------- 1 | 2 | #define NMAX 1000 /* maximum number of points */ 3 | 4 | typedef struct { 5 | int x, y; /* x and y coordinates of point */ 6 | } point; 7 | 8 | typedef struct { 9 | int n; /* how many points in problem? */ 10 | point p[NMAX+1]; /* array of points */ 11 | } tsp_instance; 12 | 13 | typedef struct { 14 | int n; /* how many elements in permutation? */ 15 | int p[NMAX+1]; /* array if indices */ 16 | } tsp_solution; 17 | 18 | -------------------------------------------------------------------------------- /src/main/c/wgraph.h: -------------------------------------------------------------------------------- 1 | 2 | /* wgraph.h 3 | 4 | Header file for weighted graph type 5 | 6 | by Steven Skiena 7 | */ 8 | 9 | /* 10 | Copyright 2003 by Steven S. Skiena; all rights reserved. 11 | 12 | Permission is granted for use in non-commerical applications 13 | provided this copyright notice remains intact and unchanged. 14 | 15 | This program appears in my book: 16 | 17 | "Programming Challenges: The Programming Contest Training Manual" 18 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 19 | 20 | See our website www.programming-challenges.com for additional information. 21 | 22 | This book can be ordered from Amazon.com at 23 | 24 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 25 | 26 | */ 27 | 28 | 29 | #define MAXV 100 /* maximum number of vertices */ 30 | #define MAXDEGREE 50 /* maximum outdegree of a vertex */ 31 | 32 | typedef struct { 33 | int v; /* neighboring vertex */ 34 | int weight; /* edge weight */ 35 | } edge; 36 | 37 | typedef struct { 38 | edge edges[MAXV+1][MAXDEGREE]; /* adjacency info */ 39 | int degree[MAXV+1]; /* outdegree of each vertex */ 40 | int nvertices; /* number of vertices in the graph */ 41 | int nedges; /* number of edges in the graph */ 42 | int directed; /* is the graph directed? */ 43 | } graph; 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/backtrack/BacktrackCallback.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | /** 4 | * Back track callback API. 5 | * 6 | * @param input data type. 7 | * @author csong2022 8 | */ 9 | public interface BacktrackCallback { 10 | /** 11 | * Test the first k elements of vector a are a complete solution for the given problem. 12 | * 13 | * @param a solution vector. 14 | * @param k first k elements. 15 | * @param input allow pass general information. 16 | * @return true if the first k elements of vector a are a complete solution, otherwise false. 17 | */ 18 | boolean isaSolution(int[] a, int k, T input); 19 | 20 | /** 21 | * Process a complete solution once it is constructed. 22 | * 23 | * @param a solution vector. 24 | * @param k first k elements. 25 | * @param input general input. 26 | */ 27 | void processSolution(int[] a, int k, T input); 28 | 29 | /** 30 | * Fills an array c with the complete set of possible candidates for the kth position of a, 31 | * given the contents of the first k - 1 positions. 32 | * 33 | * @param a solution vector. 34 | * @param k kth element. 35 | * @param input general input. 36 | * @param c candidates. 37 | * @return the number of candidates. 38 | */ 39 | int constructCandidates(int[] a, int k, T input, int[] c); 40 | 41 | /** 42 | * Make a move based on updated kth position of a. 43 | * 44 | * @param a solution vector. 45 | * @param k kth element. 46 | * @param input general input. 47 | */ 48 | void makeMove(int[] a, int k, T input); 49 | 50 | /** 51 | * Undo the move based on updated kth position of a. 52 | * 53 | * @param a solution vector. 54 | * @param k kth element. 55 | * @param input general input. 56 | */ 57 | void unmakeMove(int[] a, int k, T input); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/backtrack/Subsets.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.backtrack; 20 | 21 | /** 22 | * Construct all subsets via backtracking. 23 | *

24 | * Translate from subsets.c. 25 | * 26 | * @author csong2022 27 | */ 28 | public class Subsets implements BacktrackCallback { 29 | @Override 30 | public void processSolution(int[] a, int k, Integer n) { 31 | System.out.print("{"); 32 | for (int i = 1; i <= k; i++) 33 | if (a[i] == 1) System.out.printf(" %d", i); 34 | 35 | System.out.println(" }"); 36 | } 37 | 38 | @Override 39 | public boolean isaSolution(int[] a, int k, Integer n) { 40 | return k == n; 41 | } 42 | 43 | @Override 44 | public void makeMove(int[] a, int k, Integer n) { 45 | } 46 | 47 | @Override 48 | public void unmakeMove(int[] a, int k, Integer n) { 49 | } 50 | 51 | @Override 52 | public int constructCandidates(int[] a, int k, Integer n, int[] c) { 53 | c[0] = 1; 54 | c[1] = 0; 55 | return 2; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/dp/Binomial.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | 20 | package com.algorist.dp; 21 | 22 | /** 23 | * Compute the binomial coefficients using dynamic programming. 24 | *

25 | * Translate from binomial.c. 26 | * 27 | * @author csong2022 28 | */ 29 | public class Binomial { 30 | public static long binomialCoefficient(int n, int m) { 31 | if (m > n) { 32 | throw new IllegalArgumentException("m > n"); 33 | } 34 | 35 | long[][] bc = new long[n + 1][n + 1]; /* table of binomial coefficient values */ 36 | 37 | for (int i = 0; i <= n; i++) bc[i][0] = 1; 38 | for (int j = 0; j <= n; j++) bc[j][j] = 1; 39 | 40 | for (int i = 1; i <= n; i++) 41 | for (int j = 1; j < i; j++) 42 | bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]; 43 | 44 | return bc[n][m]; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/dp/EditBrute.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.dp; 20 | 21 | /** 22 | * Compute string edit distance *without* dynamic programming! 23 | *

24 | * Translate from editbrute.c. 25 | * 26 | * @author csong2022 27 | */ 28 | public class EditBrute extends EditDistance { 29 | 30 | public EditBrute(StringEdit stringEdit) { 31 | super(stringEdit); 32 | } 33 | 34 | public int stringCompare(String s, String t, int i, int j) { 35 | int[] opt = new int[3]; /* cost of the three options */ 36 | int lowest_cost; /* lowest cost */ 37 | 38 | if (i == 0) return j * indel(' '); 39 | if (j == 0) return i * indel(' '); 40 | 41 | opt[MATCH] = stringCompare(s, t, i - 1, j - 1) + match(s.charAt(i), t.charAt(j)); 42 | opt[INSERT] = stringCompare(s, t, i, j - 1) + indel(t.charAt(j)); 43 | opt[DELETE] = stringCompare(s, t, i - 1, j) + indel(s.charAt(i)); 44 | 45 | lowest_cost = opt[MATCH]; 46 | for (int k = INSERT; k <= DELETE; k++) 47 | if (opt[k] < lowest_cost) lowest_cost = opt[k]; 48 | 49 | m[i][j] = new Cell(lowest_cost, -1); /* REMOVE FROM PRINTED VERSION */ 50 | 51 | return lowest_cost; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/dp/LCS.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.dp; 20 | 21 | import static com.algorist.dp.EditDistance.MAXLEN; 22 | 23 | /** 24 | * Longest common subsequence of two strings. 25 | *

26 | * Generify from lcs.c. 27 | * 28 | * @author csong2022 29 | */ 30 | public class LCS extends StringEdit { 31 | 32 | @Override 33 | int match(char c, char d) { 34 | return (c == d) ? 0 : MAXLEN; 35 | } 36 | 37 | @Override 38 | void matchOut(String s, String t, int i, int j) { 39 | if (s.charAt(i) == t.charAt(j)) System.out.printf("%c", s.charAt(i)); 40 | } 41 | 42 | @Override 43 | void insertOut(String t, int j) { 44 | } 45 | 46 | @Override 47 | void deleteOut(String s, int i) { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/dp/SubStringEdit.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.dp; 20 | 21 | /** 22 | * Approximately match one string as a substring of another, where is s in t? 23 | *

24 | * Generify from stringedit.c. 25 | * 26 | * @author csong2022 27 | */ 28 | public class SubStringEdit extends StringEdit { 29 | 30 | int[] goalCell(String s, String t, EditDistance.Cell[][] m) { 31 | int i = s.length() - 1; 32 | int j = 0; 33 | 34 | for (int k = 1; k < t.length(); k++) 35 | if (m[i][k].cost < m[i][j].cost) j = k; 36 | 37 | return new int[]{i, j}; 38 | } 39 | 40 | void rowInit(int i, EditDistance.Cell[][] m) { /* what is m[0][i]? */ 41 | m[0][i] = new EditDistance.Cell(0, -1); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/geometry/Distance.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.geometry; 20 | 21 | import static java.lang.Math.sqrt; 22 | 23 | /** 24 | * Compute Euclidian distances. 25 | *

26 | * Translate from distance.c. 27 | * 28 | * @author csong2022 29 | */ 30 | public class Distance { 31 | 32 | public static double distance(double[] a, double[] b) { 33 | if (a.length < 1) { 34 | throw new IllegalArgumentException("a vector is empty."); 35 | } 36 | if (b.length < 1) { 37 | throw new IllegalArgumentException("b vector is empty."); 38 | } 39 | if (a.length != b.length) { 40 | throw new IllegalArgumentException("Two vectors have different dimension."); 41 | } 42 | 43 | double d = 0.0; 44 | 45 | for (int i = 0; i < a.length; i++) 46 | d += (a[i] - b[i]) * (a[i] - b[i]); 47 | 48 | return sqrt(d); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/AbstractGraphSearch.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | /** 4 | * Common graph search code between BFS and DFS. 5 | * 6 | * @param edges node type. 7 | * @author csong2022 8 | */ 9 | public abstract class AbstractGraphSearch implements GraphSearch { 10 | final Graph g; /* The graph */ 11 | final boolean[] processed; /* which vertices have been processed */ 12 | final boolean[] discovered; /* which vertices have been found */ 13 | final int[] parent; /* discovery relation */ 14 | 15 | AbstractGraphSearch(Graph g) { 16 | this.g = g; 17 | this.processed = new boolean[g.nvertices() + 1]; 18 | this.discovered = new boolean[g.nvertices() + 1]; 19 | this.parent = new int[g.nvertices() + 1]; 20 | 21 | initialize(); 22 | } 23 | 24 | public void initialize() { 25 | for (int i = 0; i <= g.nvertices(); i++) { 26 | processed[i] = discovered[i] = false; 27 | parent[i] = -1; 28 | } 29 | } 30 | 31 | public int parent(int v) { 32 | return parent[v]; 33 | } 34 | 35 | @SuppressWarnings("BooleanMethodIsAlwaysInverted") 36 | public boolean discovered(int v) { 37 | return discovered[v]; 38 | } 39 | 40 | @Override 41 | public Iterable findPath(int start, int end) { 42 | return GraphSearchUtils.findPath(start, end, this.parent); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/Connected.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.graph; 20 | 21 | /** 22 | * Compute the connected components of a graph. 23 | *

24 | * Translate from connected.c. 25 | * 26 | * @author csong2022 27 | */ 28 | public class Connected { 29 | public Connected(Graph g) { 30 | BFS bfs = new BFS<>(g); 31 | ConnectedCallback callback = new ConnectedCallback<>(); 32 | 33 | int c = 0; 34 | for (int i = 1; i <= g.nvertices(); i++) 35 | if (!bfs.discovered(i)) { 36 | c++; 37 | System.out.printf("Component %d:", c); 38 | bfs.search(i, callback); 39 | System.out.println(); 40 | } 41 | } 42 | 43 | private static class ConnectedCallback extends DefaultGraphSearchCallback { 44 | @Override 45 | public void processVertexEarly(int v) { 46 | System.out.printf(" %d", v); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/DefaultGraphSearchCallback.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | public class DefaultGraphSearchCallback implements GraphSearchCallback { 4 | @Override 5 | public void processVertexEarly(int v) { 6 | } 7 | 8 | @Override 9 | public void processVertexLate(int v) { 10 | } 11 | 12 | @Override 13 | public void processEdge(int x, int y) { 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/EdgeNode.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | /** 4 | * Adjacency list node. 5 | *

6 | * Generify from edgenode type in graph.h. 7 | * 8 | * @author csong2022 9 | */ 10 | public interface EdgeNode { 11 | /** 12 | * @return adjacent vertex. 13 | */ 14 | int y(); /* adjancency info */ 15 | 16 | /** 17 | * Create a copy of edge node for given vertex. 18 | * 19 | * @param v vertex. 20 | * @return copy of edge node for given vertex. 21 | */ 22 | EdgeNode copy(int v); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/GraphReader.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * Graph reader. 7 | * 8 | * @param edge node type. 9 | * @author csong2022 10 | */ 11 | public interface GraphReader { 12 | /** 13 | * Read graph from input. 14 | * 15 | * @param scanner input. 16 | * @param directed directed or not? 17 | * @return graph. 18 | */ 19 | Graph readGraph(Scanner scanner, boolean directed); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/GraphSearch.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | /** 4 | * Graph traversal. 5 | * 6 | * @param edge node type. 7 | * @author csong2022 8 | */ 9 | public interface GraphSearch { 10 | /** 11 | * Graph traverse from starting point. 12 | * 13 | * @param start starting vertex. 14 | * @param callback graph traversal callback. 15 | */ 16 | void search(final int start, final GraphSearchCallback callback); 17 | 18 | /** 19 | * Find path from starting point to end. 20 | * 21 | * @param start starting point. 22 | * @param end ending point. 23 | * @return path from starting to end point. 24 | */ 25 | Iterable findPath(final int start, final int end); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/GraphSearchCallback.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | /** 4 | * Graph traversal callback. 5 | *

6 | * Generify the customization point for graph traversal. 7 | * 8 | * @param edge node type. 9 | * @author csong2022 10 | */ 11 | public interface GraphSearchCallback { 12 | 13 | /** 14 | * Validate node. 15 | * 16 | * @param p edge node. 17 | * @return true if node is valid, otherwise false. 18 | */ 19 | default boolean validateEdge(T p) { 20 | return true; 21 | } 22 | 23 | /** 24 | * bfs-dfs.c 25 | * process_vertex_early(int) 26 | * 27 | * @param v vertex. 28 | */ 29 | void processVertexEarly(int v); 30 | 31 | /** 32 | * bfs-dfs.c 33 | * process_vertex_late(int) 34 | * 35 | * @param v vertex. 36 | */ 37 | void processVertexLate(int v); 38 | 39 | /** 40 | * bfs-dfs.c 41 | * process_edge(int, int) 42 | * 43 | * @param x edge starting point. 44 | * @param y edge end point. 45 | */ 46 | void processEdge(int x, int y); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/GraphSearchUtils.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.datastructure.Stack; 4 | 5 | /** 6 | * Graph search utilities. 7 | * 8 | * @author csong2022 9 | */ 10 | public class GraphSearchUtils { 11 | 12 | public static Iterable findPath(final int start, final int end, final int[] parents) { 13 | final Stack path = new Stack<>(); 14 | findPath(start, end, parents, path); 15 | return path; 16 | } 17 | 18 | private static void findPath(final int start, final int end, final int[] parents, final Stack path) { 19 | if ((start == end) || (end == -1)) { 20 | path.push(start); 21 | } else { 22 | path.push(end); 23 | findPath(start, parents[end], parents, path); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/UnweightedEdgeNode.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | /** 4 | * Unweighted edge note. 5 | * 6 | * @author csong2022 7 | */ 8 | public class UnweightedEdgeNode implements EdgeNode { 9 | private final int y; /* adjancency info */ 10 | 11 | public UnweightedEdgeNode(int y) { 12 | this.y = y; 13 | } 14 | 15 | @Override 16 | public int y() { 17 | return y; 18 | } 19 | 20 | public UnweightedEdgeNode copy(int v) { 21 | return new UnweightedEdgeNode(v); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/graph/UnweightedGraphReader.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * Unweighted graph reader. 7 | * 8 | * @author csong2022 9 | */ 10 | public class UnweightedGraphReader implements GraphReader { 11 | 12 | @Override 13 | public Graph readGraph(Scanner scanner, boolean directed) { 14 | final int nvertices = scanner.nextInt(); 15 | final Graph g = new Graph<>(nvertices, directed); 16 | 17 | final int nedges = scanner.nextInt(); 18 | 19 | int x, y; /* vertices in edge (x,y) */ 20 | for (int i = 1; i <= nedges; i++) { 21 | x = scanner.nextInt(); 22 | y = scanner.nextInt(); 23 | g.insertEdge(x, new UnweightedEdgeNode(y), directed); 24 | } 25 | 26 | return g; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/numerical/Primes.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.numerical; 20 | 21 | import static java.lang.Math.sqrt; 22 | 23 | /** 24 | * Compute the prime factorization of an integer. 25 | *

26 | * Translate from primes.c. 27 | * 28 | * @author csong2022 29 | */ 30 | public class Primes { 31 | 32 | public static void primeFactorization(final long x) { 33 | long c = x; /* remaining product to factor */ 34 | while ((c % 2) == 0) { 35 | System.out.println(2); 36 | c /= 2; 37 | } 38 | 39 | long i = 3; /* counter */ 40 | while (i <= (sqrt(c) + 1)) { 41 | if ((c % i) == 0) { 42 | System.out.println(i); 43 | c /= i; 44 | } else 45 | i += 2; 46 | } 47 | 48 | if (c > 1) System.out.println(c); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/utils/IterableUtils.java: -------------------------------------------------------------------------------- 1 | package com.algorist.utils; 2 | 3 | import java.util.Iterator; 4 | 5 | import static com.algorist.datastructure.ArrayUtils.newArray; 6 | 7 | /** 8 | * Iterable utils. 9 | * 10 | * @author csong2022 11 | */ 12 | public class IterableUtils { 13 | /** 14 | * Format iterable to single line. 15 | * 16 | * @param iterable iterable. 17 | * @param element type. 18 | * @return space delimited string. 19 | */ 20 | public static String toString(final Iterable iterable) { 21 | final StringBuilder builder = new StringBuilder(); 22 | 23 | final Iterator iterator = iterable.iterator(); 24 | 25 | if (iterator.hasNext()) { 26 | builder.append(iterator.next()); 27 | } 28 | 29 | while (iterator.hasNext()) { 30 | builder.append(' ').append(iterator.next()); 31 | } 32 | 33 | return builder.toString(); 34 | } 35 | 36 | /** 37 | * Convert iterable to array. 38 | * 39 | * @param iterable iterable. 40 | * @param size size of collection. 41 | * @param element type. 42 | * @return array. 43 | */ 44 | public static T[] toArray(Iterable iterable, int size) { 45 | T[] arr = newArray(size); 46 | 47 | int m = 0; 48 | for (T item : iterable) { 49 | arr[m++] = item; 50 | } 51 | return arr; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/wgraph/FlowGraph.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.wgraph; 20 | 21 | import com.algorist.graph.Graph; 22 | 23 | /** 24 | * Flow graph. 25 | * 26 | * @author csong2022 27 | */ 28 | public class FlowGraph extends Graph { 29 | public FlowGraph(int nvertices, boolean directed) { 30 | super(nvertices, directed); 31 | } 32 | 33 | public void addResidualEdges() { 34 | for (int i = 1; i <= nvertices(); i++) { 35 | for (FlowEdgeNode p : edges(i)) { 36 | if (findEdge(p.y(), i) == null) { 37 | insertEdge(p.y(), new FlowEdgeNode(i, 0), true); 38 | } 39 | } 40 | } 41 | } 42 | 43 | public void print() { 44 | for (int i = 1; i <= nvertices(); i++) { 45 | System.out.printf("%d: ", i); 46 | for (FlowEdgeNode p : edges(i)) { 47 | System.out.printf(" %s", p.toString()); 48 | } 49 | System.out.println(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/wgraph/FlowGraphReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.wgraph; 20 | 21 | import com.algorist.graph.GraphReader; 22 | 23 | import java.util.Scanner; 24 | 25 | /** 26 | * Flow graph reader. 27 | * 28 | * @author csong2022 29 | */ 30 | public class FlowGraphReader implements GraphReader { 31 | @Override 32 | public FlowGraph readGraph(Scanner scanner, boolean directed) { 33 | int nvertices = scanner.nextInt(); 34 | int m = scanner.nextInt(); 35 | 36 | FlowGraph g = new FlowGraph(nvertices, directed); 37 | 38 | for (int i = 1; i <= m; i++) { 39 | int x = scanner.nextInt(); 40 | int y = scanner.nextInt(); 41 | int w = scanner.nextInt(); 42 | FlowEdgeNode edgeNode = new FlowEdgeNode(y, w); 43 | g.insertEdge(x, edgeNode, directed); 44 | } 45 | return g; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/wgraph/Floyd.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.wgraph; 20 | 21 | /** 22 | * Compute all-pairs shortest paths in weighted graphs. 23 | * 24 | * @author csong2022 25 | */ 26 | public class Floyd { 27 | public Floyd(AdjacencyMatrix g) { 28 | int through_k; /* distance through vertex k */ 29 | for (int k = 1; k <= g.nvertices(); k++) 30 | for (int i = 1; i <= g.nvertices(); i++) 31 | for (int j = 1; j <= g.nvertices(); j++) { 32 | through_k = g.weight(i, k) + g.weight(k, j); 33 | if (through_k < g.weight(i, j)) 34 | g.setWeight(i, j, through_k); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/wgraph/WeightedEdgeNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.wgraph; 20 | 21 | import com.algorist.graph.UnweightedEdgeNode; 22 | 23 | /** 24 | * Weighted edge node. 25 | * 26 | * @author csong2022 27 | */ 28 | public class WeightedEdgeNode extends UnweightedEdgeNode { 29 | private final int weight; /* edge weight */ 30 | 31 | public WeightedEdgeNode(int y, int weight) { 32 | super(y); 33 | this.weight = weight; 34 | } 35 | 36 | public int weight() { 37 | return this.weight; 38 | } 39 | 40 | @Override 41 | public WeightedEdgeNode copy(int v) { 42 | return new WeightedEdgeNode(v, this.weight); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/algorist/wgraph/WeightedGraphReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commercial applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.wgraph; 20 | 21 | import com.algorist.graph.Graph; 22 | import com.algorist.graph.GraphReader; 23 | 24 | import java.util.Scanner; 25 | 26 | /** 27 | * Weighted graph reader. 28 | * 29 | * @author csong2022 30 | */ 31 | public class WeightedGraphReader implements GraphReader { 32 | 33 | @Override 34 | public Graph readGraph(Scanner scanner, boolean directed) { 35 | final int nvertices = scanner.nextInt(); 36 | final Graph g = new Graph<>(nvertices, directed); 37 | 38 | final int nedges = scanner.nextInt(); 39 | 40 | int x, y, w; /* placeholder for edge and weight */ 41 | for (int i = 1; i <= nedges; i++) { 42 | x = scanner.nextInt(); 43 | y = scanner.nextInt(); 44 | w = scanner.nextInt(); 45 | g.insertEdge(x, new WeightedEdgeNode(y, w), directed); 46 | } 47 | 48 | return g; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/algorist/backtrack/NQueensTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | import com.algorist.test.TestCaseWithoutInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | 9 | import static com.algorist.backtrack.Backtrack.NMAX; 10 | 11 | public class NQueensTest implements TestCaseWithoutInput { 12 | public void process() { 13 | Backtrack backtrack = new Backtrack(); 14 | NQueens nQueens = new NQueens(); 15 | 16 | int[] a = new int[NMAX]; /* solution vector */ 17 | 18 | for (int i = 1; i <= 10; i++) { 19 | nQueens.setSolutionCount(0); 20 | backtrack.backtrack(a, 0, i, nQueens); 21 | System.out.printf("n=%d solution_count=%d%n", i, nQueens.solutionCount()); 22 | } 23 | } 24 | 25 | @Test 26 | public void test() throws IOException { 27 | TestEngine.execute(this, "8-queens-out"); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/backtrack/PathsTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | import com.algorist.graph.Graph; 4 | import com.algorist.graph.UnweightedEdgeNode; 5 | import com.algorist.graph.UnweightedGraphReader; 6 | import com.algorist.test.TestCaseWithInput; 7 | import com.algorist.test.TestEngine; 8 | import org.junit.Test; 9 | 10 | import java.util.Scanner; 11 | 12 | import static com.algorist.backtrack.Backtrack.NMAX; 13 | 14 | public class PathsTest implements TestCaseWithInput { 15 | @Override 16 | public void process(Scanner scanner) { 17 | Graph g = new UnweightedGraphReader().readGraph(scanner, false); 18 | g.print(); 19 | 20 | Backtrack backtrack = new Backtrack(); 21 | Paths paths = new Paths<>(g); 22 | 23 | int[] a = new int[NMAX]; /* solution vector */ 24 | for (int i = 1; i <= g.nvertices(); i++) { 25 | System.out.printf("%nPaths from 1 to %d:%n", i); 26 | backtrack.backtrack(a, 0, i, paths); 27 | } 28 | } 29 | 30 | @Test 31 | public void test() throws Exception { 32 | TestEngine.execute(this, "paths-graph", "paths-graph-out"); 33 | } 34 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/backtrack/PermutationsTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | import com.algorist.test.TestCaseWithoutInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import static com.algorist.backtrack.Backtrack.NMAX; 8 | 9 | public class PermutationsTest implements TestCaseWithoutInput { 10 | @Override 11 | public void process() { 12 | int[] a = new int[NMAX]; /* solution vector */ 13 | 14 | Permutations permutations = new Permutations(); 15 | 16 | Backtrack backtrack = new Backtrack(); 17 | backtrack.backtrack(a, 0, 0, permutations); 18 | backtrack.backtrack(a, 0, 1, permutations); 19 | backtrack.backtrack(a, 0, 2, permutations); 20 | backtrack.backtrack(a, 0, 3, permutations); 21 | } 22 | 23 | @Test 24 | public void test() throws Exception { 25 | TestEngine.execute(this, "permutations-out"); 26 | } 27 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/backtrack/SubsetsTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | import com.algorist.test.TestCaseWithoutInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | 9 | import static com.algorist.backtrack.Backtrack.NMAX; 10 | 11 | public class SubsetsTest implements TestCaseWithoutInput { 12 | 13 | @Override 14 | public void process() { 15 | int[] a = new int[NMAX]; /* solution vector */ 16 | 17 | Subsets subsets = new Subsets(); 18 | 19 | Backtrack backtrack = new Backtrack(); 20 | backtrack.backtrack(a, 0, 3, subsets); 21 | } 22 | 23 | @Test 24 | public void test() throws IOException { 25 | TestEngine.execute(this, "subsets-out"); 26 | } 27 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/backtrack/SudokuTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | import static com.algorist.backtrack.Sudoku.Board.DIMENSION; 10 | import static com.algorist.backtrack.Sudoku.Board.copy; 11 | 12 | public class SudokuTest implements TestCaseWithInput { 13 | @Override 14 | public void process(Scanner scanner) { 15 | int[] a = new int[DIMENSION * DIMENSION + 1]; 16 | Sudoku.Board board = Sudoku.Board.read(scanner); /* Sudoku board structure */ 17 | board.print(); 18 | 19 | Sudoku.Board temp = new Sudoku.Board(); 20 | copy(board, temp); 21 | 22 | Backtrack backtrack = new Backtrack(); 23 | boolean[] speed = {true, false}; 24 | boolean[] intelligence = {true, false}; 25 | 26 | for (boolean fast : speed) 27 | for (boolean smart : intelligence) { 28 | Sudoku sudoku = new Sudoku(backtrack, fast, smart); 29 | 30 | System.out.println("----------------------------------"); 31 | sudoku.resetSteps(); 32 | Sudoku.Board.copy(temp, board); 33 | backtrack.setFinished(false); 34 | 35 | backtrack.backtrack(a, 0, board, sudoku); 36 | /*print_board(&board);*/ 37 | 38 | System.out.printf("It took %d steps to find this solution ", sudoku.steps()); 39 | System.out.printf("for fast=%d smart=%d%n", fast ? 1 : 0, smart ? 1 : 0); 40 | } 41 | } 42 | 43 | @Test 44 | public void test() throws Exception { 45 | TestEngine.execute(this, "puzzle", "puzzle-out"); 46 | } 47 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/backtrack/TSPTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.backtrack; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Ignore; 6 | import org.junit.Test; 7 | 8 | import java.io.IOException; 9 | import java.util.Scanner; 10 | 11 | import static com.algorist.backtrack.TSP.solutionCost; 12 | 13 | @Ignore 14 | public class TSPTest implements TestCaseWithInput { 15 | 16 | @Override 17 | public void process(Scanner scanner) { 18 | TSP.TspInstance t; /* tsp points */ 19 | TSP.TspSolution s; /* tsp solution */ 20 | 21 | t = TSP.TspInstance.read(scanner); 22 | 23 | s = TSP.TspSolution.read(scanner); 24 | System.out.printf("OPTIMAL SOLUTION COST = %7.1f%n", solutionCost(s, t)); 25 | s.print(); 26 | 27 | s = new TSP.TspSolution(t.n); 28 | System.out.printf("solutionCost = %7.1f%n", solutionCost(s, t)); 29 | s.print(); 30 | 31 | Annealing annealing = new Annealing(); 32 | s = annealing.repeatedAnnealing(t, 3); 33 | System.out.printf("repeated annealing %d iterations, cost = %7.1f%n", 34 | annealing.solutionCount(), solutionCost(s, t)); 35 | s.print(); 36 | } 37 | 38 | @Test 39 | public void test() throws IOException { 40 | TestEngine.execute(this, "tsp48-in", "tsp48-out"); 41 | } 42 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/datastructure/PriorityQueueTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.datastructure; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.Comparator; 6 | 7 | import static org.junit.Assert.assertEquals; 8 | import static org.junit.Assert.assertFalse; 9 | 10 | public class PriorityQueueTest { 11 | @Test 12 | public void test() { 13 | final Integer[] a = new Integer[10]; 14 | for (int i = 0; i < a.length; i++) { 15 | a[i] = a.length - i; 16 | } 17 | 18 | final PriorityQueue q1 = PriorityQueue.makeHeap(a, 0, a.length - 1); 19 | final PriorityQueue q2 = PriorityQueue.makeHeap1(a, 0, a.length - 1, Comparator.naturalOrder()); 20 | 21 | assertFalse(q1.isEmpty()); 22 | q1.print(); 23 | 24 | assertEquals(q2.size(), q1.size()); 25 | 26 | for (int i = 0; i < q1.size(); i++) { 27 | assertEquals(q2.extractMin(), q1.extractMin()); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/datastructure/QueueTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.datastructure; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.core.Is.is; 7 | import static org.junit.Assert.*; 8 | 9 | public class QueueTest { 10 | private Queue q; 11 | 12 | @Before 13 | public void setUp() { 14 | this.q = new Queue<>(1); 15 | } 16 | 17 | @Test 18 | public void testDequeueEmptyQueue() { 19 | assertTrue(this.q.isEmpty()); 20 | this.q.print(); 21 | assertNull(this.q.dequeue()); 22 | } 23 | 24 | @Test 25 | public void test() { 26 | assertTrue(this.q.isEmpty()); 27 | 28 | for (int i = 1; i <= 16; i++) { 29 | this.q.enqueue(i); 30 | } 31 | 32 | this.q.print(); 33 | assertFalse(this.q.isEmpty()); 34 | 35 | int i; 36 | 37 | i = 1; 38 | for (Integer x : this.q) { 39 | assertThat(x, is(i++)); 40 | } 41 | 42 | for (i = 1; i <= 16; i++) { 43 | assertThat(this.q.dequeue(), is(i)); 44 | } 45 | 46 | assertTrue(this.q.isEmpty()); 47 | } 48 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/datastructure/SetUnionTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.datastructure; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertTrue; 7 | 8 | public class SetUnionTest { 9 | private SetUnion uf; 10 | 11 | @Before 12 | public void setUp() { 13 | int[][] pairs = new int[][]{ 14 | {5, 4}, 15 | {4, 9}, 16 | {7, 6}, 17 | {10, 5}, 18 | {3, 2}, 19 | {9, 10}, 20 | {6, 1}, 21 | {8, 3}, 22 | {7, 2}, 23 | {2, 1}, 24 | {7, 8} 25 | }; 26 | this.uf = new SetUnion(pairs.length); 27 | 28 | for (int[] pair : pairs) { 29 | this.uf.unionSets(pair[0], pair[1]); 30 | } 31 | } 32 | 33 | @Test 34 | public void test() { 35 | this.uf.print(); 36 | 37 | final int[] c1 = {1, 2, 3, 6, 7, 8}; 38 | assertTrue(this.withinSameComponent(c1)); 39 | 40 | final int[] c2 = {4, 5, 9, 10}; 41 | assertTrue(this.withinSameComponent(c2)); 42 | } 43 | 44 | private boolean withinSameComponent(final int[] c1) { 45 | for (int i = 1; i < c1.length; i++) { 46 | if (!this.uf.sameComponent(c1[0], c1[i])) { 47 | return false; 48 | } 49 | } 50 | return true; 51 | } 52 | 53 | 54 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/datastructure/StackTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.datastructure; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.hamcrest.core.Is.is; 7 | import static org.junit.Assert.*; 8 | 9 | public class StackTest { 10 | private Stack s; 11 | 12 | @Before 13 | public void setUp() { 14 | this.s = new Stack<>(1); 15 | } 16 | 17 | @Test 18 | public void testPopEmptyStack() { 19 | assertTrue(this.s.isEmpty()); 20 | this.s.print(); 21 | assertNull(this.s.pop()); 22 | } 23 | 24 | @Test 25 | public void test() { 26 | assertTrue(this.s.isEmpty()); 27 | 28 | for (int i = 1; i <= 16; i++) { 29 | this.s.push(i); 30 | } 31 | 32 | this.s.print(); 33 | assertFalse(this.s.isEmpty()); 34 | 35 | for (int i = 16; i >= 1; i--) { 36 | assertThat(this.s.pop(), is(i)); 37 | } 38 | assertTrue(this.s.isEmpty()); 39 | } 40 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/datastructure/WarTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.datastructure; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | import static com.algorist.datastructure.War.rankCard; 11 | import static com.algorist.datastructure.War.war; 12 | 13 | public class WarTest implements TestCaseWithInput { 14 | @Override 15 | public void process(Scanner scanner) { 16 | Queue[] decks = new Queue[2]; /* player's decks */ 17 | char value, suit, c; /* input characters */ 18 | 19 | while (true) { 20 | for (int i = 0; i <= 1; i++) { 21 | decks[i] = new Queue<>(); 22 | 23 | if (!scanner.hasNext()) return; 24 | String line = scanner.nextLine(); 25 | int j = 0; 26 | while (j < line.length()) { 27 | c = line.charAt(j++); 28 | if (c != ' ') { 29 | value = c; 30 | suit = line.charAt(j++); 31 | decks[i].enqueue(rankCard(value, suit)); 32 | } 33 | } 34 | } 35 | 36 | war(decks[0], decks[1]); 37 | } 38 | } 39 | 40 | @Test 41 | public void test() throws IOException { 42 | TestEngine.execute(this, "war-in", "war-out"); 43 | } 44 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/dp/ElevatorTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.dp; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | import static com.algorist.dp.Elevator.MAX_RIDERS; 10 | 11 | public class ElevatorTest implements TestCaseWithInput { 12 | public void process(Scanner scanner) { 13 | int nriders = scanner.nextInt(); 14 | int nstops = scanner.nextInt(); 15 | 16 | int[] stops = new int[MAX_RIDERS]; 17 | for (int i = 1; i <= nriders; i++) 18 | stops[i] = scanner.nextInt(); 19 | 20 | Elevator elevator = new Elevator(stops, nstops); 21 | 22 | for (int i = 1; i <= nriders; i++) 23 | System.out.printf("%d%n", elevator.stops[i]); 24 | 25 | int laststop = elevator.optimizeFloors(); 26 | 27 | elevator.printCostTable(); 28 | System.out.println(); 29 | elevator.printParentTable(); 30 | 31 | System.out.printf("cost = %d%n", elevator.m[laststop][nstops]); 32 | 33 | elevator.reconstructPath(laststop, nstops); 34 | } 35 | 36 | @Test 37 | public void test() throws Exception { 38 | TestEngine.execute(this, "elevator-in", "elevator-out"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/com/algorist/dp/FibTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.dp; 2 | 3 | import com.algorist.test.TestCaseWithoutInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Ignore; 6 | import org.junit.Test; 7 | 8 | import static com.algorist.dp.Fib.*; 9 | 10 | @Ignore 11 | public class FibTest implements TestCaseWithoutInput { 12 | @Override 13 | public void process() { 14 | for (int i = 0; i < MAXN; i++) { 15 | System.out.printf("fib_c(%d) = %d%n", i, fibCDriver(i)); 16 | } 17 | 18 | for (int i = 0; i < MAXN; i++) { 19 | System.out.printf("fib_dp(%d) = %d%n", i, fibDp(i)); 20 | } 21 | 22 | for (int i = 0; i < MAXN; i++) { 23 | System.out.printf("fib_dp2(%d) = %d%n", i, fibDp2(i)); 24 | } 25 | 26 | for (int i = 0; i < MAXN; i++) { 27 | System.out.printf("fib(%d) = %d%n", i, fibR(i)); 28 | } 29 | } 30 | 31 | @Test 32 | public void test() throws Exception { 33 | TestEngine.execute(this, "fib-out"); 34 | } 35 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/dp/LCSTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.dp; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | public class LCSTest implements TestCaseWithInput { 10 | public void process(Scanner scanner) { 11 | String s = ' ' + scanner.next(); 12 | String t = ' ' + scanner.next(); 13 | 14 | LCS stringEdit = new LCS(); 15 | EditDistance editDistance = new EditDistance(stringEdit); 16 | 17 | int complen = editDistance.stringCompare(s, t); 18 | int lcslen = (s.length() + t.length() - 2 - complen) / 2; 19 | 20 | System.out.printf("length of longest common subsequence = %d%n", lcslen); 21 | 22 | int[] p = stringEdit.goalCell(s, t, editDistance.m); 23 | int i = p[0], j = p[1]; 24 | 25 | editDistance.reconstructPath(s, t, i, j); 26 | System.out.println(); 27 | } 28 | 29 | @Test 30 | public void test() throws Exception { 31 | TestEngine.execute(this, "stringedit-in", "lcs-out"); 32 | } 33 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/dp/PartitionTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.dp; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | import static com.algorist.dp.Partition.partition; 10 | import static com.algorist.dp.Partition.read; 11 | 12 | public class PartitionTest implements TestCaseWithInput { 13 | 14 | public void process(Scanner scanner) { 15 | Partition.Data data = read(scanner); 16 | partition(data.s, data.n, data.k); 17 | } 18 | 19 | @Test 20 | public void testData1() throws Exception { 21 | TestEngine.execute(this, "partition-data1", "partition-data1-out"); 22 | } 23 | 24 | @Test 25 | public void testData2() throws Exception { 26 | TestEngine.execute(this, "partition-data2", "partition-data2-out"); 27 | } 28 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/dp/StringEditTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.dp; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | public class StringEditTest implements TestCaseWithInput { 10 | public void process(Scanner scanner) { 11 | String s = ' ' + scanner.next(); 12 | String t = ' ' + scanner.next(); 13 | 14 | StringEdit stringEdit = new StringEdit(); 15 | EditDistance editDistance = new EditDistance(stringEdit); 16 | 17 | System.out.printf("matching cost = %d %n", editDistance.stringCompare(s, t)); 18 | 19 | editDistance.printMatrix(s, t, true); 20 | System.out.println(); 21 | editDistance.printMatrix(s, t, false); 22 | 23 | int[] p = stringEdit.goalCell(s, t, editDistance.m); 24 | int i = p[0], j = p[1]; 25 | System.out.printf("%d %d%n", i, j); 26 | 27 | editDistance.reconstructPath(s, t, i, j); 28 | System.out.println(); 29 | } 30 | 31 | @Test 32 | public void test() throws Exception { 33 | TestEngine.execute(this, "stringedit-in", "stringedit-out"); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/dp/SubStringEditTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.dp; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | public class SubStringEditTest implements TestCaseWithInput { 10 | public void process(Scanner scanner) { 11 | String s = ' ' + scanner.next(); 12 | String t = ' ' + scanner.next(); 13 | 14 | SubStringEdit stringEdit = new SubStringEdit(); 15 | EditDistance editDistance = new EditDistance(stringEdit); 16 | 17 | System.out.printf("matching cost = %d %n", editDistance.stringCompare(s, t)); 18 | 19 | editDistance.printMatrix(s, t, true); 20 | System.out.println(); 21 | editDistance.printMatrix(s, t, false); 22 | 23 | int[] p = stringEdit.goalCell(s, t, editDistance.m); 24 | int i = p[0], j = p[1]; 25 | System.out.printf("%d %d%n", i, j); 26 | 27 | editDistance.reconstructPath(s, t, i, j); 28 | System.out.println(); 29 | } 30 | 31 | @Test 32 | public void test() throws Exception { 33 | TestEngine.execute(this, "substringedit-in", "substringedit-out"); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/geometry/DistanceTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.geometry; 2 | 3 | import com.algorist.test.TestCaseWithoutInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import static com.algorist.geometry.Distance.distance; 8 | 9 | public class DistanceTest implements TestCaseWithoutInput { 10 | public void process() { 11 | double[] a = new double[]{6, 2, 3}; 12 | double[] b = new double[]{6, 3, 4}; 13 | System.out.printf("distance = %f%n", distance(a, b)); 14 | } 15 | 16 | @Test 17 | public void test() throws Exception { 18 | TestEngine.execute(this, "distance-out"); 19 | } 20 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/geometry/OrderTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.geometry; 2 | 3 | import com.algorist.test.TestCaseWithoutInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | 9 | import static com.algorist.geometry.Order.*; 10 | 11 | public class OrderTest implements TestCaseWithoutInput { 12 | @Override 13 | public void process() { 14 | System.out.println("row_major"); 15 | rowMajor(5, 5); 16 | 17 | System.out.println("\ncolumn_major"); 18 | columnMajor(3, 3); 19 | 20 | System.out.println("\nsnake_order"); 21 | snakeOrder(5, 5); 22 | 23 | System.out.println("\ndiagonal_order"); 24 | diagonalOrder(3, 4); 25 | 26 | System.out.println("\ndiagonal_order"); 27 | diagonalOrder(4, 3); 28 | } 29 | 30 | @Test 31 | public void test() throws IOException { 32 | TestEngine.execute(this, "order-out"); 33 | } 34 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/geometry/SuperManTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.geometry; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | import static com.algorist.geometry.Geometry.Circle; 11 | import static com.algorist.geometry.Geometry.Point; 12 | import static com.algorist.geometry.Geometry.Point.readPoint; 13 | 14 | public class SuperManTest implements TestCaseWithInput { 15 | @Override 16 | public void process(Scanner scanner) { 17 | Point s = readPoint(scanner); 18 | Point t = readPoint(scanner); 19 | 20 | int ncircles = scanner.nextInt(); 21 | Circle[] c = new Circle[ncircles + 1]; 22 | 23 | for (int i = 1; i <= ncircles; i++) { 24 | Point center = readPoint(scanner); 25 | double r = scanner.nextDouble(); 26 | c[i] = new Circle(center, r); 27 | } 28 | 29 | System.out.printf("%7.3f %7.3f%n", s.x, s.y); 30 | System.out.printf("%7.3f %7.3f%n", t.x, t.y); 31 | System.out.printf("%d%n", ncircles); 32 | for (int i = 1; i <= ncircles; i++) 33 | System.out.printf("%7.3f %7.3f %7.3f%n", c[i].c.x, c[i].c.y, c[i].r); 34 | 35 | new SuperMan(s, t, c, ncircles); 36 | } 37 | 38 | @Test 39 | public void testSuperin1() throws IOException { 40 | TestEngine.execute(this, "superin1", "superin1-out"); 41 | } 42 | 43 | @Test 44 | public void testSuperin2() throws IOException { 45 | TestEngine.execute(this, "superin2", "superin2-out"); 46 | } 47 | 48 | @Test 49 | public void testSuperin3() throws IOException { 50 | TestEngine.execute(this, "superin3", "superin3-out"); 51 | } 52 | 53 | @Test 54 | public void testSuperin4() throws IOException { 55 | TestEngine.execute(this, "superin4", "superin4-out"); 56 | } 57 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/geometry/TriangulateTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.geometry; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | import static com.algorist.geometry.Geometry.Point; 11 | import static com.algorist.geometry.Geometry.Point.readPoints; 12 | import static com.algorist.geometry.Geometry.Polygon; 13 | import static com.algorist.geometry.Triangulate.area; 14 | import static com.algorist.geometry.Triangulate.areaTriangulation; 15 | 16 | public class TriangulateTest implements TestCaseWithInput { 17 | @Override 18 | public void process(Scanner scanner) { 19 | Point[] points = readPoints(scanner); 20 | 21 | Polygon p = new Polygon(points); 22 | 23 | System.out.printf(" area via triangulation = %f%n", areaTriangulation(p)); 24 | System.out.printf(" area slick = %f%n", area(p)); 25 | } 26 | 27 | @Test 28 | public void testTri1() throws IOException { 29 | TestEngine.execute(this, "tri1", "tri1-out"); 30 | } 31 | 32 | @Test 33 | public void testTri2() throws IOException { 34 | TestEngine.execute(this, "tri2", "tri2-out"); 35 | } 36 | 37 | @Test 38 | public void testTri3() throws IOException { 39 | TestEngine.execute(this, "tri3", "tri3-out"); 40 | } 41 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/BiconnectedTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class BiconnectedTest implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | UnweightedGraphReader reader = new UnweightedGraphReader(); 14 | Graph g = reader.readGraph(scanner, false); 15 | g.print(); 16 | 17 | new Biconnected<>(g); 18 | } 19 | 20 | @Test 21 | public void testGrid() throws IOException { 22 | TestEngine.execute(this, "grid", "biconnected-grid"); 23 | } 24 | 25 | @Test 26 | public void testTree() throws IOException { 27 | TestEngine.execute(this, "tree", "biconnected-tree"); 28 | } 29 | 30 | @Test 31 | public void testArt3() throws IOException { 32 | TestEngine.execute(this, "art3", "biconnected-art3"); 33 | } 34 | 35 | @Test 36 | public void testBaase() throws IOException { 37 | TestEngine.execute(this, "baase", "biconnected-baase"); 38 | } 39 | 40 | @Test 41 | public void testClrGraph() throws IOException { 42 | TestEngine.execute(this, "clr-graph", "biconnected-clr-graph"); 43 | } 44 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/BipartiteTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class BipartiteTest implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | UnweightedGraphReader reader = new UnweightedGraphReader(); 14 | Graph g = reader.readGraph(scanner, false); 15 | g.print(); 16 | 17 | Bipartite bipartite = new Bipartite<>(g); 18 | for (int i = 1; i <= g.nvertices(); i++) 19 | System.out.printf(" %d", bipartite.color(i).ordinal()); 20 | System.out.println(); 21 | } 22 | 23 | @Test 24 | public void testGrid() throws IOException { 25 | TestEngine.execute(this, "grid", "bipartite-grid"); 26 | } 27 | 28 | @Test 29 | public void testTree() throws IOException { 30 | TestEngine.execute(this, "tree", "bipartite-tree"); 31 | } 32 | 33 | @Test 34 | public void testArt3() throws IOException { 35 | TestEngine.execute(this, "art3", "bipartite-art3"); 36 | } 37 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/ConnectedTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class ConnectedTest implements TestCaseWithInput { 11 | 12 | @Override 13 | public void process(Scanner scanner) { 14 | UnweightedGraphReader reader = new UnweightedGraphReader(); 15 | Graph g = reader.readGraph(scanner, false); 16 | g.print(); 17 | 18 | new Connected<>(g); 19 | } 20 | 21 | @Test 22 | public void testConnected() throws IOException { 23 | TestEngine.execute(this, "connected-in", "connected-out"); 24 | } 25 | 26 | @Test 27 | public void testGrid() throws IOException { 28 | TestEngine.execute(this, "grid", "grid-connected-out"); 29 | } 30 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/FindCycleTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import org.junit.Test; 4 | 5 | public class FindCycleTest { 6 | @Test 7 | public void test() { 8 | final Graph g = new Graph<>(4, true); 9 | g.insertEdge(1, new UnweightedEdgeNode(2), true); 10 | g.insertEdge(2, new UnweightedEdgeNode(3), true); 11 | g.insertEdge(3, new UnweightedEdgeNode(1), true); 12 | g.insertEdge(3, new UnweightedEdgeNode(4), true); 13 | 14 | g.print(); 15 | 16 | new FindCycle(g); 17 | } 18 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/StrongTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class StrongTest implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | UnweightedGraphReader reader = new UnweightedGraphReader(); 14 | Graph g = reader.readGraph(scanner, true); 15 | g.print(); 16 | 17 | new Strong<>(g); 18 | } 19 | 20 | @Test 21 | public void testG1() throws IOException { 22 | TestEngine.execute(this, "g-1", "strong-g-1"); 23 | } 24 | 25 | @Test 26 | public void testG2() throws IOException { 27 | TestEngine.execute(this, "g-2", "strong-g-2"); 28 | } 29 | 30 | @Test 31 | public void testG3() throws IOException { 32 | TestEngine.execute(this, "g-3", "strong-g-3"); 33 | } 34 | 35 | @Test 36 | public void testG4() throws IOException { 37 | TestEngine.execute(this, "g-4", "strong-g-4"); 38 | } 39 | 40 | @Test 41 | public void testG5() throws IOException { 42 | TestEngine.execute(this, "g-5", "strong-g-5"); 43 | } 44 | 45 | @Test 46 | public void testClr() throws IOException { 47 | TestEngine.execute(this, "strong-clr", "strong-clr-out"); 48 | } 49 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/TopSort1Test.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class TopSort1Test implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | UnweightedGraphReader reader = new UnweightedGraphReader(); 14 | Graph g = reader.readGraph(scanner, true); 15 | g.print(); 16 | 17 | new TopSort1<>(g); 18 | } 19 | 20 | @Test 21 | public void test() throws IOException { 22 | TestEngine.execute(this, "grid", "grid-topsort1-out"); 23 | } 24 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/graph/TopSortTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.graph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class TopSortTest implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | UnweightedGraphReader reader = new UnweightedGraphReader(); 14 | Graph g = reader.readGraph(scanner, true); 15 | g.print(); 16 | 17 | TopSort topSort = new TopSort<>(g); 18 | 19 | int[] out = topSort.sorted(); 20 | for (int i = 1; i <= g.nvertices(); i++) 21 | System.out.printf(" %d", out[i]); 22 | System.out.println(); 23 | } 24 | 25 | @Test 26 | public void test() throws IOException { 27 | TestEngine.execute(this, "grid", "grid-topsort-out"); 28 | } 29 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/numerical/BigNumTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.numerical; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | import static com.algorist.numerical.BigNum.*; 10 | 11 | public class BigNumTest implements TestCaseWithInput { 12 | @Override 13 | public void process(Scanner scanner) { 14 | int a, b; 15 | BigNum n1, n2, n3, zero; 16 | 17 | while (scanner.hasNext()) { 18 | a = scanner.nextInt(); 19 | b = scanner.nextInt(); 20 | System.out.printf("a = %d b = %d%n", a, b); 21 | n1 = intToBignum(a); 22 | n2 = intToBignum(b); 23 | 24 | n3 = add(n1, n2); 25 | 26 | System.out.print("addition -- "); 27 | n3.print(); 28 | 29 | System.out.printf("compare_bignum a ? b = %d%n", compare(n1, n2)); 30 | 31 | n3 = subtract(n1, n2); 32 | System.out.print("subtraction -- "); 33 | n3.print(); 34 | 35 | n3 = multiply(n1, n2); 36 | System.out.print("multiplication -- "); 37 | n3.print(); 38 | 39 | zero = intToBignum(0); 40 | if (compare(zero, n2) == 0) 41 | System.out.println("division -- NaN "); 42 | else { 43 | n3 = divide(n1, n2); 44 | System.out.print("division -- "); 45 | n3.print(); 46 | } 47 | System.out.println("--------------------------"); 48 | } 49 | } 50 | 51 | @Test 52 | public void test() throws Exception { 53 | TestEngine.execute(this, "bignum-in", "bignum-out"); 54 | } 55 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/numerical/GCDTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.numerical; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | public class GCDTest implements TestCaseWithInput { 10 | @Override 11 | public void process(Scanner scanner) { 12 | long p, q; 13 | long x, y, g1, g2; 14 | 15 | while (scanner.hasNext()) { 16 | p = scanner.nextLong(); 17 | q = scanner.nextLong(); 18 | System.out.printf("gcd of p=%d and q=%d = %d%n", p, q, g1 = GCD.gcd1(p, q)); 19 | long[] r = GCD.gcd(p, q); 20 | x = r[1]; 21 | y = r[2]; 22 | System.out.printf(" %d*%d + %d*%d = %d%n", p, x, q, y, g2 = r[0]); 23 | 24 | if (g1 != g2) System.out.println("ERROR: GCD"); 25 | if ((p * x + q * y) != g1) System.out.println("ERROR: DIOPHONINE SOLUTION WRONG!"); 26 | } 27 | } 28 | 29 | @Test 30 | public void test() throws Exception { 31 | TestEngine.execute(this, "gcd-in", "gcd-out"); 32 | } 33 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/numerical/MatrixTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.numerical; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class MatrixTest implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | Matrix a = Matrix.read(scanner); 14 | a.print(); 15 | 16 | Matrix b = Matrix.read(scanner); 17 | b.print(); 18 | 19 | Matrix c = Matrix.multiply(a, b); 20 | c.print(); 21 | } 22 | 23 | @Test 24 | public void testMatrixData1() throws IOException { 25 | TestEngine.execute(this, "matrix-data1", "matrix-data1-out"); 26 | } 27 | 28 | @Test 29 | public void testMatrixData2() throws IOException { 30 | TestEngine.execute(this, "matrix-data2", "matrix-data2-out"); 31 | } 32 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/numerical/PrimesTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.numerical; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | import static com.algorist.numerical.Primes.primeFactorization; 11 | 12 | public class PrimesTest implements TestCaseWithInput { 13 | @Override 14 | public void process(Scanner scanner) { 15 | long p; 16 | while (scanner.hasNext()) { 17 | p = scanner.nextLong(); 18 | System.out.printf("prime factorization of p=%d %n", p); 19 | primeFactorization(p); 20 | } 21 | } 22 | 23 | @Test 24 | public void test() throws IOException { 25 | TestEngine.execute(this, "primes-in", "primes-out"); 26 | } 27 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/sort/SentinelsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2003 by Steven S. Skiena; all rights reserved. 3 | 4 | Permission is granted for use in non-commerical applications 5 | provided this copyright notice remains intact and unchanged. 6 | 7 | This program appears in my book: 8 | 9 | "Programming Challenges: The Programming Contest Training Manual" 10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 11 | 12 | See our website www.programming-challenges.com for additional information. 13 | 14 | This book can be ordered from Amazon.com at 15 | 16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 17 | 18 | */ 19 | package com.algorist.sort; 20 | 21 | import org.junit.Test; 22 | 23 | /** 24 | * Example search program using sentinels 25 | * 26 | * @author csong2022 27 | */ 28 | public class SentinelsTest { 29 | private static final int MAXINT = 1000000; 30 | 31 | @Test 32 | public void test() { 33 | int i, n, x; 34 | int[] a = new int[100]; 35 | 36 | for (i = 1; i <= 20; i++) a[i] = i; 37 | n = 20; 38 | x = -1; 39 | 40 | i = n; 41 | while ((a[i] >= x) && (i >= 1)) { 42 | a[i + 1] = a[i]; 43 | i = i - 1; 44 | } 45 | a[i + 1] = x; 46 | 47 | System.out.printf("without sentinel, i=%d a[1]=%d%n", i, a[1]); 48 | for (i = 1; i <= 25; i++) System.out.printf("%d ", a[i]); 49 | x = -2; 50 | 51 | n = 21; 52 | i = n; 53 | a[0] = -MAXINT; 54 | while (a[i] >= x) { 55 | a[i + 1] = a[i]; 56 | i = i - 1; 57 | } 58 | a[i + 1] = x; 59 | 60 | System.out.printf("%nwith sentinel, i=%d a[1]=%d%n", i, a[1]); 61 | for (i = 1; i <= 25; i++) System.out.printf("%d ", a[i]); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/com/algorist/string/NameTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.string; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | import static com.algorist.string.Name.processMergers; 11 | import static com.algorist.string.Name.readChanges; 12 | 13 | public class NameTest implements TestCaseWithInput { 14 | private static final int MAXLEN = 1001; /* longest possible string */ 15 | 16 | @Override 17 | public void process(Scanner scanner) { 18 | String[][] mergers = readChanges(scanner); 19 | 20 | int nlines = scanner.nextInt(); 21 | scanner.nextLine(); 22 | 23 | char[] s = new char[MAXLEN]; 24 | for (int i = 1; i <= nlines; i++) { 25 | String line = scanner.nextLine(); 26 | System.arraycopy(line.toCharArray(), 0, s, 0, line.length()); 27 | 28 | int slen = processMergers(s, line.length(), mergers, mergers.length); 29 | System.out.println(new String(s, 0, slen)); 30 | } 31 | } 32 | 33 | @Test 34 | public void test() throws IOException { 35 | TestEngine.execute(this, "name-in", "name-out"); 36 | } 37 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/test/TestCaseWithInput.java: -------------------------------------------------------------------------------- 1 | package com.algorist.test; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * Test case takes input, and produce output to stdout. 7 | * 8 | * @author csong2022 9 | */ 10 | public interface TestCaseWithInput { 11 | /** 12 | * Processes input, executes testing logic, produces output to stdout. 13 | * 14 | * @param scanner program input from stdin or file input. 15 | */ 16 | void process(Scanner scanner); 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/algorist/test/TestCaseWithoutInput.java: -------------------------------------------------------------------------------- 1 | package com.algorist.test; 2 | 3 | /** 4 | * Test case takes no input, and produce output to stdout. 5 | * 6 | * @author csong2022 7 | */ 8 | public interface TestCaseWithoutInput { 9 | /** 10 | * Executes testing logic, produces output to stdout. 11 | */ 12 | void process(); 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/algorist/wgraph/DijkstraTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.wgraph; 2 | 3 | import com.algorist.graph.Graph; 4 | import com.algorist.test.TestCaseWithInput; 5 | import com.algorist.test.TestEngine; 6 | import com.algorist.utils.IterableUtils; 7 | import org.junit.Test; 8 | 9 | import java.io.IOException; 10 | import java.util.Scanner; 11 | 12 | public class DijkstraTest implements TestCaseWithInput { 13 | @Override 14 | public void process(Scanner scanner) { 15 | WeightedGraphReader reader = new WeightedGraphReader(); 16 | Graph g = reader.readGraph(scanner, false); 17 | 18 | Dijkstra dijkstra = new Dijkstra(g, 1); 19 | 20 | System.out.println(); 21 | for (int i = 1; i <= g.nvertices(); i++) { 22 | /*printf(" %d parent=%d\n",i,parent[i]);*/ 23 | Iterable path = dijkstra.findPath(1, i); 24 | System.out.println(IterableUtils.toString(path)); 25 | } 26 | } 27 | 28 | @Test 29 | public void test() throws IOException { 30 | TestEngine.execute(this, "wgrid", "wgrid-dijkstra-out"); 31 | } 32 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/wgraph/FloydTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.wgraph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.io.IOException; 8 | import java.util.Scanner; 9 | 10 | public class FloydTest implements TestCaseWithInput { 11 | @Override 12 | public void process(Scanner scanner) { 13 | AdjacencyMatrix g = AdjacencyMatrix.readAdjacencyMatrix(scanner, false); 14 | g.printGraph(); 15 | 16 | new Floyd(g); 17 | 18 | g.print(); 19 | } 20 | 21 | @Test 22 | public void test() throws IOException { 23 | TestEngine.execute(this, "wgrid", "wgrid-floyd-out"); 24 | } 25 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/wgraph/NetflowTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.wgraph; 2 | 3 | import com.algorist.test.TestCaseWithInput; 4 | import com.algorist.test.TestEngine; 5 | import org.junit.Test; 6 | 7 | import java.util.Scanner; 8 | 9 | public class NetflowTest implements TestCaseWithInput { 10 | public void process(Scanner scanner) { 11 | int source = scanner.nextInt(); 12 | int sink = scanner.nextInt(); 13 | FlowGraph g = new FlowGraphReader().readGraph(scanner, true); 14 | 15 | new Netflow(g, source, sink); 16 | g.print(); 17 | 18 | int flow = 0; /* total flow */ 19 | for (FlowEdgeNode p : g.edges(source)) { 20 | flow += p.flow(); 21 | } 22 | 23 | System.out.printf("total flow = %d%n", flow); 24 | } 25 | 26 | @Test 27 | public void testNetflow1() throws Exception { 28 | TestEngine.execute(this, "netflow1-in", "netflow1-out"); 29 | } 30 | 31 | @Test 32 | public void testNetflow2() throws Exception { 33 | TestEngine.execute(this, "netflow2-in", "netflow2-out"); 34 | } 35 | 36 | @Test 37 | public void testNetflow3() throws Exception { 38 | TestEngine.execute(this, "netflow3-in", "netflow3-out"); 39 | } 40 | } -------------------------------------------------------------------------------- /src/test/java/com/algorist/wgraph/PrimTest.java: -------------------------------------------------------------------------------- 1 | package com.algorist.wgraph; 2 | 3 | import com.algorist.graph.Graph; 4 | import com.algorist.test.TestCaseWithInput; 5 | import com.algorist.test.TestEngine; 6 | import com.algorist.utils.IterableUtils; 7 | import org.junit.Test; 8 | 9 | import java.io.IOException; 10 | import java.util.Scanner; 11 | 12 | public class PrimTest implements TestCaseWithInput { 13 | @Override 14 | public void process(Scanner scanner) { 15 | WeightedGraphReader reader = new WeightedGraphReader(); 16 | Graph g = reader.readGraph(scanner, false); 17 | 18 | Prim prim = new Prim(g, 1); 19 | System.out.println("Out of Prim"); 20 | 21 | System.out.println(); 22 | for (int i = 1; i <= g.nvertices(); i++) { 23 | /*printf(" %d parent=%d\n",i,parent[i]);*/ 24 | Iterable path = prim.findPath(1, i); 25 | System.out.println(IterableUtils.toString(path)); 26 | } 27 | } 28 | 29 | @Test 30 | public void test() throws IOException { 31 | TestEngine.execute(this, "wgrid", "wgrid-prim-out"); 32 | } 33 | 34 | } --------------------------------------------------------------------------------