forked from srinathr91/TestJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
187 lines (159 loc) · 5.23 KB
/
Graph.java
File metadata and controls
187 lines (159 loc) · 5.23 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
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
public class Graph {
// Rather than making an algorithm to reverse vertices, each graph maintains both
// the normal and reverse versions of itself.
private Map<Integer, Vertex> vertices;
private Map<Integer, Vertex> reversedVertices;
// List of the number of nodes found in each SCC
List<Integer> sccSizes;
// Variable to hold the running size of the current SCC
int components;
// List of vertices in the order they complete in a DFS pass on the reversed graph G
Stack<Vertex> finishingTimes;
/**
*
* @param vertices
*/
public Graph(Map<Integer,Vertex> vertices, Map<Integer,Vertex> reversedVertices) {
this.vertices = vertices;
this.reversedVertices = reversedVertices;
sccSizes = new ArrayList<Integer>();
finishingTimes = new Stack<Vertex>();
}
/**
* Constructs a graph directly from a text file (format specified in assignment)
*
* @param filePath a string with the full filePath with digraph representation (see assignment for spec)
* @throws IOException
*/
public Graph(String filePath) throws IOException {
// Both normal and reversed graphs will be constructed
Map<Integer, Vertex> vertices = new HashMap<Integer, Vertex>();
Map<Integer, Vertex> reversedVertices = new HashMap<Integer, Vertex>();
// Open necessary objects to read file contents
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = bufferedReader.readLine();
while(line != null) {
// Split each line into head and tail
String[] edge = line.split(" ");
int head = Integer.parseInt(edge[0]);
int tail = Integer.parseInt(edge[1]);
// In the reversed graph, the head and tail are switched
int headReversed = Integer.parseInt(edge[1]);
int tailReversed = Integer.parseInt(edge[0]);
// For both head and tail in normal and reversed graphs, if head or tail is not already a vertex
// add it to the graph.
if(!vertices.containsKey(head)) {
Vertex v = new Vertex(head);
vertices.put(head, v);
}
if(!reversedVertices.containsKey(headReversed)) {
Vertex vR = new Vertex(headReversed);
reversedVertices.put(headReversed, vR);
}
if(!vertices.containsKey(tail)) {
Vertex v2 = new Vertex(tail);
vertices.put(tail, v2);
}
if(!reversedVertices.containsKey(tailReversed)) {
Vertex v2R = new Vertex(tailReversed);
reversedVertices.put(tailReversed, v2R);
}
// Add the tail to the list of adjacent nodes for the head (construct the digraph connections)
vertices.get(head).addAdjacentNode(vertices.get(tail));
reversedVertices.get(headReversed).addAdjacentNode(reversedVertices.get(tailReversed));
line = bufferedReader.readLine(); // get the next line
}
bufferedReader.close(); // clean up
// Finally, set the graph
this.vertices = vertices;
this.reversedVertices = reversedVertices;
sccSizes = new ArrayList<Integer>();
finishingTimes = new Stack<Vertex>();
}
/**
*
* @return
*/
public Map<Integer, Vertex> getVertices() {
return vertices;
}
/**
*
* @param visited
*/
public void setAllVisited(boolean visited) {
for(Vertex n : this.getVertices().values()) {
n.setVisited(false);
}
}
public void setReversedGraph(Map<Integer, Vertex> nodesReversed) {
this.reversedVertices = nodesReversed;
}
public Map<Integer, Vertex> getReversedVertices() {
return this.reversedVertices;
}
public Stack<Vertex> depthFirstSearchLoop() {
for(Vertex v : this.getVertices().values()) {
if(!v.isVisited()) {
depthFirstSearch(v);
}
}
return this.finishingTimes;
}
public void depthFirstSearchLoop(Stack<Vertex> finishingTimes) {
Vertex v;
while(!finishingTimes.isEmpty()) {
v = finishingTimes.pop();
// TODO correct bug, fixed, but finishingTimes come in with references to nodes
// on reversed graphs. Have to convert to normal graph here
v = this.getVertices().get(v.getValue());
if(!v.isVisited()) {
components = 1;
this.depthFirstSearch(v);
sccSizes.add(components);
}
}
}
/**
*
* @param g
* @param v
*/
public void depthFirstSearch(Vertex v) {
v.setVisited(true);
for(Vertex adjacentVertex : v.getAdjacentVertices()) {
if(!adjacentVertex.isVisited()) {
components++;
this.depthFirstSearch(adjacentVertex);
}
}
finishingTimes.push(v);
}
/**
* Returns a sorted list (ascending order) with size equal to the number of strongly connected components
* (SCCs) in the directed graph (digraph) g. The value of each element is the number of vertices in the
* respective SCC.
*
* @param g
* @return
*/
public List<Integer> stronglyConnectedComponents() {
Graph gReversed = new Graph(this.reversedVertices, this.vertices);
Stack<Vertex> finishingTimes = gReversed.depthFirstSearchLoop();
gReversed.setAllVisited(false);
// This line fixed bug... Should be using a stack, not a queue, which is what unreversed list does
this.depthFirstSearchLoop(finishingTimes);
Collections.sort(sccSizes);
return sccSizes;
}
}