Skip to content

Commit 3064d2e

Browse files
authored
Merge pull request #642 from hardik-dadhich/hardik
Added Find Total Number of Connected Component in Graph
2 parents ac1718e + ae38244 commit 3064d2e

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ If you want to uninstall algorithms, it is as simple as:
165165
- [maximum_flow_dfs](algorithms/graph/maximum_flow_dfs.py)
166166
- [all_pairs_shortest_path](algorithms/graph/all_pairs_shortest_path.py)
167167
- [bellman_ford](algorithms/graph/bellman_ford.py)
168+
- [Count Connected Components](algoritms/graph/count_connected_number_of_component.py)
168169
- [heap](algorithms/heap)
169170
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
170171
- [skyline](algorithms/heap/skyline.py)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#count connected no of component using DFS
2+
'''
3+
In graph theory, a component, sometimes called a connected component,
4+
of an undirected graph is a subgraph in which any
5+
two vertices are connected to each other by paths.
6+
7+
Example:
8+
9+
10+
1 3------------7
11+
|
12+
|
13+
2--------4
14+
| |
15+
| | output = 2
16+
6--------5
17+
18+
'''
19+
20+
# Code is Here
21+
22+
def dfs(source,visited,l):
23+
''' Function that performs DFS '''
24+
25+
visited[source] = True
26+
for child in l[source]:
27+
if not visited[child]:
28+
dfs(child,visited,l)
29+
30+
def count_components(l,size):
31+
'''
32+
Function that counts the Connected components on bases of DFS.
33+
return type : int
34+
'''
35+
36+
count = 0
37+
visited = [False]*(size+1)
38+
for i in range(1,size+1):
39+
if not visited[i]:
40+
dfs(i,visited,l)
41+
count+=1
42+
return count
43+
44+
45+
# Driver code
46+
if __name__ == '__main__':
47+
n,m = map(int, input("Enter the Number of Nodes and Edges \n").split(' '))
48+
l = [[] for _ in range(n+1)]
49+
for i in range(m):
50+
print("Enter the edge's Nodes in form of a b\n")
51+
a,b = map(int,input().split(' '))
52+
l[a].append(b)
53+
l[b].append(a)
54+
print("Total number of Connected Components are : ", count_components(l,n))

tests/test_graph.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from algorithms.graph import all_pairs_shortest_path
1010
from algorithms.graph import bellman_ford
1111
from algorithms.graph import bellman_ford
12-
12+
from algorithms.graph import count_connected_number_of_component
1313

1414
import unittest
1515

@@ -209,3 +209,58 @@ def test_bellman_ford(self):
209209
}
210210

211211
self.assertEqual(True, bellman_ford(graph2, 'a'))
212+
213+
214+
class TestConnectedComponentInGraph(unittest.TestCase):
215+
"""
216+
Class for testing different cases for connected components in graph
217+
"""
218+
def test_count_connected_components(self):
219+
"""
220+
Test Function that test the different cases of count connected components
221+
222+
0----------2 1--------5 3
223+
|
224+
|
225+
4
226+
227+
output = 3
228+
"""
229+
expected_result = 3
230+
231+
# adjacency list representation of graph
232+
l = [[2],
233+
[5],
234+
[0,4],
235+
[],
236+
[2],
237+
[1]
238+
]
239+
240+
size = 5
241+
result = count_connected_number_of_component.count_components(l,size)
242+
self.assertEqual(result,expected_result)
243+
244+
def test_connected_components_with_empty_graph(self):
245+
246+
"""
247+
input :
248+
output : 0
249+
"""
250+
251+
l = [[]]
252+
expected_result = 0
253+
size = 0
254+
result = count_connected_number_of_component.count_components(l,size)
255+
self.assertEqual(result,expected_result)
256+
257+
def test_connected_components_without_edges_graph(self):
258+
"""
259+
input : 0 2 3 4
260+
output : 4
261+
"""
262+
l = [[0],[],[2],[3],[4]]
263+
size = 4
264+
expected_result = 4
265+
result = count_connected_number_of_component.count_components(l,size)
266+
self.assertEqual(result,expected_result)

0 commit comments

Comments
 (0)