| Total Complexity | 9 |
| Total Lines | 30 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | from abc import ABCMeta, abstractmethod |
||
| 3 | class ConnectedComponents(object): |
||
| 4 | |||
| 5 | marked = None |
||
| 6 | _id = None |
||
| 7 | _count = 0 |
||
| 8 | |||
| 9 | def __init__(self, G): |
||
| 10 | vertex_count = G.vertex_count() |
||
| 11 | self.marked = [False] * vertex_count |
||
| 12 | self._id = [-1] * vertex_count |
||
| 13 | for v in range(vertex_count): |
||
| 14 | if not self.marked[v]: |
||
| 15 | self.dfs(G, v) |
||
| 16 | self._count += 1 |
||
| 17 | |||
| 18 | def dfs(self, G, v): |
||
| 19 | self.marked[v] = True |
||
| 20 | self._id[v] = self._count |
||
| 21 | for w in G.adj(v): |
||
| 22 | if not self.marked[w]: |
||
| 23 | self.dfs(G, w) |
||
| 24 | |||
| 25 | def connected(self, v, w): |
||
| 26 | return self._id[v] == self._id[w] |
||
| 27 | |||
| 28 | def count(self): |
||
| 29 | return self._count |
||
| 30 | |||
| 31 | def id(self, v): |
||
| 32 | return self._id[v] |