Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find"— Presentation transcript:

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

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

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

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

5 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 ?

6 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

7 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

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

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

10 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

11 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

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

13 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

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

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

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

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

18 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

19 Question 3c (“moed A” ) 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

20 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

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


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

Similar presentations


Ads by Google