├── datafiles ├── i.2 ├── plates1-in ├── plates2-in ├── test ├── plates3-in ├── distance-out ├── editbrute-in ├── i.4 ├── tree ├── stringedit-in ├── tree3 ├── i.2-out ├── 10055-out ├── primes-in ├── simple-acyclic ├── simple-wgraph-dijkstra-out ├── tree2 ├── bridge-tree ├── lcs-out ├── partition-data1-out ├── partition-data2-out ├── backedge-tree ├── complete-4-graph ├── separate-comps ├── treeplus ├── backedge-parent-tree ├── substringedit-in ├── tri1-out ├── tri2-out ├── tri3-out ├── 10055-in ├── simple-wgraph ├── simple-wgraph-prim-out ├── superin4 ├── biconnected-complete-4-graph ├── bipartite-tree ├── partition-data1 ├── partition-data2 ├── convex-bad.10-out ├── elevator-in ├── i.9 ├── simple-acyclic-topsort-out ├── spbug-in ├── t.in ├── i.4-out ├── i.9-out ├── matrix-data2 ├── paths-graph ├── subsets-out ├── gcd-in ├── i.10 ├── simple-strong ├── convex-bad.10 ├── permutations-out ├── matrix-data1 ├── superin1 ├── superin2 ├── superin2~ ├── superin3~ ├── art3 ├── polly-out ├── superin3 ├── matrix-data2-out ├── t1.in ├── baase ├── netflow1-in ├── strong-clr ├── chin ├── chin1 ├── chin-out ├── chin1-out ├── i.10-out ├── regular-tree ├── g-1 ├── biconnected-tree ├── g-5 ├── list-in ├── matrix-data1-out ├── superin4-out ├── separate-comps-connected-out ├── polly-in ├── biconnected-art3 ├── i.19 ├── i.19-out ├── biconnected-backedge-tree ├── name-out ├── biconnected-backedge-parent-tree ├── connected-in ├── bipartite-art3 ├── biconnected-bridge-tree ├── tri3 ├── g-3 ├── bignum-in ├── g-2 ├── g-4 ├── grid4 ├── tri2 ├── netflow1-out ├── bignum-in.v1 ├── geotest-in ├── plates1-out ├── 8-queens-out ├── biconnected-baase ├── list-out ├── war-out-old ├── clr-graph ├── primes-out ├── name-in ├── strong-clr-out ├── treelist-out ├── war-out ├── superin2-out ├── t1.out ├── simple-strong-strong-out ├── strong-g-1 ├── strong-g-5 ├── grid ├── grid-topsort-out ├── grid-topsort1-out ├── plates2-out ├── strong-g-2 ├── strong-g-3 ├── wgrid-dijkstra-out ├── strong-g-4 ├── VDError-uniq.dat-out ├── biconnected-grid ├── superin1-out ├── tri1 ├── connected-out ├── bipartite-grid ├── cgtest-in ├── gridw ├── wgrid-disconnected2 ├── grid-connected-out ├── editbrute-out ├── superin3-out ├── wgrid-disconnected1 ├── wgrid-prim-out ├── wgrid ├── super.m ├── netflow2-in ├── war-in ├── elevator-out ├── paths-graph-out ├── biconnected-clr-graph ├── order-out ├── regular-tree-bfs-demo-out ├── netflow2-out ├── gcd-out ├── wgrid.m ├── stringedit-out ├── regular-tree-dfs-demo-out ├── VDError-uniq.dat ├── VDError.dat ├── outgraph.m ├── substringedit-out ├── cgtest-out └── grid-bfs-demo-out ├── run_criterion_tests.sh ├── tsp-examples ├── tsp5 ├── tsp-48sol ├── tsp-48 └── tsp48 ├── .gitattributes ├── component-graphs ├── run ├── strong-clr ├── g-1 ├── g-5 ├── g-3 ├── g-2 ├── g-4 ├── ANSWERS └── convert.m ├── criterion_test.c ├── bool.h ├── original ├── bool.h ├── 10055.c ├── tsp.h ├── binomial.c ├── backtrack.h ├── queue.h ├── graph.h ├── distance.c ├── editdistance.h ├── graphp.h ├── wgraph.h ├── sentinel.c ├── primes.c ├── subsets.c ├── findcycle.c ├── backtrack.c ├── bfs-demo.c ├── dfs-demo.c ├── connected.c ├── permutations.c ├── geometry.h ├── gcd.c ├── queue.c ├── order.c ├── topsort.c ├── 8-queens.c ├── annealing.h ├── dijkstra.c ├── partition.c ├── graph.c ├── prim.c ├── paths.c ├── fib.c ├── polly.c ├── geotest.c ├── stringedit.c ├── superman.c └── lcs.c ├── sudoku-examples ├── champ ├── evil18 ├── evil2 ├── puzzle ├── evil0 ├── evil1 ├── minimum1 ├── minimum2 ├── minimum3 └── puzzle0 ├── .travis.yml ├── distance.h ├── 10055.pascal ├── 10055.cc ├── 10055.c ├── tests └── distance_test.c ├── tsp.h ├── item.h ├── random.h ├── list.h ├── backtrack.h ├── bfs-dfs.h ├── binomial.c ├── stack.h ├── 10055.java ├── tree.h ├── distance.c ├── set_union.h ├── queue.h ├── editbrute.h ├── wgraph.h ├── priority_queue.h ├── primes.c ├── sentinel.c ├── findcycle.c ├── cgtest.c ├── stack.c ├── bfs-demo.c ├── connected.c ├── editdistance.h ├── backtrack.c ├── queue.c ├── graph.h ├── set_union.c ├── topsort1.c ├── gcd.c ├── order.c ├── dfs-demo.c ├── random.c └── geotest.c /datafiles/i.2: -------------------------------------------------------------------------------- 1 | 2 2 | 0 1 3 | 1 1 4 | -------------------------------------------------------------------------------- /datafiles/plates1-in: -------------------------------------------------------------------------------- 1 | 4.0 4.0 0.5 2 | -------------------------------------------------------------------------------- /datafiles/plates2-in: -------------------------------------------------------------------------------- 1 | 10.0 10.0 0.5 2 | -------------------------------------------------------------------------------- /datafiles/test: -------------------------------------------------------------------------------- 1 | 1 2 2 | 3 2 3 | 0 0 4 | -------------------------------------------------------------------------------- /run_criterion_tests.sh: -------------------------------------------------------------------------------- 1 | ./criterion_test -------------------------------------------------------------------------------- /datafiles/plates3-in: -------------------------------------------------------------------------------- 1 | 100.0 100.0 0.5 2 | -------------------------------------------------------------------------------- /datafiles/distance-out: -------------------------------------------------------------------------------- 1 | distance = 1.414214 2 | -------------------------------------------------------------------------------- /datafiles/editbrute-in: -------------------------------------------------------------------------------- 1 | afdsdf 2 | sadfasd 3 | -------------------------------------------------------------------------------- /datafiles/i.4: -------------------------------------------------------------------------------- 1 | 4 2 | 0 0 3 | 0 1 4 | 1 1 5 | 1 0 6 | -------------------------------------------------------------------------------- /datafiles/tree: -------------------------------------------------------------------------------- 1 | 5 4 2 | 1 2 3 | 1 3 4 | 2 4 5 | 2 5 6 | -------------------------------------------------------------------------------- /datafiles/stringedit-in: -------------------------------------------------------------------------------- 1 | thou-shalt-not 2 | you-should-not 3 | -------------------------------------------------------------------------------- /datafiles/tree3: -------------------------------------------------------------------------------- 1 | 6 5 2 | 1 2 3 | 2 3 4 | 2 4 5 | 3 5 6 | 3 6 7 | -------------------------------------------------------------------------------- /datafiles/i.2-out: -------------------------------------------------------------------------------- 1 | (0.000000,1.000000) 2 | (1.000000,1.000000) 3 | -------------------------------------------------------------------------------- /datafiles/10055-out: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 0 4 | 9988888 5 | 39 6 | 2 7 | 20 8 | -------------------------------------------------------------------------------- /datafiles/primes-in: -------------------------------------------------------------------------------- 1 | 17 2 | 234 3 | 128 4 | 2147483647 5 | 9409 6 | 1 7 | -------------------------------------------------------------------------------- /datafiles/simple-acyclic: -------------------------------------------------------------------------------- 1 | 5 5 2 | 1 2 3 | 1 3 4 | 1 5 5 | 4 1 6 | 2 5 7 | -------------------------------------------------------------------------------- /datafiles/simple-wgraph-dijkstra-out: -------------------------------------------------------------------------------- 1 | 2 | 1 3 | 1 2 4 | 1 3 5 | 1 4 6 | -------------------------------------------------------------------------------- /datafiles/tree2: -------------------------------------------------------------------------------- 1 | 6 5 2 | 1 2 3 | 1 3 4 | 1 6 5 | 2 4 6 | 2 5 7 | 3 6 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/bridge-tree: -------------------------------------------------------------------------------- 1 | 6 6 2 | 1 2 3 | 1 3 4 | 2 4 5 | 4 5 6 | 4 6 7 | 5 6 8 | -------------------------------------------------------------------------------- /datafiles/lcs-out: -------------------------------------------------------------------------------- 1 | length of longest common subsequence = 10 2 | ou-shl-not 3 | -------------------------------------------------------------------------------- /datafiles/partition-data1-out: -------------------------------------------------------------------------------- 1 | { 1 2 3 4 5 } 2 | { 6 7 } 3 | { 8 9 } 4 | -------------------------------------------------------------------------------- /datafiles/partition-data2-out: -------------------------------------------------------------------------------- 1 | { 1 1 1 } 2 | { 1 1 1 } 3 | { 1 1 1 } 4 | -------------------------------------------------------------------------------- /datafiles/backedge-tree: -------------------------------------------------------------------------------- 1 | 6 6 2 | 1 2 3 | 1 3 4 | 2 4 5 | 2 5 6 | 4 6 7 | 6 2 8 | -------------------------------------------------------------------------------- /datafiles/complete-4-graph: -------------------------------------------------------------------------------- 1 | 4 6 2 | 1 2 3 | 1 3 4 | 1 4 5 | 2 3 6 | 2 4 7 | 3 4 8 | -------------------------------------------------------------------------------- /datafiles/separate-comps: -------------------------------------------------------------------------------- 1 | 7 6 2 | 1 2 3 | 1 3 4 | 2 3 5 | 4 5 6 | 5 6 7 | 4 6 8 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /datafiles/backedge-parent-tree: -------------------------------------------------------------------------------- 1 | 6 6 2 | 1 2 3 | 1 3 4 | 2 4 5 | 4 5 6 | 5 2 7 | 5 6 8 | -------------------------------------------------------------------------------- /datafiles/substringedit-in: -------------------------------------------------------------------------------- 1 | skiena 2 | my-name-is-not-skienna-skena-skina-skieng-etc 3 | -------------------------------------------------------------------------------- /datafiles/tri1-out: -------------------------------------------------------------------------------- 1 | area via triangulation = 0.343249 2 | area slick = 0.343249 3 | -------------------------------------------------------------------------------- /datafiles/tri2-out: -------------------------------------------------------------------------------- 1 | area via triangulation = 0.170820 2 | area slick = 0.170820 3 | -------------------------------------------------------------------------------- /datafiles/tri3-out: -------------------------------------------------------------------------------- 1 | area via triangulation = 0.224567 2 | area slick = 0.224567 3 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/simple-wgraph: -------------------------------------------------------------------------------- 1 | 4 6 2 | 1 2 2 3 | 1 3 5 4 | 1 4 1 5 | 2 4 1 6 | 2 3 4 7 | 4 3 6 8 | -------------------------------------------------------------------------------- /datafiles/simple-wgraph-prim-out: -------------------------------------------------------------------------------- 1 | Out of Prim 2 | 3 | 1 4 | 1 4 2 5 | 1 4 2 3 6 | 1 4 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/biconnected-complete-4-graph: -------------------------------------------------------------------------------- 1 | 1: 4 3 2 2 | 2: 4 3 1 3 | 3: 4 2 1 4 | 4: 3 2 1 5 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/convex-bad.10-out: -------------------------------------------------------------------------------- 1 | (0.000000,-12.000000) 2 | (1.000000,10.000000) 3 | (0.000000,12.000000) 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/simple-acyclic-topsort-out: -------------------------------------------------------------------------------- 1 | 1: 5 3 2 2 | 2: 5 3 | 3: 4 | 4: 1 5 | 5: 6 | 4 1 3 2 5 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/subsets-out: -------------------------------------------------------------------------------- 1 | { 1 2 3 } 2 | { 1 2 } 3 | { 1 3 } 4 | { 1 } 5 | { 2 3 } 6 | { 2 } 7 | { 3 } 8 | { } 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /component-graphs/run: -------------------------------------------------------------------------------- 1 | ../strong < g-1 2 | ../strong < g-2 3 | ../strong < g-3 4 | ../strong < g-4 5 | ../strong < g-5 6 | -------------------------------------------------------------------------------- /criterion_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Test(test, simple) { 4 | cr_assert(1, "hello world!"); 5 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/simple-strong: -------------------------------------------------------------------------------- 1 | 9 10 2 | 1 2 3 | 2 3 4 | 3 1 5 | 2 5 6 | 5 3 7 | 5 6 8 | 6 7 9 | 7 8 10 | 8 6 11 | 8 9 12 | -------------------------------------------------------------------------------- /bool.h: -------------------------------------------------------------------------------- 1 | /* bool.h 2 | Simple boolean datatype 3 | */ 4 | 5 | #define TRUE 1 6 | #define FALSE 0 7 | 8 | typedef int bool; 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /original/bool.h: -------------------------------------------------------------------------------- 1 | /* bool.h 2 | Simple boolean datatype 3 | */ 4 | 5 | #define TRUE 1 6 | #define FALSE 0 7 | 8 | typedef int bool; 9 | 10 | -------------------------------------------------------------------------------- /sudoku-examples/champ: -------------------------------------------------------------------------------- 1 | 800750003 2 | 030048020 3 | 100000006 4 | 340070008 5 | 790480031 6 | 208000074 7 | 500814007 8 | 080327040 9 | 400569002 10 | -------------------------------------------------------------------------------- /sudoku-examples/evil18: -------------------------------------------------------------------------------- 1 | 070000009 2 | 000700500 3 | 032050080 4 | 007630054 5 | 000080000 6 | 360095200 7 | 020010730 8 | 004300000 9 | 100000090 10 | -------------------------------------------------------------------------------- /sudoku-examples/evil2: -------------------------------------------------------------------------------- 1 | 100000000 2 | 005406100 3 | 080000090 4 | 004010500 5 | 070090020 6 | 006080300 7 | 020100070 8 | 000503600 9 | 000000000 10 | -------------------------------------------------------------------------------- /sudoku-examples/puzzle: -------------------------------------------------------------------------------- 1 | 800000320 2 | 070304968 3 | 300060500 4 | 000831400 5 | 400090002 6 | 001425000 7 | 008050006 8 | 123607050 9 | 057000004 10 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /sudoku-examples/evil0: -------------------------------------------------------------------------------- 1 | 300600000 2 | 500007000 3 | 870090400 4 | 080050000 5 | 064000790 6 | 000020030 7 | 001040078 8 | 000300002 9 | 000005004 10 | 11 | -------------------------------------------------------------------------------- /sudoku-examples/evil1: -------------------------------------------------------------------------------- 1 | 000000000 2 | 000000000 3 | 000000000 4 | 000000000 5 | 000000000 6 | 000000000 7 | 000000000 8 | 000000000 9 | 000000000 10 | 11 | -------------------------------------------------------------------------------- /sudoku-examples/minimum1: -------------------------------------------------------------------------------- 1 | 000000010 2 | 400000000 3 | 020000000 4 | 000050407 5 | 008000300 6 | 001090000 7 | 300400200 8 | 050100000 9 | 000806000 10 | -------------------------------------------------------------------------------- /sudoku-examples/minimum2: -------------------------------------------------------------------------------- 1 | 000000010 2 | 400000000 3 | 020000000 4 | 000050604 5 | 008000300 6 | 001090000 7 | 300400200 8 | 050100000 9 | 000807000 10 | -------------------------------------------------------------------------------- /sudoku-examples/minimum3: -------------------------------------------------------------------------------- 1 | 000000012 2 | 000035000 3 | 000600070 4 | 700000300 5 | 000400800 6 | 100000000 7 | 000120000 8 | 080000040 9 | 050000600 10 | -------------------------------------------------------------------------------- /sudoku-examples/puzzle0: -------------------------------------------------------------------------------- 1 | 070020000 2 | 420501003 3 | 509307400 4 | 760000004 5 | 008609300 6 | 900000012 7 | 004702106 8 | 600803079 9 | 000090040 10 | -------------------------------------------------------------------------------- /component-graphs/strong-clr: -------------------------------------------------------------------------------- 1 | 8 13 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/regular-tree: -------------------------------------------------------------------------------- 1 | 15 14 2 | 1 2 3 | 1 3 4 | 2 4 5 | 2 5 6 | 3 6 7 | 3 7 8 | 4 8 9 | 4 9 10 | 5 10 11 | 5 11 12 | 6 12 13 | 6 13 14 | 7 14 15 | 7 15 16 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /component-graphs/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 | -------------------------------------------------------------------------------- /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: 2 7 | bridge articulation vertex: 2 8 | root articulation vertex: 1 9 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /component-graphs/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/separate-comps-connected-out: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 3 1 3 | 3: 2 1 4 | 4: 6 5 5 | 5: 6 4 6 | 6: 4 5 7 | 7: 8 | Component 1: 1 3 2 9 | Component 2: 4 6 5 10 | Component 3: 7 11 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | before_install: 3 | - sudo add-apt-repository -y ppa:snaipewastaken/ppa 4 | - sudo apt-get -y update 5 | - sudo apt-get install criterion-dev 6 | script: make && ./test-script.sh 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/biconnected-backedge-tree: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 6 5 4 1 3 | 3: 1 4 | 4: 6 2 5 | 5: 2 6 | 6: 2 4 7 | parent articulation vertex: 2 8 | bridge articulation vertex: 2 9 | root articulation vertex: 1 10 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /distance.h: -------------------------------------------------------------------------------- 1 | #ifndef DISTANCE_H 2 | #define DISTANCE_H 3 | 4 | #include 5 | 6 | #define DIMENSION 3 7 | 8 | typedef int point[DIMENSION]; 9 | 10 | double distance(point, point); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /datafiles/biconnected-backedge-parent-tree: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 5 4 1 3 | 3: 1 4 | 4: 5 2 5 | 5: 6 2 4 6 | 6: 5 7 | bridge articulation vertex: 5 8 | parent articulation vertex: 2 9 | root articulation vertex: 1 10 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/biconnected-bridge-tree: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 4 1 3 | 3: 1 4 | 4: 6 5 2 5 | 5: 6 4 6 | 6: 5 4 7 | parent articulation vertex: 4 8 | bridge articulation vertex: 2 9 | non-leaf articulation vertex: 4 10 | root articulation vertex: 1 11 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /component-graphs/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /original/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /component-graphs/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 | -------------------------------------------------------------------------------- /component-graphs/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | non-leaf articulation vertex: 5 15 | -------------------------------------------------------------------------------- /10055.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(void) { 5 | long long a, b, c; 6 | 7 | while (cin >> a >> b) { 8 | if (b > a) { 9 | c = b - a; 10 | } else { 11 | c = a - b; 12 | } 13 | cout << c << endl; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /10055.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int main(void) { 5 | long p, q, r; 6 | 7 | while (scanf("%ld %ld", &p, &q) != EOF) { 8 | if (q > p) { 9 | r = q - p; 10 | } else { 11 | r = p - q; 12 | } 13 | printf("%ld\n",r); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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=90, suit=100 5 | Warning: bad input value=50, suit=102 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /datafiles/simple-strong-strong-out: -------------------------------------------------------------------------------- 1 | 1: 2 2 | 2: 5 3 3 | 3: 1 4 | 4: 5 | 5: 6 3 6 | 6: 7 7 | 7: 8 8 | 8: 9 6 9 | 9: 10 | 9 is in component 1 11 | 6 is in component 2 12 | 8 is in component 2 with 6 13 | 7 is in component 2 with 6 14 | 1 is in component 3 15 | 3 is in component 3 with 1 16 | 5 is in component 3 with 1 17 | 2 is in component 3 with 1 18 | 4 is in component 4 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tests/distance_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../distance.h" 4 | 5 | 6 | #define EXPECTED_DISTANCE 1.414214 7 | 8 | Test(distance_test, simple) { 9 | double result; 10 | point a = {6, 2, 3}; 11 | point b = {6, 3, 4}; 12 | 13 | result = distance(a, b); 14 | 15 | // cr_expect_eq(result, EXPECTED_DISTANCE, 16 | // "Distance not what was expected. Expected: [%.6f] Actual: [%.6f]\n", EXPECTED_DISTANCE, result); 17 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /component-graphs/ANSWERS: -------------------------------------------------------------------------------- 1 | 2 | In[9]:= OutSCC[10, 0.2, 1] 3 | 4 | Out[9]= {{7}, {4, 5, 3}, {1}, {2}, {10}, {9}, {6}, {8}} 5 | 6 | In[10]:= OutSCC[10, 0.2, 2] 7 | 8 | Out[10]= {{8}, {10, 7, 6, 4, 9, 2, 5, 3}, {1}} 9 | 10 | In[11]:= OutSCC[10, 0.2, 3] 11 | 12 | Out[11]= {{4, 6, 5, 10, 3, 9, 7, 1}, {2}, {8}} 13 | 14 | In[12]:= OutSCC[10, 0.2, 4] 15 | 16 | Out[12]= {{9}, {10, 6, 4, 3, 7, 8, 5, 2, 1}} 17 | 18 | In[13]:= OutSCC[10, 0.2, 5] 19 | 20 | Out[13]= {{3}, {2}, {7, 4, 6, 5}, {10}, {1}, {8}, {9}} 21 | 22 | 23 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tsp.h: -------------------------------------------------------------------------------- 1 | #define NMAX 1000 /* maximum number of points */ 2 | 3 | typedef struct { 4 | int x, y; /* x and y coordinates of point */ 5 | } point; 6 | 7 | typedef struct { 8 | int n; /* how many points in problem? */ 9 | point p[NMAX+1]; /* array of points */ 10 | } tsp_instance; 11 | 12 | typedef struct { 13 | int n; /* how many elements in permutation? */ 14 | int p[NMAX+1]; /* array if indices */ 15 | } tsp_solution; 16 | 17 | double solution_cost(tsp_solution *s, tsp_instance *t); 18 | void initialize_solution(int n, tsp_solution *s); 19 | void copy_solution(tsp_solution *s, tsp_solution *t); 20 | void random_solution(tsp_solution *s); 21 | double transition(tsp_solution *s, tsp_instance *t, int i, int j); 22 | -------------------------------------------------------------------------------- /item.h: -------------------------------------------------------------------------------- 1 | 2 | /* item.h 3 | 4 | Header file for linked 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /original/binomial.c: -------------------------------------------------------------------------------- 1 | /* binomial.c 2 | Compute the binomial coefficients using dynamic programming 3 | 4 | begun: February 10, 2002 5 | by: Steven Skiena 6 | */ 7 | 8 | #define MAXN 100 /* largest n or m */ 9 | 10 | long binomial_coefficient(n,m) 11 | int n,m; /* computer n choose m */ 12 | { 13 | int i,j; /* counters */ 14 | long bc[MAXN][MAXN]; /* table of binomial coefficient values */ 15 | 16 | for (i=0; i<=n; i++) bc[i][0] = 1; 17 | 18 | for (j=0; j<=n; j++) bc[j][j] = 1; 19 | 20 | 21 | for (i=1; i<=n; i++) 22 | for (j=1; j 8 | 9 | #define MAXN 100 /* largest n or m */ 10 | 11 | /* computer n choose m */ 12 | 13 | /* [[[ binomial_coeff_cut */ 14 | long binomial_coefficient(int n, int m) { 15 | int i, j; /* counters */ 16 | long bc[MAXN][MAXN]; /* table of binomial coefficient values */ 17 | 18 | for (i = 0; i <= n; i++) { 19 | bc[i][0] = 1; 20 | } 21 | 22 | for (j = 0; j <= n; j++) { 23 | bc[j][j] = 1; 24 | } 25 | 26 | for (i = 1; i <= n; i++) { 27 | for (j = 1; j < i; j++) { 28 | bc[i][j] = bc[i-1][j-1] + bc[i-1][j]; 29 | } 30 | } 31 | 32 | return(bc[n][m]); 33 | } 34 | /* ]]] */ 35 | 36 | int main(void) { 37 | int a, b; 38 | 39 | while (1) { 40 | scanf("%d %d", &a, &b); 41 | printf("%d\n", binomial_coefficient(a, b)); 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /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 | #define STACKSIZE 1000 29 | 30 | typedef struct { 31 | int s[STACKSIZE+1]; /* body of queue */ 32 | int top; /* position of top element */ 33 | int count; /* number of stack elements */ 34 | } stack; 35 | 36 | void init_stack(stack *s); 37 | void push(stack *s, int x); 38 | int pop(stack *s); 39 | void print_stack(stack *s); 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | /* [[[ struct_tree_cut */ 33 | typedef struct tree { 34 | item_type item; /* data item */ 35 | struct tree *parent; /* pointer to parent */ 36 | struct tree *left; /* pointer to left child */ 37 | struct tree *right; /* pointer to right child */ 38 | } tree; 39 | /* ]]] */ 40 | -------------------------------------------------------------------------------- /original/graph.h: -------------------------------------------------------------------------------- 1 | /* graph.h 2 | Header file for graph data type 3 | 4 | by: Steven Skiena 5 | */ 6 | 7 | /* 8 | Copyright 2003 by Steven S. Skiena; all rights reserved. 9 | 10 | Permission is granted for use in non-commerical applications 11 | provided this copyright notice remains intact and unchanged. 12 | 13 | This program appears in my book: 14 | 15 | "Programming Challenges: The Programming Contest Training Manual" 16 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 17 | 18 | See our website www.programming-challenges.com for additional information. 19 | 20 | This book can be ordered from Amazon.com at 21 | 22 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 23 | 24 | */ 25 | 26 | 27 | #define MAXV 100 /* maximum number of vertices */ 28 | #define MAXDEGREE 50 /* maximum outdegree of a vertex */ 29 | 30 | typedef struct { 31 | int edges[MAXV+1][MAXDEGREE]; /* adjacency info */ 32 | int degree[MAXV+1]; /* outdegree of each vertex */ 33 | int nvertices; /* number of vertices in the graph */ 34 | int nedges; /* number of edges in the graph */ 35 | } graph; 36 | 37 | 38 | -------------------------------------------------------------------------------- /datafiles/regular-tree-bfs-demo-out: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 5 4 1 3 | 3: 7 6 1 4 | 4: 9 8 2 5 | 5: 11 10 2 6 | 6: 13 12 3 7 | 7: 15 14 3 8 | 8: 4 9 | 9: 4 10 | 10: 5 11 | 11: 5 12 | 12: 6 13 | 13: 6 14 | 14: 7 15 | 15: 7 16 | processed vertex 1 17 | processed edge (1,3) 18 | processed edge (1,2) 19 | processed vertex 3 20 | processed edge (3,7) 21 | processed edge (3,6) 22 | processed vertex 2 23 | processed edge (2,5) 24 | processed edge (2,4) 25 | processed vertex 7 26 | processed edge (7,15) 27 | processed edge (7,14) 28 | processed vertex 6 29 | processed edge (6,13) 30 | processed edge (6,12) 31 | processed vertex 5 32 | processed edge (5,11) 33 | processed edge (5,10) 34 | processed vertex 4 35 | processed edge (4,9) 36 | processed edge (4,8) 37 | processed vertex 15 38 | processed vertex 14 39 | processed vertex 13 40 | processed vertex 12 41 | processed vertex 11 42 | processed vertex 10 43 | processed vertex 9 44 | processed vertex 8 45 | -1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 46 | 47 | 1 48 | 1 2 49 | 1 3 50 | 1 2 4 51 | 1 2 5 52 | 1 3 6 53 | 1 3 7 54 | 1 2 4 8 55 | 1 2 4 9 56 | 1 2 5 10 57 | 1 2 5 11 58 | 1 3 6 12 59 | 1 3 6 13 60 | 1 3 7 14 61 | 1 3 7 15 62 | -------------------------------------------------------------------------------- /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 28 | 29 | #include "distance.h" 30 | 31 | double distance(point a, point b) { 32 | int i; 33 | double d = 0.0; 34 | 35 | for (i = 0; i < DIMENSION; i++) { 36 | d = d + (a[i] - b[i]) * (a[i] - b[i]); 37 | } 38 | 39 | return(sqrt(d)); 40 | } 41 | 42 | int main(void) { 43 | point a = {6, 2, 3}; 44 | point b = {6, 3, 4}; 45 | 46 | printf("distance = %f\n", distance(a, b)); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /original/editdistance.h: -------------------------------------------------------------------------------- 1 | 2 | /* editdistance.h 3 | 4 | Header file for string comparison 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 MAXLEN 101 /* longest possible string */ 30 | 31 | #define MATCH 0 /* enumerated type symbol for match */ 32 | #define INSERT 1 /* enumerated type symbol for insert */ 33 | #define DELETE 2 /* enumerated type symbol for delete */ 34 | 35 | typedef struct { 36 | int cost; /* cost of reaching this cell */ 37 | int parent; /* parent cell */ 38 | } cell; 39 | 40 | -------------------------------------------------------------------------------- /original/graphp.h: -------------------------------------------------------------------------------- 1 | /* graphp.h 2 | Header file for pointer-based graph data type 3 | 4 | by: Steven Skiena 5 | */ 6 | 7 | /* 8 | Copyright 2003 by Steven S. Skiena; all rights reserved. 9 | 10 | Permission is granted for use in non-commerical applications 11 | provided this copyright notice remains intact and unchanged. 12 | 13 | This program appears in my book: 14 | 15 | "Programming Challenges: The Programming Contest Training Manual" 16 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 17 | 18 | See our website www.programming-challenges.com for additional information. 19 | 20 | This book can be ordered from Amazon.com at 21 | 22 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 23 | 24 | */ 25 | 26 | 27 | #define MAXV 100 /* maximum number of vertices */ 28 | 29 | struct edgenode { 30 | int y; /* adjancency info */ 31 | int weight; /* edge weight, if any */ 32 | struct edgenode *next; /* next edge in list */ 33 | 34 | typedef struct { 35 | edgenode *edges[MAXV+1]; /* adjacency info */ 36 | int degree[MAXV+1]; /* outdegree of each vertex */ 37 | int nvertices; /* number of vertices in the graph */ 38 | int nedges; /* number of edges in the graph */ 39 | } graph; 40 | 41 | 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | #include "bool.h" 29 | 30 | #define SET_SIZE 1000 31 | 32 | /* [[[ set_union_cut */ 33 | typedef struct { 34 | int p[SET_SIZE+1]; /* parent element */ 35 | int size[SET_SIZE+1]; /* number of elements in subtree i */ 36 | int n; /* number of elements in set */ 37 | } set_union; 38 | /* ]]] */ 39 | 40 | void set_union_init(set_union *s, int n); 41 | void union_sets(set_union *s, int s1, int s2); 42 | bool same_component(set_union *s, int s1, int s2); 43 | void print_set_union(set_union *s); 44 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | #include "item.h" 29 | 30 | #define QUEUESIZE 1000 31 | 32 | typedef struct { 33 | int /*item_type*/ q[QUEUESIZE+1]; /* body of queue */ 34 | int first; /* position of first element */ 35 | int last; /* position of last element */ 36 | int count; /* number of queue elements */ 37 | } queue; 38 | 39 | void init_queue(queue *q); 40 | void enqueue(queue *q, item_type x); 41 | item_type dequeue(queue *q); 42 | item_type headq(queue *q); 43 | int empty_queue(queue *q); 44 | void print_queue(queue *q); 45 | -------------------------------------------------------------------------------- /editbrute.h: -------------------------------------------------------------------------------- 1 | 2 | /* editbrute.h 3 | 4 | Header file for string comparison 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 | #include "bool.h" 28 | 29 | #define MAXLEN 101 /* longest possible string */ 30 | #define MATCH 0 /* enumerated type symbol for match */ 31 | #define INSERT 1 /* enumerated type symbol for insert */ 32 | #define DELETE 2 /* enumerated type symbol for delete */ 33 | 34 | typedef struct { 35 | int cost; /* cost of reaching this cell */ 36 | int parent; /* parent cell */ 37 | } cell; 38 | 39 | int string_compare(char *s, char *t, int i, int j); 40 | void reconstruct_path(char *s, char *t, int i, int j); 41 | void print_matrix(char *s, char *t, bool costQ); 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /original/primes.c: -------------------------------------------------------------------------------- 1 | /* primes.c 2 | 3 | Compute the prime factorization of an integer. 4 | 5 | by: Steven Skiena 6 | begun: February 18, 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 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 | -------------------------------------------------------------------------------- /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 | #include "item.h" 29 | 30 | #define PQ_SIZE 1000 31 | 32 | /* [[[ priority_queue_struct_cut */ 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 | void pq_init(priority_queue *q); 40 | int pq_parent(int n); 41 | int pq_young_child(int n); 42 | void pq_swap(priority_queue *q, int i, int j); 43 | void bubble_up(priority_queue *q, int p); 44 | void bubble_down(priority_queue *q, int p); 45 | void pq_insert(priority_queue *q, item_type x); 46 | item_type extract_min(priority_queue *q); 47 | int empty_pq(priority_queue *q); 48 | void print_pq(priority_queue *q); 49 | void make_heap(priority_queue *q, item_type s[], int n); 50 | void make_heap1(priority_queue *q, item_type s[], int n); 51 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /primes.c: -------------------------------------------------------------------------------- 1 | /* primes.c 2 | 3 | Compute the prime factorization of an integer. 4 | 5 | by: Steven Skiena 6 | begun: February 18, 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 | #include 30 | #include 31 | 32 | void prime_factorization(long x) { 33 | long i; /* counter */ 34 | long c; /* remaining product to factor */ 35 | 36 | c = x; 37 | 38 | while ((c % 2) == 0) { 39 | printf("%d\n", 2); 40 | c = c / 2; 41 | } 42 | 43 | i = 3; 44 | 45 | while (i <= (sqrt(c) + 1)) { 46 | if ((c % i) == 0) { 47 | printf("%ld\n", i); 48 | c = c / i; 49 | } else { 50 | i = i + 2; 51 | } 52 | } 53 | 54 | if (c > 1) { 55 | printf("%ld\n", c); 56 | } 57 | } 58 | 59 | int main(void) { 60 | long p; 61 | 62 | while (scanf("%ld", &p) != EOF) { 63 | printf("prime factorization of p=%ld \n", p); 64 | prime_factorization(p); 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /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 | #include 30 | 31 | #define MAXINT 1000000 32 | 33 | int a[100]; 34 | 35 | int main(void) { 36 | int i, n, x; 37 | 38 | for (i = 1; i <= 20; i++) { 39 | a[i] = i; 40 | } 41 | n = 20; 42 | x = -1; 43 | 44 | i = n; 45 | while ((a[i] >= x) && (i >= 1)) { 46 | a[i + 1] = a[i]; 47 | i = i - 1; 48 | } 49 | a[i + 1] = x; 50 | 51 | printf("without sentinel, i=%d a[1]=%d\n", i, a[1]); 52 | for (i = 1; i <= 25; i++) { 53 | printf("%d ", a[i]); 54 | } 55 | x = -2; 56 | 57 | n = 21; 58 | i = n; 59 | a[0] = - MAXINT; 60 | while (a[i] >= x) { 61 | a[i + 1] = a[i]; 62 | i = i - 1; 63 | } 64 | a[i + 1] = x; 65 | 66 | printf("\nwith sentinel, i=%d a[1]=%d\n", i, a[1]); 67 | for (i = 1; i <= 25; i++) { 68 | printf("%d ", a[i]); 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /original/findcycle.c: -------------------------------------------------------------------------------- 1 | 2 | /* findcycle.c 3 | 4 | Identify a cycle in a graph, if one exists. 5 | 6 | begun: March 6, 2002 7 | by: Steven Skiena 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 "graph.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 | extern bool finished; /* flag for early search cutoff */ 38 | 39 | 40 | process_vertex_early(int v) 41 | { 42 | } 43 | 44 | process_vertex_late(int v) 45 | { 46 | } 47 | 48 | process_edge(int x, int y) 49 | { 50 | if (parent[x] != y) { /* found back edge! */ 51 | printf("Cycle from %d to %d:",y,x); 52 | find_path(y,x,parent); 53 | printf("\n\n"); 54 | finished = TRUE; 55 | } 56 | } 57 | 58 | bool valid_edge(int e) 59 | { 60 | return (TRUE); 61 | } 62 | 63 | 64 | 65 | main() 66 | { 67 | graph g; 68 | int i; 69 | 70 | read_graph(&g,FALSE); 71 | print_graph(&g); 72 | 73 | initialize_search(&g); 74 | dfs(&g,1); 75 | } 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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; i 30 | 31 | #include "bool.h" 32 | #include "bfs-dfs.h" 33 | 34 | extern bool processed[]; /* which vertices have been processed */ 35 | extern bool discovered[]; /* which vertices have been found */ 36 | extern int parent[]; /* discovery relation */ 37 | 38 | extern bool finished; /* flag for early search cutoff */ 39 | 40 | void process_vertex_early(int v) { 41 | 42 | } 43 | 44 | void process_vertex_late(int v) { 45 | 46 | } 47 | 48 | /* [[[ cyclepedge_cut */ 49 | void process_edge(int x, int y) { 50 | if (parent[x] != y) { /* found back edge! */ 51 | printf("Cycle from %d to %d:", y, x); 52 | find_path(y, x, parent); 53 | printf("\n\n"); 54 | finished = TRUE; 55 | } 56 | } 57 | /* ]]] */ 58 | 59 | int main(void) { 60 | graph g; 61 | int i; 62 | 63 | read_graph(&g, FALSE); 64 | print_graph(&g); 65 | 66 | initialize_search(&g); 67 | dfs(&g, 1); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /original/connected.c: -------------------------------------------------------------------------------- 1 | 2 | /* connected.c 3 | 4 | Compute the connected components of a graph. 5 | 6 | by: Steven Skiena 7 | begun: March 6, 2002 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 "graph.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(" %d",v); 41 | } 42 | 43 | process_vertex_late(int v) 44 | { 45 | } 46 | 47 | process_edge(int x, int y) 48 | { 49 | } 50 | 51 | bool valid_edge(int e) 52 | { 53 | return (TRUE); 54 | } 55 | 56 | 57 | 58 | connected_components(graph *g) 59 | { 60 | int c; /* component number */ 61 | int i; /* counter */ 62 | 63 | initialize_search(g); 64 | 65 | c = 0; 66 | for (i=1; i<=g->nvertices; 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 | -------------------------------------------------------------------------------- /cgtest.c: -------------------------------------------------------------------------------- 1 | /* cgtest.c 2 | 3 | Driver program for computational geometry routines; give the 4 | basic geometric primatives a workout. 5 | 6 | begun: July 23, 2002 7 | by: Steven Skiena 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 | #include 30 | #include 31 | 32 | #include "bool.h" 33 | #include "geometry.h" 34 | 35 | 36 | int main(void) { 37 | point p1, p2, q1, q2, i; 38 | line l1, l2; 39 | segment s1, s2, s3, s4; 40 | 41 | while (scanf("%lf %lf", &p1[X], &p1[Y]) != EOF ) { 42 | scanf("%lf %lf", &p2[X], &p2[Y]); 43 | scanf("%lf %lf", &q1[X], &q1[Y]); 44 | scanf("%lf %lf", &q2[X], &q2[Y]); 45 | 46 | points_to_segment(p1, p2, &s1); 47 | points_to_segment(q1, q2, &s2); 48 | 49 | points_to_line(p1, p2, &l1); 50 | points_to_line(q1, q2, &l2); 51 | 52 | print_segment(s1); 53 | print_segment(s2); 54 | 55 | printf("segments_intersect test\n"); 56 | printf("%d\n", segments_intersect(s1, s2)); 57 | 58 | printf("intersection point\n"); 59 | intersection_point(l1, l2, i); 60 | print_point(i); 61 | 62 | printf("--------------------------------\n"); 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /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 | #include 31 | 32 | #include "bool.h" 33 | #include "stack.h" 34 | 35 | void init_stack(stack *s) { 36 | s->top = -1; 37 | s->count = 0; 38 | } 39 | 40 | 41 | void push(stack *s, int x) { 42 | if (s->count >= STACKSIZE) { 43 | printf("Warning: stack overflow push x=%d\n", x); 44 | } else { 45 | s->top = s->top + 1; 46 | s->s[s->top] = x; 47 | s->count = s->count + 1; 48 | } 49 | } 50 | 51 | int pop(stack *s) { 52 | int x; 53 | 54 | if (s->count <= 0) { 55 | printf("Warning: empty stack pop.\n"); 56 | } else { 57 | x = s->s[s->top]; 58 | s->top = s->top - 1; 59 | s->count = s->count - 1; 60 | } 61 | return(x); 62 | } 63 | 64 | int empty_stack(stack *s) { 65 | if (s->count <= 0) { 66 | return (TRUE); 67 | } 68 | return (FALSE); 69 | } 70 | 71 | void print_stack(stack *s) { 72 | int i; /* counter */ 73 | 74 | for (i = (s->count - 1); i >= 0; i--) { 75 | printf("%d ", s->s[i]); 76 | } 77 | printf("\n"); 78 | } 79 | -------------------------------------------------------------------------------- /bfs-demo.c: -------------------------------------------------------------------------------- 1 | 2 | /* bfs-demo.c 3 | 4 | Driver program demonstrating breadth-first search 5 | 6 | begun: March 26, 2002 7 | by: Steven Skiena 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 | #include 30 | 31 | #include "bfs-dfs.h" 32 | #include "bool.h" 33 | 34 | extern bool processed[]; /* which vertices have been processed */ 35 | extern bool discovered[]; /* which vertices have been found */ 36 | extern int parent[]; /* discovery relation */ 37 | 38 | /* [[[ pvearly_cut */ 39 | void process_vertex_early(int v) { 40 | printf("processed vertex %d\n", v); 41 | } 42 | /* ]]] */ 43 | 44 | /* [[[ pvlate_cut */ 45 | void process_vertex_late(int v) { 46 | 47 | } 48 | /* ]]] */ 49 | 50 | /* [[[ pedge_cut */ 51 | void process_edge(int x, int y) { 52 | printf("processed edge (%d,%d)\n", x, y); 53 | } 54 | /* ]]] */ 55 | 56 | int main(void) { 57 | graph g; 58 | int i; 59 | 60 | read_graph(&g, FALSE); 61 | print_graph(&g); 62 | initialize_search(&g); 63 | bfs(&g, 1); 64 | 65 | for (i = 1; i <= g.nvertices; i++) { 66 | printf(" %d", parent[i]); 67 | } 68 | printf("\n"); 69 | 70 | for (i = 1; i <= g.nvertices; i++) { 71 | find_path(1, i, parent); 72 | } 73 | printf("\n"); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /datafiles/regular-tree-dfs-demo-out: -------------------------------------------------------------------------------- 1 | 1: 3 2 2 | 2: 5 4 1 3 | 3: 7 6 1 4 | 4: 9 8 2 5 | 5: 11 10 2 6 | 6: 13 12 3 7 | 7: 15 14 3 8 | 8: 4 9 | 9: 4 10 | 10: 5 11 | 11: 5 12 | 12: 6 13 | 13: 6 14 | 14: 7 15 | 15: 7 16 | entered vertex 1 at time 2 17 | tree edge (1,3) 18 | entered vertex 3 at time 4 19 | tree edge (3,7) 20 | entered vertex 7 at time 6 21 | tree edge (7,15) 22 | entered vertex 15 at time 8 23 | back edge (15,7) 24 | exit vertex 15 at time 9 25 | tree edge (7,14) 26 | entered vertex 14 at time 12 27 | back edge (14,7) 28 | exit vertex 14 at time 13 29 | back edge (7,3) 30 | exit vertex 7 at time 15 31 | tree edge (3,6) 32 | entered vertex 6 at time 18 33 | tree edge (6,13) 34 | entered vertex 13 at time 20 35 | back edge (13,6) 36 | exit vertex 13 at time 21 37 | tree edge (6,12) 38 | entered vertex 12 at time 24 39 | back edge (12,6) 40 | exit vertex 12 at time 25 41 | back edge (6,3) 42 | exit vertex 6 at time 27 43 | back edge (3,1) 44 | exit vertex 3 at time 29 45 | tree edge (1,2) 46 | entered vertex 2 at time 32 47 | tree edge (2,5) 48 | entered vertex 5 at time 34 49 | tree edge (5,11) 50 | entered vertex 11 at time 36 51 | back edge (11,5) 52 | exit vertex 11 at time 37 53 | tree edge (5,10) 54 | entered vertex 10 at time 40 55 | back edge (10,5) 56 | exit vertex 10 at time 41 57 | back edge (5,2) 58 | exit vertex 5 at time 43 59 | tree edge (2,4) 60 | entered vertex 4 at time 46 61 | tree edge (4,9) 62 | entered vertex 9 at time 48 63 | back edge (9,4) 64 | exit vertex 9 at time 49 65 | tree edge (4,8) 66 | entered vertex 8 at time 52 67 | back edge (8,4) 68 | exit vertex 8 at time 53 69 | back edge (4,2) 70 | exit vertex 4 at time 55 71 | back edge (2,1) 72 | exit vertex 2 at time 57 73 | exit vertex 1 at time 59 74 | 75 | 1 76 | 1 2 77 | 1 3 78 | 1 2 4 79 | 1 2 5 80 | 1 3 6 81 | 1 3 7 82 | 1 2 4 8 83 | 1 2 4 9 84 | 1 2 5 10 85 | 1 2 5 11 86 | 1 3 6 12 87 | 1 3 6 13 88 | 1 3 7 14 89 | 1 3 7 15 90 | -------------------------------------------------------------------------------- /original/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 30 | 31 | #include "bool.h" 32 | #include "bfs-dfs.h" 33 | 34 | extern bool processed[]; /* which vertices have been processed */ 35 | extern bool discovered[]; /* which vertices have been found */ 36 | extern int parent[]; /* discovery relation */ 37 | 38 | void process_vertex(int v) { 39 | printf(" %d", v); 40 | } 41 | 42 | void process_vertex_early(int v) { 43 | printf(" %d", v); 44 | } 45 | 46 | void process_vertex_late(int v) { 47 | 48 | } 49 | 50 | void process_edge(int x, int y) { 51 | 52 | } 53 | 54 | /* [[[ cc_cut */ 55 | void connected_components(graph *g) { 56 | int c; /* component number */ 57 | int i; /* counter */ 58 | 59 | initialize_search(g); 60 | 61 | c = 0; 62 | for (i = 1; i <= g->nvertices; i++) { 63 | if (discovered[i] == FALSE) { 64 | c = c + 1; 65 | printf("Component %d:", c); 66 | bfs(g, i); 67 | printf("\n"); 68 | } 69 | } 70 | } 71 | /* ]]] */ 72 | 73 | int main(void) { 74 | graph g; 75 | 76 | read_graph(&g, FALSE); 77 | print_graph(&g); 78 | 79 | connected_components(&g); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /editdistance.h: -------------------------------------------------------------------------------- 1 | 2 | /* editdistance.h 3 | 4 | Header file for string comparison 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 | #include "bool.h" 28 | 29 | #define MAXLEN 101 /* longest possible string */ 30 | 31 | /* [[[ editdistance_mid_cut */ 32 | #define MATCH 0 /* enumerated type symbol for match */ 33 | #define INSERT 1 /* enumerated type symbol for insert */ 34 | #define DELETE 2 /* enumerated type symbol for delete */ 35 | /* ]]] */ 36 | 37 | /* [[[ editdistance_cell_struct_cut */ 38 | typedef struct { 39 | int cost; /* cost of reaching this cell */ 40 | int parent; /* parent cell */ 41 | } cell; 42 | /* ]]] */ 43 | 44 | void row_init(int i, cell m[MAXLEN+1][MAXLEN+1]); 45 | void column_init(int i, cell m[MAXLEN+1][MAXLEN+1]); 46 | int match(char c, char d); 47 | int indel(char c); 48 | void goal_cell(char *s, char *t, int *i, int *j); 49 | void delete_out(char *s, int i); 50 | void insert_out(char *t, int j); 51 | void match_out(char *s, char *t, int i, int j); 52 | int string_compare(char *s, char *t, cell m[MAXLEN+1][MAXLEN+1]); 53 | int string_compare2(char *s, char *t, int i, int j, cell m[MAXLEN+1][MAXLEN+1]); 54 | void reconstruct_path(char *s, char *t, int i, int j, cell m[MAXLEN+1][MAXLEN+1]); 55 | void print_matrix(char *s, char *t, bool costQ, cell m[MAXLEN+1][MAXLEN+1]); 56 | -------------------------------------------------------------------------------- /component-graphs/convert.m: -------------------------------------------------------------------------------- 1 | 2 | (* 3 | Format Data Files for Bandwidth Programming Assignment 4 | *) 5 | 6 | OutSCC[n_Integer,p_:0.2,i_Integer] := 7 | Block[{g}, 8 | g = RandomGraph[n,p,Directed]; 9 | PutOut[g,ToString[i],Directed]; 10 | StronglyConnectedComponents[g] 11 | ] 12 | 13 | OutPath[n_Integer] := 14 | PutOut[ InduceSubgraph[ Path[n], RandomPermutation[n] ], "p"] 15 | 16 | OutTree[n_Integer] := 17 | PutOut[ InduceSubgraph[ RandomTree[n], RandomPermutation[n] ], "t"] 18 | 19 | OutK[n_Integer] := PutOut[ K[n], "k"] 20 | 21 | OutRandom[n_Integer,p_:0.25] := PutOut[ RandomGraph[n,p], "r"] 22 | OutRandom[n_Integer,p_:0.25,Directed] := 23 | PutOut[ RandomGraph[n,p,Directed], "r", Directed] 24 | 25 | 26 | BinaryTree[n_Integer] := 27 | MakeGraph[ 28 | Range[n], 29 | ((#1==2*#2) || (#2==2*#1) || (#1==2*#1+1) || (#2==2*#1+1))& 30 | ] 31 | 32 | OutBinaryTree[n_Integer] := 33 | PutOut[ MakeUndirected[ BinaryTree[n] ], "bt"] 34 | 35 | OutGridGraph[n_Integer,m_Integer] := 36 | PutOut[ InduceSubgraph[ GridGraph[n,m], RandomPermutation[n*m] ], "gg"] 37 | 38 | 39 | PutOut[g_Graph,char_,Directed] := 40 | Block[{l,i,s}, 41 | s = StringJoin["g-",ToString[char]]; 42 | Open[s]; 43 | Write[s, V[g], M[g] ]; 44 | l = ToOrderedPairs[g]; 45 | Do [ 46 | WriteString[s, StringJoin[ ToString[ First[l[[i]]] ], 47 | " ", ToString[ Last[l[[i]]]] ], "\n"], 48 | {i,1,Length[l]} 49 | ]; 50 | Close[s] 51 | ] 52 | 53 | 54 | PutOut[g_Graph,char_] := 55 | Block[{l,i,s}, 56 | s = StringJoin["g-",ToString[char],"-",ToString[V[g]],"-", 57 | ToString[2*M[g]]]; 58 | Open[s]; 59 | Write[s, V[g], M[g] ]; 60 | l = ToUnorderedPairs[g]; 61 | Do [ 62 | WriteString[s, StringJoin[ ToString[ First[l[[i]]] ], 63 | " ", ToString[ Last[l[[i]]]] ], "\n"], 64 | {i,1,Length[l]} 65 | ]; 66 | Close[s] 67 | ] 68 | -------------------------------------------------------------------------------- /datafiles/VDError-uniq.dat: -------------------------------------------------------------------------------- 1 | 105 2 | 118.00 271.08 3 | 118.64 271.09 4 | 119.66 271.08 5 | 120.68 271.09 6 | 121.68 271.08 7 | 124.05 271.08 8 | 124.81 271.07 9 | 125.38 271.09 10 | 131.25 50.31 11 | 132.69 50.25 12 | 134.14 50.23 13 | 135.61 50.25 14 | 135.92 50.26 15 | 160.09 50.26 16 | 161.16 50.31 17 | 162.76 50.28 18 | 163.41 271.09 19 | 163.61 271.10 20 | 163.84 50.30 21 | 164.40 271.11 22 | 165.17 271.10 23 | 165.95 271.08 24 | 167.20 271.10 25 | 168.46 271.08 26 | 169.70 271.10 27 | 170.61 271.08 28 | 174.37 50.30 29 | 174.92 50.31 30 | 176.57 50.28 31 | 178.33 50.31 32 | 185.99 50.31 33 | 187.51 50.25 34 | 189.13 50.23 35 | 190.85 50.25 36 | 192.68 50.31 37 | 210.15 50.31 38 | 211.76 50.29 39 | 213.57 50.31 40 | 215.50 50.28 41 | 217.57 50.31 42 | 218.46 271.08 43 | 219.28 271.09 44 | 220.12 271.08 45 | 220.95 271.09 46 | 221.79 271.08 47 | 222.63 271.09 48 | 223.49 271.08 49 | 224.34 271.09 50 | 225.22 271.08 51 | 229.94 50.31 52 | 232.02 50.28 53 | 234.28 50.31 54 | 245.64 50.31 55 | 247.80 50.28 56 | 250.16 50.31 57 | 263.52 50.31 58 | 267.70 50.30 59 | 269.34 50.29 60 | 271.05 58.61 61 | 271.05 68.79 62 | 271.06 251.01 63 | 271.06 271.08 64 | 271.06 50.30 65 | 271.06 58.34 66 | 271.06 69.62 67 | 271.06 70.38 68 | 291.12 207.75 69 | 291.12 234.87 70 | 291.12 238.40 71 | 291.12 242.38 72 | 291.12 242.96 73 | 291.12 244.83 74 | 291.12 246.79 75 | 291.12 248.85 76 | 291.12 251.01 77 | 291.13 201.86 78 | 291.13 247.82 79 | 291.13 249.92 80 | 291.13 70.38 81 | 291.13 74.48 82 | 291.13 79.38 83 | 291.14 243.89 84 | 291.14 245.81 85 | 291.16 235.76 86 | 291.16 237.53 87 | 291.17 236.65 88 | 291.17 239.36 89 | 291.17 241.35 90 | 291.19 240.35 91 | 50.28 225.89 92 | 50.28 251.01 93 | 50.29 203.29 94 | 50.29 70.38 95 | 70.34 252.01 96 | 70.36 251.01 97 | 70.36 253.01 98 | 70.36 271.08 99 | 70.36 50.31 100 | 70.36 70.38 101 | 73.24 271.08 102 | 74.36 271.09 103 | 75.45 271.08 104 | 76.51 271.09 105 | 77.55 271.07 106 | 77.94 271.08 107 | -------------------------------------------------------------------------------- /original/geometry.h: -------------------------------------------------------------------------------- 1 | /* geometry.h 2 | Header file for geometric data types 3 | 4 | by: Steven Skiena 5 | */ 6 | 7 | /* 8 | Copyright 2003 by Steven S. Skiena; all rights reserved. 9 | 10 | Permission is granted for use in non-commerical applications 11 | provided this copyright notice remains intact and unchanged. 12 | 13 | This program appears in my book: 14 | 15 | "Programming Challenges: The Programming Contest Training Manual" 16 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 17 | 18 | See our website www.programming-challenges.com for additional information. 19 | 20 | This book can be ordered from Amazon.com at 21 | 22 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 23 | 24 | */ 25 | 26 | 27 | #define PI 3.1415926 /* ratio of circumference to diameter */ 28 | #define EPSILON 0.000001 /* a quantity small enough to be zero */ 29 | 30 | typedef struct { 31 | double a; /* x-coefficient */ 32 | double b; /* y-coefficient */ 33 | double c; /* constant term */ 34 | } line; 35 | 36 | #define DIMENSION 2 /* dimension of points */ 37 | #define X 0 /* x-coordinate index */ 38 | #define Y 1 /* y-coordinate index */ 39 | 40 | typedef double point[DIMENSION]; 41 | 42 | #define MAXPOLY 200 /* maximum number of points in a polygon */ 43 | 44 | typedef struct { 45 | int n; /* number of points in polygon */ 46 | point p[MAXPOLY]; /* array of points in polygon */ 47 | } polygon; 48 | 49 | 50 | typedef struct { 51 | point p1,p2; /* endpoints of line segment */ 52 | } segment; 53 | 54 | typedef point triangle[3]; /* triangle datatype */ 55 | 56 | typedef struct { 57 | int n; /* number of triangles in triangulation */ 58 | int t[MAXPOLY][3]; /* indicies of vertices in triangulation */ 59 | } triangulation; 60 | 61 | typedef struct { 62 | point c; /* center of circle */ 63 | double r; /* radius of circle */ 64 | } circle; 65 | 66 | 67 | /* Comparison macros */ 68 | 69 | #define max(A, B) ((A) > (B) ? (A) : (B)) 70 | #define min(A, B) ((A) < (B) ? (A) : (B)) 71 | 72 | -------------------------------------------------------------------------------- /original/gcd.c: -------------------------------------------------------------------------------- 1 | /* gcd.c 2 | 3 | Compute the greatest common divisor of two integers 4 | 5 | by: Steven Skiena 6 | begun: July 10, 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 30 | #include 31 | 32 | 33 | long gcd1(p,q) 34 | long p,q; /* integers to compute the GCD of */ 35 | { 36 | if (q > p) return(gcd1(q,p)); 37 | 38 | 39 | if (q == 0) return(p); 40 | 41 | printf(" gcd(%d,%d) &=& gcd(%d \\mod %d, %d) = gcd(%d,%d) \n",p,q,p,q,q,q,p%q); 42 | return( gcd1(q, p % q) ); 43 | } 44 | 45 | /* Find the gcd(p,q) and x,y such that p*x + q*y = gcd(p,q) */ 46 | 47 | long gcd(long p, long q, long *x, long *y) 48 | { 49 | long x1,y1; /* previous coefficients */ 50 | long g; /* value of gcd(p,q) */ 51 | 52 | if (q > p) return(gcd(q,p,y,x)); 53 | 54 | if (q == 0) { 55 | *x = 1; 56 | *y = 0; 57 | return(p); 58 | } 59 | 60 | g = gcd(q, p%q, &x1, &y1); 61 | 62 | *x = y1; 63 | *y = (x1 - floor(p/q)*y1); 64 | 65 | return(g); 66 | } 67 | 68 | 69 | 70 | main() { 71 | long p,q; 72 | long gcd(), gcd2(); 73 | long x,y,g1,g2; 74 | 75 | while (scanf("%d %d",&p,&q)!=EOF) { 76 | 77 | printf("gcd of p=%d and q=%d = %d\n",p,q,g1=gcd1(p,q)); 78 | printf(" %d*%d + %d*%d = %d\n",p,x,q,y,g2=gcd(p,q,&x,&y)); 79 | 80 | if (g1 != g2) printf("ERROR: GCD\n"); 81 | if ((p*x + q*y) != g1) printf("ERROR: DIOPHONINE SOLUTION WRONG!\n"); 82 | 83 | } 84 | } 85 | 86 | -------------------------------------------------------------------------------- /original/queue.c: -------------------------------------------------------------------------------- 1 | 2 | /* queue.c 3 | 4 | Implementation of a FIFO queue 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 "queue.h" 32 | #include "bool.h" 33 | 34 | 35 | init_queue(queue *q) 36 | { 37 | q->first = 0; 38 | q->last = QUEUESIZE-1; 39 | q->count = 0; 40 | } 41 | 42 | enqueue(queue *q, int x) 43 | { 44 | if (q->count >= QUEUESIZE) 45 | printf("Warning: queue overflow enqueue x=%d\n",x); 46 | else { 47 | q->last = (q->last+1) % QUEUESIZE; 48 | q->q[ q->last ] = x; 49 | q->count = q->count + 1; 50 | } 51 | } 52 | 53 | int dequeue(queue *q) 54 | { 55 | int x; 56 | 57 | if (q->count <= 0) printf("Warning: empty queue dequeue.\n"); 58 | else { 59 | x = q->q[ q->first ]; 60 | q->first = (q->first+1) % QUEUESIZE; 61 | q->count = q->count - 1; 62 | } 63 | 64 | return(x); 65 | } 66 | 67 | int empty(queue *q) 68 | { 69 | if (q->count <= 0) return (TRUE); 70 | else return (FALSE); 71 | } 72 | 73 | print_queue(queue *q) 74 | { 75 | int i,j; 76 | 77 | i=q->first; 78 | 79 | while (i != q->last) { 80 | printf("%c ",q->q[i]); 81 | i = (i+1) % QUEUESIZE; 82 | } 83 | 84 | printf("%2d ",q->q[i]); 85 | printf("\n"); 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /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 | #include 29 | 30 | #include "backtrack.h" 31 | #include "bool.h" 32 | 33 | /* [[[ backtrack_boolean_cut */ 34 | bool finished = FALSE; /* found all solutions yet? */ 35 | /* ]]] */ 36 | 37 | void process_solution(int a[], int k, data input); 38 | void construct_candidates(int a[], int k, data input, int c[], int *ncandidates); 39 | void make_move(int a[], int k, data input); 40 | void unmake_move(int a[], int k, data input); 41 | int is_a_solution(int a[], int k, data input); 42 | 43 | /* [[[ backtrack_cut */ 44 | void backtrack(int a[], int k, data input) { 45 | int c[MAXCANDIDATES]; /* candidates for next position */ 46 | int ncandidates; /* next position candidate count */ 47 | int i; /* counter */ 48 | 49 | if (is_a_solution(a, k, input)) { 50 | process_solution(a, k,input); 51 | } else { 52 | k = k + 1; 53 | construct_candidates(a, k, input, c, &ncandidates); 54 | for (i = 0; i < ncandidates; i++) { 55 | a[k] = c[i]; 56 | make_move(a, k, input); 57 | backtrack(a, k, input); 58 | unmake_move(a, k, input); 59 | 60 | if (finished == TRUE) { 61 | return; /* terminate early */ 62 | } 63 | } 64 | } 65 | } 66 | /* ]]] */ 67 | -------------------------------------------------------------------------------- /datafiles/VDError.dat: -------------------------------------------------------------------------------- 1 | 111 2 | 118.00 271.08 3 | 118.64 271.09 4 | 119.66 271.08 5 | 120.68 271.09 6 | 121.68 271.08 7 | 124.81 271.07 8 | 125.38 271.09 9 | 131.25 50.31 10 | 132.69 50.25 11 | 134.14 50.23 12 | 135.61 50.25 13 | 120.68 271.09 14 | 135.92 50.26 15 | 160.09 50.26 16 | 50.29 70.38 17 | 161.16 50.31 18 | 162.76 50.28 19 | 162.76 50.28 20 | 163.41 271.09 21 | 163.61 271.10 22 | 163.84 50.30 23 | 164.40 271.11 24 | 165.17 271.10 25 | 165.95 271.08 26 | 124.05 271.08 27 | 167.20 271.10 28 | 168.46 271.08 29 | 169.70 271.10 30 | 170.61 271.08 31 | 174.37 50.30 32 | 174.37 50.30 33 | 174.92 50.31 34 | 176.57 50.28 35 | 178.33 50.31 36 | 185.99 50.31 37 | 187.51 50.25 38 | 189.13 50.23 39 | 190.85 50.25 40 | 192.68 50.31 41 | 210.15 50.31 42 | 211.76 50.29 43 | 213.57 50.31 44 | 215.50 50.28 45 | 217.57 50.31 46 | 218.46 271.08 47 | 219.28 271.09 48 | 220.12 271.08 49 | 220.95 271.09 50 | 221.79 271.08 51 | 222.63 271.09 52 | 223.49 271.08 53 | 224.34 271.09 54 | 225.22 271.08 55 | 229.94 50.31 56 | 232.02 50.28 57 | 234.28 50.31 58 | 245.64 50.31 59 | 247.80 50.28 60 | 250.16 50.31 61 | 263.52 50.31 62 | 267.70 50.30 63 | 269.34 50.29 64 | 271.05 58.61 65 | 271.05 68.79 66 | 271.06 251.01 67 | 271.06 271.08 68 | 271.06 50.30 69 | 271.06 58.34 70 | 271.06 69.62 71 | 271.06 70.38 72 | 291.12 207.75 73 | 291.12 234.87 74 | 291.12 238.40 75 | 291.12 242.38 76 | 291.12 242.96 77 | 291.12 244.83 78 | 291.12 246.79 79 | 291.12 248.85 80 | 291.12 251.01 81 | 291.13 201.86 82 | 291.13 247.82 83 | 291.13 249.92 84 | 291.13 70.38 85 | 291.13 74.48 86 | 291.13 79.38 87 | 291.14 243.89 88 | 291.14 245.81 89 | 291.16 235.76 90 | 291.16 237.53 91 | 291.17 236.65 92 | 50.29 70.38 93 | 291.17 239.36 94 | 291.17 241.35 95 | 291.19 240.35 96 | 50.28 225.89 97 | 50.28 251.01 98 | 50.29 203.29 99 | 50.29 70.38 100 | 70.34 252.01 101 | 70.36 251.01 102 | 70.36 253.01 103 | 70.36 271.08 104 | 70.36 50.31 105 | 70.36 70.38 106 | 73.24 271.08 107 | 74.36 271.09 108 | 75.45 271.08 109 | 76.51 271.09 110 | 50.29 70.38 111 | 77.55 271.07 112 | 77.94 271.08 113 | -------------------------------------------------------------------------------- /queue.c: -------------------------------------------------------------------------------- 1 | 2 | /* queue.c 3 | 4 | Implementation of a FIFO queue 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 | #include 31 | 32 | #include "bool.h" 33 | #include "queue.h" 34 | 35 | void init_queue(queue *q) { 36 | q->first = 0; 37 | q->last = QUEUESIZE - 1; 38 | q->count = 0; 39 | } 40 | 41 | void enqueue(queue *q, item_type x) { 42 | if (q->count >= QUEUESIZE) { 43 | printf("Warning: queue overflow enqueue x=%d\n", x); 44 | } else { 45 | q->last = (q->last + 1) % QUEUESIZE; 46 | q->q[q->last] = x; 47 | q->count = q->count + 1; 48 | } 49 | } 50 | 51 | item_type dequeue(queue *q) { 52 | item_type x; 53 | 54 | if (q->count <= 0) { 55 | printf("Warning: empty queue dequeue.\n"); 56 | } else { 57 | x = q->q[q->first]; 58 | q->first = (q->first + 1) % QUEUESIZE; 59 | q->count = q->count - 1; 60 | } 61 | return(x); 62 | } 63 | 64 | item_type headq(queue *q) { 65 | return(q->q[q->first]); 66 | } 67 | 68 | int empty_queue(queue *q) { 69 | if (q->count <= 0) { 70 | return (TRUE); 71 | } 72 | return (FALSE); 73 | } 74 | 75 | void print_queue(queue *q) { 76 | int i, j; 77 | 78 | i = q->first; 79 | 80 | while (i != q->last) { 81 | printf("%d ", q->q[i]); 82 | i = (i + 1) % QUEUESIZE; 83 | } 84 | 85 | printf("%2d ", q->q[i]); 86 | printf("\n"); 87 | } 88 | -------------------------------------------------------------------------------- /graph.h: -------------------------------------------------------------------------------- 1 | /* graph.h 2 | Header file for pointer-based graph data type 3 | 4 | by: Steven Skiena 5 | */ 6 | 7 | /* 8 | Copyright 2003 by Steven S. Skiena; all rights reserved. 9 | 10 | Permission is granted for use in non-commerical applications 11 | provided this copyright notice remains intact and unchanged. 12 | 13 | This program appears in my book: 14 | 15 | "Programming Challenges: The Programming Contest Training Manual" 16 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. 17 | 18 | See our website www.programming-challenges.com for additional information. 19 | 20 | This book can be ordered from Amazon.com at 21 | 22 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ 23 | 24 | */ 25 | #include "bool.h" 26 | 27 | /* [[[ maxv_cut */ 28 | #define MAXV 100 /* maximum number of vertices */ 29 | /* ]]] */ 30 | 31 | #define _NULL 0 /* null pointer */ 32 | 33 | /* DFS edge types */ 34 | 35 | #define TREE 0 /* tree edge */ 36 | #define BACK 1 /* back edge */ 37 | #define CROSS 2 /* cross edge */ 38 | #define FORWARD 3 /* forward edge */ 39 | 40 | /* [[[ graph_struct_cut */ 41 | /* [[[ edge_struct_only_cut */ 42 | typedef struct edgenode { 43 | int y; /* adjancency info */ 44 | int weight; /* edge weight, if any */ 45 | struct edgenode *next; /* next edge in list */ 46 | } edgenode; 47 | /* ]]] */ 48 | /* [[[ graph_struct_only_cut */ 49 | typedef struct { 50 | edgenode *edges[MAXV+1]; /* adjacency info */ 51 | int degree[MAXV+1]; /* outdegree of each vertex */ 52 | int nvertices; /* number of vertices in the graph */ 53 | int nedges; /* number of edges in the graph */ 54 | int directed; /* is the graph directed? */ 55 | } graph; 56 | /* ]]] */ 57 | /* ]]] */ 58 | 59 | void process_vertex_early(int v); 60 | void process_vertex_late(int v); 61 | void process_edge(int x, int y); 62 | 63 | void initialize_graph(graph *g, bool directed); 64 | void read_graph(graph *g, bool directed); 65 | void print_graph(graph *g); 66 | -------------------------------------------------------------------------------- /original/order.c: -------------------------------------------------------------------------------- 1 | /* order.c 2 | 3 | Demonstrate traversal orders on a grid. 4 | 5 | by: Steven Skiena 6 | begun: July 14, 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 "geometry.h" 30 | 31 | row_major(int n, int m) 32 | { 33 | int i,j; /* counters */ 34 | 35 | for (i=1; i<=n; i++) 36 | for (j=1; j<=m; j++) 37 | process(i,j); 38 | } 39 | 40 | column_major(int n, int m) 41 | { 42 | int i,j; /* counters */ 43 | 44 | for (j=1; j<=m; j++) 45 | for (i=1; i<=n; i++) 46 | process(i,j); 47 | } 48 | 49 | 50 | snake_order(int n, int m) 51 | { 52 | int i,j; /* counters */ 53 | 54 | for (i=1; i<=n; i++) 55 | for (j=1; j<=m; j++) 56 | process(i, j + (m+1-2*j) * ((i+1) % 2)); 57 | } 58 | 59 | diagonal_order(int n, int m) 60 | { 61 | int d,j; /* diagonal and point counters */ 62 | int pcount; /* points on diagonal */ 63 | int height; /* row of lowest point */ 64 | 65 | for (d=1; d<=(m+n-1); d++) { 66 | height = 1 + max(0,d-m); 67 | pcount = min(d, (n-height+1)); 68 | for (j=0; jnvertices; i++) in[i] = 0; 41 | 42 | for (i=1; i<=g->nvertices; i++) 43 | for (j=0; jdegree[i]; j++) in[ g->edges[i][j] ] ++; 44 | } 45 | 46 | 47 | topsort(graph *g, int sorted[]) 48 | { 49 | int indegree[MAXV]; /* indegree of each vertex */ 50 | queue zeroin; /* vertices of indegree 0 */ 51 | int x, y; /* current and next vertex */ 52 | int i, j; /* counters */ 53 | 54 | compute_indegrees(g,indegree); 55 | init_queue(&zeroin); 56 | for (i=1; i<=g->nvertices; i++) 57 | if (indegree[i] == 0) enqueue(&zeroin,i); 58 | 59 | j=0; 60 | while (empty(&zeroin) == FALSE) { 61 | j = j+1; 62 | x = dequeue(&zeroin); 63 | sorted[j] = x; 64 | for (i=0; idegree[x]; i++) { 65 | y = g->edges[x][i]; 66 | indegree[y] --; 67 | if (indegree[y] == 0) enqueue(&zeroin,y); 68 | } 69 | } 70 | 71 | if (j != g->nvertices) 72 | printf("Not a DAG -- only %d vertices found\n",j); 73 | } 74 | 75 | 76 | main() 77 | { 78 | graph g; 79 | int out[MAXV]; 80 | int i; 81 | 82 | read_graph(&g,TRUE); 83 | print_graph(&g); 84 | 85 | topsort(&g,out); 86 | 87 | for (i=1; i<=g.nvertices; i++) 88 | printf(" %d",out[i]); 89 | printf("\n"); 90 | 91 | } 92 | 93 | -------------------------------------------------------------------------------- /original/8-queens.c: -------------------------------------------------------------------------------- 1 | /* 2 | 8-queens.c 3 | Solve the eight queens problem using backtracking 4 | 5 | begun: March 1, 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 | #include "backtrack.h" 30 | #include "bool.h" 31 | 32 | int solution_count; /* how many solutions are there? */ 33 | 34 | 35 | process_solution(int a[], int k) 36 | { 37 | int i; /* counter */ 38 | 39 | solution_count ++; 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 | 57 | /* What are possible elements of the next slot in the 8-queens 58 | problem? 59 | */ 60 | 61 | construct_candidates(int a[], int k, int n, int c[], int *ncandidates) 62 | { 63 | int i,j; /* counters */ 64 | bool legal_move; /* might the move be legal? */ 65 | 66 | *ncandidates = 0; 67 | for (i=1; i<=n; i++) { 68 | legal_move = TRUE; 69 | for (j=1; j 13 | #include 14 | #include 15 | 16 | 17 | #define TRACE_OUTPUT FALSE /* print the swaps as they happen */ 18 | 19 | #define PRINT_FREQUENCY 10000 /* how often we report progress */ 20 | 21 | 22 | 23 | /**************** Simulated Annealing Constants **************************/ 24 | 25 | #define REPEAT_COUNT 1 /* how many solution attempts do you 26 | want? More than 1 enables you to 27 | eyeball the output and pick the 28 | best. If you are getting stuck in 29 | local optima, this good to try. */ 30 | 31 | #define INITIAL_TEMPERATURE 1 /* start temperature -- probably 32 | leave intact */ 33 | 34 | #define COOLING_STEPS 500 /* how many times do we cool -- make 35 | higher to improve quality, lower to 36 | speed the program up. Move in 37 | tandem with the COOLING_FRACTION */ 38 | 39 | #define COOLING_FRACTION 0.97 /* how much to cool each time -- make 40 | higher to improve quality, lower to 41 | speed the program up. */ 42 | 43 | #define STEPS_PER_TEMP 1000 /* lower makes it faster, higher makes 44 | it potentially better. */ 45 | 46 | #define E 2.718 /* number e -- probably leave intact*/ 47 | 48 | #define K 0.01 /* problem specific Boltzman's constant 49 | May have to adjust if your global 50 | value function changes the sizes 51 | of the numbers it produces. It is 52 | important that jumps seem random at 53 | the begining of the run, and rare 54 | at the end of a run, and this is 55 | a knob to tweak that. */ 56 | 57 | /*=========================================================================*/ 58 | 59 | /* The stuff below is less likely to change in adapting to new 60 | problems, but look for yourself before leaping. 61 | */ 62 | 63 | #define ERROR "ERROR" /* string denoting error id */ 64 | #define ERROR_VALUE 0.0 /* float denoting error value */ 65 | 66 | 67 | -------------------------------------------------------------------------------- /datafiles/outgraph.m: -------------------------------------------------------------------------------- 1 | (* Steven Skiena 2 | Output Combinatorica graphs in Programming Challenges format 3 | July 12, 2002 4 | 5 | *) 6 | 7 | g = SetEdgeWeights[GridGraph[5,5], {WeightingFunction->RandomInteger, 8 | WeightRange->{1,10000}} ] 9 | 10 | Edges[g,EdgeWeight] 11 | 12 | OutGraph[g_Graph] := 13 | Module[{}, 14 | Print[V[g], " ", M[g]]; 15 | Map[(Print[#[[1,1]], " ", #[[1,2]], " ", #[[2]] ])&, 16 | Edges[g,EdgeWeight]]; 17 | ] 18 | 19 | DoubleOutGraph[g_Graph,name_] := 20 | Module[{f}, 21 | f=OpenWrite[name]; 22 | Write[f,V[g], " ", 2*M[g]]; 23 | Map[(Write[f,#[[1,1]], " ", #[[1,2]], " ", #[[2]] ]; Write[f,#[[1,2]], " ", #[[1,1]], " ", #[[2]] ];)&, 24 | Edges[g,EdgeWeight]]; 25 | ] 26 | 27 | 28 | DoubleOutGraph[g,"file-test"] 29 | 30 | 31 | (* To prove the validity of dijkstra 32 | 33 | n[34]:= g >> /tmp/wgrid.m 34 | 35 | In[35]:= Dijkstra[g,1,25] 36 | 37 | Out[35]= Dijkstra[-Graph:<40, 25, Undirected>-, 1, 25] 38 | 39 | In[36]:= ?ShortestPath 40 | ShortestPath[g, start, end] finds a shortest path between vertices start and 41 | end in graph g. An option Algorithm, that takes on the values Automatic, 42 | Dijkstra or BellmanFord, is provided. This allows a choice between using 43 | Dijkstra's algorithm and the Bellman-Ford algorithm. The default is 44 | Algorithm -> Automatic. In this case, depending on whether edges have 45 | negative weights and depending on the density of the graph the algorithm 46 | chooses between Bellman-Ford and Dijkstra. 47 | 48 | In[37]:= ShortestPath[g,1,25] 49 | 50 | Out[37]= {1, 6, 11, 16, 17, 22, 23, 24, 25} 51 | 52 | In[38]:= Table[ ShortestPath[g,1,i], {i,1,25}] 53 | 54 | Out[38]= {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}, {1, 6}, 55 | 56 | > {1, 2, 7}, {1, 2, 3, 8}, {1, 2, 3, 8, 9}, {1, 2, 3, 8, 9, 10}, 57 | 58 | > {1, 6, 11}, {1, 6, 11, 12}, {1, 2, 3, 8, 13}, {1, 2, 3, 8, 13, 14}, 59 | 60 | > {1, 2, 3, 8, 9, 10, 15}, {1, 6, 11, 16}, {1, 6, 11, 16, 17}, 61 | 62 | > {1, 6, 11, 16, 17, 18}, {1, 6, 11, 16, 17, 18, 19}, 63 | 64 | > {1, 2, 3, 8, 9, 10, 15, 20}, {1, 6, 11, 16, 21}, {1, 6, 11, 16, 17, 22}, 65 | 66 | > {1, 6, 11, 16, 17, 22, 23}, {1, 6, 11, 16, 17, 22, 23, 24}, 67 | 68 | > {1, 6, 11, 16, 17, 22, 23, 24, 25}} 69 | 70 | *) 71 | -------------------------------------------------------------------------------- /set_union.c: -------------------------------------------------------------------------------- 1 | 2 | /* set_union.c 3 | 4 | Implementation of a heap / priority queue 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 | #include 31 | 32 | #include "set_union.h" 33 | 34 | /* [[[ set_union_init_cut */ 35 | void set_union_init(set_union *s, int n) { 36 | int i; /* counter */ 37 | 38 | for (i = 1; i <= n; i++) { 39 | s->p[i] = i; 40 | s->size[i] = 1; 41 | } 42 | s->n = n; 43 | } 44 | /* ]]] */ 45 | 46 | /* [[[ set_union_find_cut */ 47 | int find(set_union *s, int x) { 48 | if (s->p[x] == x) { 49 | return(x); 50 | } 51 | return(find(s, s->p[x])); 52 | } 53 | /* ]]] */ 54 | 55 | /* [[[ set_union_union_sets_cut */ 56 | void union_sets(set_union *s, int s1, int s2) { 57 | int r1, r2; /* roots of sets */ 58 | 59 | r1 = find(s, s1); 60 | r2 = find(s, s2); 61 | 62 | printf("s1=%d r1=%d s2=%d r2=%d\n", s1, r1, s2, r2); 63 | 64 | if (r1 == r2) { 65 | return; /* already in same set */ 66 | } 67 | 68 | if (s->size[r1] >= s->size[r2]) { 69 | s->size[r1] = s->size[r1] + s->size[r2]; 70 | s->p[r2] = r1; 71 | } else { 72 | s->size[r2] = s->size[r1] + s->size[r2]; 73 | s->p[r1] = r2; 74 | } 75 | } 76 | /* ]]] */ 77 | 78 | /* [[[ same_component_cut */ 79 | bool same_component(set_union *s, int s1, int s2) { 80 | return (find(s, s1) == find(s, s2)); 81 | } 82 | /* ]]] */ 83 | 84 | void print_set_union(set_union *s) { 85 | int i; /* counter */ 86 | 87 | for (i = 1; i <= s->n; i++) { 88 | printf("%i set=%d size=%d \n", i, s->p[i], s->size[i]); 89 | } 90 | printf("\n"); 91 | } 92 | -------------------------------------------------------------------------------- /topsort1.c: -------------------------------------------------------------------------------- 1 | 2 | /* topsort1.c 3 | 4 | Topologically sort a directed acyclic graph by DFS numbering (DAG) 5 | 6 | by: Steven Skiena 7 | begun: March 26, 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 | #include 31 | 32 | #include "bool.h" 33 | #include "bfs-dfs.h" 34 | #include "stack.h" 35 | 36 | stack sorted; /* topological ordering of vertices */ 37 | 38 | extern bool processed[]; /* which vertices have been processed */ 39 | extern bool discovered[]; /* which vertices have been found */ 40 | extern int parent[]; /* discovery relation */ 41 | 42 | extern int entry_time[]; /* time of vertex entry */ 43 | extern int exit_time[]; /* time of vertex exit */ 44 | 45 | 46 | void process_vertex_early(int v) { 47 | 48 | } 49 | 50 | /* [[[ pvlate_topsort_cut */ 51 | void process_vertex_late(int v) { 52 | push(&sorted, v); 53 | } 54 | /* ]]] */ 55 | 56 | /* [[[ pedge_topsort_cut */ 57 | void process_edge(int x, int y) { 58 | int class; /* edge class */ 59 | 60 | class = edge_classification(x, y); 61 | 62 | if (class == BACK) { 63 | printf("Warning: directed cycle found, not a DAG\n"); 64 | } 65 | } 66 | /* ]]] */ 67 | 68 | /* [[[ topsort_cut */ 69 | void topsort(graph *g) { 70 | int i; /* counter */ 71 | 72 | init_stack(&sorted); 73 | 74 | for (i = 1; i <= g->nvertices; i++) { 75 | if (discovered[i] == FALSE) { 76 | dfs(g, i); 77 | } 78 | } 79 | print_stack(&sorted); /* report topological order */ 80 | } 81 | /* ]]] */ 82 | 83 | int main(void) { 84 | graph g; 85 | 86 | read_graph(&g, TRUE); 87 | print_graph(&g); 88 | 89 | topsort(&g); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /gcd.c: -------------------------------------------------------------------------------- 1 | /* gcd.c 2 | 3 | Compute the greatest common divisor of two integers 4 | 5 | by: Steven Skiena 6 | begun: July 10, 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 | #include 29 | #include 30 | 31 | /* integers to compute the GCD of */ 32 | 33 | long gcd1(long p, long q) { 34 | if (q > p) { 35 | return(gcd1(q, p)); 36 | } 37 | 38 | if (q == 0) { 39 | return(p); 40 | } 41 | 42 | printf(" gcd(%ld,%ld) &=& gcd(%ld \\mod %ld, %ld) = gcd(%ld,%ld) \n", 43 | p, q, p, q, q, q, p % q); 44 | 45 | return(gcd1(q, p % q)); 46 | } 47 | 48 | /* Find the gcd(p,q) and x,y such that p*x + q*y = gcd(p,q) */ 49 | 50 | long gcd(long p, long q, long *x, long *y) { 51 | long x1, y1; /* previous coefficients */ 52 | long g; /* value of gcd(p,q) */ 53 | 54 | if (q > p) { 55 | return(gcd(q, p, y, x)); 56 | } 57 | 58 | if (q == 0) { 59 | *x = 1; 60 | *y = 0; 61 | return(p); 62 | } 63 | 64 | g = gcd(q, p%q, &x1, &y1); 65 | 66 | *x = y1; 67 | *y = (x1 - floor(p/q)*y1); 68 | 69 | return(g); 70 | } 71 | 72 | int main(void) { 73 | long p, q; 74 | long x, y, g1, g2; 75 | 76 | while (scanf("%ld %ld", &p, &q) != EOF) { 77 | g1 = gcd1(p, q); 78 | g2 = gcd(p, q, &x, &y); 79 | 80 | printf("gcd of p=%ld and q=%ld = %ld\n", p, q, g1); 81 | printf(" %ld*%ld + %ld*%ld = %ld\n", p, x, q, y, g2); 82 | 83 | if (g1 != g2) { 84 | printf("ERROR: GCD\n"); 85 | } 86 | 87 | if ((p*x + q*y) != g1) { 88 | printf("ERROR: DIOPHONINE SOLUTION WRONG!\n"); 89 | } 90 | } 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /datafiles/substringedit-out: -------------------------------------------------------------------------------- 1 | matching cost = 1 2 | m y - n a m e - i s - n o t - s k i e n n a - s k e n a - s k i n a - s k i e n g - e t c 3 | : 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 | s: 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 5 | k: 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 1 0 1 2 2 2 2 2 1 0 1 2 2 2 1 0 1 2 2 2 1 0 1 2 2 2 2 2 2 2 6 | i: 3 3 3 3 3 3 3 3 3 2 2 2 2 3 3 3 2 1 0 1 2 3 3 3 2 1 1 2 3 3 2 1 0 1 2 3 2 1 0 1 2 3 3 3 3 3 7 | e: 4 4 4 4 4 4 4 3 4 3 3 3 3 3 4 4 3 2 1 0 1 2 3 4 3 2 1 2 3 4 3 2 1 1 2 3 3 2 1 0 1 2 3 3 4 4 8 | n: 5 5 5 5 4 5 5 4 4 4 4 4 3 4 4 5 4 3 2 1 0 1 2 3 4 3 2 1 2 3 4 3 2 1 2 3 4 3 2 1 0 1 2 3 4 5 9 | a: 6 6 6 6 5 4 5 5 5 5 5 5 4 4 5 5 5 4 3 2 1 1 1 2 3 4 3 2 1 2 3 4 3 2 1 2 3 4 3 2 1 1 2 3 4 5 10 | 11 | m y - n a m e - i s - n o t - s k i e n n a - s k e n a - s k i n a - s k i e n g - e t c 12 | : -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 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 13 | s: 2 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 | k: 2 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 2 0 1 0 0 0 0 0 2 0 1 0 0 0 2 0 1 0 0 0 2 0 1 0 0 0 0 0 0 0 15 | i: 2 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 2 2 0 1 1 0 0 0 2 2 0 0 0 0 2 2 0 1 1 0 2 2 0 1 1 0 0 0 0 0 16 | e: 2 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 2 2 0 1 1 1 0 2 2 0 0 0 0 2 2 2 0 0 0 2 2 2 0 1 1 1 0 0 0 17 | n: 2 0 0 0 0 0 0 2 0 2 0 0 0 0 0 0 2 2 2 2 0 0 1 1 1 2 2 0 1 1 1 2 2 0 0 0 0 2 2 2 0 1 1 1 0 0 18 | a: 2 0 0 0 2 0 1 2 0 0 0 0 2 0 0 0 2 2 2 2 2 0 0 1 1 1 2 2 0 1 1 1 2 2 0 1 1 1 2 2 2 0 0 0 0 0 19 | 6 20 20 | MMMMMD 21 | -------------------------------------------------------------------------------- /original/dijkstra.c: -------------------------------------------------------------------------------- 1 | 2 | /* dijkstra.c 3 | 4 | Compute shortest paths in weighted graphs using Dijkstra's algorithm 5 | 6 | by: Steven Skiena 7 | date: March 6, 2002 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 31 | #include "bool.h" 32 | #include "wgraph.h" 33 | 34 | #define MAXINT 100007 35 | 36 | int parent[MAXV]; /* discovery relation */ 37 | 38 | 39 | 40 | dijkstra(graph *g, int start) /* WAS prim(g,start) */ 41 | { 42 | int i,j; /* counters */ 43 | bool intree[MAXV]; /* is the vertex in the tree yet? */ 44 | int distance[MAXV]; /* distance vertex is from start */ 45 | int v; /* current vertex to process */ 46 | int w; /* candidate next vertex */ 47 | int weight; /* edge weight */ 48 | int dist; /* best current distance from start */ 49 | 50 | for (i=1; i<=g->nvertices; i++) { 51 | intree[i] = FALSE; 52 | distance[i] = MAXINT; 53 | parent[i] = -1; 54 | } 55 | 56 | distance[start] = 0; 57 | v = start; 58 | 59 | while (intree[v] == FALSE) { 60 | intree[v] = TRUE; 61 | for (i=0; idegree[v]; i++) { 62 | w = g->edges[v][i].v; 63 | weight = g->edges[v][i].weight; 64 | /* CHANGED */ if (distance[w] > (distance[v]+weight)) { 65 | /* CHANGED */ distance[w] = distance[v]+weight; 66 | /* CHANGED */ parent[w] = v; 67 | } 68 | } 69 | 70 | v = 1; 71 | dist = MAXINT; 72 | for (i=1; i<=g->nvertices; i++) 73 | if ((intree[i] == FALSE) && (dist > distance[i])) { 74 | dist = distance[i]; 75 | v = i; 76 | } 77 | } 78 | /*for (i=1; i<=g->nvertices; i++) printf("%d %d\n",i,distance[i]);*/ 79 | } 80 | 81 | main() 82 | { 83 | graph g; 84 | int i; 85 | 86 | read_graph(&g,FALSE); 87 | dijkstra(&g,1); 88 | 89 | for (i=1; i<=g.nvertices; i++) 90 | find_path(1,i,parent); 91 | printf("\n"); 92 | 93 | } 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /order.c: -------------------------------------------------------------------------------- 1 | /* order.c 2 | 3 | Demonstrate traversal orders on a grid. 4 | 5 | by: Steven Skiena 6 | begun: July 14, 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 | #include 29 | 30 | #include "geometry.h" 31 | 32 | void process(int i, int j) { 33 | printf("(%d,%d)\n", i, j); 34 | } 35 | 36 | void row_major(int n, int m) { 37 | int i, j; /* counters */ 38 | 39 | for (i = 1; i <= n; i++) { 40 | for (j = 1; j <= m; j++) { 41 | process(i, j); 42 | } 43 | } 44 | } 45 | 46 | void column_major(int n, int m) { 47 | int i, j; /* counters */ 48 | 49 | for (j = 1; j <= m; j++) { 50 | for (i = 1; i <= n; i++) { 51 | process(i, j); 52 | } 53 | } 54 | } 55 | 56 | void snake_order(int n, int m) { 57 | int i, j; /* counters */ 58 | 59 | for (i = 1; i <= n; i++) { 60 | for (j = 1; j <= m; j++) { 61 | process(i, j + (m+1-2*j) * ((i+1) % 2)); 62 | } 63 | } 64 | } 65 | 66 | void diagonal_order(int n, int m) { 67 | int d, j; /* diagonal and point counters */ 68 | int pcount; /* points on diagonal */ 69 | int height; /* row of lowest point */ 70 | 71 | for (d = 1; d <= (m + n - 1); d++) { 72 | height = 1 + max(0, d - m); 73 | pcount = min(d, (n - height + 1)); 74 | for (j = 0; j < pcount; j++) { 75 | process(min(m , d) - j, height + j); 76 | } 77 | } 78 | } 79 | 80 | int main(void) { 81 | printf("row_major\n"); 82 | row_major(5, 5); 83 | 84 | printf("\ncolumn_major\n"); 85 | column_major(3, 3); 86 | 87 | printf("\nsnake_order\n"); 88 | snake_order(5, 5); 89 | 90 | printf("\ndiagonal_order\n"); 91 | diagonal_order(3, 4); 92 | 93 | printf("\ndiagonal_order\n"); 94 | diagonal_order(4, 3); 95 | 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /datafiles/cgtest-out: -------------------------------------------------------------------------------- 1 | segment: 1.000 5.000 2 | 3.000 5.000 3 | segment: 5.000 5.000 4 | 5.000 0.000 5 | segments_intersect test 6 | 0 7 | intersection point 8 | 5.000 5.000 9 | -------------------------------- 10 | segment: 1.000 5.000 11 | 3.000 5.000 12 | segment: 3.000 5.000 13 | 3.000 0.000 14 | segments_intersect test 15 | 1 16 | intersection point 17 | 3.000 5.000 18 | -------------------------------- 19 | segment: 2.300 5.300 20 | 5.700 6.800 21 | segment: 12.100 3.400 22 | 9.800 5.300 23 | segments_intersect test 24 | 0 25 | intersection point 26 | 7.189 7.457 27 | -------------------------------- 28 | segment: 1.000 5.000 29 | 3.000 5.000 30 | segment: 2.000 4.000 31 | -17.000 4.000 32 | segments_intersect test 33 | 0 34 | intersection point 35 | Error: Distinct parallel lines do not intersect. 36 | 7.189 7.457 37 | -------------------------------- 38 | segment: 0.000 0.000 39 | 1.000 1.000 40 | segment: 5.000 0.000 41 | 10.000 5.000 42 | segments_intersect test 43 | 0 44 | intersection point 45 | Error: Distinct parallel lines do not intersect. 46 | 7.189 7.457 47 | -------------------------------- 48 | segment: 0.000 0.000 49 | 14.300 -4.300 50 | segment: 0.000 0.000 51 | 14.300 -4.300 52 | segments_intersect test 53 | 1 54 | intersection point 55 | Warning: Identical lines, all points intersect. 56 | 0.000 0.000 57 | -------------------------------- 58 | segment: 2.000 2.000 59 | 4.000 4.000 60 | segment: 2.000 4.000 61 | 4.000 2.000 62 | segments_intersect test 63 | 1 64 | intersection point 65 | 3.000 3.000 66 | -------------------------------- 67 | segment: 0.000 0.000 68 | 5.000 5.000 69 | segment: 2.000 2.000 70 | 3.000 3.000 71 | segments_intersect test 72 | 1 73 | intersection point 74 | Warning: Identical lines, all points intersect. 75 | 0.000 0.000 76 | -------------------------------- 77 | segment: 0.000 0.000 78 | 5.000 5.000 79 | segment: 2.000 2.000 80 | 6.000 6.000 81 | segments_intersect test 82 | 1 83 | intersection point 84 | Warning: Identical lines, all points intersect. 85 | 0.000 0.000 86 | -------------------------------- 87 | segment: 0.000 0.000 88 | 2.000 2.000 89 | segment: 5.000 5.000 90 | 6.000 6.000 91 | segments_intersect test 92 | 0 93 | intersection point 94 | Warning: Identical lines, all points intersect. 95 | 0.000 0.000 96 | -------------------------------- 97 | -------------------------------------------------------------------------------- /original/partition.c: -------------------------------------------------------------------------------- 1 | /* partition.c 2 | Optimally balance partitions using dynamic programming 3 | 4 | begun: August 14, 2006 5 | by: Steven Skiena 6 | */ 7 | 8 | #define MAXN 45 /* largest number of books */ 9 | #define MAXK 10 /* largest number of dividers */ 10 | #define MAXINT 100000 /* infinity */ 11 | 12 | 13 | max(int a, int b) 14 | { 15 | return( (a > b) ? a : b ); 16 | } 17 | 18 | read_partition(int s[], int *n, int *k) 19 | { 20 | int i; /* counter */ 21 | 22 | scanf("%d %d\n",n,k); 23 | for (i=1; i<=*n; i++) 24 | scanf("%d\n",&(s[i])); 25 | } 26 | 27 | print_books(int s[], int start, int end) 28 | { 29 | int i; /* counter */ 30 | 31 | printf("\{"); 32 | for (i=start; i<=end; i++) printf(" %d ",s[i]); 33 | printf("}\n"); 34 | } 35 | 36 | print_matrix(int m[MAXN+1][MAXK+1], int n, int k) 37 | { 38 | int i,j; /* counters */ 39 | 40 | 41 | printf("\n"); 42 | for (i=1; i<=n; i++) { 43 | for (j=1; j<=k; j++) 44 | printf(" %d ",m[i][j]); 45 | printf("\n"); 46 | } 47 | } 48 | 49 | reconstruct_partition(int s[],int d[MAXN+1][MAXK+1], int n, int k) 50 | { 51 | if (k==1) 52 | print_books(s,1,n); 53 | else { 54 | reconstruct_partition(s,d,d[n][k],k-1); 55 | print_books(s,d[n][k]+1,n); 56 | } 57 | } 58 | 59 | partition(int s[], int n, int k) 60 | { 61 | int p[MAXN+1]; /* prefix sums array */ 62 | int m[MAXN+1][MAXK+1]; /* DP table for values */ 63 | int d[MAXN+1][MAXK+1]; /* DP table for dividers */ 64 | int cost; /* test split cost */ 65 | int i,j,x; /* counters */ 66 | 67 | p[0] = 0; /* construct prefix sums */ 68 | for (i=1; i<=n; i++) p[i]=p[i-1]+s[i]; 69 | 70 | for (i=1; i<=n; i++) m[i][1] = p[i]; /* initialize boundaries */ 71 | for (j=1; j<=k; j++) m[1][j] = s[1]; 72 | 73 | for (i=2; i<=n; i++) /* evaluate main recurrence */ 74 | for (j=2; j<=k; j++) { 75 | m[i][j] = MAXINT; 76 | for (x=1; x<=(i-1); x++) { 77 | cost = max(m[x][j-1], p[i]-p[x]); 78 | if (m[i][j] > cost) { 79 | m[i][j] = cost; 80 | d[i][j] = x; 81 | } 82 | } 83 | } 84 | 85 | /* 86 | print_matrix(m,n,k); 87 | print_matrix(d,n,k); 88 | */ 89 | reconstruct_partition(s,d,n,k); /* print book partition */ 90 | } 91 | 92 | 93 | 94 | main() 95 | { 96 | int s[MAXN+1]; /* book thicknesses to partition */ 97 | int n; /* how many books? */ 98 | int k; /* how many partitions? */ 99 | 100 | read_partition(s,&n,&k); 101 | partition(s,n,k); 102 | 103 | } 104 | 105 | -------------------------------------------------------------------------------- /original/graph.c: -------------------------------------------------------------------------------- 1 | 2 | /* graph.c 3 | 4 | A generic adjacency list-in-array graph data type. 5 | 6 | by: Steven Skiena 7 | begun: March 6, 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 "bool.h" 32 | #include "queue.h" 33 | #include "graph.h" 34 | 35 | initialize_graph(graph *g) 36 | { 37 | int i; /* counter */ 38 | 39 | g -> nvertices = 0; 40 | g -> nedges = 0; 41 | 42 | for (i=1; i<=MAXV; i++) g->degree[i] = 0; 43 | } 44 | 45 | read_graph(graph *g, bool directed) 46 | { 47 | int i; /* counter */ 48 | int m; /* number of edges */ 49 | int x, y; /* vertices in edge (x,y) */ 50 | 51 | initialize_graph(g); 52 | 53 | scanf("%d %d",&(g->nvertices),&m); 54 | 55 | for (i=1; i<=m; i++) { 56 | scanf("%d %d",&x,&y); 57 | insert_edge(g,x,y,directed); 58 | } 59 | } 60 | 61 | insert_edge(graph *g, int x, int y, bool directed) 62 | { 63 | if (g->degree[x] > MAXDEGREE) 64 | printf("Warning: insertion(%d,%d) exceeds max degree\n",x,y); 65 | 66 | g->edges[x][g->degree[x]] = y; 67 | g->degree[x] ++; 68 | 69 | if (directed == FALSE) 70 | insert_edge(g,y,x,TRUE); 71 | else 72 | g->nedges ++; 73 | } 74 | 75 | 76 | delete_edge(graph *g, int x, int y, bool directed) 77 | { 78 | int i; /* counter */ 79 | 80 | for (i=0; idegree[x]; i++) 81 | if (g->edges[x][i] == y) { 82 | g->degree[x] --; 83 | g->edges[x][i] = g->edges[x][g->degree[x]]; 84 | 85 | if (directed == FALSE) 86 | delete_edge(g,y,x,TRUE); 87 | 88 | return; 89 | } 90 | 91 | printf("Warning: deletion(%d,%d) not found in g.\n",x,y); 92 | } 93 | 94 | print_graph(graph *g) 95 | { 96 | int i,j; /* counters */ 97 | 98 | for (i=1; i<=g->nvertices; i++) { 99 | printf("%d: ",i); 100 | for (j=0; jdegree[i]; j++) 101 | printf(" %d",g->edges[i][j]); 102 | printf("\n"); 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /dfs-demo.c: -------------------------------------------------------------------------------- 1 | 2 | /* dfs-demo.c 3 | Driver program demonstrating depth-first search numbering and 4 | edge labeling. 5 | 6 | by: Steven Skiena 7 | begun: March 27, 2002 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 | #include 30 | #include "bool.h" 31 | 32 | #include "bfs-dfs.h" 33 | #include "queue.h" 34 | 35 | extern bool processed[]; /* which vertices have been processed */ 36 | extern bool discovered[]; /* which vertices have been found */ 37 | extern int parent[]; /* discovery relation */ 38 | 39 | int entry_time[MAXV+1]; /* time of vertex entry */ 40 | int exit_time[MAXV+1]; /* time of vertex exit */ 41 | int time; /* current event time */ 42 | 43 | void process_vertex_early(int v) { 44 | time = time + 1; 45 | entry_time[v] = time; 46 | printf("entered vertex %d at time %d\n",v, entry_time[v]); 47 | } 48 | 49 | void process_vertex_late(int v) { 50 | time = time + 1; 51 | exit_time[v] = time; 52 | printf("exit vertex %d at time %d\n",v, exit_time[v]); 53 | } 54 | 55 | void process_edge(int x, int y) { 56 | int class; /* edge class */ 57 | 58 | class = edge_classification(x, y); 59 | 60 | if (class == BACK) { 61 | printf("back edge (%d,%d)\n", x, y); 62 | } else if (class == TREE) { 63 | printf("tree edge (%d,%d)\n", x, y); 64 | } else if (class == FORWARD) { 65 | printf("forward edge (%d,%d)\n", x, y); 66 | } else if (class == CROSS) { 67 | printf("cross edge (%d,%d)\n", x, y); 68 | } else { 69 | printf("edge (%d,%d)\n not in valid class=%d", x, y, class); 70 | } 71 | } 72 | 73 | int main(void) { 74 | graph g; 75 | int i; 76 | 77 | read_graph(&g, FALSE); 78 | print_graph(&g); 79 | 80 | initialize_search(&g); 81 | dfs(&g, 1); 82 | 83 | for (i = 1; i <= g.nvertices; i++) { 84 | find_path(1, i, parent); 85 | } 86 | printf("\n"); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /original/prim.c: -------------------------------------------------------------------------------- 1 | 2 | /* prim.c 3 | 4 | Compute minimum spanning trees of graphs via Dijkstra/Prim's algorithm. 5 | 6 | by: Steven Skiena 7 | begun: March 6, 2002 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 31 | #include "bool.h" 32 | #include "wgraph.h" 33 | 34 | #define MAXINT 100007 35 | 36 | int parent[MAXV]; /* discovery relation */ 37 | 38 | 39 | /************************************************************/ 40 | 41 | 42 | prim(graph *g, int start) 43 | { 44 | int i,j; /* counters */ 45 | bool intree[MAXV]; /* is the vertex in the tree yet? */ 46 | int distance[MAXV]; /* distance vertex is from start */ 47 | int v; /* current vertex to process */ 48 | int w; /* candidate next vertex */ 49 | int weight; /* edge weight */ 50 | int dist; /* best current distance from start */ 51 | 52 | for (i=1; i<=g->nvertices; i++) { 53 | intree[i] = FALSE; 54 | distance[i] = MAXINT; 55 | parent[i] = -1; 56 | } 57 | 58 | distance[start] = 0; 59 | v = start; 60 | 61 | while (intree[v] == FALSE) { 62 | intree[v] = TRUE; 63 | for (i=0; idegree[v]; i++) { 64 | w = g->edges[v][i].v; 65 | weight = g->edges[v][i].weight; 66 | if ((distance[w] > weight) && (intree[w] == FALSE)) { 67 | distance[w] = weight; 68 | parent[w] = v; 69 | } 70 | } 71 | 72 | v = 1; 73 | dist = MAXINT; 74 | for (i=1; i<=g->nvertices; i++) 75 | if ((intree[i] == FALSE) && (dist > distance[i])) { 76 | dist = distance[i]; 77 | v = i; 78 | } 79 | } 80 | } 81 | 82 | main() 83 | { 84 | graph g; 85 | int i; 86 | 87 | read_graph(&g,FALSE); 88 | 89 | prim(&g,1); 90 | 91 | printf("Out of Prim\n"); 92 | 93 | for (i=1; i<=g.nvertices; i++) { 94 | /*printf(" %d parent=%d\n",i,parent[i]);*/ 95 | find_path(1,i,parent); 96 | } 97 | printf("\n"); 98 | 99 | } 100 | 101 | -------------------------------------------------------------------------------- /random.c: -------------------------------------------------------------------------------- 1 | /* 2 | random.c 3 | 4 | Compute random numbers within given ranges 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 | #include 29 | #include 30 | #include 31 | 32 | #include "random.h" 33 | 34 | /**************************************************************************/ 35 | 36 | /* These functions generate random numbers in the designated ranges */ 37 | 38 | void swap(int *a, int *b) { 39 | int x; 40 | 41 | x = *a; 42 | *a = *b; 43 | *b = x; 44 | } 45 | 46 | int random_int(int low, int high) { /* lower/upper bounds on numb*/ 47 | int rand(); 48 | int i, j, r; /* random number*/ 49 | 50 | i = RAND_MAX / (high - low + 1); 51 | i *= (high - low + 1); 52 | while ((j = rand()) >= i) { 53 | continue; 54 | } 55 | r = (j % (high - low + 1)) + low; 56 | if ((r < low) || (r > high)) { 57 | printf("Error: random integer %d out of range [%d,%d]\n", 58 | r, low, high); 59 | } 60 | return(r); 61 | } 62 | 63 | /* Construct a random permutation of the $n$ elements of the 64 | given array. 65 | */ 66 | 67 | void random_permutation(int a[], int n) { 68 | int i; 69 | 70 | for (i=n; i>1; i--) { 71 | swap(&a[i - 1], &a[random_int(0, i - 1)]); 72 | } 73 | } 74 | 75 | double random_float(int low, int high) { /*lower/upper bounds on numb*/ 76 | int rand(); 77 | double i, j; /* avoid arithmetic trouble */ 78 | double r; /* random number*/ 79 | 80 | i = RAND_MAX / (high - low); 81 | i *= (high - low); 82 | while ((j = rand()) >= i) { 83 | continue; 84 | } 85 | r = (j / i) * (high - low) + low; 86 | 87 | if ((r < low) || (r > high)) { 88 | printf("ERROR: random real %f out of range [%d,%d]\n", 89 | r, low, high); 90 | } 91 | return(r); 92 | } 93 | -------------------------------------------------------------------------------- /original/paths.c: -------------------------------------------------------------------------------- 1 | /* 2 | paths.c 3 | Enumerate the paths in a graph via backtracking 4 | 5 | begun: August 1, 2006 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 | #include "graph.h" 29 | #include "backtrack.h" 30 | #include "bool.h" 31 | 32 | int solution_count; /* how many solutions are there? */ 33 | 34 | graph g; /* graph to traverse */ 35 | 36 | process_solution(int a[], int k) 37 | { 38 | int i; /* counter */ 39 | 40 | solution_count ++; 41 | 42 | printf("{"); 43 | for (i=1; i<=k; i++) 44 | printf(" %d",a[i]); 45 | printf(" }\n"); 46 | 47 | } 48 | 49 | is_a_solution(int a[], int k, int t) 50 | { 51 | return (a[k] == t); 52 | } 53 | 54 | make_move(int a[], int k, int n) 55 | { 56 | } 57 | 58 | unmake_move(int a[], int k, int n) 59 | { 60 | } 61 | 62 | 63 | /* What are possible elements of the next slot in the path 64 | */ 65 | 66 | construct_candidates(int a[], int k, int n, int c[], int *ncandidates) 67 | { 68 | int i,j; /* counters */ 69 | bool in_sol[NMAX]; /* what's already in the solution? */ 70 | int last; /* last vertex on current path */ 71 | 72 | for (i=1; i 37 | #include 38 | 39 | typedef struct { 40 | char first[NAMELENGTH]; /* suitor's first name */ 41 | char last[NAMELENGTH]; /* suitor's last name */ 42 | int height; /* suitor's height */ 43 | int weight; /* suitor's weight */ 44 | } suitor; 45 | 46 | 47 | suitor suitors[NSUITORS]; /* database of suitors */ 48 | int nsuitors; /* number of suitors */ 49 | 50 | read_suitors() 51 | { 52 | char first[NAMELENGTH], last[NAMELENGTH]; 53 | int height, weight; 54 | 55 | nsuitors = 0; 56 | 57 | while (scanf("%s %s %d %d\n",suitors[nsuitors].first, 58 | suitors[nsuitors].last,&height,&weight) != EOF) { 59 | 60 | suitors[nsuitors].height = abs(height - BESTHEIGHT); 61 | if (weight > BESTWEIGHT) 62 | suitors[nsuitors].weight = weight - BESTWEIGHT; 63 | else 64 | suitors[nsuitors].weight = - weight; 65 | 66 | nsuitors ++; 67 | } 68 | } 69 | 70 | 71 | main() 72 | { 73 | int i; /* counter */ 74 | int suitor_compare(); 75 | 76 | read_suitors(); 77 | 78 | qsort(suitors, nsuitors, sizeof(suitor), suitor_compare); 79 | 80 | for (i=0; iheight < b->height) return(-1); 89 | if (a->height > b->height) return(1); 90 | 91 | if (a->weight < b->weight) return(-1); 92 | if (a->weight > b->weight) return(1); 93 | 94 | if ((result=strcmp(a->last,b->last)) != 0) return result; 95 | 96 | return(strcmp(a->first,b->first)); 97 | } 98 | 99 | -------------------------------------------------------------------------------- /original/geotest.c: -------------------------------------------------------------------------------- 1 | /* geotest.c 2 | Driver program for geometry routines 3 | 4 | by: Steven Skiena 5 | begun: July 23, 2002 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 | #include "bool.h" 29 | #include "geometry.h" 30 | #include 31 | #include 32 | 33 | 34 | 35 | main() 36 | { 37 | point p1,p2,q1,q2,i; 38 | line l1,l2,l3,l4; 39 | 40 | while ( scanf("%lf %lf",&p1[X],&p1[Y]) != EOF ) { 41 | scanf("%lf %lf",&p2[X],&p2[Y]); 42 | scanf("%lf %lf",&q1[X],&q1[Y]); 43 | scanf("%lf %lf",&q2[X],&q2[Y]); 44 | 45 | print_point(p1); 46 | print_point(p2); 47 | print_point(q1); 48 | print_point(q2); 49 | 50 | points_to_line(p1,p2,&l1); 51 | points_to_line(q1,q2,&l2); 52 | 53 | print_line(l1); 54 | print_line(l2); 55 | 56 | printf("slope and line tests\n"); 57 | point_and_slope_to_line(p1,-l1.a,&l3); 58 | print_line(l3); 59 | point_and_slope_to_line(p2,-l1.a,&l3); 60 | print_line(l3); 61 | point_and_slope_to_line(q1,-l2.a,&l3); 62 | print_line(l3); 63 | point_and_slope_to_line(q2,-l2.a,&l3); 64 | print_line(l3); 65 | 66 | printf("parallel lines test\n"); 67 | printf("%d\n", parallelQ(l1,l2)); 68 | 69 | printf("intersection point\n"); 70 | intersection_point(l1,l2,i); 71 | print_point(i); 72 | 73 | printf("closest point\n"); 74 | closest_point(p1,l1,i); 75 | print_point(i); 76 | closest_point(p2,l1,i); 77 | print_point(i); 78 | closest_point(q1,l1,i); 79 | print_point(i); 80 | closest_point(q2,l1,i); 81 | print_point(i); 82 | closest_point(p1,l2,i); 83 | print_point(i); 84 | closest_point(p2,l2,i); 85 | print_point(i); 86 | closest_point(q1,l2,i); 87 | print_point(i); 88 | closest_point(q2,l2,i); 89 | print_point(i); 90 | 91 | 92 | printf("--------------------------------\n"); 93 | } 94 | 95 | 96 | } 97 | -------------------------------------------------------------------------------- /geotest.c: -------------------------------------------------------------------------------- 1 | /* geotest.c 2 | Driver program for geometry routines 3 | 4 | by: Steven Skiena 5 | begun: July 23, 2002 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 | #include 28 | #include 29 | 30 | #include "bool.h" 31 | #include "geometry.h" 32 | 33 | int main(void) { 34 | point p1, p2, q1, q2, i; 35 | line l1, l2, l3, l4; 36 | 37 | while (scanf("%lf %lf", &p1[X], &p1[Y]) != EOF) { 38 | scanf("%lf %lf", &p2[X], &p2[Y]); 39 | scanf("%lf %lf", &q1[X], &q1[Y]); 40 | scanf("%lf %lf", &q2[X], &q2[Y]); 41 | 42 | print_point(p1); 43 | print_point(p2); 44 | print_point(q1); 45 | print_point(q2); 46 | 47 | points_to_line(p1, p2, &l1); 48 | points_to_line(q1, q2, &l2); 49 | 50 | print_line(l1); 51 | print_line(l2); 52 | 53 | printf("slope and line tests\n"); 54 | point_and_slope_to_line(p1, -l1.a, &l3); 55 | print_line(l3); 56 | point_and_slope_to_line(p2, -l1.a, &l3); 57 | print_line(l3); 58 | point_and_slope_to_line(q1, -l2.a, &l3); 59 | print_line(l3); 60 | point_and_slope_to_line(q2, -l2.a, &l3); 61 | print_line(l3); 62 | 63 | printf("parallel lines test\n"); 64 | printf("%d\n", parallelQ(l1, l2)); 65 | 66 | printf("intersection point\n"); 67 | intersection_point(l1, l2, i); 68 | print_point(i); 69 | 70 | printf("closest point\n"); 71 | closest_point(p1, l1, i); 72 | print_point(i); 73 | closest_point(p2, l1, i); 74 | print_point(i); 75 | closest_point(q1, l1, i); 76 | print_point(i); 77 | closest_point(q2, l1, i); 78 | print_point(i); 79 | closest_point(p1, l2, i); 80 | print_point(i); 81 | closest_point(p2, l2, i); 82 | print_point(i); 83 | closest_point(q1, l2, i); 84 | print_point(i); 85 | closest_point(q2, l2, i); 86 | print_point(i); 87 | 88 | printf("--------------------------------\n"); 89 | } 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /datafiles/grid-bfs-demo-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 | processed vertex 1 27 | processed edge (1,6) 28 | processed edge (1,2) 29 | processed vertex 6 30 | processed edge (6,11) 31 | processed edge (6,7) 32 | processed vertex 2 33 | processed edge (2,7) 34 | processed edge (2,3) 35 | processed vertex 11 36 | processed edge (11,16) 37 | processed edge (11,12) 38 | processed vertex 7 39 | processed edge (7,12) 40 | processed edge (7,8) 41 | processed vertex 3 42 | processed edge (3,8) 43 | processed edge (3,4) 44 | processed vertex 16 45 | processed edge (16,21) 46 | processed edge (16,17) 47 | processed vertex 12 48 | processed edge (12,17) 49 | processed edge (12,13) 50 | processed vertex 8 51 | processed edge (8,13) 52 | processed edge (8,9) 53 | processed vertex 4 54 | processed edge (4,9) 55 | processed edge (4,5) 56 | processed vertex 21 57 | processed edge (21,22) 58 | processed vertex 17 59 | processed edge (17,22) 60 | processed edge (17,18) 61 | processed vertex 13 62 | processed edge (13,18) 63 | processed edge (13,14) 64 | processed vertex 9 65 | processed edge (9,14) 66 | processed edge (9,10) 67 | processed vertex 5 68 | processed edge (5,10) 69 | processed vertex 22 70 | processed edge (22,23) 71 | processed vertex 18 72 | processed edge (18,23) 73 | processed edge (18,19) 74 | processed vertex 14 75 | processed edge (14,19) 76 | processed edge (14,15) 77 | processed vertex 10 78 | processed edge (10,15) 79 | processed vertex 23 80 | processed edge (23,24) 81 | processed vertex 19 82 | processed edge (19,24) 83 | processed edge (19,20) 84 | processed vertex 15 85 | processed edge (15,20) 86 | processed vertex 24 87 | processed edge (24,25) 88 | processed vertex 20 89 | processed edge (20,25) 90 | processed vertex 25 91 | -1 1 2 3 4 1 6 7 8 9 6 11 12 13 14 11 16 17 18 19 16 21 22 23 24 92 | 93 | 1 94 | 1 2 95 | 1 2 3 96 | 1 2 3 4 97 | 1 2 3 4 5 98 | 1 6 99 | 1 6 7 100 | 1 6 7 8 101 | 1 6 7 8 9 102 | 1 6 7 8 9 10 103 | 1 6 11 104 | 1 6 11 12 105 | 1 6 11 12 13 106 | 1 6 11 12 13 14 107 | 1 6 11 12 13 14 15 108 | 1 6 11 16 109 | 1 6 11 16 17 110 | 1 6 11 16 17 18 111 | 1 6 11 16 17 18 19 112 | 1 6 11 16 17 18 19 20 113 | 1 6 11 16 21 114 | 1 6 11 16 21 22 115 | 1 6 11 16 21 22 23 116 | 1 6 11 16 21 22 23 24 117 | 1 6 11 16 21 22 23 24 25 118 | -------------------------------------------------------------------------------- /original/stringedit.c: -------------------------------------------------------------------------------- 1 | /* stringedit.c 2 | 3 | Compute the optimal alignment matching two strings 4 | 5 | by: Steven Skiena 6 | begun: March 26, 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 31 | #include "editdistance.h" 32 | #include "bool.h" 33 | 34 | extern cell m[MAXLEN+1][MAXLEN+1]; /* dynamic programming table */ 35 | 36 | 37 | /******************************************************************/ 38 | 39 | /* For normal edit distance computation */ 40 | 41 | goal_cell(char *s, char *t, int *i, int *j) 42 | { 43 | *i = strlen(s) - 1; 44 | *j = strlen(t) - 1; 45 | } 46 | 47 | int match(char c, char d) 48 | { 49 | if (c == d) return(0); 50 | else return(1); 51 | } 52 | 53 | int indel(char c) 54 | { 55 | return(1); 56 | } 57 | 58 | row_init(int i) /* what is m[0][i]? */ 59 | { 60 | m[0][i].cost = i; 61 | if (i>0) 62 | m[0][i].parent = INSERT; 63 | else 64 | m[0][i].parent = -1; 65 | } 66 | 67 | column_init(int i) /* what is m[i][0]? */ 68 | { 69 | m[i][0].cost = i; 70 | if (i>0) 71 | m[i][0].parent = DELETE; 72 | else 73 | m[0][i].parent = -1; 74 | } 75 | 76 | /**********************************************************************/ 77 | 78 | match_out(char *s, char *t, int i, int j) 79 | { 80 | if (s[i] == t[j]) printf("M"); 81 | else printf("S"); 82 | } 83 | 84 | insert_out(char *t, int j) 85 | { 86 | printf("I"); 87 | } 88 | 89 | delete_out(char *s, int i) 90 | { 91 | printf("D"); 92 | } 93 | 94 | 95 | 96 | /**********************************************************************/ 97 | 98 | main(){ 99 | int i,j; 100 | char s[MAXLEN],t[MAXLEN]; /* input strings */ 101 | 102 | s[0] = t[0] = ' '; 103 | 104 | scanf("%s",&(s[1])); 105 | scanf("%s",&(t[1])); 106 | 107 | printf("matching cost = %d \n", string_compare(s,t, strlen(s)-1,strlen(t)-1)); 108 | 109 | print_matrix(s,t,TRUE); 110 | printf("\n"); 111 | print_matrix(s,t,FALSE); 112 | 113 | goal_cell(s,t,&i,&j); 114 | 115 | printf("%d %d\n",i,j); 116 | 117 | reconstruct_path(s,t,i,j); 118 | printf("\n"); 119 | } 120 | 121 | -------------------------------------------------------------------------------- /original/superman.c: -------------------------------------------------------------------------------- 1 | 2 | /* superman.c 3 | 4 | Compute Superman's flight path -- geometry example 5 | 6 | by: Steven Skiena 7 | begin: April 11, 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 | #define MAXN 100 /* maximum number of circles */ 32 | 33 | #include "bool.h" 34 | #include "geometry.h" 35 | #include 36 | 37 | point s; /* Superman's initial position */ 38 | point t; /* target position */ 39 | int ncircles; /* number of circles */ 40 | circle c[MAXN]; /* circles data structure */ 41 | 42 | superman() 43 | { 44 | line l; /* line from start to target position */ 45 | point close; /* closest point */ 46 | double d; /* distance from circle-center */ 47 | double xray = 0.0; /* length of intersection with circles */ 48 | double around = 0.0; /* length around circular arcs */ 49 | double angle; /* angle subtended by arc */ 50 | double travel; /* total travel distance */ 51 | int i; /* counter */ 52 | double asin(), sqrt(); 53 | double distance(); 54 | 55 | points_to_line(s,t,&l); 56 | 57 | for (i=1; i<=ncircles; i++) { 58 | closest_point(c[i].c,l,close); 59 | d = distance(c[i].c,close); 60 | if ((d>=0) && (d < c[i].r) && point_in_box(close,s,t)) { 61 | xray += 2*sqrt(c[i].r*c[i].r - d*d); 62 | angle = acos(d/c[i].r); 63 | around += ((2*angle)/(2*PI)) * (2*PI*c[i].r); 64 | printf("circle %d (%7.3lf,%7.3lf,%7.3lf) is %7.3lf away from l, xray=%7.3lf around=%7.3lf angle=%7.3lf.\n", 65 | i,c[i].c[X],c[i].c[Y],c[i].r,d,xray,around,angle); 66 | } 67 | } 68 | 69 | travel = distance(s,t) - xray + around; 70 | printf("Superman sees through %7.3lf units, and flies %7.3lf units\n", 71 | xray, travel); 72 | } 73 | 74 | 75 | main(){ 76 | int i; /* counter */ 77 | 78 | scanf("%lf %lf",&s[X],&s[Y]); 79 | scanf("%lf %lf",&t[X],&t[Y]); 80 | scanf("%d",&ncircles); 81 | for (i=1; i<=ncircles; i++) 82 | scanf("%lf %lf %lf",&c[i].c[X],&c[i].c[Y],&c[i].r); 83 | 84 | printf("%7.3lf %7.3lf\n",s[X],s[Y]); 85 | printf("%7.3lf %7.3lf\n",t[X],t[Y]); 86 | printf("%d\n",ncircles); 87 | for (i=1; i<=ncircles; i++) 88 | printf("%7.3lf %7.3lf %7.3lf\n",c[i].c[X],c[i].c[Y],c[i].r); 89 | 90 | superman(); 91 | } 92 | -------------------------------------------------------------------------------- /original/lcs.c: -------------------------------------------------------------------------------- 1 | /* lcs.c 2 | 3 | Longest common subsequence of two strings. 4 | 5 | by: Steven Skiena 6 | begun: March 26, 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 31 | #include "editdistance.h" 32 | #include "bool.h" 33 | 34 | extern cell m[MAXLEN+1][MAXLEN+1]; /* dynamic programming table */ 35 | 36 | 37 | /******************************************************************/ 38 | 39 | /* For normal edit distance computation */ 40 | 41 | goal_cell(char *s, char *t, int *i, int *j) 42 | { 43 | *i = strlen(s) - 1; 44 | *j = strlen(t) - 1; 45 | } 46 | 47 | int match(char c, char d) 48 | { 49 | if (c == d) return(0); 50 | else return(MAXLEN); 51 | } 52 | 53 | int indel(char c) 54 | { 55 | return(1); 56 | } 57 | 58 | row_init(int i) /* what is m[0][i]? */ 59 | { 60 | m[0][i].cost = i; 61 | if (i>0) 62 | m[0][i].parent = INSERT; 63 | else 64 | m[0][i].parent = -1; 65 | } 66 | 67 | column_init(int i) /* what is m[i][0]? */ 68 | { 69 | m[i][0].cost = i; 70 | if (i>0) 71 | m[i][0].parent = DELETE; 72 | else 73 | m[0][i].parent = -1; 74 | } 75 | 76 | /**********************************************************************/ 77 | 78 | match_out(char *s, char *t, int i, int j) 79 | { 80 | if (s[i] == t[j]) printf("%c",s[i]); 81 | } 82 | 83 | insert_out(char *t, int j) 84 | { 85 | } 86 | 87 | delete_out(char *s, int i) 88 | { 89 | } 90 | 91 | 92 | 93 | /**********************************************************************/ 94 | 95 | main(){ 96 | int i,j; 97 | int lcslen, complen; 98 | char s[MAXLEN],t[MAXLEN]; /* input strings */ 99 | 100 | s[0] = t[0] = ' '; 101 | 102 | scanf("%s",&(s[1])); 103 | scanf("%s",&(t[1])); 104 | 105 | complen = string_compare(s,t); 106 | lcslen = (strlen(s) + strlen(t) - 2 - complen) / 2; 107 | 108 | printf("length of longest common subsequence = %d\n", lcslen); 109 | 110 | /* 111 | print_matrix(s,t,TRUE); 112 | printf("\n"); 113 | print_matrix(s,t,FALSE); 114 | */ 115 | 116 | goal_cell(s,t,&i,&j); 117 | 118 | /* 119 | printf("%d %d\n",i,j); 120 | */ 121 | 122 | reconstruct_path(s,t,i,j); 123 | printf("\n"); 124 | } 125 | 126 | --------------------------------------------------------------------------------