Graph bfs
- 1. 10
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
Graph g:
myG.txt
6
8
0 5
2 4
2 3
1 2
0 1
3 4
3 5
0 2
V
E
Adjacency lists
adj[]
0
1
2
3
4
5
2
3
3
0
5
1
0
3
1
2
4
2
2
0
5
0
1
2
5
3 4
4
Challenge: build adjacency lists – Job done
- 2. 11
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
add 0 to queue:
myG.txt
6
8
0 5
2 4
2 3
1 2
0 1
3 4
3 5
0 2
V
E
Adjacency lists
adj[]
0
1
2
3
4
5
2
3
3
0
5
1
0
3
1
2
4
2
2
0
5
0
1
2
5
3 4
4
- 3. 12
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
add 0 to queue:
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 - -
3 - -
4 - -
5 - -
queue
0
- 4. 13
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
0
2 1 5adj[0]
- 5. 14
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
2 1 5adj[0]
- 6. 15
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
2
2 1 5adj[0]
- 7. 16
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 - -
2 0 1
3 - -
4 - -
5 - -
queue
2
2 1 5adj[0]
- 8. 17
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 - -
queue
1
2
2 1 5adj[0]
- 9. 18
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 - -
queue
1
2
2 1 5adj[0]
- 10. 19
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 0: check2, check 1 and check 5
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
2
2 1 5adj[0]
- 11. 20
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
0 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
2
2 1 5adj[0]
- 12. 21
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2:
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
2
- 13. 22
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
adj[2] 0 1 3 4
- 14. 23
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 - -
4 - -
5 0 1
queue
5
1
adj[2] 0 1 3 4
- 15. 24
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 - -
5 0 1
queue
3
5
1
adj[2] 0 1 3 4
- 16. 25
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 - -
5 0 1
queue
3
5
1
adj[2] 0 1 3 4
- 17. 26
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 2: check 0, check 1, check 3 and check 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 - -
5 0 1
queue
4
3
5
1
adj[2] 0 1 3 4
- 18. 27
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
2 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
1
adj[2] 0 1 3 4
- 19. 28
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
1
- 20. 29
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
- 21. 30
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1: check 0, and check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
- 22. 31
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1: check 0, and check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
- 23. 32
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 1: check 0, and check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
- 24. 33
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
1 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[1] 0 2
- 25. 34
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 5: check 3 and check 0
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
5
adj[5] 3 0
- 26. 35
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
5 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
adj[5] 3 0
- 27. 36
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 3: Check 5, Check 4, and Check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
adj[3] 5 4 2
- 28. 37
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 3:
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
3
- 29. 38
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 3: Check 5, Check 4, and Check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
adj[3] 5 4 2
- 30. 39
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
3 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
adj[3] 5 4 2
- 31. 40
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 4
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
4
- 32. 41
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
dequeue 4: Check 3 and Check 2
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
adj[4] 3 2
- 33. 42
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
4 done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1
queue
- 34. 43
Breadth-first search demo
Repeat until queue is empty:
• Remove vertex v from queue.
• Add to queue all unmarked vertices adjacent to v and mark them.
done
0
1
2
5
3 4
v edgeTo[v] distTo[]
0 - 0
1 0 1
2 0 1
3 2 2
4 2 2
5 0 1