|
9 | 9 | from algorithms.graph import all_pairs_shortest_path |
10 | 10 | from algorithms.graph import bellman_ford |
11 | 11 | from algorithms.graph import bellman_ford |
12 | | - |
| 12 | +from algorithms.graph import count_connected_number_of_component |
13 | 13 |
|
14 | 14 | import unittest |
15 | 15 |
|
@@ -208,4 +208,56 @@ def test_bellman_ford(self): |
208 | 208 | 'e': {'a': 7, 'b': 5, 'd': 1} |
209 | 209 | } |
210 | 210 |
|
211 | | - self.assertEqual(True, bellman_ford(graph2, 'a')) |
| 211 | +class TestConnectedComponentInGraph(unittest.TestCase): |
| 212 | + """ |
| 213 | + Class for testing different cases for connected components in graph |
| 214 | + """ |
| 215 | + def test_count_connected_components(self): |
| 216 | + """ |
| 217 | + Test Function that test the different cases of count connected components |
| 218 | + |
| 219 | + 0----------2 1--------5 3 |
| 220 | + | |
| 221 | + | |
| 222 | + 4 |
| 223 | + |
| 224 | + output = 3 |
| 225 | + """ |
| 226 | + expected_result = 3 |
| 227 | + |
| 228 | + # adjacency list representation of graph |
| 229 | + l = [[2], |
| 230 | + [5], |
| 231 | + [0,4], |
| 232 | + [], |
| 233 | + [2], |
| 234 | + [1] |
| 235 | + ] |
| 236 | + |
| 237 | + size = 5 |
| 238 | + result = count_connected_number_of_component.count_components(l,size) |
| 239 | + self.assertEqual(result,expected_result) |
| 240 | + |
| 241 | + def test_connected_components_with_empty_graph(self): |
| 242 | + |
| 243 | + """ |
| 244 | + input : |
| 245 | + output : 0 |
| 246 | + """ |
| 247 | + |
| 248 | + l = [[]] |
| 249 | + expected_result = 0 |
| 250 | + size = 0 |
| 251 | + result = count_connected_number_of_component.count_components(l,size) |
| 252 | + self.assertEqual(result,expected_result) |
| 253 | + |
| 254 | + def test_connected_components_without_edges_graph(self): |
| 255 | + """ |
| 256 | + input : 0 2 3 4 |
| 257 | + output : 4 |
| 258 | + """ |
| 259 | + l = [[0],[],[2],[3],[4]] |
| 260 | + size = 4 |
| 261 | + expected_result = 4 |
| 262 | + result = count_connected_number_of_component.count_components(l,size) |
| 263 | + self.assertEqual(result,expected_result) |
0 commit comments