Depth First Seach: Output Fix

Slides:



Advertisements
Similar presentations
Review: Search problem formulation
Advertisements

Solving Problem by Searching
Dijkstra’s Algorithm Keep Going!. Pre-Computing Shortest Paths How many paths to pre-compute? Recall: –Using single-source to single-dest find_path: Need.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Solving Problems by Searching Currently at Chapter 3 in the book Will finish today/Monday, Chapter 4 next.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Breadth-First Search Graph Algorithm Type #3. Depth-First Search.
CPSC 322, Lecture 9Slide 1 Search: Advanced Topics Computer Science cpsc322, Lecture 9 (Textbook Chpt 3.6) January, 23, 2009.
Mobile and Wireless Computing Institute for Computer Science, University of Freiburg Western Australian Interactive Virtual Environments Centre (IVEC)
Graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Milestone 3: Finding Routes ECE 297. Directions: How?
Informed search strategies Idea: give the algorithm “hints” about the desirability of different states – Use an evaluation function to rank nodes and select.
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.
Graphs – Part II CS 367 – Introduction to Data Structures.
Review: Tree search Initialize the frontier using the starting state While the frontier is not empty – Choose a frontier node to expand according to search.
Lecture 3: Uninformed Search
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
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.
Breadth First Search and Depth First Search. Greatest problem in Computer Science Has lead to a lot of new ideas and data structures Search engines before.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
NETWORK FLOWS Shruti Aggrawal Preeti Palkar. Requirements 1.Implement the Ford-Fulkerson algorithm for computing network flow in bipartite graphs. 2.For.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Chapter 3 Solving problems by searching. Search We will consider the problem of designing goal-based agents in observable, deterministic, discrete, known.
Traveling Courier / Milestone 4 Continued. Recall Pre-compute all shortest paths you might need? –Then just look up delays during pertubations How many.
Breadth-First Search Graph Algorithm Type #3.
Lecture 3: Uninformed Search
Review: Tree search Initialize the frontier using the starting state
Search: Advanced Topics Computer Science cpsc322, Lecture 9
Graphs Representation, BFS, DFS
Breadth-First Search: Complexity
May 12th – Minimum Spanning Trees
Spanning Trees.
CPU Efficiency Issues.
Algorithm Analysis CSE 2011 Winter September 2018.
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.
Single-Source Shortest Paths
Routing: Distance Vector Algorithm
Search: Advanced Topics Computer Science cpsc322, Lecture 9
Data Structures and Algorithms for Information Processing
Discussion section #2 HW1 questions?
Graphs Representation, BFS, DFS
Graphs & Graph Algorithms 2
Graphs Chapter 13.
CS 188: Artificial Intelligence Fall 2008
Search Related Algorithms
Graphs.
Big-O, Ω, ϴ Trevor Brown CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Artificial Intelligence
Search: Advanced Topics Computer Science cpsc322, Lecture 9
Networks Kruskal’s Algorithm
CSE 373: Data Structures and Algorithms
Milestone 3: Finding Routes
Breadth-First Search Graph Algorithm #2.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Efficiently Estimating Travel Time
BFS: Min. Path Issues What happened? Node 1 was re-expanded
Depth-First Search CSE 2011 Winter April 2019.
Graphs.
Graphs.
Graphs.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Graphs.
Graph Search in C++ Andrew Lindsay.
Presentation transcript:

