-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
112 lines (89 loc) · 2.12 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
class Graphe_mat:
"""
Graph using matrix. (NOT USED)
"""
def __init__(self, n):
self.n = n
self.mat = [[False] * n for _ in range(n)]
def add_edge(self, s1, s2):
self.mat[s1][s2] = True
self.mat[s2][s1] = True
def edge(self, s1, s2):
return self.mat[s1][s2]
def neighbougrs(self, s):
v = []
for i in range(self.n):
if self.mat[s][i]:
v.append(i)
return v
class Graph_dic:
"""
Graph using a dict.
"""
def __init__(self):
self.dic = {}
def add_vertice(self, v):
"""
Adding vertice v to the graph.
Parameters
----------
v: Name of the vertice
Returns
-------
"""
if v not in self.dic:
self.dic[v] = []
def add_edge(self, v1, v2):
"""
Adding edge to the graph.
Parameters
----------
v1: Vertice 1 to link with
v2: Vertice 2 to link with
Returns
-------
"""
self.add_vertice(v1)
if v2 not in self.dic[v1]:
self.add_vertice(v2)
self.dic[v1].append(v2)
self.dic[v2].append(v1)
def edge(self, v1, v2):
"""
Returns True if there is a link betwwen v1 and v2
Parameters
----------
v1: Vertice 1
v2: Vertice 2
Returns
-------
boolean
"""
return v2 in self.dic[v1]
def vertices(self):
"""
Returns all the vertices of the graph.
Returns
-------
list
"""
return list(self.dic)
def neighbors(self, v):
"""
Returns all the neighbors (vertices connected) of the vertice v.
Parameters
----------
v: Vertice from which we want to get the neighbors.
Returns
-------
list
"""
return self.dic[v]
def nb_neighbors(self):
"""
Returns the number of vertices in the graph.
Returns
-------
int
"""
return len(self.dic)