1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs. 14.1--14.3, sec. 14.5.1. "This 'telephone' has too many shortcomings to be seriously.

Slides:



Advertisements
Similar presentations
Introduction to Graph Theory Instructor: Dr. Chaudhary Department of Computer Science Millersville University Reading Assignment Chapter 1.
Advertisements

22C:19 Discrete Math Graphs Fall 2014 Sukumar Ghosh.
PSU CS Algorithms Analysis and Design Graphs.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
The future of technology, law enforcement, and justice ICJIA Feburary, 2013.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Graph & BFS.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Representing Graphs Wade Trappe. Lecture Overview Introduction Some Terminology –Paths Adjacency Matrix.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
ALG0183 Algorithms & Data Structures Lecture 18 The basics of graphs. 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks.
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
Chapter 2 Graph Algorithms.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Representing and Using Graphs
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Graphs II Lecture 21: Shortest paths and spanning trees CS2110 – Spring Tokyo subway map.
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.
Graphs Topic 21 " Hopefully, you've played around a bit with The Oracle of Bacon at Virginia and discovered how few steps are necessary to link just about.
SHORTEST PATHS Lecture 19 CS2110 – Spring
1 Dijkstra’s Algorithm Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Graph Colouring Lecture 20: Nov 25. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including.
Shortest Paths Text Discrete Mathematics and Its Applications (5 th Edition) Kenneth H. Rosen Chapter 8.6 Based on slides from Chuck Allison, Michael T.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Chapter 9: Graphs.
Great Theoretical Ideas in Computer Science for Some.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
COMPSCI 102 Introduction to Discrete Mathematics.
Graph Theory Graph Theory - History Leonhard Euler's paper on “Seven Bridges of Königsberg”, published in 1736.
SHORTEST PATHS Lecture 19 CS2110 – Fall Friday is Halloween. Why did I receive a Christmas card on Halloween?
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
GRAPHS Lecture 16 CS2110 Fall 2017.
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Shortest Path Graph represents highway system Edges have weights
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
Discrete Mathematics and Its Applications (5th Edition)
Graphs Lecture 18 CS2110 – Fall 2009.
Minimum Spanning Tree.
Graphs Chapter 13.
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
More Graphs Lecture 19 CS2110 – Fall 2013.
Discrete Mathematics and Its Applications (5th Edition)
DFS and Shortest Paths Lecture 18 CS2110 – Spring 2014.
Graphs G = (V, E) V are the vertices; E are the edges.
Graphs.
Graphs.
GRAPHS Lecture 17 CS2110 Spring 2018.
Graphs.
Shortest Paths Discrete Mathematics and Its Applications (8th Edition)
Graphs.
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
Chapter 9 Graph algorithms
Shortest Paths Discrete Mathematics and Its Applications (7th Edition)
Presentation transcript:

1 CS211, Lecture 21 Graphs shortest path algorithm Readings: Weiss, secs , sec "This 'telephone' has too many shortcomings to be seriously considered as a means of communications. " - Western Union internal memo "I think there is a world market for maybe five computers." - Watson, chair of IBM "The problem with television is that the people must sit and keep their eyes glued on a screen; the average American family hasn't time for it.” - The New York Times "Where... the ENIAC is equipped with 18,000 vacuum tubes and weighs 30 tons, computers in the future may have only 1,000 vacuum tubes and weigh only 1.5 tons.” - Popular Mechanics "There is no reason anyone would want a computer in their home." - Ken Olson, founder DEC "640K ought to be enough for anybody.” - Bill Gates "By the turn of this century, we will live in a paperless society." - Roger Smith, chair GM "I predict the Internet... will go spectacularly supernova and in 1996 catastrophically collapse." - Bob Metcalfe, inventor and 3Com founder did he mean memory or money?

2 A graph is a set V of nodes (or vertices) together with a set E of edges between them. Here, graph could represent roads between cities, or airplane flights between cities. Graphs NY chic LA london paris SF Nodes: V = { SF, NY, LA, chic, london, paris } Edges: { (SF, LA), (SF, NY), (SF, chic), (LA, chic) (LA, chic), (NY, chic), (london, paris) } |V| = size of V = number of nodes (here, 6) |E| = size of E = number of edges (here, 7)

3 Leonhard Euler ( ) started graph theory: Koenigsberg, Prussia, Koenigsberg bridges. Is it possible to travel over the bridges so that each bridge is walked over exactly once? Extend to graph theory: For which graphs is it possible to find a path that contains each edge exactly once? island land land 1 land 2 island 1 island 2 allows for more than one edge between two vertices

