CS 106X, Lecture 23 Dijkstra and A* Search

Similar documents
CS 106X, Lecture 10 Recursive Backtracking

521495A: Artificial Intelligence

CS106B Spring 2016 Cynthia Lee Assignment 7: Trailblazer

CS 106X, Lecture 16 More Linked Lists

CS 310 Advanced Data Structures and Algorithms

CS 206 Introduction to Computer Science II

The big picture: from Perception to Planning to Control

Heuristic Search. Robert Platt Northeastern University. Some images and slides are used from: 1. CS188 UC Berkeley 2. RN, AIMA

Heuristic Search. Rob Platt Northeastern University. Some images and slides are used from: AIMA

Informed search strategies (Section ) Source: Fotolia

CS 343: Artificial Intelligence

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

CS171 Introduction to Computer Science II. Graphs

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

A* optimality proof, cycle checking

CS 206 Introduction to Computer Science II

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

CS 106B, Lecture 1 Introduction to C++

Outline for today s lecture. Informed Search I. One issue: How to search backwards? Very briefly: Bidirectional search. Outline for today s lecture

Shortest Paths. CSE 373 Data Structures Lecture 21

Informed search methods

Heuristic Search and Advanced Methods

Learning Objectives. c D. Poole and A. Mackworth 2010 Artificial Intelligence, Lecture 3.3, Page 1

Today. Informed Search. Graph Search. Heuristics Greedy Search A* Search

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

CSE 100: GRAPH SEARCH

CSE 100: GRAPH ALGORITHMS

Pathfinding Algorithms and Implementations on Grid Map

INTRODUCTION TO HEURISTIC SEARCH

Graphs. The ultimate data structure. graphs 1

Queues and Unit Testing

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

CMPUT 396 Sliding Tile Puzzle

Lecture 25 Spanning Trees

CS 188: Artificial Intelligence Fall Search Gone Wrong?

Outline. Graphs. Divide and Conquer.

Fundamental Graph Algorithms Part Four

Search with Costs and Heuristic Search

CSCI 446: Artificial Intelligence

Graphs & Digraphs Tuesday, November 06, 2007

Lecture 3 - States and Searching

CPS 170: Artificial Intelligence Search

ME/CS 132: Advanced Robotics: Navigation and Vision

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

DIT411/TIN175, Artificial Intelligence. Peter Ljunglöf. 19 January, 2018

CS 188: Artificial Intelligence

Some major graph problems

SPANNING TREES. Lecture 21 CS2110 Spring 2016

Single Source, Shortest Path Problem

Announcements. CS 188: Artificial Intelligence

The Algorithms of Prim and Dijkstra. Class 19

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Lecture 11: Analysis of Algorithms (CS ) 1

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

CMU-Q Lecture 2: Search problems Uninformed search. Teacher: Gianni A. Di Caro

Week 12: Minimum Spanning trees and Shortest Paths

Graph Search Methods. Graph Search Methods

(Re)Introduction to Graphs and Some Algorithms

CS 5522: Artificial Intelligence II

CSS 343 Data Structures, Algorithms, and Discrete Math II. Graphs II. Yusuf Pisan

Algorithms and Path Planning

CS 106X Lecture 26: Inheritance and Polymorphism in C++

CS 512: Comments on Graph Search 16:198:512 Instructor: Wes Cowan

Algorithms: Lecture 10. Chalmers University of Technology

Lecture 28: Graphs: Shortest Paths (Part 1)

Heuristic Search: A* CPSC 322 Search 4 January 19, Textbook 3.6 Taught by: Vasanth

CSE 473. Chapter 4 Informed Search. CSE AI Faculty. Last Time. Blind Search BFS UC-BFS DFS DLS Iterative Deepening Bidirectional Search

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Figure 1: A directed graph.

Module 11: Additional Topics Graph Theory and Applications

Artificial Intelligence Informed Search

Shortest path problems

W4231: Analysis of Algorithms

The Shortest Path Problem

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

CSE 100: GRAPH ALGORITHMS

Unweighted Graphs & Algorithms

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

TIE Graph algorithms

A and Branch-and-Bound Search

Dijkstra s Shortest Path Algorithm

Class Overview. Introduction to Artificial Intelligence COMP 3501 / COMP Lecture 2. Problem Solving Agents. Problem Solving Agents: Assumptions

Mustafa Jarrar: Lecture Notes on Artificial Intelligence Birzeit University, Chapter 3 Informed Searching. Mustafa Jarrar. University of Birzeit

L22-23: Graph Algorithms

lecture29: Shortest Path Algorithms

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Informed Search CS457 David Kauchak Fall 2011

Graphs vs trees up front; use grid too; discuss for BFS, DFS, IDS, UCS Cut back on A* optimality detail; a bit more on importance of heuristics,

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

A* Optimality CS4804

