-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphProcessor.java
317 lines (283 loc) · 7.32 KB
/
GraphProcessor.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
/**
* @author Benjamin Meeder
* @author Adam De Gala
*/
public class GraphProcessor
{
private class Vertex
{
public String name;
boolean visited = false; //All vertices start out not visited.
ArrayList<String> outEdges = new ArrayList<>();
protected Vertex(String name)
{
this.name = name;
}
public void add(String add)
{
outEdges.add(add);
}
}
private class Graph
{
public HashMap<String, Vertex> GraphList = new HashMap<>();
public int size;
public Graph(int size)
{
this.size = size;
}
public void add(String start, String end)
{
if(GraphList.containsKey(start))
GraphList.get(start).add(end);
else
{
//We need to a new vertex
GraphList.put(start, new Vertex(start));
GraphList.get(start).add(end);
}
if(!GraphList.containsKey(end))
GraphList.put(end,new Vertex(end));
}
public void addSCC(String start, String end)
{
if(GraphList.containsKey(start))
GraphList.get(start).add(end); //Get the start vertex and add end to its list of edge
else
{
GraphList.put(start, new Vertex(start));
GraphList.get(start).add(end);
}
}
public int outDegree(String v)
{
if(GraphList.containsKey(v))
return GraphList.get(v).outEdges.size();
else
return -1;
}
public void DFSprint(String name, String sccKey) {
//Find Correct Vertex
Vertex v = GraphList.get(name);
System.out.print(v.name + " ");
v.visited = true;
sccGraph.addSCC(sccKey, v.name);
for(String e : v.outEdges)
{
Vertex f = GraphList.get(e);
if(!f.visited)
DFSprint(f.name, sccKey);
}
}
public boolean beenVisited(String name) {
Vertex v = GraphList.get(name);
return v.visited;
}
public void printGraph() {
Set<String> keys = GraphList.keySet();
for(String e : keys)
{
Vertex v = GraphList.get(e);
System.out.print(v.name + " ");
for(String f : v.outEdges)
{
System.out.print(f + " ");
}
System.out.println();
System.out.println();
}
}
}
Graph graph;
Graph grGraph;
Graph sccGraph;
public GraphProcessor (String graphData)
{
//Builds Graph
try
{
File file = new File(graphData);
Scanner in = new Scanner(file);
int size = in.nextInt();
graph = new Graph(size );
//Question: This is O(n^2) to build graph, any way better?
//I could sort it and make it nlogn
while(in.hasNextLine())
{
String start = in.next();
String end = in.next();
if(in.hasNextLine())
in.nextLine(); //Move Scanner to next line
graph.add(start, end);
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
computeSCCGraph();
}
public int outDegree(String v)
{
return graph.outDegree(v);
}
public boolean sameComponent(String u, String v)
{
Set<String> keys = sccGraph.GraphList.keySet();
for(String e : keys)
{
Vertex vertex = sccGraph.GraphList.get(e);
if(vertex.outEdges.contains(u) && vertex.outEdges.contains(v))
return true;
}
return false;
}
public ArrayList<String> componentVertices(String v) {
ArrayList<String> out = new ArrayList<>();
Set<String> keys = sccGraph.GraphList.keySet();
for(String e : keys)
{
Vertex vertex = sccGraph.GraphList.get(e);
if(vertex.outEdges.contains(v))
out.addAll(vertex.outEdges);
}
return out;
}
public int largestComponents()
{
int max = 0;
Set<String> keys = sccGraph.GraphList.keySet();
for(String e : keys)
{
Vertex v = sccGraph.GraphList.get(e);
if(v.outEdges.size() > max)
max = v.outEdges.size();
}
return max;
}
public int numComponents()
{
return sccGraph.GraphList.size();
}
public ArrayList<String> bfsPath(String u, String v)
{
Set<String> keys = graph.GraphList.keySet();
for(String e : keys)
{
graph.GraphList.get(e).visited = false; //Make sure every node starts as unchecked.
}
LinkedList<pathHolder> queue = new LinkedList<>();
graph.GraphList.get(u).visited = true;
ArrayList<String> path = new ArrayList<>();
path.add(u);
queue.add(new pathHolder(u,path));
while(!queue.isEmpty())
{
pathHolder p = queue.poll();
Vertex vertex = graph.GraphList.get(p.name);
for(String e : vertex.outEdges)
{
ArrayList<String> newPath = new ArrayList<>();
newPath.addAll(p.path);
newPath.add(e);
if(e.equals(v))
return newPath;
else
queue.add(new pathHolder(e,newPath));
}
}
return new ArrayList<>();
}
private class pathHolder
{
public pathHolder(String name, ArrayList<String> path)
{
this.name = name;
this.path = path;
}
public String name;
public ArrayList<String> path;
}
private void computeSCCGraph()
{
//It is built into the vertex class to be unvisited at start
//Get Finishes times.
Stack<Vertex> finishTimes = new Stack<>(); //Question: Can we use this Stack
Set<String> keys = graph.GraphList.keySet();
for(String e : keys)
{
if(!graph.beenVisited(e))
{
DFSordering(e, finishTimes);
}
}
grGraph = new Graph(graph.size);
sccGraph = new Graph(graph.size); //Create the two addition graphs
computeGR(); //Compute GR from graph;
//System.out.println("Size of stack is " + finishTimes.size());
//Now we have the finish times
while(finishTimes.size() != 0)
{
Vertex v = finishTimes.pop();//Find corresponding vertex in GR
if(!grGraph.beenVisited(v.name))
{
grGraph.DFSprint(v.name, v.name);
System.out.println();
System.out.println();
}
}
}
private void DFSordering(String key, Stack<Vertex> finishTimes)
{
Vertex v = graph.GraphList.get(key);
graph.GraphList.get(key).visited = true;
for(String e : v.outEdges)
{
Vertex f = graph.GraphList.get(e);
if(!f.visited)
DFSordering(f.name, finishTimes);
}
finishTimes.push(v); //After all vertexs are processed, push the graph on.
//System.out.println("Size of stack is " + finishTimes.size());
}
//Compute GR (Flipped directions)
private void computeGR()
{
Set<String> keys = graph.GraphList.keySet();
for(String e : keys)
{
Vertex v = graph.GraphList.get(e);
for(String f : v.outEdges)
{
grGraph.add(f, v.name);
}
}
}
public String generateReport() {
int max = 0;
String maxString = "";
Set<String> keys = graph.GraphList.keySet();
for(String e : keys)
{
int current = outDegree(e);
if(current > max) {
max = current;
maxString = e;
}
}
String out = "Highest out degree: " + maxString + "\n";
out += Integer.toString(max) + '\n';
out += "Number of components of the graph: " + Integer.toString(numComponents()) + "\n";
out += "Size of the largest component: " + Integer.toString(largestComponents()) + "\n";
out += "";
return out;
}
}