3
3
import main .java .util .Defs ;
4
4
import org .jgrapht .graph .DefaultUndirectedWeightedGraph ;
5
5
import org .jgrapht .graph .DefaultWeightedEdge ;
6
- import org .jgrapht .graph .SimpleWeightedGraph ;
7
6
8
7
import java .awt .*;
9
8
import java .awt .geom .Ellipse2D ;
18
17
public class GraphGenerator extends DefaultWeightedEdge {
19
18
20
19
/**
21
- * @param graph {@link SimpleWeightedGraph } to check for connectivity.
20
+ * @param graph {@link DefaultUndirectedWeightedGraph } to check for connectivity.
22
21
* @return True if {@code graph} is connected, otherwise false.
23
22
*/
24
23
private static boolean isConnected (DefaultUndirectedWeightedGraph <Integer ,
@@ -31,20 +30,20 @@ private static boolean isConnected(DefaultUndirectedWeightedGraph<Integer,
31
30
if (vertices .size () - 1 > graph .edgeSet ().size ()) return false ;
32
31
33
32
Set <Integer > visited = new HashSet <>();
34
- Deque <DefaultWeightedEdge > queue = new LinkedList <>();
33
+ Deque <DefaultWeightedEdge > queue = new ArrayDeque <>();
35
34
Integer startNode = vertices .iterator ().next ();
36
35
36
+ // BFS to determine connectivity
37
37
visited .add (startNode );
38
38
graph .edgesOf (startNode ).forEach (queue ::addLast );
39
39
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 );
48
47
}
49
48
}
50
49
return visited .size () == vertices .size ();
@@ -58,7 +57,8 @@ private static boolean isConnected(DefaultUndirectedWeightedGraph<Integer,
58
57
* @param gPanel A {@link GraphPanel} that has all graph metadata.
59
58
* @throws IllegalArgumentException If {@code graphSize} is null.
60
59
*/
61
- public static void generateGraph (GraphPanel gPanel ) {
60
+ public static void generateGraph (GraphPanel gPanel )
61
+ throws IllegalArgumentException {
62
62
63
63
if (gPanel .graphSize == null ) {
64
64
throw new IllegalArgumentException ("ERROR: GraphPanel not initialized." );
@@ -69,18 +69,17 @@ public static void generateGraph(GraphPanel gPanel) {
69
69
70
70
gPanel .nodeCoords = new HashMap <>(gPanel .nodeCount );
71
71
gPanel .nodeShapes = new HashMap <>(gPanel .nodeCount );
72
- gPanel .visited = new boolean [ gPanel .nodeCount ] ;
72
+ gPanel .visitedEdges = new HashSet <>( gPanel .nodeCount ) ;
73
73
gPanel .path = new int [gPanel .nodeCount ];
74
74
Arrays .fill (gPanel .path , Integer .MAX_VALUE );
75
75
76
76
DefaultUndirectedWeightedGraph <Integer , DefaultWeightedEdge > graph =
77
77
new DefaultUndirectedWeightedGraph <>(DefaultWeightedEdge .class );
78
78
for (int i = 0 ; i < gPanel .nodeCount ; i ++) graph .addVertex (i );
79
79
80
- // Generate either min. connected graph or a more complete graph.
80
+ // Generate either approx. min. connected graph or a more complete graph.
81
81
Random rand = new Random ();
82
- if (gPanel .isMinConnected ) {
83
- System .out .println ("TEST1" );
82
+ if (gPanel .isShortPathAlg ) {
84
83
do {
85
84
int node = rand .nextInt (gPanel .nodeCount );
86
85
int adjNode = rand .nextInt (gPanel .nodeCount );
@@ -89,8 +88,10 @@ public static void generateGraph(GraphPanel gPanel) {
89
88
}
90
89
while (!isConnected (graph ) && graph .vertexSet ().size () <= gPanel .nodeCount );
91
90
} 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
+
94
95
do {
95
96
int node = rand .nextInt (gPanel .nodeCount );
96
97
int adjNode = rand .nextInt (gPanel .nodeCount );
0 commit comments