Section 7: Dijkstra’s Justin Bare and deric Pang

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

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.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths Nicki Dell Spring 2014.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2012.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Paths.
Instructor: Lilian de Greef Quarter: Summer 2017
May 12th – Minimum Spanning Trees
CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Cse 373 May 8th – Dijkstras.
Instructor: Lilian de Greef Quarter: Summer 2017
Instructor: Lilian de Greef Quarter: Summer 2017
Section 5: HW6 and Interfaces
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
Short paths and spanning trees
Section 5: HW6 and Interfaces
Why do Java programmers wear glasses?
What do you call 8 Hobbits?
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Shortest Paths.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Section 7: Dijkstra’s & Midterm Postmortem
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Slide Courtesy: Uwash, UT
Section 5: HW6 and Interfaces
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree.
Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
Section 5: HW6 and Interfaces
CSE332: Data Abstractions Lecture 17: Shortest Paths
Shortest Paths.
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSC 380: Design and Analysis of Algorithms
Shortest Paths.
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Topological Sorting Minimum Spanning Trees Shortest Path
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:

Section 7: Dijkstra’s Justin Bare and deric Pang with material FROM Alex Mariakakis, Kellen Donohue, David Mailhot, and Dan Grossman

Things to Discuss Late days 4 assignments left, including Homework 6 Late Days are marked in Catalyst Homework 5 grades are published. Woot! Hints from a Homework-Grader: Answers.txt – Hint: “Name: <replace this with your name>” Abstraction functions and representation invariants Understand what they are and the difference between them JavaDoc Comments – Should contain a general overview of the method Minimize the number of asserts per test One domain per test – a specific functionality and input

Review: Shortest Paths with BFS 1 B From Node B A Destination Path Cost A <B,A> 1 B <B> C <B,A,C> 2 D <B,D> E <B,D,E> 1 1 1 C 1 D 1 1 E

Review: Shortest Paths with BFS 1 B From Node B A Destination Path Cost A <B,A> 1 B <B> C <B,A,C> 2 D <B,D> E <B,D,E> 1 1 1 C 1 D 1 1 E

Shortest Paths with Weights 2 B From Node B A Destination Path Cost A <B,A> 2 B <B> C <B,A,C> 5 D <B,A,C,D> 7 E <B,A,C,E> 100 100 3 C 2 D 2 6 E Paths are not the same!

Shortest Paths with Weights 2 B From Node B A Destination Path Cost A <B,A> 2 B <B> C <B,A,C> 5 D <B,A,C,D> 7 E <B,A,C,E> 100 100 3 C 2 D 2 6 E Paths are not the same!

Goal: Smallest cost? Or fewest edges?

BFS vs. Dijkstra’s Algorithm 1 500 100 1 5 -10 BFS doesn’t work when the path with minimal cost ≠ path with fewest edges Dijkstra’s Algorithm works in this case, but only if the weights are non-negative What happens if there is a negative edge? Minimize cost by repeating the cycle forever Add constant weight to all edges until positive

Dijkstra’s Algorithm Named after its inventor Edsger Dijkstra (1930-2002) Truly one of the “founders” of computer science; This is just one of his many contributions Wikipedia is a good resource: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm The idea: reminiscent of BFS, but adapted to handle weights Grow the set of nodes whose shortest distance has been computed Nodes not in the set will have a “best distance so far” A PRIORITY QUEUE will turn out to be useful for efficiency – We’ll cover this later in the slide deck

Dijkstra’s Algorithm For each node v, set v.cost = ∞ and v.known = false Set source.cost = 0 While there are unknown nodes in the graph Select the unknown node v with lowest cost Mark v as known For each edge (v,u) with weight w, c1 = v.cost + w c2 = u.cost if(c1 < c2) u.cost = c1 u.path = v // cost of best path through v to u // cost of best path to u previously known // if the new path through v is better, update

Example #1 Goal: Fully explore the graph    2 2 3 B A F H 1 2 1 5   Goal: Fully explore the graph 2 2 3 B A F H 1 2 1 5 10 4 9 3 G   vertex known? cost path A Y B ∞ C D E F G H C 2 11 D 1  E 7  Order Added to Known Set:

Example #1 2   2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B ≤ 2 C ≤ 1 D ≤ 4 E ∞ F G H C 2 11 D 1 4 E 7  Order Added to Known Set: A

Example #1 2   2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B ≤ 2 C 1 D ≤ 4 E ∞ F G H C 2 11 D 1 4 E 7  Order Added to Known Set: A, C

Example #1 2   2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B ≤ 2 C 1 D ≤ 4 E ≤ 12 F ∞ G H C 2 11 D 1 4 E 7 12 Order Added to Known Set: A, C

Example #1 2   2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B 2 C 1 D ≤ 4 E ≤ 12 F ∞ G H C 2 11 D 1 4 E 7 12 Order Added to Known Set: A, C, B

Example #1 2 4  2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B 2 C 1 D ≤ 4 E ≤ 12 F G ∞ H C 2 11 D 1 4 E 7 12 Order Added to Known Set: A, C, B

