1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

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.
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.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Graphs.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
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)
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
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.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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 Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Dijkstra's algorithm.
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.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
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
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Minimum Spanning Trees Prof. Sin-Min Lee Dept. of Computer Science, San Jose State University.
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)
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Minimum Spanning Tree Graph Theory Basics - Anil Kishore.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees and Shortest Paths
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Short paths and spanning trees
Minimum-Cost Spanning Tree
Spanning Trees.
Graphs.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
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:

1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure

2 Graph applications Graphs can be used for: Finding a route to drive from one city to another Finding connecting flights from one city to another Determining least-cost highway connections Designing optimal connections on a computer chip Implementing compilers Doing garbage collection Representing family histories Doing similarity testing (e.g. for a dating service) Playing games

3 Elementary Graph Operations Graph traversals provide the basis for many elementary graph operations : Spanning trees on graphs Graph cycles Connected components of a graph

4 Spanning Tree  Connected sub graph that includes all vertices of the original connected graph  The subgraph is a tree  If original graph has n vertices, the spanning tree has  n vertices and n-1 edges.  No cycle allowed in this sub graph

5 Minimum-cost spanning tree A minimum-cost spanning tree of a connected weighted graph is : a collection of edges connecting all vertices such that The sum of the weights of the edges is the smallest possible.

6 Spanning trees  Suppose you have a connected undirected graph  Connected  Connected: every node is reachable from every other node  Undirected  Undirected: edges do not have an associated direction ...  then a spanning tree is a connected sub graph in which there are no cycles 6 A connected, undirected graph Four of the spanning trees of the graph

7 Finding a spanning tree – We did this in class  To find a spanning tree of a graph, 1. pick an initial node and call it part of the spanning tree 2. do a search from the initial node: Each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it 7 An undirected graph Result of a BFS starting from top Result of a DFS starting from top

8 Spanning Tree - Minimum Edges  Minimum number of edges to keep the graph connected N-1  If have N vertices, spanning tree has N-1 edges

9 Minimizing costs  Suppose you want to supply a set of houses (say, in a new subdivision) with:  electric power  Water  sewage lines  telephone lines 9

10 Minimum cost spanning tree  To keep costs down:  Connect these houses with a spanning tree (of, for example, power lines) all equal distances apart  However, the houses are not all equal distances apart  To reduce costs even further, you could connect the  houses with a minimum-cost spanning tree

11 Minimum Spanning Tree (MST) Spanning tree with minimum weight

12 Algorithm For Finding MST 1. All nodes are unselected, 2. Mark node v selected to get started – can be any node 3. For each node of graph, { minimum weight Find the edge with minimum weight that connects an unselected node with a selected node Mark this unselected node as selected }

13 Demos For Finding MST A  Step 1: mark vertex A as selected A B C D

14 Demos For Finding MST  Step 2: find the minimum weighted edge connected to vertex A, and mark the other vertex on this edge as selected.  We choose {A to D} A B C D

15 Demos For Finding MST  Step 3: find the minimum weighted edge connected to vertices set { A, D },  and mark the other vertex on this edge as selected.  We choose { A, B } A B C D

16 Demos For Finding MST  Step 4:  find the minimum weighted edge connected to vertices set  { A, D, B}, and mark the other vertex on this edge as selected.  We choose {B,C} { A, D, B, C}  vertices set { A, D, B, C}  No more nodes! A B C D

17 Demos For Finding MST  Step 5: All vertices are marked as selected, So we find the minimum spanning tree A B C D

18 Complexity For Finding MST  N nodes, E edges N * E = O(N*E)  Analysis: N * E = O(N*E)  Number of nodes * the number of Edges  Better implementation:  Heap O(logE) where is E is number of edges(weights) O(E log E) Initialization of heap: O(E log E) Number of edges * log of E

19 Cost - Definition  Routing Protocol  Cost = congestion, bandwidth, delay … cost Umass Router cost

