-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph_206009L.py
77 lines (62 loc) · 1.65 KB
/
Graph_206009L.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
class Graph:
# Initialize the matrix
def __init__(self, size):
self.matrix = []
for i in range(size):
self.matrix.append([0] * size)
self.size = size
# Add edges
def addEdge(self, u, v):
self.matrix[u][v] = 1
self.matrix[v][u] = 1
# Check whether (u,v) is an edge
def isEdge(self, u, v):
if self.matrix[u][v] == 1:
print("An edge exists between %d and %d" % (u, v))
return
else:
print("No edge between %d and %d" % (u, v))
return
def BFS(self, x):
visited = [False] * self.size
queue = [x]
visited[x] = True
while queue:
vis = queue[0]
print(vis, end = ' ')
queue.pop(0)
for i in range(self.size):
if (self.matrix[vis][i] == 1 and
(not visited[i])):
queue.append(i)
visited[i] = True
def DFS(self, x):
stack = []
visited = [False] * self.size
visited[x] = True
stack.append(x)
print(x, end = " ")
for i in range(x+1):
if (self.matrix[x][i] == 1 and
(not visited[i])):
self.DFS(i)
# Creating a graph with a matrix structure of size 6
g = Graph(6)
# Adding edges to the graph
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
g.addEdge(5, 3)
g.addEdge(2, 5)
g.addEdge(4, 5)
g.addEdge(1, 5)
# Checking whether edge exists
g.isEdge(2,5)
g.isEdge(0,5)
# Performing a BFS for 5
g.BFS(5)
# Performing a DFS for 3
g.DFS(3)