Skip to content

Commit a9ca18c

Browse files
committed
Finished up the assignment and fixed a bug in transition table generation and Hopcroft minimization
1 parent 7022ace commit a9ca18c

19 files changed

+259
-722
lines changed

.idea/modules.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BFS.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/BreadthFirstSearch.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
import java.util.ArrayList;
3+
4+
class BreadthFirstSearch {
5+
private int[] shortestPath;
6+
7+
ArrayList<Integer> Bfs(int[][] transitionTable, int startIndex) {
8+
shortestPath = new int[transitionTable.length];
9+
for (int i = 0; i < shortestPath.length; i++) {
10+
shortestPath[i] = -1;
11+
}
12+
13+
createPaths(startIndex, null, transitionTable);
14+
15+
ArrayList<Integer> unreachable = new ArrayList<>();
16+
int max = Integer.MIN_VALUE;
17+
for (int j = 0; j < shortestPath.length; j++) {
18+
int currentPathLength = shortestPath[j];
19+
if (currentPathLength > max) {
20+
max = currentPathLength;
21+
}
22+
23+
if(currentPathLength == -1){
24+
unreachable.add(j);
25+
}
26+
}
27+
28+
System.out.println("Depth of tree is " + max);
29+
System.out.println("The dfsa has " + transitionTable.length + " states");
30+
31+
return unreachable;
32+
}
33+
34+
private void createPaths(int index, Path path, int[][] transitionTable) {
35+
if (path == null) {
36+
path = new Path();
37+
}
38+
39+
if (!path.checkVisited(index)) {
40+
path.addVisited(index);
41+
} else {
42+
return;
43+
}
44+
45+
if (shortestPath[index] == -1 || shortestPath[index] > path.getCurrentLength()) {
46+
shortestPath[index] = path.getCurrentLength();
47+
}
48+
49+
path.incrementLength();
50+
if (transitionTable[index][0] != -1) {
51+
createPaths(transitionTable[index][0], path, transitionTable);
52+
}
53+
if (transitionTable[index][1] != -1) {
54+
createPaths(transitionTable[index][1], path, transitionTable);
55+
}
56+
}
57+
}

src/Connection.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/NonBrokenHopefully/DFSA.java renamed to src/DFSA.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package NonBrokenHopefully;
21

32
import java.util.Random;
43

@@ -32,10 +31,6 @@ void generateTransitions(){
3231
transitions[i][2] = 1;
3332
}
3433
}
35-
36-
for(int i = 0; i < transitions.length; i++){
37-
System.out.println(i + " - a: " + transitions[i][0] + ", b: " + transitions[i][1]);
38-
}
3934
}
4035

4136
void setTransitions(int[][] transitions){

src/DataStructureAlgorithms2.iml

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/NonBrokenHopefully/Hopcrofts.java renamed to src/Hopcrofts.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package NonBrokenHopefully;
21

32
import java.util.ArrayList;
43
import java.util.ListIterator;
@@ -36,10 +35,11 @@ DFSA hopcroft(DFSA dfsa, int startState){
3635
// setDifferenceCalculation(P, W, x);
3736
}
3837

39-
int[][] newTransitionTable = createTransitionTable(P, dfsa.getTransitionTable());
38+
int[][] reconstructedTransitionTable = reconstructTransitionTable(P, dfsa.getTransitionTable());
4039
int newStartState = findStartState(P, startState);
41-
DFSA m = new DFSA(newTransitionTable.length, newStartState);
42-
m.setTransitions(newTransitionTable);
40+
41+
DFSA m = new DFSA(reconstructedTransitionTable.length, newStartState);
42+
m.setTransitions(reconstructedTransitionTable);
4343

4444
return m;
4545
}
@@ -129,40 +129,36 @@ private ArrayList<Integer> division(ArrayList<Integer> x, ArrayList<Integer> y){
129129
return division;
130130
}
131131

132-
//todo try fix this, then hopcroft works correctly
133-
private int[][] createTransitionTable(ArrayList<ArrayList<Integer>> P, int[][] originalTransitions){ //i don't think this works correctly
132+
private int[][] reconstructTransitionTable(ArrayList<ArrayList<Integer>> P, int[][] originalTransitions) {
134133
int[][] transitionTable = new int[P.size()][3];
135-
136-
for(int i = 0; i < P.size(); i++){
134+
for (int i = 0; i < P.size(); i++) {
137135
ArrayList<Integer> list = P.get(i);
138-
int newATransition = -1;
139-
int newBTransition = -1;
140-
for(int j = 0; j < list.size(); j++){
141-
int aTransition = originalTransitions[j][0];
142-
int bTransition = originalTransitions[j][1];
143-
144-
for(int k = 0; k < P.size(); k++){
145-
ArrayList<Integer> list_b = P.get(k);
146-
if(list_b.contains(aTransition)){
147-
newATransition = k;
148-
}
149136

150-
if(list_b.contains(bTransition)){
151-
newBTransition = k;
152-
}
137+
int newConnectionA = -1;
138+
int newConnectionB = -1;
153139

154-
transitionTable[k][0] = newATransition;
155-
transitionTable[k][1] = newBTransition;
140+
for (int node : list) {
141+
int oldConnectionA = originalTransitions[node][0];
142+
int oldConnectionB = originalTransitions[node][1];
143+
int TF = originalTransitions[node][2];
156144

157-
for(Integer integer: list_b){
158-
if(originalTransitions[integer][2] == 1) {
159-
transitionTable[k][2] = 1;
160-
}else{
161-
transitionTable[k][2] = 0;
162-
}
145+
if(TF == 1){
146+
transitionTable[i][2] = 1;
147+
}
148+
149+
for(int j = 0; j < P.size(); j++){
150+
if(P.get(j).contains(oldConnectionA)){
151+
newConnectionA = j;
152+
}
153+
154+
if(P.get(j).contains(oldConnectionB)){
155+
newConnectionB = j;
163156
}
164157
}
165158
}
159+
160+
transitionTable[i][0] = newConnectionA;
161+
transitionTable[i][1] = newConnectionB;
166162
}
167163

168164
return transitionTable;

0 commit comments

Comments
 (0)