Skip to content

Commit c117e19

Browse files
author
Hardik dadhich
committed
Added unit tests
1 parent 4ecdba1 commit c117e19

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
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)

algorithms/graph/count_connected_number_of_component.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
# Code is Here
2121

22-
def dfs(source,visited):
22+
def dfs(source,visited,l):
2323
''' Function that performs DFS '''
2424

2525
visited[source] = True
2626
for child in l[source]:
2727
if not visited[child]:
28-
dfs(child,visited)
28+
dfs(child,visited,l)
2929

3030
def count_components(l,size):
3131
'''
@@ -37,11 +37,11 @@ def count_components(l,size):
3737
visited = [False]*(size+1)
3838
for i in range(1,size+1):
3939
if not visited[i]:
40-
dfs(i,visited)
40+
dfs(i,visited,l)
4141
count+=1
4242
return count
4343

44-
44+
4545
# Driver code
4646
if __name__ == '__main__':
4747
n,m = map(int, input("Enter the Number of Nodes and Edges \n").split(' '))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
from algorithms.graph import count_connected_number_of_component
3+
4+
class TestConnectedComponentInGraph(unittest.TestCase):
5+
""" Class """
6+
7+
def test_count_connected_components(self):
8+
"""
9+
Test Function that test the different cases of count connected components
10+
11+
0----------2 1--------5 3
12+
|
13+
|
14+
4
15+
16+
output = 3
17+
18+
"""
19+
expected_result = 3
20+
l = [[2],
21+
[5],
22+
[0,4],
23+
[],
24+
[2],
25+
[1]
26+
]
27+
28+
size = 5
29+
result = count_connected_number_of_component.count_components(l,size)
30+
self.assertEqual(result,expected_result)
31+
32+
if __name__ == '__main__':
33+
34+
unittest.main()
35+
36+
37+

0 commit comments

Comments
 (0)