This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 581
/
Adjacency-list Representation
116 lines (97 loc) · 3.47 KB
/
Adjacency-list Representation
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
113
114
115
116
from enum import Enum
class Color(Enum):
White = 1
Gray = 2
Black = 3
'''
Node to store the real element.
Contain data and pointer to the
first element (head) of the adjacency list.
'''
class Node():
def __init__(self, d):
self.data = d
self.head = None
self.color = Color.White
'''
Node for linked list of adjacent elements.
This will contain a pointer for next node.
It will not contain the real element but the index of
element of the array containing all the vertices V.
'''
class ListNode():
def __init__(self, item_index):
self.index_of_item = item_index
self.next = None
class Graph():
def __init__(self, no_of_vertices):
self.number_of_vertices = no_of_vertices
self.heads = [None]*no_of_vertices #to store nodes, all vertices.
for i in range(0, self.number_of_vertices):
if(self.heads[i] == None):
z = Node(-1)
self.heads[i] = z
def add_node_to_graph(self, data):
z = Node(data)
for i in range(0, self.number_of_vertices):
if(self.heads[i].data < 0):
self.heads[i] = z
break
def in_head_list(self, data):
for i in range(0, self.number_of_vertices):
if(self.heads[i].data == data):
return True
return False
def add_edge(self, source, dest):
if(not(self.in_head_list(source))):
self.add_node_to_graph(source)
if(not(self.in_head_list(dest))):
self.add_node_to_graph(dest)
for i in range(0, self.number_of_vertices):
if(self.heads[i].data == source): #source node found
dest_index = 0
for j in range(0, self.number_of_vertices):
if(self.heads[j].data == dest): #destination node found
dest_index = j
break
n = ListNode(dest_index)
if(self.heads[i].head == None):
self.heads[i].head = n
else:
temp = self.heads[i].head
while(temp.next != None):
temp = temp.next
temp.next = n
break
#if graph is undirected, there will also be edge from dest to source
for i in range(0, self.number_of_vertices):
if(self.heads[i].data == dest):
source_index = 0
for j in range(0, self.number_of_vertices):
if(self.heads[j].data == source):
source_index = j
break
n = ListNode(source_index)
if(self.heads[i].head == None):
self.heads[i].head = n
else:
temp = self.heads[i].head
while(temp.next != None):
temp = temp.next
temp.next = n
break
def print_graph(self):
for i in range(0, self.number_of_vertices):
temp = self.heads[i].head
print(str(self.heads[i].data) + '\t', end='') # to print without newline
while(temp != None):
print(str(self.heads[temp.index_of_item].data) + '\t', end='')
temp = temp.next
print('')
if __name__ == '__main__':
g = Graph(4)
g.add_edge(1, 2)
g.add_edge(1, 3)
g.add_edge(2, 4)
g.add_edge(3, 2)
g.print_graph()