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.

Slides:



Advertisements
Similar presentations
Chapter 9 Greedy Technique. Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible - b feasible.
Advertisements

Lecture 15. Graph Algorithms
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.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Applied Discrete Mathematics Week 12: Trees
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
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.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
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.
WEIGHTED GRAPHS. Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation:
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Copyright © Cengage Learning. All rights reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Minimum Spanning Trees
Spring 2015 Lecture 11: Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Minimum- Spanning Trees
Graphs Upon completion you will be able to:
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Dijkstra animation. Dijksta’s Algorithm (Shortest Path Between 2 Nodes) 2 Phases:initialization;iteration Initialization: 1. Included:(Boolean) 2. Distance:(Weight)
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Trees
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Minimum-Cost Spanning Tree
Outline This topic covers Prim’s algorithm:
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum-Cost 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 Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities. CHG SF HTD OAK ATL LA SD V = {SF, OAK, CHG, HTD, ATL, LA, SD} E = {{SF, HTD}, {SF, CHG}, {SF, LA}, {SF, SD}, {SD, OAK}, {CHG, LA}, {LA, OAK}, {LA, ATL}, {LA, SD}, {ATL, HTD}, {SD, ATL}}          

Problem formulation: find the "best" path between two vertices v 1, v 2  V in graph G = (V, E). Depending on what the "best" path means, we have 2 types of problems: 1.The minimum spanning tree problem, where the "best" path means the "lowest-cost" path. 2.The shortest path problem, where the "best" path means the "shortest" path. Note that here edge weights are not necessarily Euclidean distances. Example: 2985 > , not the case here, however.            

The Weighted Graph ADT Definition A weighted graph, G, is a triple (V, E, W), where (V, E) is a graph, and W is a function from E into Z +, where Z + is a set of all positive integers. That is, W : E  Z +. Additional operations (methods) on weighted graphs: addEdge(v1, v2, weight) Returns G with new edge v1v2 added removeEdge(v1, v2, weight) Returns G with edge v1v2 removed edgeWeight(v1, v2) Returns the weight of edge v1v2

The minimum spanning tree problem Definition. A minimum spanning tree of a weighted graph is a collection of edges connecting all of the vertices such that the sum of the weights of the edges is at least as small as the sum of the weights of any other collection of edges connecting all of the vertices. Example Consider the following graph  a  b mm  l  k  j  i  h  d  e  f  g  c                      

Property of a minimum spanning tree (MST). Given any division of the vertices of a graph into two sets, the minimum spanning tree contains the shortest of the edges connecting a vertex in one of the sets to a vertex in the other set. This property tells us that we can start building the MST by selecting any vertex, and always taking next the vertex which is closest to the vertices already on the tree. If more than one "closest" vertex exists, then we can take anyone of these vertices (therefore, a MST of a graph is not unique). Example: Let V1 = {a, b, c, d}, V2 = {e, f, …, m}. Then, the MSP must contain edge fd, because W(fd) = 1. Note that V2 consists of two types of vertices: 1.Fringe vertices, which are adjacent to V1. 2.Unseen vertices, which are not adjacent to V1. Extended example to be distributed in class!

Generation of a MST : the Prim's algorithm The idea: Select an arbitrary vertex to start the tree. While there are fringe vertices remaining, select an edge of minimum weight between a tree vertex and a fringe vertex, and add the selected edge and fringe vertex to the tree. Algorithm MST (start, T) Included[start] = true // Assume Boolean array Included tells, for (node = 2) to NumberOfNodes // which vertices are already in the MST. Included[node] = false for (node = 1) to (NumberOf Nodes - 1) { edge = FindMinEdge () // Requires a loop over all of the nodes. Included[edge.IncidentNode()] = true AddEdge(edge, MST) } Efficiency result: Prim's algorithm for generating a MST is O(N^2), where N is the number of nodes in the tree. Since the number of edges is not important it is good for dense graphs.

Generation of a MST : the Kruskal's algorithm The idea: Add edges one at a time selecting at each step the shortest edge that does not form a cycle. Assume that vertices of a MST are initially viewed as one element sets, and edges are arranged in a priority queue according to their weights. Then, we remove edges from the priority queue in order of increasing weights and check if the vertices incident to that edge are already connected. If not, we connect them and this way the disconnected components gradually evolve into a tree -- the minimum spanning tree. Extended example to be distributed in class!

Efficiency result: Assume that 1.The priority queue is implemented as a heap. 2.The minimum spanning tree is implemented as a weight-balanced tree. 3.The graph is implemented by means of adjacency lists. Then: 1.The initial formation of the priority queue of edges is O(NumberOfEdges*log(NumberOfEdges)) operation. 2.The phase of removing edges from the queue and performing one or two operations requires also O(NumberOfEdges*log(NumberOfEdges)) time. Therefore, the total efficiency of the Kruskal's algorithm is O(NumberOfEdges*log(NumberOfEdges)).

The shortest-path problem Definition. The weight, or length, of a path v 0, v 1, v 2, …, v k in weighted graph k-1 G = (V, E, W) is  W(v i v i+1 ). Path v 0, v 1, v 2, …, v k is the shortest path from i = 0 v 0 to v k if there is no other path from v 0 to v k with lower weight. Definition. The distance from vertex x to vertex y (x, y  V), denoted as d(x,y) is the weight of the shortest path from x to y. The problem: Given x  V, we want to find the shortest paths from x to any other vertex in V in order of increasing distance from x. Consider the following two cases: 1.All weights are "1". Therefore, the problem becomes finding a path containing the minimum number of edges. To solve this problem, we can use the breadth-first search algorithm. 2.If edge weights are different, we can use the Dijkstra's shortest path algorithm.

The shortest-path problem: Dijkstra's algorithm Extended example to be distributed in class! To implement Dijkstra's algorithm we need the following data structures: 1.An integer array, distance, of NumberOfNodes size (assuming that edge weights are integers). 2.A Node array, path, of NumberOfNodes size. 3.A Boolean array, included, of NumberOfNodes size. Given the start node, the initialization of these arrays is the following: 1.included[start] := true, all other entries in included initialized to false. 0, if node = start 2.distance[node] := EdgeWeight(start, node) , if there does not exist a direct edge between start and node 3.path[node] := start, if there exists an edge between start and node undefined, otherwise.

Dijkstra's algorithm (contd.) The iteration phase: repeat find the node, j, that is at the minimum distance from start among those not yet included and make included[j] := true for each node, r, not yet included if r is connected by an edge to j, then if distance[j] + EdgeWeight(j, r) < distance[r] then distance[r] := distance[j] + EdgeWeight(j, r) path[r] := j // path contains the immediate predecessor of each node until included[destination_node] := true Efficiency result. If EdgeWeight operation is O(1), then Dijkstra's algorithm is O(NumberOfNodes^2).