Weighted Graphs Computing 2 COMP1927 15s1 Sedgewick Part 5: Chapter 20.1 -20.4 21.1 - 21.3.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Chapter 23 Minimum Spanning Trees
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Graph Algorithms: Part 1
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
D IJKSTRA ' S ALGORITHM By Laksman Veeravagu and Luis Barrera.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graphs Chapter 20.
Minimum Spanning Trees
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
Minimum Spanning Trees
Minimum Spanning Tree.
CSE 373 Data Structures and Algorithms
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter

W EIGHTED G RAPHS Some applications require us to consider a cost or weight costs/weights are assigned to edges Often use a geometric interpretation of weights low weight - short edge high weight - long edge Weights are not always geometric Some weights can be negative this can make some problems more difficult! Assume in our graphs we have non-negative weights

E XAMPLE : W EIGHTED G RAPHS Example: “map” of airline flight routes vertices = airports edge = flights weights = distance/time/price

W EIGHTED G RAPH I MPLEMENTATION Adjacency Matrix Representation change 0 and 1 to float/double Need a special float constant to indicate NO_EDGE Can’t use 0. It may be a valid weight Adjacency Lists Representation add float weight to each node This will work for directed or undirected graphs

A DJACENCY M ATRIX WITH W EIGHTS

A DJACENCY L IST R EPRESENTATION WITH WEIGHTS

W EIGHTED G RAPH P ROBLEMS Minimum spanning tree find the minimal weight set of edges that connect all vertices in a weighted graph might be more than one minimal solution we will assume undirected graph we will assume non-negative weights Shortest Path Problem Find minimum cost path to from one vertex to another Edges may be directed or undirected We will assume non-negative weights

M INIMAL S PANNING T REE P ROBLEM Origins Otakar Boruvka, electrical engineer in 1926 most economical construction of electric power network Some modern applications of MST: network layout: telephone, electric, computer, road, cable Has been studied intensely, still looking for faster algorithms

9 M INIMUM S PANNING T REES (MST) Reminder: Spanning tree ST of graph G(V,E) ST is a subgraph of G ( G'(V,E') where E' is a subset of E ) ST is connected and acyclic Minimum spanning tree MST of graph G MST is a spanning tree of G sum of edge weights is no larger than any other ST Problem: how to (efficiently) find MST for graph G ?

10 K RUSKAL ’ S MST A LGORITHM One approach to computing MST for graph G(V,E) : start with empty MST consider edges in increasing weight order add edge if it does not form a cycle in MST repeat until V-1 edges are added Critical operations: iterating over edges in weight order checking for cycles in a graph

11 E XECUTION T RACE OF K RUSKAL ’ S MST

12 E XERCISE : T RACE K RUSKAL ’ S A LGORITHM

K RUSKAL ’ S A LGORITHM : M INIMAL S PANNING T REE Implementation 1: Two main parts: sorting edges according to their length ( E * log E ) check if adding an edge would create a cycle Could check for cycles using DFS... but too expensive use Union-Find data structure from Sedgewick ch.1 If we use this the cost of sorting dominates so over all E log E Implementation 2: Using a pq instead of full sort Create a priority queue using weights as priority Allows us to remove edges from pq in weighted order O ( E + X *log V ), with X = number of edges shorter than the longest edge in the MST

14 P RIM ’ S A LGORITHM : M INIMAL S PANNING T REE Another approach to computing MST for graph G(V,E) : start from any vertex s and empty MST choose edge not already in MST to add to MST must not contain a self-loop must connect to a vertex already on MST must have minimal weight of all such edges check to see whether adding the new edge brought any of the non-tree vertices closer to the tree repeat until MST covers all vertices Critical operations : checking for vertex being connected in a graph finding min weight edge in a set of edges updating min weights in a set of edges

P RIM ’ S MST A LGORITHM Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

P RIM ’ S MST A LGORITHM Idea: Starting from a sub-graph containing only one vertex, we successively add the shortest vertex connecting the sub- graph with the rest of the nodes to the tree Edges in pink are in the fringe – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

