Passed
Push — master ( 167e73...dfbdfc )
by Xianshun
01:27
created

Edge.__gt__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
    # use for python 2 comparison
81
    def __cmp__(self, other):
82
        return util.cmp(self.weight, other.weight)
83
84
    # user for python 3 comparison
85
    def __lt__(self, other):
86
        return util.less(self.weight, other.weight)
87
88
    # user for python 3 comparison
89
    def __gt__(self, other):
90
        return util.greater(self.weight, other.weight)
91
92
    def __str__(self):
93
        return str(self.v) + ' => ' + str(self.w) + ' (' + str(self.weight) + ')'
94
95
96
class EdgeWeightedGraph(object):
97
    adjList = None
98
    V = 0
99
100
    def __init__(self, vertex_count):
101
        self.V = vertex_count
102
        self.adjList = [None] * vertex_count
103
        for v in range(vertex_count):
104
            self.adjList[v] = Bag()
105
106
    def add_edge(self, edge):
107
        v = edge.start()
108
        w = edge.end()
109
        self.adjList[v].add(edge)
110
        self.adjList[w].add(edge)
111
112
    def adj(self, v):
113
        return self.adjList[v].iterate()
114
115
    def vertex_count(self):
116
        return self.V
117
118
    def edges(self):
119
        for v in range(self.V):
120
            for e in self.adj(v):
121
                if e.start() == v:
122
                    yield e
123
124