Breadth-First Search Graph Algorithm #2.

Slides:



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

Artificial Intelligence By Mr. Ejaz CIIT Sahiwal.
Breadth-First Search Graph Algorithm Type #3. Depth-First Search.
Breadth First Search
Using Search in Problem Solving
Milestone 3: Finding Routes ECE 297. Directions: How?
Search Related Algorithms. Graph Code Adjacency List Representation:
Tree Searching Breadth First Search Dept First Search.
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.
1 3/21/2016 MATH 224 – Discrete Mathematics First we determine if a graph is connected.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Simple Search Algorithm
BCA-II Data Structure Using C Submitted By: Veenu Saini
GRAPH TRAVERSING BY PROF. UZAIR SALMAN
Breadth-First Search Graph Algorithm Type #3.
Graphs – Breadth First Search
CSE 373 Data Structures and Algorithms
Depth First Search Neil Tang 4/1/2010
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
Articulation Points 1 of 2 (Ideas)
Breadth-First Search: Complexity
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
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.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Data Structures and Database Applications Binary Trees in C#
Unweighted Shortest Path Neil Tang 3/11/2010
هوش مصنوعی فصل سوم: حل مسئله با جستجو
CS120 Graphs.
Recursion.
Prof. Dechter ICS 270A Winter 2003
CSC 172 DATA STRUCTURES.
Section 5: HW6 and Interfaces
Graph Search Lecture 17 CS 2110 Spring 2018.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Search Related Algorithms
Can you get there from here?
Breadth-First Searches
Lectures on Graph Algorithms: searching, testing and sorting
CSE 214 – Computer Science II Graph Walking
Depth-First Searches Introduction to AI.
Graphs Part 2 Adjacency Matrix
Tree Searching.
Subgraphs, Connected Components, Spanning Trees
Section 5: HW6 and Interfaces
Milestone 3: Finding Routes
Algorithms Lecture # 29 Dr. Sohail Aslam.
Depth First Search Neil Tang 4/10/2008
Depth-First Search CSE 2011 Winter April 2019.
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Algorithms: Design and Analysis
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Graphs Chapter 7 Visit for more Learning Resources.
Section 5: HW6 and Interfaces
BFS: Min. Path Issues What happened? Node 1 was re-expanded
Depth-First Search CSE 2011 Winter April 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Tree Searching.
Tree Searching.
Tree Searching.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Graph Search in C++ Andrew Lindsay.
Lecture 11 Graph Algorithms
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1
Depth-First Searches.
Presentation transcript:

Breadth-First Search Graph Algorithm #2

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 exists! 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

Backtracing – General Idea At each node (intersection) store the edge (streetSegment) that got you there When you reach the destination, follow these stored edges (streetSegments) back to the source Creates the path

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 }

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 exists! 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 = node at other end of prevEdge; 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