Skip to content

Commit 9aa114a

Browse files
committed
Add backtracking and stack related algorithms.
1 parent 1515deb commit 9aa114a

File tree

7 files changed

+540
-0
lines changed

7 files changed

+540
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.sean.backtracking;
2+
3+
/**
4+
* Created by Sean on 6/4/17.
5+
*/
6+
public class AllSubsets extends BackTrack {
7+
8+
public static void main(String[] args) {
9+
int[] a = new int[10];
10+
11+
AllSubsets as = new AllSubsets();
12+
as.backTrack(a, 0, 3);
13+
}
14+
15+
@Override
16+
protected void unmakeMove(int[] solutionVec, int pos, int size) {
17+
18+
}
19+
20+
@Override
21+
protected void makeMove(int[] solutionVec, int pos, int size) {
22+
23+
}
24+
25+
@Override
26+
protected int constructCandidates(int[] solutionVec, int pos, int size, int[] outCandidates) {
27+
outCandidates[0] = 1;
28+
outCandidates[1] = 0;
29+
30+
return 2;
31+
}
32+
33+
@Override
34+
protected void processSolution(int[] solutionVec, int pos, int inputSize) {
35+
System.out.print("{");
36+
for (int i = 1; i <= pos; i++) {
37+
if (solutionVec[i] == 1) {
38+
System.out.print(String.format(" %d", i));
39+
}
40+
}
41+
System.out.print(" }\n");
42+
}
43+
44+
@Override
45+
protected boolean isASolution(int[] solutionVec, int pos, int inputSize) {
46+
return pos == inputSize;
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.sean.backtracking;
2+
3+
/**
4+
* Created by Sean on 6/4/17.
5+
*/
6+
public abstract class BackTrack {
7+
public static boolean finished;
8+
9+
public void backTrack(int[] solutionVec, int pos, int inputSize) {
10+
int[] outCandidates = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
11+
int nCandidate;
12+
13+
if (isASolution(solutionVec, pos, inputSize)) {
14+
processSolution(solutionVec, pos, inputSize);
15+
} else {
16+
pos += 1;
17+
18+
nCandidate = constructCandidates(solutionVec, pos, inputSize, outCandidates);
19+
20+
for (int i = 0; i < nCandidate; i++) {
21+
solutionVec[pos] = outCandidates[i];
22+
23+
makeMove(solutionVec, pos, inputSize);
24+
backTrack(solutionVec, pos, inputSize);
25+
unmakeMove(solutionVec, pos, inputSize);
26+
27+
if (finished) // terminate early
28+
return;
29+
}
30+
}
31+
32+
}
33+
34+
protected abstract void unmakeMove(int[] solutionVec, int pos, int size);
35+
36+
protected abstract void makeMove(int[] solutionVec, int pos, int size);
37+
38+
protected abstract int constructCandidates(int[] solutionVec, int pos, int size, int[] outCandidates);
39+
40+
protected abstract void processSolution(int[] solutionVec, int pos, int inputSize);
41+
42+
protected abstract boolean isASolution(int[] solutionVec, int pos, int inputSize);
43+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.sean.backtracking;
2+
3+
/**
4+
* Created by Sean on 6/4/17.
5+
*/
6+
public class Permutation extends BackTrack {
7+
public static void main(String[] args) {
8+
int[] vec = new int[10];
9+
new Permutation().backTrack(vec, 0, 3);
10+
}
11+
12+
@Override
13+
protected void unmakeMove(int[] solutionVec, int pos, int size) {
14+
15+
}
16+
17+
@Override
18+
protected void makeMove(int[] solutionVec, int pos, int size) {
19+
20+
}
21+
22+
@Override
23+
protected int constructCandidates(int[] solutionVec, int pos, int size, int[] outCandidates) {
24+
int[] inPerm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
25+
for (int i = 0; i < inPerm.length; i++) {
26+
inPerm[i] = 0;
27+
}
28+
29+
for (int j = 0; j < pos; j++) {
30+
inPerm[solutionVec[j]] = 1;
31+
}
32+
33+
int nCount = 0;
34+
for (int i = 0; i <= size; i++) {
35+
if (inPerm[i] == 0) {
36+
outCandidates[nCount] = i;
37+
38+
nCount += 1;
39+
}
40+
}
41+
return nCount;
42+
}
43+
44+
@Override
45+
protected void processSolution(int[] solutionVec, int pos, int inputSize) {
46+
for (int i = 1; i <= pos; i++) {
47+
System.out.print(String.format(" %d", solutionVec[i]));
48+
}
49+
System.out.println("");
50+
}
51+
52+
@Override
53+
protected boolean isASolution(int[] solutionVec, int pos, int inputSize) {
54+
return pos == inputSize;
55+
}
56+
}

0 commit comments

Comments
 (0)