-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgraph_adjacency-matrix.py
More file actions
73 lines (51 loc) · 1.72 KB
/
graph_adjacency-matrix.py
File metadata and controls
73 lines (51 loc) · 1.72 KB
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
# Implementation of an undirected graph using Adjacency Matrix
class Vertex:
def __init__(self, name):
self.name = name
class Graph:
def __init__(self):
self.vertices = {} # vertex_name -> Vertex
self.edges = [] # adjacency matrix
self.edge_indices = {} # vertex_name -> index
def add_vertex(self, vertex):
# Validate input
if not isinstance(vertex, Vertex):
return False
if vertex.name in self.vertices:
return False
# 1. Add vertex
self.vertices[vertex.name] = vertex
# 2. Expand existing rows (add new column)
for row in self.edges:
row.append(0)
# 3. Add new row initialized with zeros
self.edges.append([0] * (len(self.edges) + 1))
# 4. Assign index to vertex
self.edge_indices[vertex.name] = len(self.edge_indices)
return True
def add_edge(self, u, v, weight=1):
if u not in self.vertices or v not in self.vertices:
return False
i = self.edge_indices[u]
j = self.edge_indices[v]
# Undirected graph
self.edges[i][j] = weight
self.edges[j][i] = weight
return True
def print_graph(self):
for vertex, i in sorted(self.edge_indices.items()):
print(vertex, end=" ")
for value in self.edges[i]:
print(value, end=" ")
print()
g = Graph()
# Add vertices
for ch in range(ord('A'), ord('K')):
g.add_vertex(Vertex(chr(ch)))
# Add edges
edges = ['AB', 'AE', 'BF', 'CG', 'DE', 'DH', 'EH',
'FG', 'FI', 'FJ', 'GJ', 'HI']
for edge in edges:
g.add_edge(edge[0], edge[1])
# Print graph
g.print_graph()