Skip to content

Commit b88724b

Browse files
committed
v1.0.3
1 parent 57bb630 commit b88724b

26 files changed

+302
-162
lines changed

.idea/workspace.xml

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

github/screenshots/5.PNG

1.27 MB
Loading

github/screenshots/6.PNG

1.32 MB
Loading

src/Algorithm_Visualizer.jar

-43.2 MB
Binary file not shown.

src/main/java/GUI.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ private void initGraphPanel() {
5757
*/
5858
private void chooseAlgNameActions() {
5959

60+
// Don't interupt an animation in progress
61+
if (gPanel.algThread != null && gPanel.algThread.isAlive()) {
62+
chooseAlgName.setSelectedItem(gPanel.algName);
63+
return;
64+
}
6065
String prevAlgName = gPanel.algName;
6166
gPanel.algName = (String) chooseAlgName.getSelectedItem();
6267
// Regenerate graph if needed, for the given algorithm
63-
if (Defs.isMinConnected.get(gPanel.algName) ^
64-
Defs.isMinConnected.get(prevAlgName)) {
65-
gPanel.isMinConnected = Defs.isMinConnected.get(gPanel.algName);
68+
if (Defs.isShortPathAlg.get(gPanel.algName) ^
69+
Defs.isShortPathAlg.get(prevAlgName)) {
70+
gPanel.isShortPathAlg = Defs.isShortPathAlg.get(gPanel.algName);
6671
GraphGenerator.generateGraph(gPanel);
6772
}
6873
gPanel.pauseAlgorithm();
@@ -79,6 +84,7 @@ private void chooseGraphSizeActions() {
7984
gPanel.graphSize = (String) chooseGraphSize.getSelectedItem();
8085
GraphGenerator.generateGraph(gPanel);
8186
gPanel.resetAnimation();
87+
8288
}
8389

8490

@@ -147,7 +153,6 @@ public static void main(String[] args) {
147153
}
148154

149155

150-
// TODO: -WEAKEN THE COUPLING OF ALL CLASSES.
156+
// TODO: -WEAKEN THE COUPLING OF ALG CLASSES.
151157
// -REFACTOR ENCAPSULATION OF PROJECT.
152-
// -ADD ERROR CHECKING/
153-
// -ONCE ALG ANIMATION FINISHES, CANT RESTART DIRECTLY FROM START BUTTON
158+
// -ADD MORE ERROR CHECKING.

src/main/java/GraphGenerator.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import main.java.util.Defs;
44
import org.jgrapht.graph.DefaultUndirectedWeightedGraph;
55
import org.jgrapht.graph.DefaultWeightedEdge;
6-
import org.jgrapht.graph.SimpleWeightedGraph;
76

87
import java.awt.*;
98
import java.awt.geom.Ellipse2D;
@@ -18,7 +17,7 @@
1817
public class GraphGenerator extends DefaultWeightedEdge {
1918

2019
/**
21-
* @param graph {@link SimpleWeightedGraph} to check for connectivity.
20+
* @param graph {@link DefaultUndirectedWeightedGraph} to check for connectivity.
2221
* @return True if {@code graph} is connected, otherwise false.
2322
*/
2423
private static boolean isConnected(DefaultUndirectedWeightedGraph<Integer,
@@ -31,20 +30,20 @@ private static boolean isConnected(DefaultUndirectedWeightedGraph<Integer,
3130
if (vertices.size() - 1 > graph.edgeSet().size()) return false;
3231

3332
Set<Integer> visited = new HashSet<>();
34-
Deque<DefaultWeightedEdge> queue = new LinkedList<>();
33+
Deque<DefaultWeightedEdge> queue = new ArrayDeque<>();
3534
Integer startNode = vertices.iterator().next();
3635

36+
// BFS to determine connectivity
3737
visited.add(startNode);
3838
graph.edgesOf(startNode).forEach(queue::addLast);
3939
while (visited.size() != vertices.size() && queue.size() != 0) {
40-
DefaultWeightedEdge currentEdge = queue.pollFirst();
41-
Integer source = graph.getEdgeSource(currentEdge);
42-
Integer dest = graph.getEdgeTarget(currentEdge);
43-
44-
Integer targetNode = visited.contains(source) ? dest : source;
45-
if (!visited.contains(targetNode)) {
46-
visited.add(targetNode);
47-
graph.edgesOf(targetNode).forEach(queue::addLast);
40+
DefaultWeightedEdge edge = queue.pollFirst();
41+
Integer source = graph.getEdgeSource(edge);
42+
Integer target = graph.getEdgeTarget(edge);
43+
Integer adjNode = visited.contains(source) ? target : source;
44+
if (!visited.contains(adjNode)) {
45+
visited.add(adjNode);
46+
graph.edgesOf(adjNode).forEach(queue::addLast);
4847
}
4948
}
5049
return visited.size() == vertices.size();
@@ -58,7 +57,8 @@ private static boolean isConnected(DefaultUndirectedWeightedGraph<Integer,
5857
* @param gPanel A {@link GraphPanel} that has all graph metadata.
5958
* @throws IllegalArgumentException If {@code graphSize} is null.
6059
*/
61-
public static void generateGraph(GraphPanel gPanel) {
60+
public static void generateGraph(GraphPanel gPanel)
61+
throws IllegalArgumentException {
6262

6363
if (gPanel.graphSize == null) {
6464
throw new IllegalArgumentException("ERROR: GraphPanel not initialized.");
@@ -69,18 +69,17 @@ public static void generateGraph(GraphPanel gPanel) {
6969

7070
gPanel.nodeCoords = new HashMap<>(gPanel.nodeCount);
7171
gPanel.nodeShapes = new HashMap<>(gPanel.nodeCount);
72-
gPanel.visited = new boolean[gPanel.nodeCount];
72+
gPanel.visitedEdges = new HashSet<>(gPanel.nodeCount);
7373
gPanel.path = new int[gPanel.nodeCount];
7474
Arrays.fill(gPanel.path, Integer.MAX_VALUE);
7575

7676
DefaultUndirectedWeightedGraph<Integer, DefaultWeightedEdge> graph =
7777
new DefaultUndirectedWeightedGraph<>(DefaultWeightedEdge.class);
7878
for (int i = 0; i < gPanel.nodeCount; i++) graph.addVertex(i);
7979

80-
// Generate either min. connected graph or a more complete graph.
80+
// Generate either approx. min. connected graph or a more complete graph.
8181
Random rand = new Random();
82-
if (gPanel.isMinConnected) {
83-
System.out.println("TEST1");
82+
if (gPanel.isShortPathAlg) {
8483
do {
8584
int node = rand.nextInt(gPanel.nodeCount);
8685
int adjNode = rand.nextInt(gPanel.nodeCount);
@@ -89,8 +88,10 @@ public static void generateGraph(GraphPanel gPanel) {
8988
}
9089
while (!isConnected(graph) && graph.vertexSet().size() <= gPanel.nodeCount);
9190
} else {
92-
System.out.println("TEST2");
93-
int maxEdges = gPanel.nodeCount * 6;
91+
int maxEdges;
92+
if (gPanel.graphSize.equals("Small")) maxEdges = gPanel.nodeCount * 2;
93+
else maxEdges = gPanel.nodeCount * 5;
94+
9495
do {
9596
int node = rand.nextInt(gPanel.nodeCount);
9697
int adjNode = rand.nextInt(gPanel.nodeCount);

0 commit comments

Comments
 (0)