Depth First Seach: Output Fix bool findPath (Node* currNode, int destID, list<Edge> path) { if (currNode->id == destID) return true; currNode->visited = true; for each (outEdge of currNode) { Node *toNode = outEdge.toNode; if (!toNode->visited) { list<Edge> newPath = path; newPath.push_back (outEdge); bool found = findPath (toNode, destID, newPath); if (found) return (true); } return (false); Anything Missing? {a,b} {a,b,c} a b c d {} {a,b,c,d} {a} dest source

Easy Fix Part 2 dest source bool findPath (Node* currNode, int destID, list<Edge> path) { if (currNode->id == destID) print out or save the path, then return (true); currNode->visited = true; for each (outEdge of currNode) { Node *toNode = outEdge.toNode; if (!toNode->visited) { list<Edge> newPath = path; newPath.push_back (outEdge); bool found = findPath (toNode, destID, newPath); if (found) return (true); } return (false); dest source

Depth First Search (DFS) Developed depth first search in a graph Need a visited flag Unlike a tree (can go in circles otherwise) Graph is big (>100,000 nodes, N) Complexity of DFS?

Complexity Analysis bool findPath (Node* currNode, int destID, list<Edge> path) { if (currNode->id == destID) print out or save the path, then return (true); currNode->visited = true; for each (outEdge of currNode) { Node *toNode = outEdge.toNode; if (!toNode->visited) { list<Edge> newPath = path; newPath.push_back (outEdge); bool found = findPath (toNode, destID, newPath); if (found) return (true); } return (false); At most one findPath call per Node Executes once per outgoing edge Average: about 4

Complexity findPath executes at most O(N) times Constant, O(1), work in each call Except copying path path could have O(N) nodes Total worst-case complexity: O(N2) Average path shorter  Average case somewhat better, but hard to analyze

Complexity Can we do better? Don’t pass entire path around One way: each Node stores the edge used to reach it e.g. node.reachingEdge Reconstruct path when you reach the dest By following the previous edges / “bread crumbs” Now work per findPath is O(1) Total complexity is O(N) Good for a graph algorithm!

DFS - Demo

Path Quality We’re finding a path, not the shortest path dest source We’re finding a path, not the shortest path Circuitous routes / directions! Low marks! How to fix?

Shortest Path Algorithm #2 Depth First Search with Re-Exansion Path Enumeration Shortest Path Algorithm #2 Depth First Search with Re-Exansion

Don’t Stop at First Path Found? bool findPath (Node* currNode, int destID, float pLen) { pLen = updatePathLength (pLen, currNode); if (currNode->id == destID) if (pLen < bestPathLen) { ...// Update bestPathLen and save best path (globals) } return (true); currNode->visited = true; for each (outEdge of currNode) { Node *toNode = outEdge.toNode; if (!toNode->visited) { bool found = findPath ( toNode, destID); . . . 4 4 3 3 5 1 dest 2 source

More Complex Test Case 6 7 2 3 1 dest source 6 4 8 5 This node already marked visited  won’t call findPath on it again Blocks us from ever finding best path What can we do?

Let a Node be Explored Multiple Times? bool findPath (Node* currNode, int destID, float pLen) { . . . // Check if we’ve reached the destination // and if this is the best path found so far currNode->visited = true; for each (outEdge of currNode) { Node *toNode = outEdge.toNode; if (!toNode->visited) { bool found = findPath (toNode, destID); if (found) { currNode->visited = false; // Can be explored again return (true); } currNode->visited = false; return (false);

Testing DFS with Re-expansion 2 3 6 7 1 dest source 4 8 6 5

Testing DFS with Re-expansion 2 3 4 5 1 dest source 4 5 6

Testing DFS with Re-expansion 2 3 4 5 1 dest source 2 3 4 6 Not blocked by previously visited nodes! But still avoids infinite loops! Explored all paths to find best one

Computational Complexity? dest source Solve with recurrence Graph above: N-1 nodes T(N-1) to find best path

Computational Complexity? dest source New graph has 1 more node: N nodes T(N) = 2 * T(N-1) (why?) T(N) = 2 * 2 * T(N-2) T(N) = 2 * 2 * 2 * T(N-3) Iterate until we get to T(0) = c T(N) = 2N  c  O(2N)

Algorithm changes  big quality and runtime impact! Fast Enough? N  100,000 Say one call to findPath takes 300 clock cycles @ 3 GHz  10-7 s DFS with re-expansion (path enumeration) O(2N)  2100,000  10-7 s ~1010,000 years! (Age of the universe ~1010 years) Regular DFS (never explore once visited)? Poor implementation (store / copy whole path) O(N2)  100,0002  10-7 s  1000 s Good implementation O(N)  100,000  10-7 s Less than 1 s! Algorithm changes  big quality and runtime impact!