Breadth-First Search Graph Algorithm Type #3.

Slides:



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

Problem solving with graph search
Artificial Intelligence By Mr. Ejaz CIIT Sahiwal.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Breadth-First Search Graph Algorithm Type #3. Depth-First Search.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Using Search in Problem Solving
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Milestone 3: Finding Routes ECE 297. Directions: How?
“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.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Review: Tree search Initialize the frontier using the starting state
Uniformed Search (cont.) Computer Science cpsc322, Lecture 6
Graphs Chapter 20.
Breadth First and Depth First
A vertex u is reachable from vertex v iff there is a path from v to u.
Depth First Seach: Output Fix
Breadth-First Search: Complexity
Graph Search Lecture 17 CS 2110 Fall 2017.
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.
Data Structures and Database Applications Binary Trees in C#
Unweighted Shortest Path Neil Tang 3/11/2010
Uniformed Search (cont.) Computer Science cpsc322, Lecture 6
Algorithms for Exploration
CS120 Graphs.
Prof. Dechter ICS 270A Winter 2003
More Graph Algorithms.
CSC 172 DATA STRUCTURES.
Problem Solving and Searching
Search Related Algorithms
What to do when you don’t know anything know nothing
CSE 214 – Computer Science II Graph Walking
Problem Solving and Searching
Depth-First Searches Introduction to AI.
Graphs Part 2 Adjacency Matrix
Tree Searching.
Subgraphs, Connected Components, Spanning Trees
Section 5: HW6 and Interfaces
Discrete Math 2 Shortest Path Using Matrix
CSE 373: Data Structures and Algorithms
Milestone 3: Finding Routes
Routing.
CSE 473 University of Washington
Breadth-First Search Graph Algorithm #2.
Algorithms Lecture # 29 Dr. Sohail Aslam.
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Algorithms: Design and Analysis
Efficiently Estimating Travel Time
BFS: Min. Path Issues What happened? Node 1 was re-expanded
More advanced aspects of search
A vertex u is reachable from vertex v iff there is a path from v to u.
Tree Searching.
Informed Search Idea: be smart about what paths to try.
Tree Searching.
Tree Searching.
Graphs.
Graphs.
UNINFORMED SEARCH -BFS -DFS -DFIS - Bidirectional
Graphs.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Graphs.
Graph Search in C++ Andrew Lindsay.
Lecture 11 Graph Algorithms
Informed Search Idea: be smart about what paths to try.
Lecture 4: Tree Search Strategies
Depth-First Searches.
Presentation transcript:

Breadth-First Search Graph Algorithm Type #3

Depth-First Search

More Orderly Technique?

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

Breadth First Search source dest bool bfsPath (Node* sourceNode, int destID) { list<Node *> wavefront; // Nodes to explore next 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 = {2} {} {3} {5} {3,5} {5} {5,5} {4,3,5} {4,3} {2, 3} {0} {1, 2} {3,4,3} source dest 1 2 3 4 5

Breadth First Search source dest bool bfsPath (Node* sourceNode, int destID) { list<Node *> wavefront; // Nodes to explore next 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 = {2} {} {3} {5} {3,5} {5} {5,5} {4,3,5} {4,3} {2, 3} {3,4,3} {0} {1, 2} source dest 1 2 3 4 What do I know here? 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 class Node { ... // Outgoing edges etc. int reachingEdge; // ID of the edge used to reach this node } Why did I use an int instead of an Edge?

BFS: How Do I Print Out the Path? bool bfsPath (Node* sourceNode, int destID) { list<WaveElem> 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! wavefront = {0/NO_EDGE} wavefront = {1/a, 2/b} wavefront = {} wavefront = {2/b} wavefront = {2/b, 3/e} source NO_EDGE b a 1 a 2 b e d c 3 d e 4 c f 5 dest f

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

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

Solution #1 Store shortest travel time from source at each node wavefront = {0} {1,2} {1,4} {4,3} {3,1} {2,3} b t = 5 s t = 5 s a 2 b, 5 s c t = 5 s a, 5 s 1 Store shortest travel time from source at each node Only process node if shortest travel time to it has dropped d t = 6 s d, 11 s 3 t = 3 s dest e e, 14 s 4 Fixed!

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

Solution #2 Store shortest travel time from source at each node wavefront = {2/5s,3/26s} {1/20s,2/5s} {1/10s,4/29s} {3/16s} {} {4/19s} {0/0s} {4/29s,3/16s} {3/26s,1/10s} b t = 5 s t = 20 s a 2 b, 5 s c t = 5 s Store shortest travel time from source at each node Only update reachingEdge if shortest travel time drops 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 c, 10 s a, 20 s 1 d t = 6 s d, 16 s d, 26 s 3 t = 3 s dest e e, 19 s e, 29 s 4

BFS with Re-expansion bool bfsPath (Node* sourceNode, int destID) { ... bool found = false; while (wavefront not empty && has entry < bestPathLen) { WaveElem wave = wavefront.front (); wavefront.pop_front(); // Remove from wavefront Node *node = wave.node; if (wave.pathLen < node->pathLen) { node->reachingEdge = wave.edgeID; node->pathLen = wave.pathLen; if (node->id == destID) { found = true; bestPathLen = node->pathLen; }

BFS with Re-expansion for each (outEdge of node) { Node *toNode = outEdge.toNode; wavefront.push_back ( WaveElem(toNode, outEdge.id, node.pathLen + travelTime (outEdge)); } } // End if best path to this node } // End while wavefront might yield a better solution return (found);