4 Important graph problems Traveling salesman problem (TSP): Graph is a bunch of cities, edges have weights that give the cost for traveling from one city to another. Find the cheapest way to travel to all cities and end up back at the home city. Four-color problem. Can one color countries of a map using four colors so that no two adjacent countries have the same color? Nodes: countries. Edges: edge from one country to another if they have a common boundary. Graph is planar --can draw so that no two edges intersect. Color nodes of a graph using four color so that no two adjacent nodes have the same color? About An incorrect proof published; error detected ~10 years later. 1977: Appel and Haken used computers to solve it (yes, one can).

5 A directed graph, or digraph, is a set V of vertices together with a set E of directed edges (arrows) between them. Only one edge allowed in a particular direction between the nodes. We concentrate on directed graphs. Below, “…” stands for“ Directed graph …/bootcamp.html …/index.htmlindex.html …DrJava.html 2se/1.4.2/download. html

6 Celebrity problem uses a directed graph At a party, a celebrity is a person who everyone knows and who knows no one (except themselves). How much time does it take to find a celebrity (if present)? Graph: nodes are people, Edge (p1, p2) if p1 knows p2. It’s a directed graph. Originally: thought this required required time O(|V|*|V|). Can you write an O(|V|) algorithm to find the celebrity?

7 Sometimes, we put positive weights on the edges. If we don’t assume the weight is one. For a map of roads between cities, the weight might be the shortest mileage. Directed graph …/bootcamp.html …/index.htmlindex.html …DrJava.html 2se/1.4.2/download. html

8 A path is a sequence of edges that connect by successive vertices. The length of the path is the number of edges. Simple path: all vertices (except possibly first and last) are different. Cycle: simple path in which first and last vertices are the same. A graph without cycles is called an acyclic graph. Directed graph V1V1 V0V0 V3V3 V2V2 path (V1, v2, v0) has length 2 path (V1, V2) has length 1 path (V1) has length 0 path (V1, V2, V0, V1, V2) has length 4

9 Use a boolean array b[0..|V|-1][0..|V|–1] b[r][c] = “there is an edge from V r to V c ” Adjacency matrix: representation of a digraph V1V1 V0V0 V3V3 V2V F T F F 1 F F T F 2 T F F F 3 T T T F Constant time to tell whether there is an edge from r to c. Good. Takes |V|*|V| time to construct, |V|*|V| space, and |V|*|V| time to process all edges. No good if graph is sparse (very few edges in relation to number of nodes). Usually, sparse means that the number of edges is O(|V|).

10 Use array b[0..|V|–1] of linked lists If (h, k) is an edge, k is on linked list b[h]. Adjacency list: representation of a digraph for sparse graphs V1V1 V0V0 V3V3 V2V No prescribed order of values in a linked list. Time to construct: O(|V|) + O(|E|) Time to tell whether an edge is there: O(|V|) (worst case) Time to process the edges: O(|E|)

11 Given a path from w to v, we can count the weights that are on the edges of that path. Call the sum the cost of the path. Find the shortest path –the path with minimum cost– from a start vertex v to each other vertex. Dijkstra’s shortest path algorithm (assuming edges have positive weights) V1V1 V0V0 V3V3 V2V Shortest path from V3 to V1 is: (V3, V2, V0, V1)

12 Let n = |V| –i.e. the number of vertices Store values in L[0..n-1] so that: R: L[k] the shortest path length from w to k, for 0 ≤ k < n (if there is no path from v to k, set L[k] to ∞ or, can use Integer.MAX_VALUE instead of ∞) For the graph on this slide, with w = V3, L = { 2, 7, 1, 0 } Find shortest paths from v to all nodes V1V1 V0V0 V3V3 V2V

13 Initially, L[v] = 0 // shortest path from v to v L[w] = ∞ // for all other nodes red set = {} Find shortest paths from v to all nodes v red set: the set of vertices (1) whose L-value has been calculated (2) whose neighbors have L-values < ∞ Frontier: vertices that (1) are not in the red set (2) have L-value < ∞

14 Initially, L[v] = 0 // shortest path from v to v We put the L-values in the nodes themselves. Find shortest paths from v to all nodes 4 v red set: the set of vertices (1) whose L-value has been calculated (2) whose neighbors have L-values < ∞ Frontier: vertices that (1) are not in the red set (2) have L-value < ∞ For a node w in Frontier, L[w] is the minimum path length over (v,w) paths using only red nodes (except for w).

