© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.

Slides:



Advertisements
Similar presentations
Unit-iv.
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.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Minimum Spanning Tree Algorithms
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CS Data Structures Chapter 6 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.
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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
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.
Dijkstra's algorithm.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
SPANNING TREES Lecture 21 CS2110 – Spring
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
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++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Minimum Spanning Trees
Data Structures 13th Week
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Shortest Path Graph represents highway system Edges have weights
Short paths and spanning trees
Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.
Data Structures & Algorithms Graphs
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Chapter 11 Graphs.
Minimum Spanning Tree Algorithms
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Minimum Spanning Trees
Minimum Spanning Tree.
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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 .
Presentation transcript:

© 2015 JW Ryder CSCI 203 Data Structures1

© 2015 JW Ryder CSCI 203 Data Structures2

© 2015 JW Ryder CSCI 203 Data Structures3

© 2015 JW Ryder CSCI 203 Data Structures4

© 2015 JW Ryder CSCI 203 Data Structures5

© 2015 JW Ryder CSCI 203 Data Structures6

© 2015 JW Ryder CSCI 203 Data Structures7

© 2015 JW Ryder CSCI 203 Data Structures8 bfs (), dfs () Ways to cover all connected vertices, a way to explore What does O(e) mean? Is O(e) better than O(n)? 0 < O(1) < O(e) <= O(n 2 ) Algorithm is dependent on the number of edges as far as its performance is concerned

