-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdjacencyListGraph.py
80 lines (71 loc) · 2.25 KB
/
AdjacencyListGraph.py
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
ADJACENCY LIST IMPLEMENTATION
---------------------------------------------------------------------------------------------------------
DATA STRUCTURE: DICTIONARY
--------------------------
class AdjacencyList:
def __init__(self, N, M):
self.nodes_num = N
self.edges_num = M
self.adjList = dict()
def edges(self, from_, to_):
if from_ not in self.adjList:
self.adjList[from_] = [to_]
# undirected edge is specified in problem
else:
self.adjList[from_].append(to_)
if to_ not in self.adjList:
self.adjList[to_] = [from_]
else:
self.adjList[to_].append(from_)
def find_edge(self, from_, to_):
for i in (self.adjList[from_]):
if i==to_:
return "YES"
return 'NO'
n, m = map(int, input().split())
adjacency_list = AdjacencyList(n, m)
for i in range(m):
A, B = input().split()
adjacency_list.edges(A, B)
q = int(input())
for i in range(q):
c, d = input().split()
print(adjacency_list.find_edge(c, d))
#print(adjacency_list.adjList)
DATA STRUCTURE: LINKED LIST
-------------------------------
class AdjNode:
def __init__(self, data):
self.vertex = data
self.next = None
class Graph:
def __init__(self, V):
self.v = V
self.graph = [None]*self.v
def add_edge(self, src, dest):
node = AdjNode(dest)
node.next = self.graph[src]
self.graph[src] = node
print("self.graph",
self.graph[src], self.graph[src].vertex, self.graph[src].next)
# In case of Undirected Graph
node = AdjNode(src)
node.next = self.graph[dest]
self.graph[dest] = node
def print_graph(self):
for i in range(self.v):
current = self.graph[i]
print("Head->", end='')
while current:
print("->{}".format(current.vertex),
end='')
current = current.next
print()
graph = Graph(5)
graph.add_edge(0, 1)
graph.add_edge(0, 4)
graph.add_edge(1, 3)
graph.add_edge(1, 4)
graph.add_edge(1, 2)
graph.add_edge(2, 3)
graph.add_edge(3, 4)