Skip to content

Commit d39663d

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#317 from Febaug/master
Add connected components algorithm
2 parents 63bba80 + 075b30f commit d39663d

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import java.util.ArrayList;
2+
import java.util.HashSet;
3+
import java.util.Set;
4+
5+
/**
6+
* A class that counts the number of different connected components in a graph
7+
*
8+
* @author Lukas Keul, Florian Mercks
9+
*
10+
*/
11+
class Graph<E extends Comparable<E>> {
12+
13+
class Node {
14+
E name;
15+
16+
public Node(E name) {
17+
this.name = name;
18+
}
19+
}
20+
21+
class Edge {
22+
Node startNode, endNode;
23+
24+
public Edge(Node startNode, Node endNode) {
25+
this.startNode = startNode;
26+
this.endNode = endNode;
27+
}
28+
}
29+
30+
ArrayList<Edge> edgeList;
31+
ArrayList<Node> nodeList;
32+
33+
public Graph() {
34+
edgeList = new ArrayList<Edge>();
35+
nodeList = new ArrayList<Node>();
36+
}
37+
38+
/**
39+
* Adds a new Edge to the graph. If the nodes aren't yet in nodeList, they
40+
* will be added to it.
41+
*
42+
* @param startNode
43+
* the starting Node from the edge
44+
*
45+
* @param endNode
46+
* the ending Node from the edge
47+
*/
48+
public void addEdge(E startNode, E endNode) {
49+
Node start = null, end = null;
50+
for (Node node : nodeList) {
51+
if (startNode.compareTo(node.name) == 0) {
52+
start = node;
53+
}
54+
else if (endNode.compareTo(node.name) == 0) {
55+
end = node;
56+
}
57+
}
58+
if (start == null) {
59+
start = new Node(startNode);
60+
nodeList.add(start);
61+
}
62+
if (end == null) {
63+
end = new Node(endNode);
64+
nodeList.add(end);
65+
}
66+
67+
edgeList.add(new Edge(start, end));
68+
}
69+
70+
/**
71+
* Main method used for counting the connected components. Iterates through
72+
* the array of nodes to do a depth first search to get all nodes of the
73+
* graph from the actual node. These nodes are added to the array
74+
* markedNodes and will be ignored if they are chosen in the nodeList.
75+
*
76+
* @return returns the amount of unconnected graphs
77+
*
78+
*/
79+
public int countGraphs() {
80+
int count = 0;
81+
Set<Node> markedNodes = new HashSet<Node>();
82+
83+
for (Node n : nodeList) {
84+
if (!markedNodes.contains(n)) {
85+
markedNodes.add(n);
86+
markedNodes.addAll(depthFirstSearch(n, new ArrayList<Node>()));
87+
count++;
88+
}
89+
}
90+
91+
return count;
92+
}
93+
94+
/**
95+
* Implementation of depth first search.
96+
*
97+
* @param n
98+
* the actual visiting node
99+
*
100+
* @param visited
101+
* A list of already visited nodes in the depth first search
102+
*
103+
* @return returns a set of visited nodes
104+
*
105+
*/
106+
public ArrayList<Node> depthFirstSearch(Node n, ArrayList<Node> visited) {
107+
visited.add(n);
108+
for (Edge e : edgeList) {
109+
if (e.startNode.equals(n) && !visited.contains(e.endNode)) {
110+
depthFirstSearch(e.endNode, visited);
111+
}
112+
}
113+
return visited;
114+
}
115+
}
116+
117+
public class ConnectedComponent {
118+
119+
public static void main(String[] args) {
120+
Graph graphChars = new Graph();
121+
122+
// Graph 1
123+
graphChars.addEdge('a', 'b');
124+
graphChars.addEdge('a', 'e');
125+
graphChars.addEdge('b', 'e');
126+
graphChars.addEdge('b', 'c');
127+
graphChars.addEdge('c', 'd');
128+
graphChars.addEdge('d', 'a');
129+
130+
graphChars.addEdge('x', 'y');
131+
graphChars.addEdge('x', 'z');
132+
133+
graphChars.addEdge('w', 'w');
134+
135+
Graph graphInts = new Graph();
136+
137+
// Graph 2
138+
graphInts.addEdge(1, 2);
139+
graphInts.addEdge(2, 3);
140+
graphInts.addEdge(2, 4);
141+
graphInts.addEdge(3, 5);
142+
143+
graphInts.addEdge(7, 8);
144+
graphInts.addEdge(8, 10);
145+
graphInts.addEdge(10, 8);
146+
147+
System.out.println("Amount of different char-graphs: " + graphChars.countGraphs());
148+
System.out.println("Amount of different int-graphs: " + graphInts.countGraphs());
149+
}
150+
}

0 commit comments

Comments
 (0)