Passed
Push — master ( dc545e...500623 )
by Xianshun
01:42
created

ConnectedComponents.id()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
from abc import ABCMeta, abstractmethod
2
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]