P RIM ’ S MST A LGORITHM Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

W EIGHTED G RAPH : M INIMAL S PANNING T REE II Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

W EIGHTED G RAPH : M INIMAL S PANNING T REE II Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

W EIGHTED G RAPH : M INIMAL S PANNING T REE II Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (31) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

P RIM ’ S MST A LGORITHM Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

P RIM ’ S MST A LGORITHM Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

P RIM ’ S MST A LGORITHM Idea: Starting from a subgraph containing only one vertex, we successively add the shortest vertex connecting the subgraph with the rest of the nodes to the tree Edges in pink are in the fringe Edges in black bold are in the MST – 1 (32) 0 – 2 (29) 0 – 5 (60) 0 – 6 (51) 0 – 7 (21) 1 – 7 (32) 3 – 4 (34) 3 – 5 (18) 4 – 5 (40) (51) 4 – 7 (46) 6 – 7 (25)

P RIM ’ S A LGORITHM Prim’s algorithm is just a graph search – instead of depth first (using a stack) or breadth first (using a queue), we choose a shortest first` strategy using a priority queue It can be implemented to run in O( E * log V ) steps if the steps listed above are implemented efficiently (using adjacency lists and heap), O( V 2 ) for adjacency matrix See lecture code for an implementation

25 E XERCISE : T RACE P RIM ’ S A LGORITHM

S HORTEST P ATHS Weight of a path p in graph G sum of weights on edges along path (weight(p)) Shortest path between vertices s and t a simple path p where s = first(p), t = last(p) no other simple path q has weight(q) < weight(p) Problem: how to (efficiently) find shortestPath(G,s,t)? Assumptions: weighted graph, no negative weights.

E XERCISE : What is the minimum spanning tree? What is the shortest path from 0 to 3? What is the least hops path from 0 to 2?

SHORTEST P ATH A LGORITHMS Shortest-path is useful in a wide range of applications robot navigation finding routes in maps routing in data/computer networks Flavours of shortest-path source-target (shortest path from s to t ) single-source (shortest paths from s to all other V ) all-pairs (shortest paths for all (s,t) pairs)

D IJKSTRA ’ S A LGORITHM S INGLE S OURCE S HORTEST P ATHS st

D IJKSTRA ’ S A LGORITHM : S INGLE S OURCE S HORTEST P ATH Given: weighted digraph/graph G, source vertex s Result: shortest paths from s to all other vertices dist[] : V -indexed array of distances from s st[] : V -indexed array of predecessors in shortest path Note: shortest paths can be viewed as tree rooted at s

E DGE R ELAXATION Relaxation along edge e from v to w dist[v] is length of some path from s to v dist[w] is length of some path from s to w if e gives shorter path s to w via v, then update dist[w] and st[w] Relaxation updates data on w if we find a shorter path to s. if (dist[v] + e.weight < dist[w]) { dist[w] = dist[v] + e.weight; st[w] = v; } st[v] = ?, st[w] = ? st[v] = ?, st[w] = v

D IJKSTRA ’ S A LGORITHM Data: G, s, dist[], st[], and a pq containing the set of vertices whose shortest path from s is not yet known Algorithm: initialise dist[] to all ∞, except dist[s]=0 Initialise pq with all V, with dist[v] as priority v = deleteMin from pq Get e’s that connect v to w in pq relax along e if new dist is better repeat until pq is empty

E XECUTION T RACE OF D IJKSTRA ’ S A LGORITHM

...E XECUTION T RACE OF D IJKSTRA ’ S A LGORITHN

...E XECUTION T RACE OF D IJKSTRA ’ S A LGORITHM

D IJKSTRA ’ S R ESULTS After the algorithm has completed: Shortest Path distances are in dist array Actual path can be traced back from endpoint via the predecessors in the st array

T RACE EXECUTION OF D IJKSTRA ’ S A LGORITHM F ROM STARTING VERTEX 2