From 4ecdba1ebb3ec42c6da7156ba94f32143cd37ffc Mon Sep 17 00:00:00 2001 From: Hardik dadhich Date: Wed, 4 Mar 2020 00:13:20 +0530 Subject: [PATCH 1/6] Find Total Number of Connected Component in Graph --- .../count_connected_number_of_component.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 algorithms/graph/count_connected_number_of_component.py diff --git a/algorithms/graph/count_connected_number_of_component.py b/algorithms/graph/count_connected_number_of_component.py new file mode 100644 index 000000000..19caec45a --- /dev/null +++ b/algorithms/graph/count_connected_number_of_component.py @@ -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): + ''' Function that performs DFS ''' + + visited[source] = True + for child in l[source]: + if not visited[child]: + dfs(child,visited) + +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) + 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)) From c117e195f0fe51b0d36754f842b32ca352b14af1 Mon Sep 17 00:00:00 2001 From: Hardik dadhich Date: Sat, 7 Mar 2020 14:02:19 +0530 Subject: [PATCH 2/6] Added unit tests --- README.md | 1 + .../count_connected_number_of_component.py | 8 ++-- tests/test_count_connected_components.py | 37 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/test_count_connected_components.py diff --git a/README.md b/README.md index 90a062f29..06fb2d4c4 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/algorithms/graph/count_connected_number_of_component.py b/algorithms/graph/count_connected_number_of_component.py index 19caec45a..a944cf096 100644 --- a/algorithms/graph/count_connected_number_of_component.py +++ b/algorithms/graph/count_connected_number_of_component.py @@ -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): ''' @@ -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(' ')) diff --git a/tests/test_count_connected_components.py b/tests/test_count_connected_components.py new file mode 100644 index 000000000..4348ab088 --- /dev/null +++ b/tests/test_count_connected_components.py @@ -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() + + + From 25da9ad92062537d5f98e8e2aa6db1f16e7d8c56 Mon Sep 17 00:00:00 2001 From: Hardik dadhich Date: Sat, 7 Mar 2020 14:05:57 +0530 Subject: [PATCH 3/6] fixing spaces --- tests/test_count_connected_components.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/test_count_connected_components.py b/tests/test_count_connected_components.py index 4348ab088..b9b1620c5 100644 --- a/tests/test_count_connected_components.py +++ b/tests/test_count_connected_components.py @@ -1,22 +1,25 @@ +#import modules import unittest from algorithms.graph import count_connected_number_of_component class TestConnectedComponentInGraph(unittest.TestCase): - """ Class """ + """ Class to performs unit tests """ 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 + 0----------2 1--------5 3 + | + | + 4 + + output = 3 """ expected_result = 3 + + # adjacency list representation of graph l = [[2], [5], [0,4], From e5c4699cccbc9b5e02afa8c03b7b098bcef548c8 Mon Sep 17 00:00:00 2001 From: Hardik dadhich Date: Sat, 7 Mar 2020 14:08:12 +0530 Subject: [PATCH 4/6] fixing spaces --- tests/test_count_connected_components.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_count_connected_components.py b/tests/test_count_connected_components.py index b9b1620c5..a7e1fec6d 100644 --- a/tests/test_count_connected_components.py +++ b/tests/test_count_connected_components.py @@ -9,7 +9,7 @@ def test_count_connected_components(self): """ Test Function that test the different cases of count connected components - 0----------2 1--------5 3 + 0----------2 1--------5 3 | | 4 From 9034015aafb6d05202b434f897c9340f1817ecfa Mon Sep 17 00:00:00 2001 From: Hardik dadhich Date: Fri, 13 Mar 2020 01:36:36 +0530 Subject: [PATCH 5/6] adding more unit-tests --- tests/test_count_connected_components.py | 40 ----------------- tests/test_graph.py | 56 +++++++++++++++++++++++- 2 files changed, 54 insertions(+), 42 deletions(-) delete mode 100644 tests/test_count_connected_components.py diff --git a/tests/test_count_connected_components.py b/tests/test_count_connected_components.py deleted file mode 100644 index a7e1fec6d..000000000 --- a/tests/test_count_connected_components.py +++ /dev/null @@ -1,40 +0,0 @@ -#import modules -import unittest -from algorithms.graph import count_connected_number_of_component - -class TestConnectedComponentInGraph(unittest.TestCase): - """ Class to performs unit tests """ - - 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) - -if __name__ == '__main__': - - unittest.main() - - - diff --git a/tests/test_graph.py b/tests/test_graph.py index c691f33d2..b12646a0e 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -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 @@ -208,4 +208,56 @@ def test_bellman_ford(self): 'e': {'a': 7, 'b': 5, 'd': 1} } - 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) From ae3824422eac2ba796ad117fd7f8f77a5bb9e08b Mon Sep 17 00:00:00 2001 From: Hardik dadhich Date: Fri, 13 Mar 2020 10:10:50 +0530 Subject: [PATCH 6/6] adding line which is deleted --- tests/test_graph.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_graph.py b/tests/test_graph.py index b12646a0e..b9794edf0 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -208,6 +208,9 @@ def test_bellman_ford(self): 'e': {'a': 7, 'b': 5, 'd': 1} } + self.assertEqual(True, bellman_ford(graph2, 'a')) + + class TestConnectedComponentInGraph(unittest.TestCase): """ Class for testing different cases for connected components in graph