1 / 19

MA/CSSE 473 Day 38

MA/CSSE 473 Day 38. Finish Disjoint Sets Dijkstra. MA/CSSE 473 Day 38. HW 14 Only do problems 1, 2, 3, and 9 (Fall, 2010 vs. Fall, 2012) Fill out the Course evaluation form If everybody in a section does it, everyone in that section gets 5 bonus points on the Final Exam

elita
Download Presentation

MA/CSSE 473 Day 38

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. MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra

  2. MA/CSSE 473 Day 38 • HW 14 • Only do problems 1, 2, 3, and 9 • (Fall, 2010 vs. Fall, 2012) • Fill out the Course evaluation form • If everybody in a section does it, everyone in that section gets 5 bonus points on the Final Exam • I can't see who has completed the evaluation, but I can see how many • Final Exam Monday evening, O269 • Student Questions • Finish disjoint set discussion

  3. Final Exam Format • Same as previous exams. • You may bring up to four pages of notes. • Double-sided • Hopefully that is enough for you to write down details of things you understand but don't want to memorize. • Material from • Chapters 1-9 • Excerpts from Weiss and Dasgupta that I posted on ANGEL • HW 1-14 • All class days

  4. Recap: Disjoint Set Representation • Each disjoint set is a tree, with the "marked" element as its root • Efficient representation of the trees: • an array called parent • parent[i] contains the index of i’s parent. • If i is a root, parent[i]=i 5 1 7 4 2 3 6 8

  5. Using this representation def makeset1(i): parent[i] = i • makeset(i): • findset(i): • mergetrees(i,j): • assume that i and j are the marked elements from different sets. • union(i,j): • assume that i and j are elements from different sets def findset1(i): whilei!= parent[i]: i = parent[i] returni def mergetrees1(i,j): parent[i] = j def union1(i,j): mergetrees1(findset1(i), findset1(j))

  6. Can we keep the trees from growing so fast? • Make the shorter tree the child of the taller one • What do we need to add to the representation? • rewrite makeset, mergetrees. • findset& union are unchanged. • What can we say about the maximum height of a k-node tree? def makeset2(i): parent[i] = i height[i] = 0 def mergetrees2(i,j): ifheight[i] < height[j]): parent[i] = j elifheight[i] > height[j]: parent[j] = i else: parent[i] = j height[j] = height[j] + 1

  7. Theorem: max height of a k-node tree T produced by these algorithms is lg k • Base case… • Induction hypothesis… • Induction step: • Let T be a k-node tree • T is the union of two trees: T1with k1nodes and height h1 T2with k2nodes and height h2 • What can we about the heights of these trees? • Case 1: h1≠h2. Height of T is • Case 2: h1=h2. WLOG Assume k1≥k2. Then k2≤k/2. Height of tree is 1 + h2 ≤ …

  8. Worst-case running time • Again, assume n makeset operations, followed by m union/find operations. • If m > n • If m < n

  9. Speed it up a little more • Path compression: Whenever we do a findset operation, change the parent pointer of each node that we pass through on the way to the root so that it now points directly to the root. • Replace the height array by a rank array, since the number now is only an upper bound for the height. • Look at makeset, findset, mergetrees (on next slides)

  10. Makeset This algorithm represents the set {i} as a one-node tree and initializes its rank to 0. def makeset3(i): parent[i] = i rank[i] = 0

  11. Findset • This algorithm returns the root of the tree to which i belongs and makes every node on the path from i to the root (except the root itself) a child of the root. deffindset(i): root = i whileroot != parent[root]: root = parent[root] j = parent[i] whilej!= root: parent[i] = root i = j j = parent[i] returnroot

  12. Mergetrees This algorithm receives as input the roots of two distinct trees and combines them by making the root of the tree of smaller rank a child of the other root. If the trees have the same rank, we arbitrarily make the root of the first tree a child of the other root. defmergetrees(i,j) : ifrank[i] < rank[j]: parent[i] = j elifrank[i] > rank[j]: parent[j] = i else: parent[i] = j rank[j] = rank[j] + 1

  13. Analysis • It's complicated! • R.E. Tarjan proved (1975)*: • Let t = m + n • Worst case running time is Ѳ(t α(t, n)), whereα is a function with an extremely slow growth rate. • Tarjan'sα: • α(t, n) ≤ 4 for all n ≤ 1019728 • Thus the amortized time for each operation is essentially constant time. * According to Algorithmsby R. Johnsonbaugh and M. Schaefer, 2004, Prentice-Hall, pages 160-161

  14. Dijkstra's Algorithm • For a selected vertex, (call it start) in a connected, weighted graph G, • find a shortest (minimum total weight) path from startto each other vertex • Assumption: All weights are positive

  15. Dijkstra’s algorithm approach • The shortest path from vertex v to vertex w is either • empty, if v = w, or • obtained by adding an edge (u, w) to the end of a shortest path from v to u.

  16. Dijkstra’s algorithm start-up • Begin with a subgraph that consists of only the starting vertex. • Among all edges (start, v), • choose the one with the smallest weight, • add it (along with the edge that connects it) to our subgraph. • Clearly (start, v) is the shortest path from start to v.

  17. Dijkstra’s algorithm continues • Loop: Among all vertices not in a path yet, and which are also adjacent to a vertex that is in the path, • choose one with minimal path length from start, • and add it to the path, • along with the edge that connects it to a path. • Which previously- studied algorithm that we have seen does this closely resemble? Example: g = AdjancencyListGraph( [[1, [(2, 4), (3, 2), (5, 3)]], [2, [(1, 4), (4, 5)]], [3, [(1, 2), (5, 6), (4, 1), (6,3)]], [4, [(2, 5), (6,6), (3,1)]], [5, [(1, 3), (3,6), (6,2)]], [6, [(5, 2), (3, 3), (4, 6)]] ])

  18. Dijkstra’s algorithm data structures • A parent array as in Prim’s algorithm. • A minheap that stores “unpathed” nodes as keys and minimum path lengths (as extensions of a minimum path that has already been discovered) as weights. • Use an indirect binary heap, just as we did for Prim. • A cost array that stores the minimum path length to each vertex • Not strictly necessary; could reconstruct form adjacency list and parent array.

  19. Dijkstra’s algorithm details

More Related