Breadth-First Search Graph Algorithm Type #3. Depth-First Search.

Slides:



Advertisements
Similar presentations
Jecho and Donatus Depth First and Breadth First Search.
Advertisements

Artificial Intelligence By Mr. Ejaz CIIT Sahiwal.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph Algorithms Mathematical Structures for Computer Science Chapter 6 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraph Algorithms.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs.
17-Jun-15 Searching in BeeperHunt (Mostly minor variations on old slides)
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Using Search in Problem Solving
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.
Milestone 3: Finding Routes ECE 297. Directions: How?
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
Search Related Algorithms. Graph Code Adjacency List Representation:
Tree Searching Breadth First Search Dept First Search.
GRAPHS
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Administration CI meetings resume next week, as usual Some TAs in labs, plus Dr. Betz –Go to your lab as usual –If your TA is not there, ask for help from.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Lecture 3: Uninformed Search
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Ricochet Robots Mitch Powell Daniel Tilgner. Abstract Ricochet robots is a board game created in Germany in A player is given 30 seconds to find.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Search Techniques CS480/580 Fall Introduction Trees: – Root, parent, child, sibling, leaf node, node, edge – Single path from root to any node Graphs:
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
Graphs and Paths : Chapter 15 Saurav Karmakar
A* Reference: “Artificial Intelligence for Games”, Ian Millington.
Source: David Lee Matuszek
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Graphs – Part III CS 367 – Introduction to Data Structures.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Breadth-First Search Graph Algorithm Type #3.
Depth First Seach: Output Fix
Breadth-First Search: Complexity
Tutorial 8 An optional eighth tutorial will be held the week of March 6. This tutorial will give you practice with and feedback on oral presentation and.
Search Related Algorithms
Milestone 3: Finding Routes
Breadth-First Search Graph Algorithm #2.
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Efficiently Estimating Travel Time
BFS: Min. Path Issues What happened? Node 1 was re-expanded
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Graph Search in C++ Andrew Lindsay.
Presentation transcript:

Breadth-First Search Graph Algorithm Type #3

Depth-First Search

More Orderly Technique?

Breadth First Search (BFS) int main () { Node *sourceNode = getNodebyID (sourceID); bool found = bfsPath (sourceNode, destID);... } bool bfsPath (Node* sourceNode, int destID) {... } source dest start – 0 hops 1 hop away 2 hops away 3 hops away

Breadth First Search bool bfsPath (Node* sourceNode, int destID) { list wavefront; wavefront.push_back (sourceNode); while (wavefront not empty) { Node *currNode = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront if (currNode->id == destID) return (true); for each (outEdge of currNode) { Node *toNode = outEdge.toNode; wavefront.push_back (toNode); } } return (false); // No path exits! } source dest wavefront = {0} {1, 2} {2, 3} {3,4,3} {4,3,5} {5} wavefront = {} {2} {3} {4,3} {3,5} {5,5} {5}

BFS: How Do I Print Out the Path? source dest bool bfsPath (Node* sourceNode, int destID) { list wavefront; wavefront.push_back (sourceNode); while (wavefront not empty) { Node *currNode = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront if (currNode->id == destID) return (true); for each (outEdge of currNode) { Node *toNode = outEdge.toNode; wavefront.push_back (toNode); } return (false); // No path exits! } wavefront = {0} {1, 2} {2, 3} {3,4,3} {4,3,5} {5} wavefront = {} {2} {3} {4,3} {3,5} What do I know here? {5,5} {5}

BFS: How Do I Print Out the Path? Need more information! struct waveElem { Node *node; int edgeID; // ID of edge used to reach this node waveElem (Node *n, int id) {node = n; edgeID = id;} }; #define NO_EDGE -1 // Illegal edge ID  no edge Why did I use an int instead of an Edge?

BFS: How Do I Print Out the Path? source dest bool bfsPath (Node* sourceNode, int destID) { list wavefront; wavefront.push_back (waveElem (sourceNode, NO_EDGE)); while (wavefront not empty) { waveElem curr = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront if (curr.node->id == destID) return (true); for each (outEdge of curr.node) { Node *toNode = outEdge.toNode; wavefront.push_back ( waveElem(toNode, outEdge.id); } return (false); // No path exits! } {4/c,5/f} f c {5/f} Know I got here from edge f. Good enough? a b de

BFS: How Do I Print Out the Path? bool bfsPath (Node* sourceNode, int destID) { list wavefront; wavefront.push_back (waveElem (sourceNode, NO_EDGE)); while (wavefront not empty) { waveElem curr = wavefront.front (); wavefront.pop_front(); // Remove node from wavefront curr.node->reachingEdge = curr.edgeID; if (curr.node->id == destID) return (true); for each (outEdge of curr.node) { Node *toNode = outEdge.toNode; wavefront.push_back ( waveElem(toNode, outEdge.id); } } return (false); // No path exits! } source dest f c a b de NO_EDGE a b ec f d

BFS: How Do I Print Out the Path? int main () { Node *sourceNode = getNodebyID (sourceID); bool found = bfsPath (sourceNode, destID); if (found) list path = bfsTraceBack (destID); } list bfsTraceBack (int destID) { list path; Node *currNode = getNodebyID (destID); prevEdge = currNode->reachingEdge; while (prevEdge != NO_EDGE) { path.push_front (prevEdge); currNode = prevEdge.fromNode; prevEdge = currNode->reachingEdge; } return (path); } source dest f c a b de NO_EDGE a b c f d path = {}path = {f} path = {f,d}path = {f,d,b}

reachingEdge = areachingEdge = c Min. Path Issues source dest c a b d wavefront = {0}{1,2}{2,3}{3,1} 4 {1,4}{4,3} b d e e What happened? Node 1 was re-expanded Overwrote the reachingEdge Messed up the path traceback

d source reachingEdge = a Solution? dest c a b d wavefront = {0}{1,2}{2,3}{3,1} 4 {1,4} {4} b e e Mark nodes as visited when you reach them Never allow them to be visited again

d source reachingEdge = a Solution? dest c a b d wavefront = {0}{1,2}{2,3}{3,1} 4 {1,4} {4} b e e Another test case With travel times t = 5 s t = 20 s

e d source reachingEdge = a Visited Flag: Problem dest c a b d b e t = 5 s t = 20 s Shortest path Path we obtain

a, 20 sc, 10 s Solution #2 source dest c a b d wavefront = {0}{1,2}{2,3}{3,1} 4 {1,4}{4,3} b, 5 s d, 26 s e, 29 s e 1.Store shortest travel time from source at each node 2.Only process node if shortest travel time to it has dropped t = 5 s t = 20 s t = 6 s t = 3 s Half fixed. Got right path, but wrong travel time at dest!

a, 20 s Solution #2 source dest c a b d wavefront = {0}{1,2}{2,3}{3,1} b, 5 s d, 26 s Another Test Case t = 5 s t = 20 s t = 6 s This path is wrong !

a, 20 sc, 10 s Solution #2++ source dest c a b d wavefront = {0/0s}{1/20s,2/5s}{2/5s,3/26s}{3/26s,1/10s} 4 {1/10s,4/29s}{4/29s,3/16s} b, 5 s d, 26 s e, 29 s e 1.Store shortest travel time from source at each node 2.Only update reachingEdge if shortest travel time drops 3.Don’t stop BFS on first reach of destination Continue BFS until all entries in wavefront have higher travel time than best path found so far t = 5 s t = 20 s t = 6 s t = 3 s d, 16 s e, 19 s {3/16s}{4/19s}