-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.py
53 lines (43 loc) · 1.48 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
class Graph:
def __init__(self, graph_dict={}):
self._graph = graph_dict
def add_vertex(self, vertex):
if vertex not in self._graph:
self._graph[vertex] = []
def add_edge(self, v1, v2):
if v1 in self._graph:
self._graph[v1].append(v2)
else:
self._graph[v1] = [v2]
def vertices(self):
return list(self._graph.keys())
def edges(self):
return self._generate_edges()
def _generate_edges(self):
edges = []
for vertex in self._graph:
for neighbour in self._graph[vertex]:
if (vertex, neighbour) not in edges:
edges.append((vertex, neighbour))
return edges
def is_isolated(self, vertex):
if vertex not in self._graph.keys():
return False
if not len(self._graph[vertex]):
return True
for i in self._graph.keys():
if vertex in i:
return False
else: return True
def degree(self, vertex):
if vertex not in self._graph.keys():
return -1
return len(self._graph[vertex]) #+ self._graph[vertex].count(vertex)
def __str__(self):
res = "Vertices: "
for vertex in self.vertices():
res += str(vertex) + " "
res += "\nEdges: "
for edge in self._generate_edges():
res += str(edge) + " "
return res