CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
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.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graphs – Part III CS 367 – Introduction to Data Structures.
Graphs – Breadth First Search
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Cse 373 May 8th – Dijkstras.
Cinda Heeren / Geoffrey Tien
Introduction to Graphs
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 17: Shortest Paths
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Representation, BFS, DFS
Graphs & Graph Algorithms 2
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Can you get there from here?
CSE 373: Data Structures and Algorithms
Graphs.
CSE 373: Data Structures and Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Chapter 11 Graphs.
Based on slides of Dan Suciu
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Slide Courtesy: Uwash, UT
Single-source shortest paths
Lecture 13 Algorithm Analysis
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms: Design and Analysis
CSE 373: Data Structures and Algorithms
Fundamental Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSC 380: Design and Analysis of Algorithms
Lecture 18: Implementing Graphs
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Spring 2016
Lecture 22: Implementing Dijkstra’s
Directed Graphs (Part II)
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 21: Graphs III

Depth-first search depth-first search (DFS): finds a path between two vertices by exploring each possible path as many steps as possible before backtracking often implemented recursively

DFS example All DFS paths from A to others (assumes ABC edge order) A -> B -> D A -> B -> F A -> B -> F -> E A -> C A -> C -> G What are the paths that DFS did not find?

DFS pseudocode Pseudo-code for depth-first search: dfs(v1, v2): dfs(v1, v2, path): path += v1. mark v1 as visited. if v1 is v2: path is found. for each unvisited neighbor vi of v1 where there is an edge from v1 to vi: if dfs(vi, v2, path) finds a path, path is found. path -= v1. path is not found.

DFS observations guaranteed to find a path if one exists easy to retrieve exactly what the path is (to remember the sequence of edges taken) if we find it optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path Example: DFS(A, E) may return A -> B -> F -> E

Another DFS example Using DFS, find a path from BOS to LAX. v BOS ORD JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6

Breadth-first search breadth-first search (BFS): finds a path between two nodes by taking one step down all paths and then immediately backtracking often implemented by maintaining a list or queue of vertices to visit BFS always returns the path with the fewest edges between the start and the goal vertices

BFS example All BFS paths from A to others (assumes ABC edge order) A -> C A -> E A -> B -> D A -> B -> F A -> C -> G What are the paths that BFS did not find?

BFS pseudocode Pseudo-code for breadth-first search: bfs(v1, v2): List := {v1}. mark v1 as visited. while List not empty: v := List.removeFirst(). if v is v2: path is found. for each unvisited neighbor vi of v where there is an edge from v to vi: mark vi as visited List.addLast(vi). path is not found.

BFS observations optimality: in unweighted graphs, optimal. (fewest edges = best) In weighted graphs, not optimal. (path with fewest edges might not have the lowest weight) disadvantage: harder to reconstruct what the actual path is 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 observation: any particular vertex is only part of one partial path at a time We can keep track of the path by storing predecessors for each vertex (references to the previous vertex in that path)

Another BFS example Using BFS, find a path from BOS to LAX. v BOS ORD JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6

DFS, BFS runtime What is the expected runtime of DFS, in terms of the number of vertices V and the number of edges E ? What is the expected runtime of BFS, in terms of the number of vertices V and the number of edges E ? Answer: O(|V| + |E|) each algorithm must potentially visit every node and/or examine every edge once. why not O(|V| * |E|) ? What is the space complexity of each algorithm? O(|V| + |E|) Space: DFS O(1), BFS O(|V| + |E|)

VertexInfo class public class VertexInfo<V> { public V v; public boolean visited; public VertexInfo(V v) { this.v = v; this.clear(); } public void clear() { this.visited = false;

Dijkstra's algorithm Dijkstra's algorithm: finds shortest (minimum weight) path between a particular pair of vertices in a weighted directed graph with nonnegative edge weights solves the "one vertex, shortest path" problem basic algorithm concept: create a table of information about the currently known best way to reach each vertex (distance, previous vertex) and improve it until it reaches the best solution in a graph where: vertices represent cities, edge weights represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and any other

Dijkstra pseudocode Dijkstra(v1, v2): for each vertex v: // Initialization v's distance := infinity. v's previous := none. v1's distance := 0. List := {all vertices}. while List is not empty: v := remove List vertex with minimum distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers.

Example: Initialization  Distance(source) = 0 Distance (all vertices but source) = A 2 B  4 1 3 10 C 2 D 2 E    5 8 4 6 F 1 G   Pick vertex in List with minimum distance.

Example: Update neighbors' distance 2 A 2 B 4 1 3 10 C 2 D 2 E   1 5 8 4 6 Distance(B) = 2 F 1 G Distance(D) = 1  

Example: Remove vertex with minimum distance G F B E C D 4 1 2 10 3 6 8 5  Pick vertex in List with minimum distance, i.e., D

Example: Update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Distance(C) = 1 + 2 = 3 Distance(E) = 1 + 2 = 3 Distance(F) = 1 + 8 = 9 Distance(G) = 1 + 4 = 5 F 1 G 9 5

Example: Continued... Pick vertex in List with minimum distance (B) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Note : distance(D) not updated since D is already known and distance(E) not updated since it is larger than previously computed F 1 G 9 5

Example: Continued... Pick vertex List with minimum distance (E) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G No updating 9 5

Example: Continued... Pick vertex List with minimum distance (C) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Distance(F) = 3 + 5 = 8 F 1 G 8 5

Example: Continued... Pick vertex List with minimum distance (G) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G Previous distance 6 5 Distance(F) = min (8, 5+1) = 6

Example (end) 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G 6 5 Pick vertex not in S with lowest cost (F) and update neighbors

Correctness Dijkstra’s algorithm is a greedy algorithm make choices that currently seem the best locally optimal does not always mean globally optimal Correct because maintains following two properties: for every known vertex, recorded distance is shortest distance to that vertex from source vertex for every unknown vertex v, its recorded distance is shortest path distance to v from source vertex, considering only currently known vertices and v

“Cloudy” Proof: The Idea Next shortest path from inside the known cloud Least cost node v THE KNOWN CLOUD competitor v' Source If the path to v is the next shortest path, the path to v' must be at least as long. Therefore, any path through v' to v cannot be shorter!

Dijkstra pseudocode Dijkstra(v1, v2): for each vertex v: // Initialization v's distance := infinity. v's previous := none. v1's distance := 0. List := {all vertices}. while List is not empty: v := remove List vertex with minimum distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers.

Time Complexity: Using List The simplest implementation of the Dijkstra's algorithm stores vertices in an ordinary linked list or array Good for dense graphs (many edges) |V| vertices and |E| edges Initialization O(|V|) While loop O(|V|) Find and remove min distance vertices O(|V|) Potentially |E| updates Update costs O(1) Reconstruct path O(|E|) Total time O(|V2| + |E|) = O(|V2| )

Time Complexity: Priority Queue For sparse graphs, (i.e. graphs with much less than |V2| edges) Dijkstra's implemented more efficiently by priority queue Initialization O(|V|) using O(|V|) buildHeap While loop O(|V|) Find and remove min distance vertices O(log |V|) using O(log |V|) deleteMin Potentially |E| updates Update costs O(log |V|) using decreaseKey Reconstruct path O(|E|) Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|) |V| = O(|E|) assuming a connected graph

Dijkstra's Exercise Use Dijkstra's algorithm to determine the lowest cost path from vertex A to all of the other vertices in the graph. Keep track of previous vertices so that you can reconstruct the path later. A G F B E C D 20 10 50 40 80 30 90 H