15 Initially, L[v] = 0 // shortest path from v to v We put the L=values in the nodes themselves. Find shortest paths from v to all nodes v red set: the set of vertices (1) whose L-value has been calculated (2) whose neighbors have L-values < ∞ Frontier: vertices that (1) are not in the red set (2) have L-value < ∞ For a node w in Frontier, L[w] is the minimum path length over (v,w) paths using only red nodes (except for w).

16 At each iteration: Let f be the Frontier node with smallest L value. Make f red and for each node w adjacent to f: if (L[f] + weight (f,w) < L[w]) { L[w]= L[f] + weight(f,w); Put w in Frontier (make blue); } Invariant: shows only edges from red to blue, blue to black Frontier F. For w in here, L[w] is min path length over paths from v to w for which all nodes are red except w. Red set. For red w, L[w] has been calculated. Neighbors have L[w] < ∞ … … rest of nodes

17 L[w]= ∞ for all nodes w; L[v]= 0; F= {v}; while (F not empty) { f= a node in F with min L value; Make f red (delete from F); for each node w adjacent to f: if (L[f] + weight (f,w) < L[w]) { L[w]= L[f] + weight(f,w); if (w is not in F) put w in F; } Frontier F. For w in here, L[w] is min path length over paths from v to w for which all nodes are red except w. Red set. For red w, L[w] has been calculated. Neighbors have L[w] < ∞ … … rest of nodes Invariant: shows only edges from red to blue, blue to black

18 L[w]= ∞ for all nodes w; L[v]= 0; F= {v}; while (F not empty) { // outer loop: |V| iterations f= a node in F with min L value; // done |V| times Make f red (delete from F); // done |V| times for each node w adjacent to f: if (L[w] = ∞) { // done |E| times L[w]= L[f] + weight(f,w); // done < |V| times Put w in F; // done < |V| times } else if (L[f] + weight (f,w) < L[w]) // done < |E| – |V| times L[w]= L[f] + weight(f,w); // done < |E| – |V| times } How much time? What data structure do we use for F?

19 L[w]= ∞ for all nodes w; L[v]= 0; F= {v}; while (F not empty) { f= a node in F with min L value; Make f red (delete from F); for each node w adjacent to f: if (L[w] = ∞) { L[w]= L[f] + weight(f,w); Put w in F; } else if (L[f] + weight (f,w) < L[w]) L[w]= L[f] + weight(f,w); } What data structure do we use for F? A min-heap --a heap with minimum at the top. f= node with min L value: constant time Delete f from F: log |F| time Put w in F: log |F| time Total time: O(|V| log |V|)

20 /** An instance is an edge */ public class Edge { public int start; // An edge from vertex start public int end; // to vertex end public int weight; // the weight of the edge (> 0) /** Constructor: and edge (start, end) with weight w */ public Edge(int start, int end, int w) { this.start= start; this.end= end; weight= w; }

21 public class AdjacencyList { private LNode[] V; // The adjacency list for the graph private int Esize; // number of edges /** Constructor: a graph of n nodes, with e edges given by edge set E[0..e-1]. Precondition: the edges are all different. */ public AdjacencyList(int n, int e, Edge[] E) {} /** = number of nodes in graph */ public int Vsize() {} /** = number of edges in graph */ public int Esize() {} /** = an enumerator over edges with start vertex w next() returns an Edge */ public Iterator adjacencyEnumerator(int w) {} }

22 /** = an array of shortest paths from node v to other nodes in graph g */ public static int[] shortest(AdjacencyList g, int v) { int[] L= new int[g.Vsize()]; // The shortest-path lengths from v for (int k= 0; k != L.length; k= k+1) L[k]= Integer.MAX_VALUE; L[v]= 0; int[] F= new int[g.Vsize()]; // Set F is in min-heap int Fsize= 0; // F[0..Fsize-1] // Add v to heap F and store the new heap size in Fsize Fsize= addHeap(F, Fsize, v); // Invariant: The red nodes have their L-values calculated and their neighbors have L-values < MAX_VALUE. F is the set of nodes with L-value < MAX_VALUE that are not red. …

23 while (Fsize != 0) { int f= F[0]; Fsize= removeMinHeap(F, Fsize); Iterator it= g.adjacencyEnumerator(f); while (it.hasNext()) { Edge e= (Edge)it.next(); if (L[e.end] == Integer.MAX_VALUE) { L[e.end]= L[f] + e.weight; Fsize= addHeap(F, Fsize, e.end); } else { // e.end is already in F if (L[f] + e.weight < L[e.end]) { L[e.end]= L[f] + e.weight; BubbleUp(F, Fsize, e.end); } return L; }