Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardik dadhich committed Mar 7, 2020
1 parent 4ecdba1 commit c117e19
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ If you want to uninstall algorithms, it is as simple as:
- [maximum_flow_dfs](algorithms/graph/maximum_flow_dfs.py)
- [all_pairs_shortest_path](algorithms/graph/all_pairs_shortest_path.py)
- [bellman_ford](algorithms/graph/bellman_ford.py)
- [Count Connected Components](algoritms/graph/count_connected_number_of_component.py)
- [heap](algorithms/heap)
- [merge_sorted_k_lists](algorithms/heap/merge_sorted_k_lists.py)
- [skyline](algorithms/heap/skyline.py)
Expand Down
8 changes: 4 additions & 4 deletions algorithms/graph/count_connected_number_of_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

# Code is Here

def dfs(source,visited):
def dfs(source,visited,l):
''' Function that performs DFS '''

visited[source] = True
for child in l[source]:
if not visited[child]:
dfs(child,visited)
dfs(child,visited,l)

def count_components(l,size):
'''
Expand All @@ -37,11 +37,11 @@ def count_components(l,size):
visited = [False]*(size+1)
for i in range(1,size+1):
if not visited[i]:
dfs(i,visited)
dfs(i,visited,l)
count+=1
return count


# Driver code
if __name__ == '__main__':
n,m = map(int, input("Enter the Number of Nodes and Edges \n").split(' '))
Expand Down
37 changes: 37 additions & 0 deletions tests/test_count_connected_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest
from algorithms.graph import count_connected_number_of_component

class TestConnectedComponentInGraph(unittest.TestCase):
""" Class """

def test_count_connected_components(self):
"""
Test Function that test the different cases of count connected components
0----------2 1--------5 3
|
|
4
output = 3
"""
expected_result = 3
l = [[2],
[5],
[0,4],
[],
[2],
[1]
]

size = 5
result = count_connected_number_of_component.count_components(l,size)
self.assertEqual(result,expected_result)

if __name__ == '__main__':

unittest.main()



0 comments on commit c117e19

Please sign in to comment.