Repository: keon/algorithms
Pull request #642 (2020-03-03 18:58:12)

Added Find Total Number of Connected Component in Graph
Issue: https://github.com/keon/algorithms/issues/641
1) Added `count_connected_number_of_component.py` file in `Algorithm /graph` section.




Patch:
diff --git a/README.md b/README.md
index 90a062f..06fb2d4 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
new file mode 100644
index 0000000..a944cf0
--- /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,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))


Test patch:
diff --git a/tests/test_graph.py b/tests/test_graph.py
index c691f33..b9794ed 100644
--- a/tests/test_graph.py
+++ b/tests/test_graph.py
@@ -9,7 +9,7 @@ from algorithms.graph import maximum_flow_dfs
 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
 
@@ -209,3 +209,58 @@ class TestBellmanFord(unittest.TestCase):
         } 
     
         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)