Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 additions & 0 deletions algorithms/graph/count_connected_number_of_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#count connected no of component using DFS
'''
In graph theory, a component, sometimes called a connected component,
of an undirected graph is a subgraph in which any
two vertices are connected to each other by paths.

Example:


1 3------------7
|
|
2--------4
| |
| | output = 2
6--------5

'''

# Code is Here

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

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

def count_components(l,size):
'''
Function that counts the Connected components on bases of DFS.
return type : int
'''

count = 0
visited = [False]*(size+1)
for i in range(1,size+1):
if not visited[i]:
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(' '))
l = [[] for _ in range(n+1)]
for i in range(m):
print("Enter the edge's Nodes in form of a b\n")
a,b = map(int,input().split(' '))
l[a].append(b)
l[b].append(a)
print("Total number of Connected Components are : ", count_components(l,n))
57 changes: 56 additions & 1 deletion tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from algorithms.graph import all_pairs_shortest_path
from algorithms.graph import bellman_ford
from algorithms.graph import bellman_ford

from algorithms.graph import count_connected_number_of_component

import unittest

Expand Down Expand Up @@ -209,3 +209,58 @@ def test_bellman_ford(self):
}

self.assertEqual(True, bellman_ford(graph2, 'a'))


class TestConnectedComponentInGraph(unittest.TestCase):
"""
Class for testing different cases for connected components in graph
"""
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

# adjacency list representation of graph
l = [[2],
[5],
[0,4],
[],
[2],
[1]
]

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

def test_connected_components_with_empty_graph(self):

"""
input :
output : 0
"""

l = [[]]
expected_result = 0
size = 0
result = count_connected_number_of_component.count_components(l,size)
self.assertEqual(result,expected_result)

def test_connected_components_without_edges_graph(self):
"""
input : 0 2 3 4
output : 4
"""
l = [[0],[],[2],[3],[4]]
size = 4
expected_result = 4
result = count_connected_number_of_component.count_components(l,size)
self.assertEqual(result,expected_result)