1 / 13

Tirgul 13

Tirgul 13. Today we’ll solve two questions from last year’s exams. a. b. c. Question 3a (“moed A”). a) Define Spanning Tree and Minimal Spanning Tree.

maren
Télécharger la présentation

Tirgul 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. a b c 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)}

  3. a 2 2 b 1 c Question 3a (“moed A”) 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)}

  4. Question 3b (“moed A”) 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...)

  5. e1 v u Gv Gu e2 v’ u’ 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 Gv andGu the two connected components of Gwhen removing e1 from T1. Examine thepath from v to u in T2: there must be anedge e2=(v’,u’) in T2 such that v’ is in Gvand u’ is in Gu. 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 Gv or in Gu then there is exactly one path from x to y since Gv and Gu are connected with no cycles. If x is in Gv and y is in Gu then there is also exactly one path between them: from x to v’, then to u’, and then to y.

  6. Back to question 3b 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 suchthat is a spanning tree with weight:so T1 is not an MST.

  7. 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).

  8. a 3 2 b 1 c 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 willbe edge (a,b), now we add c back andadd the edge (b,c) to the tree.But this is not the MST! • (exercise - find a correct proof by induction...)

  9. Question 3c (“moed A”) 3c) Write an algorithm that receives an undirected graph G=(V,E)and a sub-graph T=(V,ET) and determines if T is a spanningtree 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

  10. 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

  11. 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.

  12. 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 a b 2 2 s 3 c

  13. Question 3b - “moed B” • 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.

More Related