forked from tushartushar/DesigniteJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
174 lines (149 loc) · 5.59 KB
/
Graph.java
File metadata and controls
174 lines (149 loc) · 5.59 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
package Designite.utils.models;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Graph {
private List<Vertex> vertices = new ArrayList<>();
private Map<Vertex, List<Vertex>> adjacencyList = new HashMap<>();
private Map<Vertex, List<Vertex>> directedAdjacencyList = new HashMap<>();
private Map<Vertex, List<Vertex>> reversedDirectedAdjacencyList = new HashMap<>();
private Map<Vertex, Boolean> visitedVertices = new HashMap<>();
private List<List<Vertex>> connectedComponents = new ArrayList<>();
private Map<Vertex, List<Vertex>> connectedComponnentsMapping = new HashMap<>();
private List<List<Vertex>> stronglyConnectedComponents = new ArrayList<>();
private Map<Vertex, List<Vertex>> stronglyConnectedComponnentsMapping = new HashMap<>();
private List<Vertex> helperVertexList = new ArrayList<>();
public void computeConnectedComponents() {
initializeVisitedVerices();
for (Vertex vertex : vertices) {
if (!visitedVertices.get(vertex)) {
List<Vertex> connectedComponent = new ArrayList<>();
depthFirstSearch(connectedComponent, vertex, GraphAlingment.UNDIRECTED);
connectedComponents.add(connectedComponent);
updateMapping(connectedComponnentsMapping, connectedComponent);
}
}
}
public void computeStronglyConnectedComponents() {
reversePassDFS();
straightPassDFS();
}
public void reversePassDFS() {
initializeVisitedVerices();
for (int i = vertices.size()-1; i >= 0 ; i--) {
if (!visitedVertices.get(vertices.get(i))) {
depthFirstSearch(new ArrayList<>(), vertices.get(i), GraphAlingment.REVERSE_DIRECTED);
}
}
}
public void straightPassDFS() {
initializeVisitedVerices();
for (int i = vertices.size()-1; i >= 0 ; i--) {
if (!visitedVertices.get(helperVertexList.get(i))) {
List<Vertex> stronglyConnectedComponent = new ArrayList<>();
depthFirstSearch(stronglyConnectedComponent, helperVertexList.get(i), GraphAlingment.DIRECTED);
stronglyConnectedComponents.add(stronglyConnectedComponent);
updateMapping(stronglyConnectedComponnentsMapping, stronglyConnectedComponent);
}
}
}
private void initializeVisitedVerices() {
for (Vertex vertex : vertices) {
visitedVertices.put(vertex, false);
}
}
private void updateMapping(Map<Vertex, List<Vertex>> mapping, List<Vertex> component) {
for (Vertex vertex : component) {
mapping.put(vertex, component);
}
}
private void depthFirstSearch(List<Vertex> connectedComponent, Vertex vertex, GraphAlingment align) {
visitedVertices.put(vertex, true);
if (align != GraphAlingment.REVERSE_DIRECTED) {
connectedComponent.add(vertex);
}
for (Vertex adjacentVertex : getAdjacentVertices(vertex, align)) {
if (!visitedVertices.get(adjacentVertex)) {
depthFirstSearch(connectedComponent, adjacentVertex, align);
}
}
if (align == GraphAlingment.REVERSE_DIRECTED) {
helperVertexList.add(vertex);
}
}
public void addVertex(Vertex vertex) {
if (!vertices.contains(vertex)) {
initializeVertex(vertex);
}
}
public void addEdge(Edge edge) {
initializeEdge(edge);
addDirectedEdge(edge);
addUndirectedEdge(edge);
}
private void initializeEdge(Edge edge) {
if (!adjacencyList.containsKey(edge.getFirstVertex())) {
initializeVertex(edge.getFirstVertex());
}
if (!adjacencyList.containsKey(edge.getSecondVertex())) {
initializeVertex(edge.getSecondVertex());
}
}
private void initializeVertex(Vertex vertex) {
adjacencyList.put(vertex, new ArrayList<>());
directedAdjacencyList.put(vertex, new ArrayList<>());
reversedDirectedAdjacencyList.put(vertex, new ArrayList<>());
vertices.add(vertex);
}
private void addDirectedEdge(Edge edge) {
List<Vertex> adjacentVertices = directedAdjacencyList.get(edge.getFirstVertex());
if (!adjacentVertices.contains(edge.getSecondVertex())) {
adjacentVertices.add(edge.getSecondVertex());
directedAdjacencyList.put(edge.getFirstVertex(), adjacentVertices);
addReverseDirectedEdge(edge);
}
}
private void addReverseDirectedEdge(Edge edge) {
List<Vertex> adjacentVertices = reversedDirectedAdjacencyList.get(edge.getSecondVertex());
adjacentVertices.add(edge.getFirstVertex());
reversedDirectedAdjacencyList.put(edge.getSecondVertex(), adjacentVertices);
}
private void addUndirectedEdge(Edge edge) {
for (Vertex vertex : edge.getVertices()) {
List<Vertex> adjacentVertices = adjacencyList.get(vertex);
if (!adjacentVertices.contains(edge.getOtherVertex(vertex))) {
adjacentVertices.add(edge.getOtherVertex(vertex));
adjacencyList.put(vertex, adjacentVertices);
}
}
}
private List<Vertex> getAdjacentVertices(Vertex vertex, GraphAlingment align) {
if (align == GraphAlingment.UNDIRECTED) {
return adjacencyList.get(vertex);
} else if (align == GraphAlingment.DIRECTED) {
return directedAdjacencyList.get(vertex);
} else if (align == GraphAlingment.REVERSE_DIRECTED) {
return reversedDirectedAdjacencyList.get(vertex);
}
return null;
}
public List<List<Vertex>> getConnectedComponnents() {
return connectedComponents;
}
public List<List<Vertex>> getStronglyConnectedComponents() {
return stronglyConnectedComponents;
}
public List<Vertex> getComponentOfVertex(Vertex vertex) {
return connectedComponnentsMapping.get(vertex);
}
public List<Vertex> getStrongComponentOfVertex(Vertex vertex) {
return stronglyConnectedComponnentsMapping.get(vertex);
}
public boolean inSameConnectedComponent(Vertex vertex1, Vertex vertex2) {
return getComponentOfVertex(vertex1).equals(getComponentOfVertex(vertex2));
}
private enum GraphAlingment {
UNDIRECTED, DIRECTED, REVERSE_DIRECTED
}
}