Tirgul 13 Today we’ll solve two questions from last year’s exams.

Slides:



Advertisements
Similar presentations
Greedy Algorithms Greed is good. (Some of the time)
Advertisements

10.4 Spanning Trees. Def Def: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G See handout.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
Tirgul 8 Graph algorithms: Strongly connected components.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Discussion #36 Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
Tree tree = connected graph with no cycle tree = connected graph with |V|-1 edges tree = graph with |V|-1 edges and no cycles.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
A general approximation technique for constrained forest problems Michael X. Goemans & David P. Williamson Presented by: Yonatan Elhanani & Yuval Cohen.
DAST 2005 Tirgul 11 (and more) sample questions. DAST 2005 Q.Let G = (V,E) be an undirected, connected graph with an edge weight function w : E→R. Let.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees
More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.
All Shortest Paths Questions from exercises and exams.
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.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
Analysis of Algorithms
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2012 Lecture 10.
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.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
EMIS 8374 Optimal Trees updated 25 April slide 1 Minimum Spanning Tree (MST) Input –A (simple) graph G = (V,E) –Edge cost c ij for each edge e 
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
Introduction to Graph Theory
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
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)
Minimum- Spanning Trees
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
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.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum Spanning Tree.
Connected Components Minimum Spanning Tree
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Trees
Autumn 2016 Lecture 10 Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Winter 2019 Lecture 10 Minimum Spanning Trees
Minimum Spanning Trees
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

Tirgul 13 Today we’ll solve two questions from last year’s exams.

Question 3a (“moed A”) a) Define Spanning Tree and Minimal Spanning Tree. 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 bc

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)} Question 3a (“moed A”) a bc 22 1

b) Either prove or disprove the following claim: In a weighted (connected) graph, if every edge has a different weight then G has exactly one MST. First notice that if the edge weights are not distinct, then the claim is incorrect, for example the previous graph. So, can we come up with a counter-example when weights are distinct ? (no, but thinking about it for a few minutes sometimes helps...) Question 3b (“moed A”)

A useful feature of spanning trees Claim: Suppose T1 and T2 are two spanning trees of G. Then for any edge e1 in T1\T2 there exists an edge e2 in T2\T1 such that is also a spanning tree. Proof: Suppose e1=(v,u). Denote by G v and G u the two connected components of G when removing e1 from T1. Examine the path from v to u in T2: there must be an edge e2=(v’,u’) in T2 such that v’ is in G v and u’ is in G u. Let. T’ is connected and has no cycles [thus it is a spanning tree, as claimed]: take two vertices x and y in G. If both are in G v or in G u then there is exactly one path from x to y since G v and G u are connected with no cycles. If x is in G v and y is in G u then there is also exactly one path between them: from x to v’, then to u’, and then to y. GvGv GuGu vu v’u’ e1 e2

Claim: In a weighted (connected) graph, if every edge has a different weight, then G has exactly one MST. Proof: Suppose by contradiction that there are two MSTs, T1 and T2. Suppose also that the largest edge in T1\T2 is larger than the largest edge in T2\T1 (notice they can’t be equal). Let e1 be the largest edge in T1\T2. There is an edge e2 in T2\T1 such that is a spanning tree with weight: so T1 is not an MST. Back to question 3b

Some wrong proofs for this claim A common (but wrong) argument from exams of last year: “The Generic-MST algorithm always has a unique safe edge to add, thus it can create only one MST.” Why this is wrong: –First, there might be other ways to find an MST besides the Generic-MST algorithm. Showing that a specific algorithm always returns the same MST does not prove that this is the only MST! –Second, it is not true that there is always one unique safe edge (!) For example, Prim and Kruskal might choose a different edge at the first step, although they are both Generic-MST variants (Prim will choose the smallest edge that one of its vertices is s, the starting vertex, and Kruskal will choose the smallest edge in the entire graph - see the graph in the slide and suppose prim starts from vertex a).

What about induction ?? Induction is not always the best way to prove things! Another common (but wrong) proof from last year was: –An induction on the number of vertices in G. In the induction step, remove some vertex v and its edges. Then there is one MST T* by the induction assumption. Now add v back - the only possible MST now is T* plus the smallest edge that connects v to T*. This proof is not correct. For example: If we remove vertex c then the MST will be edge (a,b), now we add c back and add the edge (b,c) to the tree. But this is not the MST! (exercise - find a correct proof by induction...) a bc 32 1

3c) Write an algorithm that receives an undirected graph G=(V,E) and a sub-graph T=(V,E T ) 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 E T if findSet(u) == findSet(v) return false else union(u,v) return true Question 3c (“moed A”)

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 |E T |=|V|-1. Let’s see why: Suppose T 1,...,T k are the connected components of T, and n i is the no. of vertices in T i. Since T i has no cycles it is a tree. Thus | E T i |= n i -1, so: Therefore, |E T |=|V|-1 k=1

So the algorithm is: check-spanning-tree(T) if (|E T | != |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. Question 3c - continued

Question 3a - “moed B” 3a) Both in Dijkstra and in Prim we have a set of nodes S (that initially contains only s), and we add one additional node in each iteration. Prove or disprove that in both algorithms the nodes are added to S in the same order. Answer: the claim is not correct. For example: –Prim takes s,a,b,c –Dijkstra takes s,a,c,b s ab c 2 2 3

Consider a directed graph with positive weights. Give an algorithm that receives a node s and prints the shortest cycle that contains s. Answer: create a modified graph G’ as follows. Add one new node s’. For every edge s -> v add an edge s’ -> v with the same weight. Now find a shortest path from s’ to s. This is also a shortest cycle that contains s. Explanation: –Every cycle that contains s in G corresponds to a path from s’ to s in G’ –Every path from s’ to s in G’ corresponds to a cycle that contains s in G. Question 3b - “moed B”