forked from MirjanaBlazhevska/FEIT01L006
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
99 lines (79 loc) · 3.3 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
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=True):
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_vertices(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())
class WeightedGraph(object):
def __init__(self, graph_dict=defaultdict(dict)):
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, weight in self.graph_dict[vertex].items():
edges.append((vertex, neighbour, weight))
return edges
def add_vertex(self, vertex):
if vertex not in self.graph_dict:
self.graph_dict[vertex] = {}
def add_edge(self, edge, weight, add_reversed=True):
vertex1, vertex2 = edge
self.graph_dict[vertex1][vertex2] = weight
if add_reversed:
self.graph_dict[vertex2][vertex1] = weight
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]:
del self.graph_dict[vertex][vertex_to_remove]
def remove_edge(self, edge_to_remove, remove_reversed=True):
vertex1, vertex2 = edge_to_remove
if vertex2 in self.graph_dict[vertex1]:
del self.graph_dict[vertex1][vertex2]
if remove_reversed:
if vertex1 in self.graph_dict[vertex2]:
del self.graph_dict[vertex2][vertex1]
def isolated_vertices(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())