20 Minimum-Cost Spanning Trees collection of edges connecting all vertices  A minimum-cost spanning tree of a connected weighted graph is a collection of edges connecting all vertices such that the sum of the weights of the edges is the smallest possible. Prim’s algorithmalways pick the edge with the smallest weight to any node. It is a greedy algorithm. Prim’s algorithm: always pick the edge with the smallest weight to any node. It is a greedy algorithm. h a f e b c d g 9 h a f e b c d g 9

21 Kruskal’s algorithm T = empty spanning tree; E = set of edges; N = number of nodes in graph; while T has fewer than N - 1 edges { remove an edge (v, w) of lowest cost from E if adding (v, w) to T would create a cycle then discard (v, w) else add (v, w) to T } 21

22 MST Algorithms  Finding an edge of lowest cost can be done just by sorting the edges –  so could use a heap  Efficient testing for a cycle requires a fairly complex algorithm (UNION-FIND)

23 MCST --- Another Example h a f e b c d g 9 h a f e b c d g 9 h a f e b c d g 9 h a f e b c d g 9

24 MCST --- Example (cont’d) h a f e b c d g 9 h a f e b c d g 9 h a f e b c d g 9 h a f e b c d g 9 All nodes visited!

25 Shortest Path Problem  Weight: cost, distance, travel time, hop … u v

26 Prim’s Algorithm: Heap Implementation Consider using a heap to hold the keys D[0…n-1]. We use a ( Min Heap): To find the minimum in heap array using tree formulas,  To find the minimum in heap array using tree formulas, we call Extract_Min(heap) we call Extract_Min(heap)

27 ExtractMin  Extract_Min removes the minimum element from the heap  which is in the root  by swapping in the last element. O(1)  Then sift down last element(LgV)  We get O(log V) using the usual removal heap operation

28 PRIMS ALGORITHM- Heap implementation  Extract_Min removes the minimum element from the heap which is in the root. Heap Implementation: O(E log V) or Number of Edges times the log of # of Vertices

29 Single-Source Shortest Paths – Prim’s  To find the shortest path from one given node to all the others  Given a source x.  Find the path from x to v  such that v is in V-x and the path is minimum.  If the weights in the edges are all one we already have a solution: breadth first search

30 Single Source Shortest Paths Given a weighted connected graph G=(V,E), and given a pair of vertices v s (source) and v d (destination) Î V what is the shortest path from v s to v d ? That is, what is the path that has the smallest sum of edge weights? Source vertex v s is vertex 1 Bold line is shortest path from 1(v s ) to 2 (v d )

31 Single Source Shortest Paths SP from A to H = SP from A to E + SP from E to H. SP from A to H = SP from A to B + SP from B to H. SP from A to H = SP from A to C + SP from C to H. In general: SP from A to H = SP from A to v i + SP from v i to H;  v i. A B E F H15 A B E G H14 A C E F H16 A C E G H15 A D E F H26 A D E G H25 A B D CE F G H

32 Single Source Shortest Paths Use Dijkstra’s (greedy) algorithm if graph has only positive weights Use Bellman-Ford’s algorithm if graph has positive and negative weights (but not negative cycles…) Both algorithms can run on directed or undirected graphs

33 Single-Source Shortest Paths  The solution when we have different weights is a variation of Prim's algorithm  Before we chose the node that was closest to the partial minimum tree.  Now we want the node which is closest to the source node

34 Single-Source Shortest Paths Algorithm: 1. Start from the source node 2. make this node part of the a shortest path tree A=(V A,E A ) 3. Find an edge (x,y) such that x in the V A and y is not, so that the path from the initial node to y is minimum 4. add y and the edge (x,y) to the tree A. Algorithm: 1. Start from the source node 2. make this node part of the a shortest path tree A=(V A,E A ) 3. Find an edge (x,y) such that x in the V A and y is not, so that the path from the initial node to y is minimum 4. add y and the edge (x,y) to the tree A.

35 Single-Source Shortest Paths  The "question" here is: how do we find the next edge to be added in the tree?  We again can make use of existing data structures  As the case with Prim's algorithms for the MST we can use a priority queue  The algorithms described in the following slides is normally attributed to E. Dijkstra  Let's understand the algorithm graphically first.

