Skip to content

Commit 312db35

Browse files
Graph: Adjacency List
Implementation of Adjacency List using different Data Structures.
1 parent fae8707 commit 312db35

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

AdjacencyListGraph.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
1-
# Adjacency List Implementation:
1+
ADJACENCY LIST IMPLEMENTATION
2+
---------------------------------------------------------------------------------------------------------
3+
DATA STRUCTURE: DICTIONARY
4+
--------------------------
5+
class AdjacencyList:
6+
def __init__(self, N, M):
7+
self.nodes_num = N
8+
self.edges_num = M
9+
self.adjList = dict()
10+
11+
def edges(self, from_, to_):
12+
if from_ not in self.adjList:
13+
self.adjList[from_] = [to_]
14+
# undirected edge is specified in problem
15+
else:
16+
self.adjList[from_].append(to_)
17+
if to_ not in self.adjList:
18+
self.adjList[to_] = [from_]
19+
else:
20+
self.adjList[to_].append(from_)
21+
def find_edge(self, from_, to_):
22+
for i in (self.adjList[from_]):
23+
if i==to_:
24+
return "YES"
25+
return 'NO'
26+
n, m = map(int, input().split())
27+
adjacency_list = AdjacencyList(n, m)
28+
for i in range(m):
29+
A, B = input().split()
30+
adjacency_list.edges(A, B)
31+
q = int(input())
32+
for i in range(q):
33+
c, d = input().split()
34+
print(adjacency_list.find_edge(c, d))
35+
#print(adjacency_list.adjList)
36+
37+
38+
39+
40+
DATA STRUCTURE: LINKED LIST
41+
-------------------------------
242
class AdjNode:
343
def __init__(self, data):
444
self.vertex = data

0 commit comments

Comments
 (0)