Algorithm Design and Analysis

CS 310 Advanced Data Structures and Algorithms

CS Algorithms and Complexity

Informed Search A* Algorithm

COMP219: Artificial Intelligence. Lecture 10: Heuristic Search

Transcription:

CS 106X, Lecture 23 Dijkstra and A* Search reading: Programming Abstractions in C++, Chapter 18 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, Marty Stepp, Ashley Taylor and others.

Plan For Today Recap: DFS and BFS Dijkstra s Algorithm Announcements A* Search 2

Plan For Today Recap: DFS and BFS Dijkstra s Algorithm Announcements A* Search 3

Depth-first search (18.4) depth-first search (DFS): Finds a path between two vertices by exploring each possible path as far as possible before backtracking. Often implemented recursively. Many graph algorithms involve visiting or marking vertices. DFS from a to h (assuming A-Z order) visits: a b e f c i d g h path found: {a, d, g, h} a d g b e h c f i 4

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 5

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 6

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 7

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 8

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 9

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 10

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 11

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 12

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 13

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 14

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 15

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 16

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 17

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 18

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 19

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 20

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 21

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 22

DFS A B C D Let s say we are starting at A and searching for C. E F G H I Mark current as visited Explore all the unvisited nodes from this node 23

DFS Details In an n-node, m-edge graph, takes O(m + n) time with an adjacency list Visit each edge once, visit each node at most once Pseudocode: dfs from v 1 : mark v 1 as seen. for each of v 1 's unvisited neighbors n: dfs(n) 24

DFS that finds path dfs from v 1 to v 2 : mark v 1 as visited, and add to path. perform a dfs from each of v 1 's unvisited neighbors n to v 2 : if dfs(n, v 2 ) succeeds: a path is found! yay! if all neighbors fail: remove v 1 from path. a d g b e h c f i To retrieve the DFS path found, pass a collection parameter to each call and choose-explore-unchoose. 25

DFS observations discovery: DFS is guaranteed to find a path if one exists. a b c retrieval: It is easy to retrieve exactly what the path is (the sequence of edges taken) if we find it d g e h f i choose - explore - unchoose optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path Example: dfs(a, i) returns {a, b, e, f, c, i} rather than {a, d, h, i}. 26

Finding Shortest Paths We can find paths between two nodes, but how can we find the shortest path? Fewest number of steps to complete a task? Least amount of edits between two words? 27

Breadth-First Search (BFS) Idea: processing a node involves knowing we need to visit all its neighbors (just like DFS) Need to keep a TODO list of nodes to process Keep a Queue of nodes as our TODO list Idea: dequeue a node, enqueue all its neighbors Still will return the same nodes as reachable, just might have shorter paths 28

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: a 29

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: e, b, g 30

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: e, b, g 31

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: b, g, f 32

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: b, g, f 33

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: g, f, c 34

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: g, f, c 35

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: f, c, h 36

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: f, c, h 37

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: c, h 38

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: c, h 39

BFS a b c d Let s say we are starting at a and searching for c. e f g h i Dequeue a node add all its unseen neighbors to the queue queue: h 40

BFS Details In an n-node, m-edge graph, takes O(m + n) time with an adjacency list Visit each edge once, visit each node at most once bfs from v 1 to v 2 : create a queue of vertexes to visit, initially storing just v 1. mark v 1 as visited. while queue is not empty and v 2 is not seen: dequeue a vertex v from it, mark that vertex v as visited, and add each unvisited neighbor n of v to the queue. How could we modify the pseudocode to look for a specific path? 41

BFS observations optimality: a b c always finds the shortest path (fewest edges). in unweighted graphs, finds optimal cost path. d e f In weighted graphs, not always optimal cost. g h i retrieval: harder to reconstruct the actual sequence of vertices or edges in the path once you find it conceptually, BFS is exploring many possible paths in parallel, so it's not easy to store a path array/list in progress solution: We can keep track of the path by storing predecessors for each vertex (each vertex can store a reference to a previous vertex). DFS uses less memory than BFS, easier to reconstruct the path once found; but DFS does not always find shortest path. BFS does. 42

BFS that finds path bfs from v 1 to v 2 : create a queue of vertexes to visit, initially storing just v 1. mark v 1 as visited. prev a d b e c f while queue is not empty and v 2 is not seen: g dequeue a vertex v from it, mark that vertex v as visited, and add each unvisited neighbor n of v to the queue, while setting n's previous to v. h i 43

Plan For Today Recap: DFS and BFS Dijkstra s Algorithm Announcements A* Search 44

Search Search without weights: What is the shortest path from A to D? B C A D 45

Search Search with weights: What is the shortest path from A to D? B 5 C 10 3 A 12345 D 46

Search Search with weights: What is the shortest path from A to D? B 5 C 10 3 A 12345 D 47

