-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacencyList.py
More file actions
51 lines (42 loc) · 1.31 KB
/
AdjacencyList.py
File metadata and controls
51 lines (42 loc) · 1.31 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
# class Graph:
# def __init__(self,data) -> None:
# self.nodeVal=data
# self.next=None
from Graph import Graph
class AList:
def __init__(self, all_nodes) -> None:
self.nodes=all_nodes
self.aL=[None]*self.nodes
def insert(self, src, dest):
'''
Creats Adjacency List
1. Create src and dest node.
2. put the content of aL[src] in dest node's next.
3. Replace the content of aL[src] with dest node.
4. All data prior to inserting dest node will be in the dest node's next variable.
5. As this is an undirected graph, do the same for src node and aL[dest].
'''
src_node=Graph(src)
dest_node=Graph(dest)
dest_node.next=self.aL[src]
self.aL[src]=dest_node
src_node.next=self.aL[dest]
self.aL[dest]=src_node
def print_graph(self) ->None:
for i in range(self.nodes):
print(f"{i} is adjacent to : |{i}|", end=" ")
k=self.aL[i]
while(k is not None ):
print(f"-->{k.nodeVal}",end="")
k=k.next
print()
if __name__=="__main__":
al=AList(5)
al.insert(0,2)
al.insert(0,3)
al.insert(0,4)
al.insert(1,3)
al.insert(1,4)
al.insert(2,3)
al.insert(3,4)
al.print_graph()