-
Notifications
You must be signed in to change notification settings - Fork 5
/
graph.py
50 lines (40 loc) · 1.65 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
from collections import defaultdict
class Graph(object):
def __init__(self, graph_dict=defaultdict(list)):
self.graph_dict = graph_dict
def vertices(self):
return list(self.graph_dict.keys())
def edges(self):
edges = []
for vertex in self.graph_dict:
for neighbour in self.graph_dict[vertex]:
edges.append((vertex, neighbour))
return edges
def add_vertex(self, vertex):
if vertex not in self.graph_dict:
self.graph_dict[vertex] = []
def add_edge(self, edge, add_reversed=True):
vertex1, vertex2 = edge
self.graph_dict[vertex1].append(vertex2)
if add_reversed:
self.graph_dict[vertex2].append(vertex1)
def remove_vertex(self, vertex_to_remove):
del self.graph_dict[vertex_to_remove]
for vertex in self.vertices():
if vertex_to_remove in self.graph_dict[vertex]:
self.graph_dict[vertex].remove(vertex_to_remove)
def remove_edge(self, edge_to_remove, remove_reversed=False):
vertex1, vertex2 = edge_to_remove
if vertex2 in self.graph_dict[vertex1]:
self.graph_dict[vertex1].remove(vertex2)
if remove_reversed:
if vertex1 in self.graph_dict[vertex2]:
self.graph_dict[vertex2].remove(vertex1)
def isolated_verices(self):
isolated_vertices = []
for vertex in self.graph_dict:
if not self.graph_dict[vertex]:
isolated_vertices.append(vertex)
return isolated_vertices
def __str__(self):
return 'Vertices: {}\nEdges: {}'.format(self.vertices(), self.edges())