Least-Cost Paths BFS uses a queue to keep track of which nodes to use next BFS pseudocode: bfs from v 1 : add v 1 to the queue. while queue is not empty: dequeue a node n enqueue n's unseen neighbors How could we modify this pseudocode to dequeue the least-cost nodes instead of the closest nodes? Use a priority queue instead of a queue 48

Edsger Dijkstra (1930-2002) famous Dutch computer scientist and prof. at UT Austin Turing Award winner (1972) Noteworthy algorithms and software: THE multiprogramming system (OS) layers of abstraction Compiler for a language that can do recursion Dijkstra's algorithm Dining Philosophers Problem: resource contention, deadlock semaphores famous papers: "Go To considered harmful" "On the cruelty of really teaching computer science" 49

Dijkstra's Algorithm (18.6) Dijkstra's algorithm: Finds the minimum-weight path between a pair of vertices in a weighted directed graph. Solves the "one vertex, shortest path" problem in weighted graphs. basic algorithm concept: Create a table of information about the currently known best way to reach each vertex (cost, previous vertex), and improve it until it reaches the best solution. Example: In a graph where vertices are cities and weighted edges are roads between cities, Dijkstra's algorithm can be used to find the shortest route from one city to any other. 50

Dijkstra pseudocode dijkstra(v 1, v 2 ): consider every vertex to have a cost of infinity, except v 1 which has a cost of 0. create a priority queue of vertexes, ordered by cost, storing only v 1. while the pqueue is not empty: dequeue a vertex v from the pqueue, and mark it as visited. for each of the unvisited neighbors n of v, we now know that we can reach this neighbor with a total cost of (v's cost + the weight of the edge from v to n). if the neighbor is not in the pqueue, or this is cheaper than n's current cost, we should enqueue the neighbor n to the pqueue with this new cost, and with v as its previous vertex. when we are done, we can reconstruct the path from v 2 back to v 1 by following the previous pointers. 51

Dijkstra example dijkstra(a, F); color key white: unexamined yellow: enqueued green: visited v 1 's distance := 0. all other distances :=. 0 A 4 1 3 10 2 B 2 2 C D E 5 8 4 6 F 1 G pqueue = {A:0} 52

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 C D E 5 8 1 4 6 F 1 G pqueue = {D:1, B:2} 53

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 3 C 2 D 2 E 3 5 8 1 4 6 F 1 G 9 5 pqueue = {B:2, C:3, E:3, G:5, F:9} 54

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 3 C D E 3 5 8 1 4 6 F 1 G 9 5 pqueue = {C:3, E:3, G:5, F:9} 55

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 3 C D E 3 5 8 1 4 6 F 1 G 8 5 pqueue = {E:3, G:5, F:8} 56

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 3 C D E 3 5 8 1 4 6 F 1 G 8 5 pqueue = {G:5, F:8} 57

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 3 C D E 3 5 8 1 4 6 F 1 G 6 5 pqueue = {F:6} 58

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 3 C D E 3 5 8 1 4 6 F 1 G 6 5 pqueue = {} 59

Dijkstra example dijkstra(a, F); 0 2 A 2 B 4 1 3 10 2 2 3 C D E 3 5 8 1 4 6 F 1 G 6 5 60

Algorithm properties Dijkstra's algorithm is a greedy algorithm: Make choices that currently seem the best. Locally optimal does not always mean globally optimal. It is correct because it maintains the following two properties: 1) for every marked vertex, the current recorded cost is the lowest cost to that vertex from the source vertex. 2) for every unmarked vertex v, its recorded distance is shortest path distance to v from source vertex, considering only currently known vertices and v. 61

Dijkstra exercise Run Dijkstra's algorithm from vertex E to all of the other vertices in the graph. (see next slide) Keep track of previous vertices so that you can reconstruct the path. Q: What path does it find from E to H? 10 F 70 A. {E, B, F, C, H} B. {E, B, A, D, C, H} C. {E, G, A, B, F, D, C, H} D. {E, G, D, C, H} E. none of the above B 80 20 20 A 40 80 D 10 C 20 H E 30 G 120 62

Dijkstra tracing 10 F 70 B 80 20 20 A 40 80 D 10 C 20 H E 30 G 120 63

Plan For Today Recap: DFS and BFS Dijkstra s Algorithm Announcements A* Search 64

Announcements Mid-quarter grade reports are available on the course website 65

Plan For Today Recap: DFS and BFS Dijkstra s Algorithm Announcements A* Search 66

Improving on Dijkstra s If we want to travel from Stanford to San Francisco, Dijkstra's algorithm will look at path distances around Stanford. But, we know something about how to get to San Francisco -- we know that we generally need to go Northwest from Stanford. This is more information! Let's not only prioritize by weights, but also give some priority to the direction we want to go. E.g., we will add more information based on a heuristic, which could be direction in the case of a street map. 67

Finding a maze path Suppose we are searching for a path in a maze. The 'cost' of a square is the min. number of steps we take to get there. What does Dijkstra's algorithm do here? What "should" it do? a c 68

Dijkstra observations Dijkstra's algorithm uses a priority queue and examines possible paths in increasing order of their known cost or distance. The idea is that paths with a lower distance-so-far are more likely to lead to paths with a lower total distance at the end. a b c known distance a-b unknown distance b-c But what about the remaining distance? What if we knew that a path that was promising so far will be unlikely to lead to a good result? Can we modify the algorithm to take advantage of this information? 69

Dijkstra observations Dijkstra's algorithm works by incrementally computing the shortest path to intermediary nodes in the graph in case they prove to be useful. Some of these paths are in the "wrong" direction. 6? 6? 5 5? 4 4 3 5? 4 5? 6? 5? 4 3 2 3 4 5 6? 5? 4 3 2 3 2 1 2 1 1 2 1 2 3 4 5? 4 5? 6? 2 3 4 4 3 3 5 4 3 2 3 4 5? 5? 4 6? 5? 4 5? 3 4 4 5? 5 6? 6? The algorithm has no "big-picture" conception of how to get to the destination; the algorithm explores outward in all directions. Could we give the algorithm a hint? Explore in a smarter order? What if we knew more about the vertices or graph being searched? 70

Heuristics heuristic: A speculation, estimation, or educated guess that guides the search for a solution to a problem. Example: Spam filters flag a message as probable spam if it contains certain words, has certain attachments, is sent to many people,... In the context of graph searches: A function that approximates the distance from a known vertex to another destination vertex. Example: Estimate the distance between two places on a Google Maps graph to be the direct straight-line distance between them. admissible heuristic: One that never overestimates the distance. Okay if the heuristic underestimates sometimes (e.g. Google Maps). Only ignore paths that in the best case are worse than your current path 71

The A* algorithm A* ("A star"): A modified version of Dijkstra's algorithm that uses a heuristic function to guide its order of path exploration. a b c known distance a-b Suppose we are looking for paths from start vertex a to c. Any intermediate vertex b has two costs: estimated distance b-c (via heuristic) The known (exact) cost from the start vertex a to b. The heuristic (estimated) cost from b to the end vertex c. Idea: Run Dijkstra's algorithm, but use this priority in the pqueue: priority(b) = cost(a, b) + Heuristic(b, c) Chooses to explore paths with lower estimated cost. 72

Example: Maze heuristic Suppose we are searching paths in a maze. The 'cost' of a square is the min. number of steps we take to get there. What would be a good heuristic for the remaining distance? a c 73

Maze heuristic Idea: Use "Manhattan distance" (straight-line) between the points. H(p 1, p 2 ) = abs(p 1.x - p 2.x) + abs(p 1.y - p 2.y) // dx + dy The idea: Dequeue/explore neighbors with lower (cost+heuristic). 8 7 6 5 4 5 7 6 5 4 3 4 6 5 4 3 2 3 5 4 3 2 1 2 4 a 2 1 c 1 5 4 3 2 1 2 6 5 4 3 2 3 74

Dijkstra 75

A* 76

Recall: Dijkstra code dijkstra(v 1, v 2 ): consider every vertex to have a cost of infinity, except v 1 which has a cost of 0. create a priority queue of vertexes, ordered by cost, storing only v 1. while the pqueue is not empty: dequeue a vertex v from the pqueue, and mark it as visited. for each of the unvisited neighbors n of v, we now know that we can reach this neighbor with a total cost of (v's cost + the weight of the edge from v to n). if the neighbor is not in the pqueue, or this is cheaper than n's current cost, we should enqueue the neighbor n to the pqueue with this new cost, and with v as its previous vertex. when we are done, we can reconstruct the path from v 2 back to v 1 by following the previous pointers. 77

A* pseudocode astar(v 1, v 2 ): consider every vertex to have a cost of infinity, except v 1 which has a cost of 0. create a priority queue of vertexes, ordered by (cost+heuristic), storing only v 1 with a priority of H(v 1, v 2 ). while the pqueue is not empty: dequeue a vertex v from the pqueue, and mark it as visited. for each of the unvisited neighbors n of v, we now know that we can reach this neighbor with a total cost of (v's cost + the weight of the edge from v to n). if the neighbor is not in the pqueue, or this is cheaper than n's current cost, we should enqueue the neighbor n to the pqueue with this new cost plus H(n, v 2 ), and with v as its previous vertex. when we are done, we can reconstruct the path from v 2 back to v 1 by following the previous pointers. * (basically, add H(...) to costs of elements in PQ to improve PQ processing order) 78

Recap Recap: DFS and BFS Dijkstra s Algorithm Announcements A* Search Next time: Minimum Spanning Trees 79