forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.py
More file actions
65 lines (62 loc) · 1.46 KB
/
BFS.py
File metadata and controls
65 lines (62 loc) · 1.46 KB
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
#Breadth first search
class queue():
def __init__(self):
self.array = []
def push(self, data):
self.array.append(data)
def pop(self):
if not self.array:
return
del self.array[0]
def top(self):
return self.array[0]
def isEmpty(self):
if not self.array:
return True
return False
def bfs(m, q, result, visited):
#If q is not empty
if not q.isEmpty():
#Pop the top element
t = q.top()
q.pop()
#Add it to result
result.append(t)
#Add all its adjacent unvisited elements to the q
for adjacentElement in range(9):
if(m[t][adjacentElement] == 1 and visited[adjacentElement] == 0):
#Mark it visited
visited[adjacentElement] = 1
#Add it to the q
q.push(adjacentElement)
bfs(m, q, result, visited)
return result
def foo(matrix):
q = queue()
q.push(0)
v = [0 for col in range(9)]
v[0] = 1
r = bfs(matrix, q, [], v)
print r
#Main Program
matrix = [[0 for col in range(9)] for row in range(9)]
matrix[0][1] = 1
matrix[0][8] = 1
matrix[1][0] = 1
matrix[8][0] = 1
matrix[8][2] = 1
matrix[8][6] = 1
matrix[2][3] = 1
matrix[2][4] = 1
matrix[2][5] = 1
matrix[2][8] = 1
matrix[3][2] = 1
matrix[4][2] = 1
matrix[4][7] = 1
matrix[5][2] = 1
matrix[5][6] = 1
matrix[6][8] = 1
matrix[6][5] = 1
matrix[7][4] = 1
matrix[7][6] = 1
foo(matrix)