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

## Presentation on theme: "Tirgul 13 Today we’ll solve two questions from last year’s exams."— 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”

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

Similar presentations