24 | * Translate from subsets.c.
25 | *
26 | * @author csong2022
27 | */
28 | public class Subsets implements BacktrackCallback
25 | * Translate from binomial.c.
26 | *
27 | * @author csong2022
28 | */
29 | public class Binomial {
30 | public static long binomialCoefficient(int n, int m) {
31 | if (m > n) {
32 | throw new IllegalArgumentException("m > n");
33 | }
34 |
35 | long[][] bc = new long[n + 1][n + 1]; /* table of binomial coefficient values */
36 |
37 | for (int i = 0; i <= n; i++) bc[i][0] = 1;
38 | for (int j = 0; j <= n; j++) bc[j][j] = 1;
39 |
40 | for (int i = 1; i <= n; i++)
41 | for (int j = 1; j < i; j++)
42 | bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j];
43 |
44 | return bc[n][m];
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/dp/EditBrute.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2003 by Steven S. Skiena; all rights reserved.
3 |
4 | Permission is granted for use in non-commercial applications
5 | provided this copyright notice remains intact and unchanged.
6 |
7 | This program appears in my book:
8 |
9 | "Programming Challenges: The Programming Contest Training Manual"
10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
11 |
12 | See our website www.programming-challenges.com for additional information.
13 |
14 | This book can be ordered from Amazon.com at
15 |
16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
17 |
18 | */
19 | package com.algorist.dp;
20 |
21 | /**
22 | * Compute string edit distance *without* dynamic programming!
23 | *
24 | * Translate from editbrute.c.
25 | *
26 | * @author csong2022
27 | */
28 | public class EditBrute extends EditDistance {
29 |
30 | public EditBrute(StringEdit stringEdit) {
31 | super(stringEdit);
32 | }
33 |
34 | public int stringCompare(String s, String t, int i, int j) {
35 | int[] opt = new int[3]; /* cost of the three options */
36 | int lowest_cost; /* lowest cost */
37 |
38 | if (i == 0) return j * indel(' ');
39 | if (j == 0) return i * indel(' ');
40 |
41 | opt[MATCH] = stringCompare(s, t, i - 1, j - 1) + match(s.charAt(i), t.charAt(j));
42 | opt[INSERT] = stringCompare(s, t, i, j - 1) + indel(t.charAt(j));
43 | opt[DELETE] = stringCompare(s, t, i - 1, j) + indel(s.charAt(i));
44 |
45 | lowest_cost = opt[MATCH];
46 | for (int k = INSERT; k <= DELETE; k++)
47 | if (opt[k] < lowest_cost) lowest_cost = opt[k];
48 |
49 | m[i][j] = new Cell(lowest_cost, -1); /* REMOVE FROM PRINTED VERSION */
50 |
51 | return lowest_cost;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/dp/LCS.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2003 by Steven S. Skiena; all rights reserved.
3 |
4 | Permission is granted for use in non-commercial applications
5 | provided this copyright notice remains intact and unchanged.
6 |
7 | This program appears in my book:
8 |
9 | "Programming Challenges: The Programming Contest Training Manual"
10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
11 |
12 | See our website www.programming-challenges.com for additional information.
13 |
14 | This book can be ordered from Amazon.com at
15 |
16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
17 |
18 | */
19 | package com.algorist.dp;
20 |
21 | import static com.algorist.dp.EditDistance.MAXLEN;
22 |
23 | /**
24 | * Longest common subsequence of two strings.
25 | *
26 | * Generify from lcs.c.
27 | *
28 | * @author csong2022
29 | */
30 | public class LCS extends StringEdit {
31 |
32 | @Override
33 | int match(char c, char d) {
34 | return (c == d) ? 0 : MAXLEN;
35 | }
36 |
37 | @Override
38 | void matchOut(String s, String t, int i, int j) {
39 | if (s.charAt(i) == t.charAt(j)) System.out.printf("%c", s.charAt(i));
40 | }
41 |
42 | @Override
43 | void insertOut(String t, int j) {
44 | }
45 |
46 | @Override
47 | void deleteOut(String s, int i) {
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/dp/SubStringEdit.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2003 by Steven S. Skiena; all rights reserved.
3 |
4 | Permission is granted for use in non-commercial applications
5 | provided this copyright notice remains intact and unchanged.
6 |
7 | This program appears in my book:
8 |
9 | "Programming Challenges: The Programming Contest Training Manual"
10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
11 |
12 | See our website www.programming-challenges.com for additional information.
13 |
14 | This book can be ordered from Amazon.com at
15 |
16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
17 |
18 | */
19 | package com.algorist.dp;
20 |
21 | /**
22 | * Approximately match one string as a substring of another, where is s in t?
23 | *
24 | * Generify from stringedit.c.
25 | *
26 | * @author csong2022
27 | */
28 | public class SubStringEdit extends StringEdit {
29 |
30 | int[] goalCell(String s, String t, EditDistance.Cell[][] m) {
31 | int i = s.length() - 1;
32 | int j = 0;
33 |
34 | for (int k = 1; k < t.length(); k++)
35 | if (m[i][k].cost < m[i][j].cost) j = k;
36 |
37 | return new int[]{i, j};
38 | }
39 |
40 | void rowInit(int i, EditDistance.Cell[][] m) { /* what is m[0][i]? */
41 | m[0][i] = new EditDistance.Cell(0, -1);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/geometry/Distance.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2003 by Steven S. Skiena; all rights reserved.
3 |
4 | Permission is granted for use in non-commercial applications
5 | provided this copyright notice remains intact and unchanged.
6 |
7 | This program appears in my book:
8 |
9 | "Programming Challenges: The Programming Contest Training Manual"
10 | by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
11 |
12 | See our website www.programming-challenges.com for additional information.
13 |
14 | This book can be ordered from Amazon.com at
15 |
16 | http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
17 |
18 | */
19 | package com.algorist.geometry;
20 |
21 | import static java.lang.Math.sqrt;
22 |
23 | /**
24 | * Compute Euclidian distances.
25 | *
26 | * Translate from distance.c.
27 | *
28 | * @author csong2022
29 | */
30 | public class Distance {
31 |
32 | public static double distance(double[] a, double[] b) {
33 | if (a.length < 1) {
34 | throw new IllegalArgumentException("a vector is empty.");
35 | }
36 | if (b.length < 1) {
37 | throw new IllegalArgumentException("b vector is empty.");
38 | }
39 | if (a.length != b.length) {
40 | throw new IllegalArgumentException("Two vectors have different dimension.");
41 | }
42 |
43 | double d = 0.0;
44 |
45 | for (int i = 0; i < a.length; i++)
46 | d += (a[i] - b[i]) * (a[i] - b[i]);
47 |
48 | return sqrt(d);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/graph/AbstractGraphSearch.java:
--------------------------------------------------------------------------------
1 | package com.algorist.graph;
2 |
3 | /**
4 | * Common graph search code between BFS and DFS.
5 | *
6 | * @param
24 | * Translate from connected.c.
25 | *
26 | * @author csong2022
27 | */
28 | public class Connected
6 | * Generify from edgenode type in graph.h.
7 | *
8 | * @author csong2022
9 | */
10 | public interface EdgeNode {
11 | /**
12 | * @return adjacent vertex.
13 | */
14 | int y(); /* adjancency info */
15 |
16 | /**
17 | * Create a copy of edge node for given vertex.
18 | *
19 | * @param v vertex.
20 | * @return copy of edge node for given vertex.
21 | */
22 | EdgeNode copy(int v);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/graph/GraphReader.java:
--------------------------------------------------------------------------------
1 | package com.algorist.graph;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Graph reader.
7 | *
8 | * @param
6 | * Generify the customization point for graph traversal.
7 | *
8 | * @param
26 | * Translate from primes.c.
27 | *
28 | * @author csong2022
29 | */
30 | public class Primes {
31 |
32 | public static void primeFactorization(final long x) {
33 | long c = x; /* remaining product to factor */
34 | while ((c % 2) == 0) {
35 | System.out.println(2);
36 | c /= 2;
37 | }
38 |
39 | long i = 3; /* counter */
40 | while (i <= (sqrt(c) + 1)) {
41 | if ((c % i) == 0) {
42 | System.out.println(i);
43 | c /= i;
44 | } else
45 | i += 2;
46 | }
47 |
48 | if (c > 1) System.out.println(c);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/algorist/utils/IterableUtils.java:
--------------------------------------------------------------------------------
1 | package com.algorist.utils;
2 |
3 | import java.util.Iterator;
4 |
5 | import static com.algorist.datastructure.ArrayUtils.newArray;
6 |
7 | /**
8 | * Iterable utils.
9 | *
10 | * @author csong2022
11 | */
12 | public class IterableUtils {
13 | /**
14 | * Format iterable to single line.
15 | *
16 | * @param iterable iterable.
17 | * @param