Example #1 2 4  2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B 2 C 1 D 4 E ≤ 12 F ≤ 4 G ∞ H C 2 11 D 1 4 E 7 12 Order Added to Known Set: A, C, B, D

Example #1 2 4  2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B 2 C 1 D 4 E ≤ 12 F G ∞ H C 2 11 D 1 E 4 7 12 Order Added to Known Set: A, C, B, D, F

Example #1 2 4 7 2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B 2 C 1 D 4 E ≤ 12 F G ∞ H ≤ 7 C 2 11 D 1 4 E 7 12 Order Added to Known Set: A, C, B, D, F

Example #1 2 4 7 2 2 3 B A F H 1 2 1 5 10 4 9 3 G  1 vertex known? cost path A Y B 2 C 1 D 4 E ≤ 12 F G ∞ H 7 C 2 11 D 1 4 E 7 12 Order Added to Known Set: A, C, B, D, F, H

Example #1 2 4 7 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 8 1 vertex known? cost path A Y B 2 C 1 D 4 E ≤ 12 F G ≤ 8 H 7 C 2 7 11 D 1 E 4 12 Order Added to Known Set: A, C, B, D, F, H

Example #1 2 4 7 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 8 1 C vertex known? cost path A Y B 2 C 1 D 4 E ≤ 12 F G 8 H 7 2 11 D 1 4 7 E 12 Order Added to Known Set: A, C, B, D, F, H, G

Example #1 2 4 7 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 8 4 1 C vertex known? cost path A Y B 2 C 1 D 4 E ≤ 11 G F 8 H 7 2 11 D 1 7 E 11 Order Added to Known Set: A, C, B, D, F, H, G

Example #1 2 4 7 2 2 3 B A F H 1 2 1 5 10 4 9 3 G 8 1 C vertex known? cost path A Y B 2 C 1 D 4 E 11 G F 8 H 7 2 7 11 D 1 4 E 11 Order Added to Known Set: A, C, B, D, F, H, G, E

Interpreting the Results A B D C F H E G 2 4 7 1 11 8 3 10 9 5 vertex known? cost path A Y B 2 C 1 D 4 E 11 G F 8 H 7 2 2 3 B A F H 1 1 4 3 G C D E

Example #2  2 B A 1  1 5 2 E  1 D 1 3 5 C   6 G 2  10 F 2 B A 1  1 5 2 E  1 vertex known? cost path A Y B ∞ C D E F G D 1 3 5 C   6 G 2  10 F Order Added to Known Set:

Example #2 3 2 B A 1 2 1 5 2 E 1 1 vertex known? cost path A Y B 3 E C 2 D 1 F 4 G 6 D 1 3 2 5 C 6 6 2 G 4 10 F Order Added to Known Set: A, D, C, E, B, F, G

Pseudocode Attempt #1 dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 while(not all nodes are known) { b = dequeue b.known = true for each edge (b,a) in G { if(b.cost + weight((b,a)) < a.cost){ a.cost = b.cost + weight((b,a)) a.path = b } … O(V^2) = O(V + V-1 + V-2 + …) = O (V*(V+1)/2)

Can We Do Better? Increase efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices Priority queue is like a queue, but returns elements by lowest value instead of FIFO Insert and remove run in O(log n) Naïve Dijkstra’s: O(|# of nodes|2) With priority queue: O(|# of edges| + |# of nodes| * log|# of nodes|)

Priority Queue Increase efficiency by considering lowest cost unknown vertex with sorting instead of looking at all vertices Priority queue is like a queue, but returns elements by lowest value instead of FIFO Two ways to implement: Comparable class Node implements Comparable<Node> public int compareTo(other) Comparator class NodeComparator extends Comparator<Node> new PriorityQueue(new NodeComparator())

Pseudocode Attempt #2 dijkstra(Graph G, Node start) { for each node: x.cost=infinity, x.known=false start.cost = 0 build-heap H with all nodes while(heap is not empty) { b = H.extractMin() b.known = true for each edge (b,a) in G { if(b.cost + weight((b,a)) < a.cost){ a.cost = b.cost + weight((b,a)) a.path = b } …

Homework 7 Modify your graph to use generics Will have to update HW #5 and HW #6 tests Implement Dijkstra’s algorithm Search algorithm that accounts for edge weights Note: This should not change your implementation of Graph. Dijkstra’s is performed on a Graph, not within a Graph.

Homework 7 The more well-connected two characters are, the lower the weight and the more likely that a path is taken through them The weight of an edge is equal to the inverse of how many comic books the two characters share Ex: If Amazing Amoeba and Zany Zebra appeared in 5 comic books together, the weight of their edge would be 1/5

Hw7 Important Notes!!! DO NOT access data from hw6/src/data Copy over data files from hw6/src/data into hw7/src/data, and access data in hw7 from there instead Remember to do this! Or tests will fail when grading. DO NOT modify ScriptFileTests.java

Hw7 Test script Command Notes HW7 LoadGraph command is slightly different from HW6 After graph is loaded, there should be at most one directed edge from one node to another, with the edge label being the multiplicative inverse of the number of books shared Example: If 8 books are shared between two nodes, the edge label will be 1/8 Since the edge relationship is symmetric, there would be another edge going the other direction with the same edge label