GRAPHS: SEARCH
Delve into the world of graphs, trees, and their applications in computer science. Explore concepts such as tree traversal, graph search algorithms, and more. Discover how to print vertices in a tree and the various traversal methods used. Gain insights into stack and queue operations, tree BFS, and practical implementations. Dive deep into the foundational knowledge of these essential data structures.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
GRAPHS: SEARCH David Kauchak CS 62 Spring 2021
Admin Assignment 9: Text Generation
Graphs A graph is a set of vertices V and a set of edges (u,v) E where u,v V A B F D E C G
Searching a tree A B E D C F G How can we print out all of the vertices in a tree?
Searching a tree A B E D C F G We could do any of the traversals we saw before: pre-order, in-order, post-order
A flash from the past Stack: - LIFO - Add to the back - Remove from the back Queue: - FIFO - Add to the back - Remove from the front
Searching a tree treeBFS( start ) q = new Queue() q.add(start) treeSearch(q) treeDFS( start ) s = new Stack() s.add(start) treeSearch(s) treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c)
Tree BFS A treeBFS( start ) q = new Queue() q.add(start) treeSearch(q) B E D C F G q: Visited:
Tree BFS A treeBFS( start ) q = new Queue() q.add(start) treeSearch(q) B E D C F G q: A Visited:
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: A printed: What order will the nodes get printed out? Assume children are traversed left to right.
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: A printed:
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: printed: A
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: printed: A
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: B D E printed: A
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: B D E printed: A
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: D E printed: A B
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: D E printed: A B
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: D E C F printed: A B
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: D E C F printed: A B
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: E C F printed: A B D
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: E C F printed: A B D
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: E C F printed: A B D
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: E C F printed: A B D
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: C F printed: A B D E
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: C F printed: A B D E
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: C F G printed: A B D E
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: C F G printed: A B D E How are we exploring the vertices?
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G Frontier: all vertices a given number of edges from the start/root toVisit-queue: C F G printed: A B D E
Tree BFS = Tree breadth first search A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G Frontier: all vertices a given number of edges from the start/root toVisit-queue: C F G printed: A B D E
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: C F G printed: A B D E
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: F G printed: A B D E C
Tree BFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-queue: printed: A B D E C F G
Tree DFS treeDFS( start ) s = new Stack() s.add(start) treeSearch(s) A B E D C F G s: printed:
Tree DFS treeDFS( start ) s = new Stack() s.add(start) treeSearch(s) A B E D C F G s: A printed:
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: A printed: What order will the nodes get printed out? Assume children are traversed left to right.
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: A printed:
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: printed: A
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: printed: A
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D E printed: A
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D E printed: A
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D printed: A E
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D printed: A E
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D G printed: A E
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D G printed: A E
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D printed: A E G
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D printed: A E G
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B D printed: A E G
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B printed: A E G D
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G toVisit-stack: B printed: A E G D How are we exploring the vertices?
Tree DFS A treeSearch( toVisit ) while !toVisit.empty() v = toVisit.remove() // visit v, e.g., print it out for c in v.getChildren() toVisit.add(c) B E D C F G Frontier: go as far down one branch as possible, working right to left toVisit-stack: B printed: A E G D