Skip to content

Commit 7769fe0

Browse files
Add files via upload
Takes O(1) time to add nodes to head of the Linked List for each vertex.
1 parent 2541905 commit 7769fe0

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

AdjacencyListGraph.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Adjacency List Implementation:
2+
class AdjNode:
3+
def __init__(self, data):
4+
self.vertex = data
5+
self.next = None
6+
7+
class Graph:
8+
def __init__(self, V):
9+
self.v = V
10+
self.graph = [None]*self.v
11+
12+
def add_edge(self, src, dest):
13+
node = AdjNode(dest)
14+
node.next = self.graph[src]
15+
self.graph[src] = node
16+
print("self.graph",
17+
self.graph[src], self.graph[src].vertex, self.graph[src].next)
18+
# In case of Undirected Graph
19+
node = AdjNode(src)
20+
node.next = self.graph[dest]
21+
self.graph[dest] = node
22+
23+
def print_graph(self):
24+
for i in range(self.v):
25+
current = self.graph[i]
26+
print("Head->", end='')
27+
while current:
28+
print("->{}".format(current.vertex),
29+
end='')
30+
current = current.next
31+
print()
32+
33+
graph = Graph(5)
34+
graph.add_edge(0, 1)
35+
graph.add_edge(0, 4)
36+
graph.add_edge(1, 3)
37+
graph.add_edge(1, 4)
38+
graph.add_edge(1, 2)
39+
graph.add_edge(2, 3)
40+
graph.add_edge(3, 4)

AdjacencyListGraphShell.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
2+
Type "help", "copyright", "credits" or "license()" for more information.
3+
>>> [None]*5
4+
[None, None, None, None, None]
5+
>>>
6+
======== RESTART: G:/ProgramsPython/Algorithms/AdjacencyListGraph.py ========
7+
self.graph <__main__.AdjNode object at 0x04177C10> 1 None
8+
self.graph <__main__.AdjNode object at 0x04177C30> 4 <__main__.AdjNode object at 0x04177C10>
9+
self.graph <__main__.AdjNode object at 0x04177DB0> 3 None
10+
self.graph <__main__.AdjNode object at 0x04177DF0> 4 <__main__.AdjNode object at 0x04177DB0>
11+
self.graph <__main__.AdjNode object at 0x04177E30> 2 <__main__.AdjNode object at 0x04177DF0>
12+
self.graph <__main__.AdjNode object at 0x04177C90> 3 None
13+
self.graph <__main__.AdjNode object at 0x04186190> 4 None
14+
>>> graph.graph[0]
15+
<__main__.AdjNode object at 0x04177C30>
16+
>>> graph.graph[0].vertex
17+
4
18+
>>> graph.graph[0].next.vertex
19+
1
20+
>>> graph.graph[1].vertex
21+
2
22+
>>> graph.graph[1].next.vertex
23+
4
24+
>>> graph.graph[1].next.next.vertex
25+
3
26+
>>> graph.graph[0].next.next
27+
>>>
28+
======== RESTART: G:/ProgramsPython/Algorithms/AdjacencyListGraph.py ========
29+
self.graph <__main__.AdjNode object at 0x03747C70> 1 None
30+
self.graph <__main__.AdjNode object at 0x03747C30> 4 <__main__.AdjNode object at 0x03747C70>
31+
self.graph <__main__.AdjNode object at 0x03747DF0> 3 None
32+
self.graph <__main__.AdjNode object at 0x03747E30> 4 <__main__.AdjNode object at 0x03747DF0>
33+
self.graph <__main__.AdjNode object at 0x03747E70> 2 <__main__.AdjNode object at 0x03747E30>
34+
self.graph <__main__.AdjNode object at 0x03747C50> 3 None
35+
self.graph <__main__.AdjNode object at 0x0375A1D0> 4 None
36+
>>> graph.print_graph()
37+
Head->->4->1
38+
Head->->2->4->3
39+
Head->->3
40+
Head->->4
41+
Head->
42+
>>>
43+
======== RESTART: G:/ProgramsPython/Algorithms/AdjacencyListGraph.py ========
44+
self.graph <__main__.AdjNode object at 0x03BE7BD0> 1 None
45+
self.graph <__main__.AdjNode object at 0x03BE7DB0> 4 <__main__.AdjNode object at 0x03BE7BD0>
46+
self.graph <__main__.AdjNode object at 0x03BE7E30> 3 <__main__.AdjNode object at 0x03BE7BF0>
47+
self.graph <__main__.AdjNode object at 0x03BF61B0> 4 <__main__.AdjNode object at 0x03BE7E30>
48+
self.graph <__main__.AdjNode object at 0x03BF6250> 2 <__main__.AdjNode object at 0x03BF61B0>
49+
self.graph <__main__.AdjNode object at 0x03BF6290> 3 <__main__.AdjNode object at 0x03BF6190>
50+
self.graph <__main__.AdjNode object at 0x03BF62D0> 4 <__main__.AdjNode object at 0x03BF61D0>
51+
>>> graph.print_graph()
52+
Head->->4->1
53+
Head->->2->4->3->0
54+
Head->->3->1
55+
Head->->4->2->1
56+
Head->->3->1->0
57+
>>>

0 commit comments

Comments
 (0)