36 An Example (adapted from Sedgewick) A F B D C E G HI JK LM

37 An Example (adapted from Sedgewick) A F B D C E G HI JK LM A0A 0A0A

38 An Example (adapted from Sedgewick) A F B D C E G HI JK LM B1B 1B1B 2F2F 2F2F 6G6G 6G6G

39 An Example (adapted from Sedgewick) A F B D C E G HI JK LM C C F F D D E E G G

40 An Example (adapted from Sedgewick) A F B D C E G HI JK LM F2F 2F2F 3D3D 3D3D 6E6E 6E6E 6G6G 6G6G

41 An Example (adapted from Sedgewick) A F B D C E G HI JK LM D3D 3D3D 4L4L 4L4L 4E4E 4E4E 6G6G 6G6G

42 An Example (adapted from Sedgewick) A F B D C E G HI JK LM L4L 4L4L 4E4E 4E4E 6G6G 6G6G

43 4E4E 4E4E 5M5M 5M5M An Example (adapted from Sedgewick) A F B D C E G HI JK LM G6G 6G6G 7J7J 7J7J 1

44 5M5M 5M5M An Example (adapted from Sedgewick) A F B D C E G HI JK LM G5G 5G5G 7J7J 7J7J 2

45 An Example (adapted from Sedgewick) A F B D C E G HI JK LM G5G 5G5G 7J7J 7J7J

46 An Example (adapted from Sedgewick) A F B D C E G HI JK LM J6J 6J6J 8H8H 8H8H

47 An Example (adapted from Sedgewick) A F B D C E G HI JK LM K7K 7K7K 8H8H 8H8H 1

48 An Example (adapted from Sedgewick) A F B D C E G HI JK LM I8I 8I8I 8H8H 8H8H 3

49 An Example (adapted from Sedgewick) A F B D C E G HI JK LM H8H 8H8H

50 An Example (adapted from Sedgewick) A F B D C E G HI JK LM

51 The Algorithm  The algorithm here is the almost the same as the one for the minimum spanning tree  The distance to the initial node is considered instead of the absolute value of the edge  Inserting the node in the priority queue is done by considering all the edges that leave the node plus the value (the priority) assigned to the node that is being removed.  Delete is done based on the priority queue strategy

52 ANOTHER EXAMPLE

53 Dijkstra’s Algorithm (Cont’d) A B D CE F G H A 2 B 1 C 6 D 3 E ? F ? G ? H A B D CE F G H A 2 B 1 C 6 D 3 E 10 F 8 G ? H S = {A,C,B,E} S = {A,C,B} Cost of SP from A to v i through S

54 Dijkstra’s Algorithm (Cont’d) A B D CE F G H A 2 B 1 C 6 D 3 E 10 F 8 G ? H A B D CE F G H A 2 B 1 C 6 D 3 E 10 F 8 G 14 H S = {A,C,B,E,D,G} S = {A,C,B,E,D} Cost of SP from A to v i through S

55 Dijkstra’s Algorithm (Cont’d) A B D CE F G H A 2 B 1 C 6 D 3 E 10 F 8 G 14 H A B D CE F G H A 2 B 1 C 6 D 3 E 10 F 8 G 14 H S = {A,C,B,E,D,G,F,H} S = {A,C,B,E,D,G,F} Cost of SP from A to v i through S

56 Dijkstra’s Algorithm for Shortest Paths S = {0} /* Current MST */ for i = 0 to n D[i] = M[0][i] /* Shortest path length from 0 to i */ for i = 1 to n-1 find the smallest D[v] such that v  S S = S  {v} for all vertices u  S if (D[u] > D[v] + M[v][u]) then D[u] = D[v] + M[v][u]

57 Dijkstra’s Algorithm --- Example ? 3 ? 4 ? ? 4 ?

58 Example – Dijkstra Algorithm  Greedy Algorithm  Assume all weight of edge > source node from node V 0 to other nodes V1V1 10 V2V2 5 V3V3  V4V4  best

59 Animation Online  Dijkstra’s Shortest Path Algorithm - at my website