Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2541905

Browse files
Create Graph-AdjacencyList1
Takes O(n) time complexity to add nodes to linked list for every vertex. The edges are added at the end of Linked List for each vertex.
1 parent 03db552 commit 2541905

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎Graph-AdjacencyList1

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /