ComSSA DSA Revision Depth First and Breadth First
Description: ComSSA DSA Revision Depth First and Breadth First Search Adjacency Lists and Matrices Semester 1, 2023 For optimum use, run in Slideshow mode from beginning and use your mouse wheel to navigate. Introduction We will walk you through Depth
Related Topics
Download Presentation
"ComSSA DSA Revision Depth First and Breadth First" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
slide1. ComSSA DSA RevisionDepth First and Breadth First SearchAdjacency Lists and Matrices Semester 1, 2023 For optimum use, run in Slideshow mode “from beginning” and use your mouse wheel to navigate.<br>
slide2. Introduction We will walk you through Depth First Search (DFS)and Breadth First Search (BFS) with an example.
This will be useful in both the mid-semester test and in Part 4 (Manual BFS/DFS) activity in Prac 6.
On the left, you will see what is happening.On the right, we provide a way to write it down.<br>
slide3. Depth First Search “Tunnel vision” – can only see what is right in front of it. A B D C E G F I H J<br>
slide4. Depth First Search Start at A. A B D C E G F I H J A<br>
slide5. Depth First Search What can A see?
Which is best? A B D C E G F I H J B , C , D A<br>
slide6. Depth First Search B is best, move to B. A B D C E G F I H J B , C , D A B<br>
slide7. Depth First Search What can B see?
Which is best? A B D C E G F I H J D , E B , C , D A B<br>
slide8. Depth First Search D is best, move to D. A B D C E G F I H J D D , E B , C , D A B<br>
slide9. Depth First Search Keep going… A B D C E G F I H J D D , E B , C , D A B C , E , G<br>
slide10. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C<br>
slide11. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G<br>
slide12. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G<br>
slide13. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I<br>
slide14. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H<br>
slide15. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I<br>
slide16. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F<br>
slide17. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J<br>
slide18. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E<br>
slide19. No possibilities! Backtrack to F… Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E<br>
slide20. F still has J left Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E<br>
slide21. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J<br>
slide22. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I<br>
slide23. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I I<br>
slide24. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I I Complete! All nodes found. Path Vertices: A, B, D, C, G, H, F, E, J, I<br>
slide25. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I I Complete! All nodes found. Path Vertices: A, B, D, C, G, H, F, E, J, I
Edges: AB, BD, DC, CG, GH, HF, FE, FJ, JI<br>
slide26. Breadth First Search Is not a path, but a queue. Remove the top each time, spill its contents A B D C E G F I H J A enq<br>
slide27. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue We will now repeat this
process with B, as it is atthe top of the queue.<br>
slide28. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq<br>
slide29. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq<br>
slide30. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq D doesn’t yield anything new.
We keep going.<br>
slide31. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq E deq F enq<br>
slide32. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq E deq F enq G deq H I enq<br>
slide33. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq E deq F enq G deq H I enq F deq J enq<br>
slide34. Breadth First Search A B D C E G F I H J A deq B C D enq B deq E enq C deq G enq D deq E deq F enq G deq H I enq F deq J enq There are no further vertices to reveal. H, I and J are dequeued.<br>
slide35. Breadth First Search A B D C E G F I H J A deq B C D enq B deq E enq C deq G enq D deq E deq F enq G deq H I enq F deq J enq BFS: A, B, C, D, E, G, F, H, I, J.<br>
slide36. Adjacency list and matrix In programs, we can’t “see” the graph, but we can store the graph in the form of an adjacency list and/or adjacency matrix. A B D C E G F I H J<br>
slide37. Adjacency list and matrix In programs, we can’t “see” the graph, but we can store the graph in the form of an adjacency list and/or adjacency matrix. A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J:<br>
slide38. Adjacency list and matrix A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D<br>
slide39. Adjacency list and matrix A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G<br>
slide40. Adjacency list and matrix A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I<br>
slide41. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide42. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide43. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide44. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide45. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide46. Activities Now it’s your turn!
Make an adjacency list and matrix for the following graphs.
Then do a manual DFS and BFS, showing the order in which you visit each node. Assume nodes are sorted alphabetically.
Printed versions of these graphs and the finished example we just walked through will be distributed to your tables.<br>
slide47. Activity 3.1 . A B C D E G F H I J L K M N O (Credit: Sam Bray, 2018)<br>
slide48. Activity 3.2 . A B C D G E H I J K L F<br>
slide49. Solutions to activities The next slides contain the solutions, so you can check your work in the previous activities.<br>
slide50. Activity 3.1 SOLUTION D D B , C A B E , H E G G Path Vertices: A, B, D, E, G, H, I, M, N, K, J, F, C, L, O
Edges: AB, BD, DE, EG, DH, HI, HM, MN, NK, KJ, JF, FC, JL, NO H I , M I M N K J F C L O K, O N, O F, L J C Breadth-first search:
deq A enq B enq Cdeq B enq D
deq C enq F
deq D enq E
enq H
deq F enq J
deq E enq G
deq H enq I
enq M
deq J enq K
enq L
deq M enq N
enq O Adjacency list:
A: K:
B: L:
C: M:
D: N:
E: O:
F:
G:
H:
I:
J: J, N
J
H, N, O
K, M, O
M, N B, C
A, D
A, F
B, E, H
D, G
C, J
E
D, I, M
H
F, K, L A, B, C, D, F,E, H, J, G, I,M, K, L, N, O<br>
slide51. Activity 3.2 SOLUTION D D B, C A B E, G E C, H C Path Vertices: A, B, D, E, C, H, G, F, I, J, L, K
Edges: AB, BD, DE, EC, EH, HG, GF, HI, IJ, JL, LK H G, I, J G F I J L K J, K F K L Breadth-first search:
deq A enq B enq Cdeq B enq D
deq C enq E
deq D enq G
deq E enq H
deq G enq F
deq H enq I
enq J
deq I enq K
deq J enq L Adjacency list:
A:
B:
C:
D:
E:
F:
G:
H:
I:
J:
K:
L: B, C
A, D
A, E
B, E, G
C, D, H
G
D, F, H
E, G, I, J
H, J, K
H, I, L
I, L
J, K A, B, C, D, E,G, H, F, I, J, K, L<br>
slide2. Introduction We will walk you through Depth First Search (DFS)and Breadth First Search (BFS) with an example.
This will be useful in both the mid-semester test and in Part 4 (Manual BFS/DFS) activity in Prac 6.
On the left, you will see what is happening.On the right, we provide a way to write it down.<br>
slide3. Depth First Search “Tunnel vision” – can only see what is right in front of it. A B D C E G F I H J<br>
slide4. Depth First Search Start at A. A B D C E G F I H J A<br>
slide5. Depth First Search What can A see?
Which is best? A B D C E G F I H J B , C , D A<br>
slide6. Depth First Search B is best, move to B. A B D C E G F I H J B , C , D A B<br>
slide7. Depth First Search What can B see?
Which is best? A B D C E G F I H J D , E B , C , D A B<br>
slide8. Depth First Search D is best, move to D. A B D C E G F I H J D D , E B , C , D A B<br>
slide9. Depth First Search Keep going… A B D C E G F I H J D D , E B , C , D A B C , E , G<br>
slide10. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C<br>
slide11. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G<br>
slide12. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G<br>
slide13. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I<br>
slide14. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H<br>
slide15. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I<br>
slide16. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F<br>
slide17. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J<br>
slide18. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E<br>
slide19. No possibilities! Backtrack to F… Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E<br>
slide20. F still has J left Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E<br>
slide21. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J<br>
slide22. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I<br>
slide23. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I I<br>
slide24. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I I Complete! All nodes found. Path Vertices: A, B, D, C, G, H, F, E, J, I<br>
slide25. Depth First Search A B D C E G F I H J D D , E B , C , D A B C , E , G C G G H , I H F , I F E , J E J I I Complete! All nodes found. Path Vertices: A, B, D, C, G, H, F, E, J, I
Edges: AB, BD, DC, CG, GH, HF, FE, FJ, JI<br>
slide26. Breadth First Search Is not a path, but a queue. Remove the top each time, spill its contents A B D C E G F I H J A enq<br>
slide27. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue We will now repeat this
process with B, as it is atthe top of the queue.<br>
slide28. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq<br>
slide29. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq<br>
slide30. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq D doesn’t yield anything new.
We keep going.<br>
slide31. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq E deq F enq<br>
slide32. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq E deq F enq G deq H I enq<br>
slide33. Breadth First Search A B D C E G F I H J A deq B C D enq top of queue B deq E enq C deq G enq D deq E deq F enq G deq H I enq F deq J enq<br>
slide34. Breadth First Search A B D C E G F I H J A deq B C D enq B deq E enq C deq G enq D deq E deq F enq G deq H I enq F deq J enq There are no further vertices to reveal. H, I and J are dequeued.<br>
slide35. Breadth First Search A B D C E G F I H J A deq B C D enq B deq E enq C deq G enq D deq E deq F enq G deq H I enq F deq J enq BFS: A, B, C, D, E, G, F, H, I, J.<br>
slide36. Adjacency list and matrix In programs, we can’t “see” the graph, but we can store the graph in the form of an adjacency list and/or adjacency matrix. A B D C E G F I H J<br>
slide37. Adjacency list and matrix In programs, we can’t “see” the graph, but we can store the graph in the form of an adjacency list and/or adjacency matrix. A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J:<br>
slide38. Adjacency list and matrix A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D<br>
slide39. Adjacency list and matrix A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G<br>
slide40. Adjacency list and matrix A B D C E G F I H J Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I<br>
slide41. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide42. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide43. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide44. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide45. Adjacency list and matrix Adjacency List
A:
B:
C:
D:
E:
F:
G:
H:
I:J: B, C, D
A, D, E
A, D, G
B, C, E, G
B, D, F
E, H, J
C, D, H, I
F, G, I
G, H, JF, I Adjacency Matrix<br>
slide46. Activities Now it’s your turn!
Make an adjacency list and matrix for the following graphs.
Then do a manual DFS and BFS, showing the order in which you visit each node. Assume nodes are sorted alphabetically.
Printed versions of these graphs and the finished example we just walked through will be distributed to your tables.<br>
slide47. Activity 3.1 . A B C D E G F H I J L K M N O (Credit: Sam Bray, 2018)<br>
slide48. Activity 3.2 . A B C D G E H I J K L F<br>
slide49. Solutions to activities The next slides contain the solutions, so you can check your work in the previous activities.<br>
slide50. Activity 3.1 SOLUTION D D B , C A B E , H E G G Path Vertices: A, B, D, E, G, H, I, M, N, K, J, F, C, L, O
Edges: AB, BD, DE, EG, DH, HI, HM, MN, NK, KJ, JF, FC, JL, NO H I , M I M N K J F C L O K, O N, O F, L J C Breadth-first search:
deq A enq B enq Cdeq B enq D
deq C enq F
deq D enq E
enq H
deq F enq J
deq E enq G
deq H enq I
enq M
deq J enq K
enq L
deq M enq N
enq O Adjacency list:
A: K:
B: L:
C: M:
D: N:
E: O:
F:
G:
H:
I:
J: J, N
J
H, N, O
K, M, O
M, N B, C
A, D
A, F
B, E, H
D, G
C, J
E
D, I, M
H
F, K, L A, B, C, D, F,E, H, J, G, I,M, K, L, N, O<br>
slide51. Activity 3.2 SOLUTION D D B, C A B E, G E C, H C Path Vertices: A, B, D, E, C, H, G, F, I, J, L, K
Edges: AB, BD, DE, EC, EH, HG, GF, HI, IJ, JL, LK H G, I, J G F I J L K J, K F K L Breadth-first search:
deq A enq B enq Cdeq B enq D
deq C enq E
deq D enq G
deq E enq H
deq G enq F
deq H enq I
enq J
deq I enq K
deq J enq L Adjacency list:
A:
B:
C:
D:
E:
F:
G:
H:
I:
J:
K:
L: B, C
A, D
A, E
B, E, G
C, D, H
G
D, F, H
E, G, I, J
H, J, K
H, I, L
I, L
J, K A, B, C, D, E,G, H, F, I, J, K, L<br>