Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.

Slides:



Advertisements
Similar presentations
Problem solving with graph search
Advertisements

Single Source Shortest Paths
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
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.
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Sociology and CS Philip Chan. How close are people connected? Are people closely connected, not closely connected, isolated into groups, …
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
1 Breadth First Search AB F I EH DC G FIFO Queue - front.
CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Lecture 3 Informed Search CSE 573 Artificial Intelligence I Henry Kautz Fall 2001.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
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.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Data Structures Week 9 Towards Weighted BFS So, far we have measured d s (v) in terms of number of edges in the path from s to v. Equivalent to assuming.
Search Related Algorithms. Graph Code Adjacency List Representation:
GRAPHS
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
CS261 Data Structures Dijkstra’s Algorithm – Edge List Representation.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
A* Path Finding Ref: A-star tutorial.
Shortest Paths PowerPoint adapted from Alan Tam’s Shortest Path, 2004 with slight modifications.
Breadth-first and depth-first traversal CS1114
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CS 367 Introduction to Data Structures Lecture 13.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Paths.
Programming Abstractions
Shortest Paths and Minimum Spanning Trees
Programming Abstractions
Programming Abstractions
CS 106B Homework 7: Trailblazer
Programming Abstractions
Dijkstra’s Algorithm We are given a directed weighted graph
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
A* Path Finding Ref: A-star tutorial.
Outline This topic covers Prim’s algorithm:
Graph Traversals Depth-First Traversals. Algorithms. Example.
Shortest Path Algorithms
Breadth First Search - A B C D E F G H I front FIFO Queue.
Shortest Paths.
Dijkstra's Shortest Path Algorithm
Implementation of Dijkstra’s Algorithm
Graphs: Shortest path and mst
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010

Shortest paths What is the shortest path from a to d? A B CE D

Shortest paths BFS A B CE D

Shortest paths What is the shortest path from a to d? A B CE D

Shortest paths We can still use BFS A B CE D

Shortest paths We can still use BFS A B CE D A B CE D

Shortest paths We can still use BFS A B CE D

Shortest paths What is the problem? A B CE D

Shortest paths Running time is dependent on the weights A B C A B C

Shortest paths A B C A B C

Shortest paths A B C

A B C

A B C Nothing will change as we expand the frontier until we’ve gone out 100 levels

Dijkstra’s algorithm map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Uses a priority queue to keep track of the next shortest path from the starting vertex Vertices are kept in three sets: “visited”: those vertices who’s correct paths have been found. This occurs when a vertex is popped off the queue “frontier”: those vertices that we know about and have a path for, but not necessarily the vertices’ shortest paths. Vertices on the frontier are in the queue “rest”: the remaining vertices that we have not seen yet

Dijkstra’s algorithm map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } enqueue start; while (queue not empty) { dequeue v; if (v is not visited) { visit v; enqueue all of v’s neighbors; } } BFS

Dijkstra’s algorithm map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } enqueue start; while (queue not empty) { dequeue v; if (v is not visited) { visit v; enqueue all of v’s neighbors; } } BFS - “parents” keeps track of shortest path - only keep track of what the next vertex on the shortest path is

Dijkstra’s algorithm map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } enqueue start; while (queue not empty) { dequeue v; if (v is not visited) { visit v; enqueue all of v’s neighbors; } } BFS

Dijkstra’s algorithm map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } enqueue start; while (queue not empty) { dequeue v; if (v is not visited) { visit v; enqueue all of v’s neighbors; } } BFS

Dijkstra’s algorithm map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } enqueue start; while (queue not empty) { dequeue v; if (v is not visited) { visit v; enqueue all of v’s neighbors; } } BFS

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; }

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap A 0 Parent A: A

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: A B 3

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: A B 3

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: A C: A C 1 B 3

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: A C: A B 3

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: A C: A B 3

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: A C: A B 3

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A B 2

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A B 2

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A E: C B 2 E 5

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A E: C B 2 E 5 Frontier: all nodes reachable from starting node within a given distance

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A D: B E: B E 3 D 5

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A D: B E: B D 5

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A D: B E: B

A B CE D map shortest paths(int start, const map > > & graph) { map parents; priorityqueue62 frontier; parents[start]=start; frontier.push(start, 0); while (!frontier.is_empty()) { int v = frontier.top_serialnumber(); int p = frontier.top_priority(); frontier.pop(); for (the neighbors (n,w) of v) if (n == parents[v]) ; // do nothing else if (n is not in the frontier and has not been visited){ parents[n] = v; frontier.push(n, p + w); }else if (p + w < frontier.get_priority(n)) { parents[n] = v; frontier.reduce_priority(n, p + w); } } // end while return parents; } Heap Parent A: A B: C C: A D: B E: B