© 2015 JW Ryder CSCI 203 Data Structures9 Connected Components void connected (void) { // determine connected components of graph int i; for (i = 0; i < n; i++) if (!visited [i]) { dfs (i); cout << endl; }// end then } // end connected

© 2015 JW Ryder CSCI 203 Data Structures10 Spanning Trees bfs () or dfs () implicitly partitions edges of G into 2 sets –T - Tree edges –N - Non-Tree edges T is the set of edges traversed during execution and N are the rest Can determine (save) edge list by recording edges as algorithm arrives at each T then represents the head of the traversed edge list

© 2015 JW Ryder CSCI 203 Data Structures11 Spanning Tree Definition Any tree that consists solely of edges in G and that includes all the vertices in G May use dfs () or bfs () to create a spanning tree When dfs () used, spanning tree known as dfs spanning tree When bfs () used, what is it called?

© 2015 JW Ryder CSCI 203 Data Structures12 Adding N edges Suppose we add a non-tree edge (v, w) into any spanning tree T Result ==> cycle created consisting of edge (v,w) and all edges on the path from w to v in T Show example with one of the drawn spanning trees

© 2015 JW Ryder CSCI 203 Data Structures13 Minimum Cost Spanning Trees (MST) The cost of a spanning tree of a weighted undirected graph is the sum of the costs (weights) of the edges in the spanning tree. A minimum cost spanning tree is a spanning tree of least cost

© 2015 JW Ryder CSCI 203 Data Structures14 3 Common MST Algorithms Kruskal Prim Sollin All three use a design method called the Greedy Method

© 2015 JW Ryder CSCI 203 Data Structures15 Greedy Method Construct an optimal solution in stages At each stage we make a decision that is the best decision (using some criterion) Cannot change decision later so the decision must result in a feasible solution Can be applied to wide variety of problems

© 2015 JW Ryder CSCI 203 Data Structures16 Selection Criteria Selection of item at each stage is based on either least cost or highest profit criterion Feasible solution is one which works within the constraints specified by problem

© 2015 JW Ryder CSCI 203 Data Structures17 MST - Least Cost Solution must –use only edges within the graph G –use exactly n - 1 edges –may not use edges that would produce a cycle

© 2015 JW Ryder CSCI 203 Data Structures18 Kruskal’s Algorithm Builds a MST by adding edges to T one at a time Selects edges in T in non- decreasing order of cost Edge added to T if it does not form a cycle with edges that are already in T G is connected, has n > 0 vertices, exactly n - 1 edges will be selected to include in T

© 2015 JW Ryder CSCI 203 Data Structures19 Kruskal Continued Draw and do example (only talking no algorithm) Initially, E is set of all edges in G

© 2015 JW Ryder CSCI 203 Data Structures20 Kruskal’s Algorithm T = { }; while (T has < n-1 edges) && (E not Empty) { choose a least cost edge (v,w) from E; delete (v,w) from E; if ((v,w) doesn’t create a cycle in T) add (v,w) to T; else discard (v,w); } // end while if (T contains < n-1 edges) printf (“No Spanning Tree\n”);

© 2015 JW Ryder CSCI 203 Data Structures21 Kruskal Particulars Must be able to determine edge with minimum cost edge and delete it –Min-heap; determine and delete next least cost edge in O(lg e); can sort in O(e lg e) To check that new edge (v,w) doesn’t create cycle in T and to add edge to T, use union-find operations. –View each connected component in T as a set containing the vertices in that component –Initially T is empty, each vertex of G is separate set –Before adding edge (v,w), use find operation to determine if v and w are in same set Yes? 2 vertices already connected No? add edge

© 2015 JW Ryder CSCI 203 Data Structures22 Shortest Path Graph represents highway system Edges have weights Length of a path is sum of weights of edges along path Starting vertex is source, ending vertex is destination One way streets are possible; digraph Weights are positive

© 2015 JW Ryder CSCI 203 Data Structures23 Single Source All Destinations Directed Graph, G = (V, E) Weighting function w(e), w(e) > 0 Source vertex is V 0

© 2015 JW Ryder CSCI 203 Data Structures24 Graph in Figure 1 Shortest path from V 0 to all other vertices V 0 to V 1 Greedy algorithm to generate shortest paths S is set of vertices whose shortest paths have been found For w not is S, distance[w] is length of shortest path starting from V 0 going through vertices only in S and ending at w Generate paths in non- decreasing order of lengths

© 2015 JW Ryder CSCI 203 Data Structures25 Observation 1 If the next shortest path is to vertex u, then the path from V 0 to u goes through only those vertices that are in S –Show all intermediate vertices on the shortest path from V 0 to u are already in S –Assume vertex w on path not in S –Path from V 0 to u must go through w and length from V 0 to w must be less than length from V 0 to u –Since paths generated in non- decreasing order we must have generated path to w before –Contradiction! –Ergo cannot be any intermediate vertex that is not in S

© 2015 JW Ryder CSCI 203 Data Structures26 Observation 2 Vertex u is chosen so that it has the minimum distance among all vertices not in S If several vertices with same distance the choice is not important

© 2015 JW Ryder CSCI 203 Data Structures27 Observation 3 Once u is selected and the shortest path has been generated from V 0 to u, u becomes a member of S Adding u to S can change the shortest paths starting at V 0, going through vertices only in S, and ending at vertex w that is not currently in S If distance changes, we have found a shorter path from V 0 to w passing through u –Intermediate vertices are in S, subpath from u to w can be chosen so that there are no intermediate vertices –The length of the shorter path is distance[u] + length( )

© 2015 JW Ryder CSCI 203 Data Structures28 Who made these Observations? Observations and Algorithm credited to Edsger Dijkstra

© 2015 JW Ryder CSCI 203 Data Structures29 Mechanics n vertices numbered from 0 to n-1 Set S maintained as an array with name found[ ] –found[i] = TRUE (1) if vertex i in S else FALSE (0) Graph represented by its cost adjacency matrix –cost [i][j] = weight of edge –If edge not in G set cost [i][j] to some large number Choice of number is arbitrary but can’t overflow sign Must be > any other number in matrix distance [u] + cost [u][w] can’t produce overflow

© 2015 JW Ryder CSCI 203 Data Structures30 Shortest Path Parameters Draw distance [ ], found [ ], cost [ ] [ ], n, v for Figure 1 Draw figures on board, all based on n V0V0 V1V1 V4V4 V5V5 V3V3 V2V Figure 1

© 2015 JW Ryder CSCI 203 Data Structures31 Basic Idea with Words Initialize set S to Empty Initialize distance[ ] to the distances for the source vertex from cost matrix Put v into S; Make sure distance to self is 0 For i = 0 to Number of Vertices in G - 2 do Choose next vertex u to add to S. Will be vertex from the set E-S with the shortest distance Add u to S For w = each vertex in G // 0.. n-1 if w is not already in S if distance from v to u then to w < current shortest to w Make shortest distance from v through u to w for distance [w] endif endfor

© 2015 JW Ryder CSCI 203 Data Structures32 choose ( ) int choose (int distance [ ], int n, short int found [ ] ) { // Find smallest distance not yet checked int i, min, minpos; min = INT_MAX; minpos = -1; for (i = 0; i < n; i++) if (distance [i] < min && !found [i]) { min = distance [i]; minpos = i; } return minpos; } // end choose ( )

© 2015 JW Ryder CSCI 203 Data Structures33 shortestpath ( ) void shortestpath (int v, int cost [ ][MAX_VERTICES], int distance [ ], int n, short int found [ ]) { // distance [i] is shortest path from vertex v to i, found [i] holds a 0 if shortest path for vertex i has not been found else 1, cost is the cost adjacency matrix int i, u, w; for (i = 0; i < n; i++) { // Initialize part found [i] = FALSE; distance [i] = cost [v][i]; } // end for found [v] = TRUE; distance [v] = 0; for (i = 0; i < n-2; i++) { u = choose (distance, n, found); // Pick next vertex to add to S found [u] = TRUE; // Add it to S for (w = 0; w < n; w++) { // Check all vertices adjacent to if (!found [w]) // u but not yet in S if (distance [u] + cost [u][w] < distance [w]) distance [w] = distance [u] + cost [u][w]; } // end for

© 2015 JW Ryder CSCI 203 Data Structures34 Shortest Path Analysis O(n 2 ) n vertices, adjacency matrix First for loop takes O(n) Second for loop executed n-2 times - O(n) –This loop needs O(n) - each execution choose () - O(n) Update distance [ ] - O(n) –Total time needed is O(n 2 ) Each edge in G examined at least once O(e) –Minimum possible with cost adjacency O(n 2 )