Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find

Slides:



Advertisements
Similar presentations
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Advertisements

Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Tirgul 13 Today we’ll solve two questions from last year’s exams.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Lecture X Disjoint Set Operations
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Minimum Spanning Trees Problem Description Why Compute MST? MST in Unweighted Graphs MST in Weighted Graphs –Kruskal’s Algorithm –Prim’s Algorithm 1.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Introduction to Algorithms
Disjoint Sets Data Structure
Topological Sort Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Minimum Spanning Trees
Many slides here are based on E. Demaine , D. Luebke slides
Many slides here are based on D. Luebke slides
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Analysis of Algorithms CS 477/677
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Minimum Spanning Tree.
Minimum Spanning Trees
Algorithms Searching in a Graph.
Greedy Algorithms Comp 122, Spring 2004.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Binhai Zhu Computer Science Department, Montana State University
Chapter 23: Minimum Spanning Trees: A graph optimization problem
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 .
Minimum Spanning Trees
Presentation transcript:

Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find Solving a question from an exam

T4 Q. 3 Question: Your car can only drive 30km without filling its gas tank. Gas stations are only inside cities. Give a list of all cities you can reach from your home city. Answer: We will view each city as a node in a graph. Two nodes will have an edge if the distance between the cities is no more than 30km. Then run BFS/DFS on the home node - this finds exactly all the reachable nodes/cities.

T4 Q. 3 (continued) Run-time: (when we have n cities) Building the nodes - O(n) Building the edges - O(n2) [for each city we have to go over all other cities] Running BFS/DFS - O(V + E) = O(n + n2) = O(n2)

T4 Q. 4a Question: Prove that Dijkstra inserts the nodes to S in the order of their distance from s. Reminder: We proved in class that when a node w is inserted to S then We did not prove that if w is inserted after y then - this is actually the question, phrased differently.

Getting intuition by examples 10 z 2 Insertion to S: s,x,y,z 5 x 1 s Can you find another example that contradicts the claim ?

Why not ? Proof: By contradiction, but w is inserted before y. Let us look at the step that inserts w. In the lecture we proved that in this step - Find x - the first node on the path to y that is outside S. Observation: So we have and therefore Dijkstra should now insert x to S, and not w. x x’ S

A common wrong proof in your answers Step 1 (correct) : If y is inserted after w then d[y] must have changed after w was inserted. Step 2 (incorrect): If d[y] changes after w is inserted then the path to y goes through w. Conclusion (correct): Since the path to y doesn’t go through w (it is shorter), y cannot be inserted after w. A counter example to step 2: y 10 w 2 Insertion to S: s,w,x,y 1 x 5 s

T4 Q. 4b Question: We transform a weighted graph G to an unweighted Graph H by extending an edge with weight w to w edges with w-1 “dummy” nodes (we assume all weights are positive integers). Prove that BFS discovers the nodes of H in the same order that Dijkstra inserts the nodes of G into S. We also assume that any two nodes have different distances from s.

T4 Q. 4b - Answer From 4a we know that Dijkstra inserts the nodes in the order of their distance. We saw in class that the BFS queue always contains nodes of at most two levels, in the order L,...L,L+1,...,L+1 From this we can conclude that BFS also discovers nodes according to their distance. We need one more observation: The distance of a node x from s is the same in both graphs G and H. Conclusion: If Dijkstra finds x before w then BFS will find x before w as well.

Spanning Trees Spanning Tree: Given a graph G=(V,E) , a spanning tree T of G is a connected graph T=(V,E’) with no cycles (the vertices of T are the ones of G and the edges of T are a subset of those of G). For example, this graph has three spanning trees: {(a,b);(a,c)}, {(a,b);(b,c)}, {(a,c);(b,c)} a b c

Minimal Spanning Trees Minimal Spanning Tree (MST): Given a weighted graph G=(V,E) (where the weights are w(e) for all e in E), define the weight of a spanning tree T as . Then a minimal spanning tree T is a spanning tree with minimal weight, i.e. T satisfies: For example, this graph has two minimal spanning trees: {(a,b);(b,c)}, {(a,c);(b,c)} a b c 2 1

How to find MST - Prim’s Algorithm MST-Prim(G,w,r) Q  V[G] // initialize queue of vertices for each u  Q key[u]   key[r]  0 [r]  NULL while Q   // do until queue is empty u  Extract-Min(Q) for each v  Adj[u] // update key of neighbors if v  Q and w(u,v) < key[v] then [v]  u key[v]  w(u,v) Running time: O( V log V + E log V ) = O( E log V )

Example 2 b d 2 5 c a 1 1 3 y 10 w 2 1 x 5 s Discovery order: s, w, c, a, b, d, y, x

Union Find A dynamic collection S = {S1,S2,…,Sk} of disjoint sets. Each set Si is identified by a representative member. Operations: Make-Set(x): create a new set in S, whose only member is x (assuming x is not already in one of the sets). Union(x, y): replace the two sets Sx and Sy that contain x and y, by their union (assuming they are disjoint). Find-Set(x): find and return the representative of the set containing x.

Finding MST - Kruskal’s Algorithm Kruskal-MST(G=(V,E), w) T =  for each vertex v  V Make-Set(v) sort E by non-decreasing weights for each edge e=(u,v)  E { if (FindSet(u) != FindSet(v)) { T = T  {e} Union(u, v) } return T The running time depends on our implementation of Union-Find.

Union Find Implementation We will represent Disjoint sets by Forests: each set is represented by a tree, and the representative is the root. Each element points to its parent in the tree. Each node holds its rank (this is slightly different from what you saw in class): In make-set() the rank is initialized to zero.

Union By Rank and Path Compression Make the root of the with the lower rank the child of the root with the higher rank. If both ranks are equal – increase the rank of the new root by 1. Path Compression: In any find-path() make all nodes on the path to the root point directly to the root (rank does not change). Find-Set(a)

Union Find performance Theorem: A sequence of m operations can be performed on a disjoint-set forest with union by rank and path compression in worst case time of is the inverse Ackerman function. It grows very slowly and can be considered a constant less than 5 for all practical purposes. Conclusion: Kruskal’s algorithm with union find has running time of

Question 3c (“moed A” - 2001) Q: Write an algorithm that receives an undirected graph G=(V,E) and a sub-graph T=(V,ET) and determines if T is a spanning tree of G (not necessarily minimal). First, let’s check cycles, using the disjoint sets d.s. learned in class: check-cycles(T) for all v in V make-set(v) for all (u,v) in ET if findSet(u) == findSet(v) return false else union(u,v) return true

Question 3c - continued In order to check if T is a spanning tree, we need to check two things by definition: that every two vertices x,y are connected using edges from T, and that T has no cycles. In fact, if we know that T has no cycles, it is enough to check that |ET|=|V|-1. Let’s see why: Suppose T1,...,Tk are the connected components of T, and ni is the no. of vertices in Ti. Since Ti has no cycles it is a tree. Thus | ETi|= ni-1, so: Therefore, |ET|=|V|-1 <=> k=1

Question 3c - continued So the algorithm is: check-spanning-tree(T) if (|ET| != |V|-1) return false return check-cycles(T) The run-time is O((|V|)|V|) if we implement the disjoint set using path compression. Another possible algorithm - a modified DFS: change DFS-visit to return false if arriving to a node with a color that is not white, and after running DFS, check that all nodes are black.