Passed
Push — master ( be817c...e0c5f6 )
by Xianshun
01:49
created

EdgeWeightedGraph.add_edge()   A

Complexity

Conditions 1

Size

Total Lines 5

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 5
rs 9.4285
1
from pyalgs.algorithms.commons import util
2
from pyalgs.data_structures.commons.bag import Bag
3
4 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
class Graph(object):
6
    V = 0
7
    adjList = None
8
9
    def __init__(self, V):
10
        self.V = V
11
        self.adjList = [None] * V
12
        for v in range(V):
13
            self.adjList[v] = Bag()
14
15
    def vertex_count(self):
16
        return self.V
17
18
    def adj(self, v):
19
        return self.adjList[v].iterate()
20
21
    def add_edge(self, v, w):
22
        self.adjList[v].add(w)
23
        self.adjList[w].add(v)
24
25 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
26
class Digraph(object):
27
    V = 0
28
    adjList = None
29
30
    def __init__(self, V):
31
        self.V = V
32
        self.adjList = [None] * V
33
        for v in range(V):
34
            self.adjList[v] = Bag()
35
36
    def vertex_count(self):
37
        return self.V
38
39
    def adj(self, v):
40
        return self.adjList[v].iterate()
41
42
    def add_edge(self, v, w):
43
        self.adjList[v].add(w)
44
45
    def reverse(self):
46
        g = Digraph(self.V)
47
        for v in range(self.V):
48
            for w in self.adjList[v].iterate():
49
                g.add_edge(w, v)
50
51
        return g
52
53
54
class Edge(object):
55
    v = None
56
    w = None
57
    weight = 0
58
59
    def __init__(self, v=None, w=None, weight=None):
60
        if weight is None:
61
            weight = 0
62
        self.v = v
63
        self.w = w
64
        self.weight = weight
65
66
    def start(self):
67
        return self.v
68
69
    def end(self):
70
        return self.w
71
72
    def other(self, v):
73
        if self.v == v:
74
            return self.w
75
        elif self.w == v:
76
            return self.v
77
        else:
78
            raise ValueError('mismatched vertex detected')
79
80
    def __cmp__(self, other):
81
        return util.cmp(self.weight, other.weight)
82
83
    def __str__(self):
84
        return str(self.v) + ' => ' + str(self.w) + ' (' + str(self.weight) + ')'
85
86
87
class EdgeWeightedGraph(object):
88
    adjList = None
89
    V = 0
90
91
    def __init__(self, vertex_count):
92
        self.V = vertex_count
93
        self.adjList = [None] * vertex_count
94
        for v in range(vertex_count):
95
            self.adjList[v] = Bag()
96
97
    def add_edge(self, edge):
98
        v = edge.start()
99
        w = edge.end()
100
        self.adjList[v].add(edge)
101
        self.adjList[w].add(edge)
102
103
    def adj(self, v):
104
        return self.adjList[v].iterate()
105
106
    def vertex_count(self):
107
        return self.V
108
109
    def edges(self):
110
        for v in range(self.V):
111
            for e in self.adj(v):
112
                if e.start() == v:
113
                    yield e
114
115