-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
162 lines (135 loc) · 5.57 KB
/
main.java
File metadata and controls
162 lines (135 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//Thomas Setzler
//CSCI 310 Advanced Aglorithims
//GraphADT
import graphADT.weightedGraph;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class main {
public static void main(String[] args){
//size 10 graph
weightedGraph size10 = new weightedGraph();
size10.init(10,false);
size10 = populator(size10);
size10.graphPrint("size10.txt");
System.out.println("size 10 done");
//size 100
weightedGraph size100 = new weightedGraph();
size100.init(100,false);
size100 = populator(size100);
size100.graphPrint("size100.txt");
System.out.println("size 100 done");
long start = System.nanoTime();
System.out.println("Size 100 is Strongly Connected? " + stronglyConnected(size100));
long end = System.nanoTime();
long ellapsedTime = (end - start);
System.out.print("For size 100, the time(in nanoseconds) to compute strongly connected is: ");
System.out.println(ellapsedTime);
/*
// had to comment out size 10,000 and size 100,000 as my computer cannot handle that size of a data set
//size 10,000
weightedGraph size10000 = new weightedGraph();
size10000.init(10000,false);
size10000 = populator(size10000);
size10000.graphPrint("size10,000.txt");
System.out.println("size 10,000 done");
//size 100,000
weightedGraph size100000 = new weightedGraph();
size100000.init(100000,false);
size100000 = populator(size100000);
size100000.graphPrint("size100,000.txt");
System.out.println("size 100,000 done");
*/
//BIPARTITE GRAPH
int i;
weightedGraph bipartite = new weightedGraph();
bipartite.init(5,true);
for (i =0; i < 5; i++){
bipartite.setValue(i,i+1);//sets the value of each node to a value greater than one
}
bipartite.addEdge(0,1,5);
bipartite.addEdge(0,2,5);
bipartite.addEdge(1,3,4);
bipartite.addEdge(4,1,3);
bipartite.graphPrint("bipartiteGraph.txt");
System.out.println("DONE");
} //end main
public static weightedGraph populator(weightedGraph G){
//fills the graph with edges based on randomizations
//variables and random pools
Random rand = new Random();
Random randWeight = new Random();
int newWeight;
int size = G.getNodeCT();
int temp = 0;
int i = 0;
int j = 0;
for(i = 0; i < size; i++){
// loops through each node and assigns it edges to other nodes randomly
G.setValue(i,i);
for (j = 0; (j < (size/3 + (size /5))); j++) {
// gives each node a number of edges contigent on the size of the graph
temp = rand.nextInt(size);
while (temp == i) {
//prevents node from having an edge to itself
temp = rand.nextInt(size);
}//end while
newWeight = randWeight.nextInt(20); // generates a random weight
G.addEdge(i,temp,newWeight);
}//end J Loop
} // end I Loop
return G;
}// end populator
public static boolean stronglyConnected(weightedGraph G){
//returns true or false depending on if G is strongly connected
int size = G.getNodeCT();
boolean[] connected = new boolean[size];
int i = 0;
int[] tempNeighbors;
int j = 0;
int connectCT = 0;
boolean backtrack = false;
for(i =0; i< G.getNodeCT(); i++){
//clears array each itteration and resets values
tempNeighbors = new int[size];
tempNeighbors = G.getNeighbors(i);
backtrack = false;
if( i == 0){
//assumes that if node 0 is not connected, it cannot be strongly connected.
connected[i] = true;
for(j=0; j<tempNeighbors.length;j++){
connected[tempNeighbors[i]] = true;
}
}
else{
for(j =0; j < tempNeighbors.length; j++){
// iterates through the neighbors of each node and adds them to a list of booleans of connected nodes if one is connected
if(connected[tempNeighbors[j]] == true){
if(backtrack == false){
j = 0;
backtrack = true;
connected[i] = true;} // backtracks so entire list of neighbors can be added if it is determined they are connected
}
else if(backtrack == true) {
//once backtracking begins, adds each connected node to list
if ( connected[tempNeighbors[j]] != true){
connected[tempNeighbors[j]] = true;
}
}
}//end j loop
}//end i loop
}
//if connectedNodes = size of graph, then it is connected
for( i =0; i< connected.length;i++){
//checks the amount of trues in the connected array
if( connected[i] == true){
connectCT++;
}
}
if(connectCT == size){
return true;
}
else{
return false;
}
}//end StronglyConnected
}