Skip to content

Commit 2903e0b

Browse files
committed
Added Strongly Connected Components
1 parent 220cb78 commit 2903e0b

File tree

6 files changed

+285
-0
lines changed

6 files changed

+285
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package strongly_connected_components.kosaraju;
2+
3+
import java.util.Stack;
4+
5+
public class DepthFirstOrder {
6+
private Stack<Vertex> stack;
7+
8+
public DepthFirstOrder(Graph graph) {
9+
stack = new Stack<>();
10+
11+
for(Vertex vertex : graph.getVertices()) {
12+
if( !vertex.isVisited() ) {
13+
dfs(vertex);
14+
}
15+
}
16+
}
17+
18+
private void dfs(Vertex vertex) {
19+
vertex.setVisited(true);
20+
21+
for(Vertex v : vertex.getAdjList()) {
22+
if(!v.isVisited()) {
23+
dfs(v);
24+
}
25+
}
26+
27+
stack.push(vertex);
28+
}
29+
30+
public Stack<Vertex> getReversePostOrder() {
31+
return this.stack;
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package strongly_connected_components.kosaraju;
2+
3+
public class Edge {
4+
private double weight;
5+
private Vertex startVertex;
6+
private Vertex targetVertex;
7+
8+
public Edge(double weight, Vertex startVertex, Vertex targetVertex) {
9+
this.weight = weight;
10+
this.startVertex = startVertex;
11+
this.targetVertex = targetVertex;
12+
}
13+
14+
public double getWeight() {
15+
return weight;
16+
}
17+
18+
public void setWeight(double weight) {
19+
this.weight = weight;
20+
}
21+
22+
public Vertex getStartVertex() {
23+
return startVertex;
24+
}
25+
26+
public void setStartVertex(Vertex startVertex) {
27+
this.startVertex = startVertex;
28+
}
29+
30+
public Vertex getTargetVertex() {
31+
return targetVertex;
32+
}
33+
34+
public void setTargetVertex(Vertex targetVertex) {
35+
this.targetVertex = targetVertex;
36+
}
37+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package strongly_connected_components.kosaraju;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Graph {
7+
private List<Vertex> vertices;
8+
private List<Edge> edges;
9+
10+
public Graph() {
11+
this.vertices = new ArrayList<>();
12+
this.edges = new ArrayList<>();
13+
}
14+
15+
public Graph(List<Vertex> vertices, List<Edge> edges) {
16+
this.vertices = vertices;
17+
this.edges = edges;
18+
}
19+
20+
public List<Vertex> getVertices() {
21+
return vertices;
22+
}
23+
24+
public void setVertices(List<Vertex> vertices) {
25+
this.vertices = vertices;
26+
}
27+
28+
public List<Edge> getEdges() {
29+
return edges;
30+
}
31+
32+
public void setEdges(List<Edge> edges) {
33+
this.edges = edges;
34+
}
35+
36+
public Graph transposeGraph() {
37+
Graph transpose = new Graph();
38+
39+
List<Vertex> transposeVertices = new ArrayList<>(this.vertices);
40+
41+
for(Edge edge : this.edges) {
42+
/** Reverse the edges */
43+
Vertex target = edge.getTargetVertex();
44+
Vertex start = edge.getStartVertex();
45+
int indexOfEdge = transposeVertices.indexOf(target);
46+
transposeVertices.get(indexOfEdge).addNeighbours(start);
47+
}
48+
49+
transpose.setVertices(transposeVertices);
50+
51+
return transpose;
52+
}
53+
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package strongly_connected_components.kosaraju;
2+
3+
public class KosarajuAlgorithm {
4+
private int[] id; // id[V] = id of SCC of the given V (vertex)
5+
private int count; // no. of SCC
6+
private boolean[] marked;
7+
8+
public KosarajuAlgorithm(Graph graph) {
9+
10+
DepthFirstOrder dfs = new DepthFirstOrder(graph.transposeGraph());
11+
12+
marked = new boolean[graph.getVertices().size()];
13+
id = new int[graph.getVertices().size()];
14+
15+
for(Vertex vertex : dfs.getReversePostOrder()) {
16+
if(!marked[vertex.getId()]) {
17+
dfs(vertex);
18+
count++;
19+
}
20+
}
21+
}
22+
23+
private void dfs(Vertex vertex) {
24+
marked[vertex.getId()] = true;
25+
id[vertex.getId()] = count;
26+
vertex.setComponentID(count);
27+
28+
for(Vertex v : vertex.getAdjList()) {
29+
if(!marked[v.getId()]) {
30+
dfs(v);
31+
}
32+
}
33+
}
34+
35+
public int getCount() {
36+
return count;
37+
}
38+
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package strongly_connected_components.kosaraju;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class RunKosaraju {
7+
8+
public static void main(String[] args) {
9+
List<Vertex> vertexList = new ArrayList<>();
10+
11+
vertexList.add(new Vertex(0, "A"));
12+
vertexList.add(new Vertex(1, "B"));
13+
vertexList.add(new Vertex(2, "C"));
14+
vertexList.add(new Vertex(3, "D"));
15+
vertexList.add(new Vertex(4, "E"));
16+
vertexList.add(new Vertex(5, "F"));
17+
vertexList.add(new Vertex(6, "G"));
18+
vertexList.add(new Vertex(7, "H"));
19+
20+
List<Edge> edgeList = new ArrayList<>();
21+
22+
edgeList.add(new Edge(1, vertexList.get(0), vertexList.get(1)));
23+
24+
edgeList.add(new Edge(1, vertexList.get(1), vertexList.get(4)));
25+
edgeList.add(new Edge(1, vertexList.get(1), vertexList.get(5)));
26+
edgeList.add(new Edge(1, vertexList.get(1), vertexList.get(2)));
27+
28+
29+
edgeList.add(new Edge(1, vertexList.get(2), vertexList.get(6)));
30+
edgeList.add(new Edge(1, vertexList.get(2), vertexList.get(3)));
31+
32+
edgeList.add(new Edge(1, vertexList.get(3), vertexList.get(2)));
33+
edgeList.add(new Edge(1, vertexList.get(3), vertexList.get(7)));
34+
35+
edgeList.add(new Edge(1, vertexList.get(4), vertexList.get(0)));
36+
edgeList.add(new Edge(1, vertexList.get(4), vertexList.get(5)));
37+
38+
edgeList.add(new Edge(1, vertexList.get(5), vertexList.get(6)));
39+
40+
edgeList.add(new Edge(1, vertexList.get(6), vertexList.get(5)));
41+
42+
edgeList.add(new Edge(1, vertexList.get(7), vertexList.get(3)));
43+
edgeList.add(new Edge(1, vertexList.get(7), vertexList.get(6)));
44+
45+
KosarajuAlgorithm kosarajuAlgorithm = new KosarajuAlgorithm(new Graph(vertexList, edgeList));
46+
System.out.println(kosarajuAlgorithm.getCount());
47+
48+
for(Vertex vertex : vertexList) {
49+
System.out.println(vertex.getName()+": "+vertex.getComponentID());
50+
}
51+
}
52+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package strongly_connected_components.kosaraju;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Vertex {
8+
private int id;
9+
private String name;
10+
private boolean visited;
11+
private List<Vertex> adjList;
12+
private int componentID;
13+
14+
public Vertex(int id, String name) {
15+
this.id = id;
16+
this.name = name;
17+
this.visited = false;
18+
this.adjList = new ArrayList<>();
19+
}
20+
21+
public int getId() {
22+
return id;
23+
}
24+
25+
public void setId(int id) {
26+
this.id = id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public boolean isVisited() {
38+
return visited;
39+
}
40+
41+
public void setVisited(boolean visited) {
42+
this.visited = visited;
43+
}
44+
45+
public List<Vertex> getAdjList() {
46+
return adjList;
47+
}
48+
49+
public void setAdjList(List<Vertex> adjList) {
50+
this.adjList = adjList;
51+
}
52+
53+
public int getComponentID() {
54+
return componentID;
55+
}
56+
57+
public void setComponentID(int componentID) {
58+
this.componentID = componentID;
59+
}
60+
61+
public void addNeighbours(Vertex ...vertices) {
62+
adjList.addAll(Arrays.asList(vertices));
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return this.name;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)