CSE 373: Data Structures and Algorithms. Disjoint Sets. Autumn Shrirang (Shri) Mare
|
|
- Mark Paul
- 5 years ago
- Views:
Transcription
1 CSE 373: Data Structures and Algorithms Disjoint Sets Autumn 2018 Shrirang (Shri) Mare Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials... 1
2 Announcements Final exam info (logistics, topics covered, and practice material) is on course website. Homework 6 is due this Friday at noon. Homework 7 will be posted this Friday evening. - Fill out the team sign up form by tomorrow 5pm to get the repo in time. - Fill out the partner pool form by tomorrow 5pm to get assigned to a partner. CSE 373 AU 18 2
3 Today Trees and Forests Kruskal s algorithm Disjoint Set ADT CSE 373 AU 18 3
4 Trees and Forests A tree is an undirected, connected, and acyclic graph. A graph, however, can have unconnected vertices, or multiple trees. Collection of trees is a called a forest. A forest is any undirected and acyclic graph. - By definition a tree is a forest CSE 373 AU 18 4
5 Worksheet question 1 Review CSE 373 AU 18 5
6 Worksheet question 2a Review Step Components Edge Include? 6 A 3 1 B E 1 C D 5 F CSE 373 AU 18 6
7 <latexit sha1_base64="d8mbhvzwryhpx9trnkdkkhjog7i=">aaadm3icbvlbthrbej1hveb6a300jhu3jjgy3ovfh4leec8jbhzi2a2p6a7z6ddtpenuadfj/ph/4puv+hxwzo6cgjvmuqzup+pujyvwpns7p+of1q3bd+4u3mvff/dw0eol5sch3pzouf9ybd1rgp60mtqpkmg6khxhnmg6te43a//hgtmvrnkp44kgoy6mspxawkat5xhvknbimqr1ydovslyjyxfv2b5slubumdunv/pt1jnq22grwfakpdglgaiuuughvt8jceugjbpogwqlcqgcshlhdzkwj/fwbuctgesipcrjocc1yti/zv3vlkg9sfq+groxxl7jianwsximu5nwjacrcaboanyqkk3jmcylmr/od6w8kmj1uk+qexzz258hlivsnlnnaenctq7zmno/i7w3cidthm52+s4iag/iycv8nsx1umvdrucm0pspnwgmu7yg1wnprzkzlndofyuukkgjzyk9fshocuthrbpuyw+rzvetwgglhnq6/hoodn3bzuafuffjpohihepmr/tq4/98x2vi3w4rzyoykbftoltudyd8rcy7ixh0mbuuplysidj0kalf2lwuos+t6+bcu8ldw1lpyf+8rjpnxnpexoapkrtoimqyav16yolygrsx7tzt3lto6e3lyh2tx/rx9c7guxnzi9gz6ew0gvwin9fg9chajfqrih/ev+lf8z/w89zm62pr8zr0iz7lpi2uskv/f4tukjq=</latexit> <latexit sha1_base64="d8mbhvzwryhpx9trnkdkkhjog7i=">aaadm3icbvlbthrbej1hveb6a300jhu3jjgy3ovfh4leec8jbhzi2a2p6a7z6ddtpenuadfj/ph/4puv+hxwzo6cgjvmuqzup+pujyvwpns7p+of1q3bd+4u3mvff/dw0eol5sch3pzouf9ybd1rgp60mtqpkmg6khxhnmg6te43a//hgtmvrnkp44kgoy6mspxawkat5xhvknbimqr1ydovslyjyxfv2b5slubumdunv/pt1jnq22grwfakpdglgaiuuughvt8jceugjbpogwqlcqgcshlhdzkwj/fwbuctgesipcrjocc1yti/zv3vlkg9sfq+groxxl7jianwsximu5nwjacrcaboanyqkk3jmcylmr/od6w8kmj1uk+qexzz258hlivsnlnnaenctq7zmno/i7w3cidthm52+s4iag/iycv8nsx1umvdrucm0pspnwgmu7yg1wnprzkzlndofyuukkgjzyk9fshocuthrbpuyw+rzvetwgglhnq6/hoodn3bzuafuffjpohihepmr/tq4/98x2vi3w4rzyoykbftoltudyd8rcy7ixh0mbuuplysidj0kalf2lwuos+t6+bcu8ldw1lpyf+8rjpnxnpexoapkrtoimqyav16yolygrsx7tzt3lto6e3lyh2tx/rx9c7guxnzi9gz6ew0gvwin9fg9chajfqrih/ev+lf8z/w89zm62pr8zr0iz7lpi2uskv/f4tukjq=</latexit> <latexit sha1_base64="d8mbhvzwryhpx9trnkdkkhjog7i=">aaadm3icbvlbthrbej1hveb6a300jhu3jjgy3ovfh4leec8jbhzi2a2p6a7z6ddtpenuadfj/ph/4puv+hxwzo6cgjvmuqzup+pujyvwpns7p+of1q3bd+4u3mvff/dw0eol5sch3pzouf9ybd1rgp60mtqpkmg6khxhnmg6te43a//hgtmvrnkp44kgoy6mspxawkat5xhvknbimqr1ydovslyjyxfv2b5slubumdunv/pt1jnq22grwfakpdglgaiuuughvt8jceugjbpogwqlcqgcshlhdzkwj/fwbuctgesipcrjocc1yti/zv3vlkg9sfq+groxxl7jianwsximu5nwjacrcaboanyqkk3jmcylmr/od6w8kmj1uk+qexzz258hlivsnlnnaenctq7zmno/i7w3cidthm52+s4iag/iycv8nsx1umvdrucm0pspnwgmu7yg1wnprzkzlndofyuukkgjzyk9fshocuthrbpuyw+rzvetwgglhnq6/hoodn3bzuafuffjpohihepmr/tq4/98x2vi3w4rzyoykbftoltudyd8rcy7ixh0mbuuplysidj0kalf2lwuos+t6+bcu8ldw1lpyf+8rjpnxnpexoapkrtoimqyav16yolygrsx7tzt3lto6e3lyh2tx/rx9c7guxnzi9gz6ew0gvwin9fg9chajfqrih/ev+lf8z/w89zm62pr8zr0iz7lpi2uskv/f4tukjq=</latexit> Worksheet question 2b Review 1: function Kruskal(Graph G) 2: initialize each vertex to be a component 3: sort all edges by weight 4: for each edge (u, v) in sorted order do 5: if u and v are in di erent components then 6: add edge (u,v) to the MST 7: update u and v to be in the same component 8: end if 9: end for 10: end function CSE 373 AU 18 7
8 <latexit sha1_base64="d8mbhvzwryhpx9trnkdkkhjog7i=">aaadm3icbvlbthrbej1hveb6a300jhu3jjgy3ovfh4leec8jbhzi2a2p6a7z6ddtpenuadfj/ph/4puv+hxwzo6cgjvmuqzup+pujyvwpns7p+of1q3bd+4u3mvff/dw0eol5sch3pzouf9ybd1rgp60mtqpkmg6khxhnmg6te43a//hgtmvrnkp44kgoy6mspxawkat5xhvknbimqr1ydovslyjyxfv2b5slubumdunv/pt1jnq22grwfakpdglgaiuuughvt8jceugjbpogwqlcqgcshlhdzkwj/fwbuctgesipcrjocc1yti/zv3vlkg9sfq+groxxl7jianwsximu5nwjacrcaboanyqkk3jmcylmr/od6w8kmj1uk+qexzz258hlivsnlnnaenctq7zmno/i7w3cidthm52+s4iag/iycv8nsx1umvdrucm0pspnwgmu7yg1wnprzkzlndofyuukkgjzyk9fshocuthrbpuyw+rzvetwgglhnq6/hoodn3bzuafuffjpohihepmr/tq4/98x2vi3w4rzyoykbftoltudyd8rcy7ixh0mbuuplysidj0kalf2lwuos+t6+bcu8ldw1lpyf+8rjpnxnpexoapkrtoimqyav16yolygrsx7tzt3lto6e3lyh2tx/rx9c7guxnzi9gz6ew0gvwin9fg9chajfqrih/ev+lf8z/w89zm62pr8zr0iz7lpi2uskv/f4tukjq=</latexit> <latexit sha1_base64="d8mbhvzwryhpx9trnkdkkhjog7i=">aaadm3icbvlbthrbej1hveb6a300jhu3jjgy3ovfh4leec8jbhzi2a2p6a7z6ddtpenuadfj/ph/4puv+hxwzo6cgjvmuqzup+pujyvwpns7p+of1q3bd+4u3mvff/dw0eol5sch3pzouf9ybd1rgp60mtqpkmg6khxhnmg6te43a//hgtmvrnkp44kgoy6mspxawkat5xhvknbimqr1ydovslyjyxfv2b5slubumdunv/pt1jnq22grwfakpdglgaiuuughvt8jceugjbpogwqlcqgcshlhdzkwj/fwbuctgesipcrjocc1yti/zv3vlkg9sfq+groxxl7jianwsximu5nwjacrcaboanyqkk3jmcylmr/od6w8kmj1uk+qexzz258hlivsnlnnaenctq7zmno/i7w3cidthm52+s4iag/iycv8nsx1umvdrucm0pspnwgmu7yg1wnprzkzlndofyuukkgjzyk9fshocuthrbpuyw+rzvetwgglhnq6/hoodn3bzuafuffjpohihepmr/tq4/98x2vi3w4rzyoykbftoltudyd8rcy7ixh0mbuuplysidj0kalf2lwuos+t6+bcu8ldw1lpyf+8rjpnxnpexoapkrtoimqyav16yolygrsx7tzt3lto6e3lyh2tx/rx9c7guxnzi9gz6ew0gvwin9fg9chajfqrih/ev+lf8z/w89zm62pr8zr0iz7lpi2uskv/f4tukjq=</latexit> <latexit sha1_base64="d8mbhvzwryhpx9trnkdkkhjog7i=">aaadm3icbvlbthrbej1hveb6a300jhu3jjgy3ovfh4leec8jbhzi2a2p6a7z6ddtpenuadfj/ph/4puv+hxwzo6cgjvmuqzup+pujyvwpns7p+of1q3bd+4u3mvff/dw0eol5sch3pzouf9ybd1rgp60mtqpkmg6khxhnmg6te43a//hgtmvrnkp44kgoy6mspxawkat5xhvknbimqr1ydovslyjyxfv2b5slubumdunv/pt1jnq22grwfakpdglgaiuuughvt8jceugjbpogwqlcqgcshlhdzkwj/fwbuctgesipcrjocc1yti/zv3vlkg9sfq+groxxl7jianwsximu5nwjacrcaboanyqkk3jmcylmr/od6w8kmj1uk+qexzz258hlivsnlnnaenctq7zmno/i7w3cidthm52+s4iag/iycv8nsx1umvdrucm0pspnwgmu7yg1wnprzkzlndofyuukkgjzyk9fshocuthrbpuyw+rzvetwgglhnq6/hoodn3bzuafuffjpohihepmr/tq4/98x2vi3w4rzyoykbftoltudyd8rcy7ixh0mbuuplysidj0kalf2lwuos+t6+bcu8ldw1lpyf+8rjpnxnpexoapkrtoimqyav16yolygrsx7tzt3lto6e3lyh2tx/rx9c7guxnzi9gz6ew0gvwin9fg9chajfqrih/ev+lf8z/w89zm62pr8zr0iz7lpi2uskv/f4tukjq=</latexit> Kruskal s Algorithm 1: function Kruskal(Graph G) 2: initialize each vertex to be a component 3: sort all edges by weight 4: for each edge (u, v) in sorted order do 5: if u and v are in di erent components then 6: add edge (u,v) to the MST 7: update u and v to be in the same component 8: end if 9: end for 10: end function CSE 373 AU 18 8
9 Minimum spanning forest Given a forest, find a set of minimum spanning trees for each tree in the given forest. Question: Which algorithm (Prims or Kruskals) would you use to find a minimum spanning forest? CSE 373 AU 18 9
10 Disjoint Set ADT CSE 373 AU 18 10
11 New ADT state Count of Elements behavior Set ADT Set of elements - Elements must be unique! - No required order create(x) - creates a new set with a single member, x add(x) - adds x into set if it is unique, otherwise add is ignored remove(x) removes x from set size() returns current number of elements in set state Disjoint-Set ADT Set of Sets - Disjoint: Elements must be unique across sets - No required order - Each set has representative Count of Sets behavior makeset(x) creates a new set within the disjoint set where the only member is x. Picks representative for set findset(x) looks up the set containing element x, returns representative of that set union(x, y) looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set D B C A D C F B A G H CSE 373 AU 18 11
12 Example new() makeset(a) makeset(b) makeset(c) makeset(d) makeset(e) findset(a) findset(d) union(a, c) Rep: 0 a Rep: 4 e Rep: 2 c Rep: 1 b Rep: 3 d CSE 373 AU 18 12
13 Example new() makeset(a) makeset(b) makeset(c) makeset(d) makeset(e) findset(a) findset(d) union(a, c) union(b, d) Rep: 0 a c Rep: 4 e Rep: 1 b Rep: 3 d CSE 373 AU 18 13
14 Example new() makeset(a) makeset(b) makeset(c) makeset(d) makeset(e) findset(a) findset(d) union(a, c) union(b, d) findset(a) == findset(c) Rep: 0 a c Rep: 4 e Rep: 1 b d findset(a) == findset(d) CSE 373 AU 18 14
15 Implementation Disjoint-Set ADT state Set of Sets - Disjoint: Elements must be unique across sets - No required order - Each set has representative Count of Sets behavior makeset(x) creates a new set within the disjoint set where the only member is x. Picks representative for set findset(x) looks up the set containing element x, returns representative of that set union(x, y) looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set state behavior TreeDisjointSet<E> Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x state behavior TreeSet(x) TreeSet<E> SetNode overallroot add(x) remove(x, y) getrep()-returns data of overallroot state E data SetNode(x) SetNode<E> Collection<SetNode> children behavior addchild(x) removechild(x, y) CSE 373 AU 18 15
16 Implement makeset(x) makeset(0) makeset(1) makeset(2) makeset(3) forest state behavior TreeDisjointSet<E> Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x makeset(4) makeset(5) Worst case runtime? O(1) CSE 373 AU 18 16
17 Implement union(x, y) forest union(3, 5) state TreeDisjointSet<E> Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory behavior makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x > -> -> -> -> -> CSE 373 AU 18 17
18 Implement union(x, y) union(3, 5) union(2, 1) forest state TreeDisjointSet<E> Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory behavior makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x > -> -> -> -> -> CSE 373 AU 18 18
19 Implement union(x, y) union(3, 5) union(2, 1) union(2, 5) forest state TreeDisjointSet<E> Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory behavior makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x > -> -> -> -> -> CSE 373 AU 18 19
20 Implement union(x, y) union(3, 5) union(2, 1) union(2, 5) forest state behavior TreeDisjointSet<E> Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x CSE 373 AU 18 20
21 TreeDisjointSet<E> Implement findset(x) findset(0) findset(3) findset(5) forest state Collection<TreeSet> forest Dictionary<NodeValues, NodeLocations> nodeinventory behavior makeset(x)-create a new tree of size 1 and add to our forest findset(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Worst case runtime? O(n) Worst case runtime of union? O(n) CSE 373 AU 18 21
22 Improving union Problem: Trees can be unbalanced Solution: Union-by-rank! - let rank(x) be a number representing the upper bound of the height of x so rank(x) >= height(x) - Keep track of rank of all trees - When unioning make the tree with larger rank the root - If it s a tie, pick one randomly and increase rank by one rank = 0 rank = 2 rank = 0 rank = 0 rank = CSE 373 AU 18 22
23 Worksheet question 3 Given the following disjoint-set what would be the result of the following calls on union if we add the union-by-rank optimization. Draw the forest at each stage with corresponding ranks for each tree. rank = 2 rank = 0 rank = 2 rank = union(2, 13) union(4, 12) union(2, 8) CSE 373 AU 18 23
24 Worksheet question 3 Given the following disjoint-set what would be the result of the following calls on union if we add the union-by-rank optimization. Draw the forest at each stage with corresponding ranks for each tree. rank = union(2, 13) union(4, 12) union(2, 8) Does this improve the worst case runtimes? 1 5 findset is more likely to be O(log(n)) than O(n) CSE 373 AU 18 24
25 Improving findset() Problem: Every time we call findset() you must traverse all the levels of the tree to find representative Solution: Path Compression - Collapse tree into fewer levels by updating parent pointer of each node you visit - Whenever you call findset() update each node you touch s parent pointer to point directly to overallroot rank = 3 rank = 2 findset(5) 8 8 findset(4) Does this improve the worst case runtimes? findset is more likely to be O(1) than O(log(n)) CSE 373 AU 18 25
Disjoint Sets. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION
Disjoint Sets Data Structures and Algorithms CSE 373 SP 8 - KASEY CHAMPION Warm Up Finding a MST using Kruskal s algorithm b 8 c 7 d 4 2 9 a 7 i 6 4 4 e 0 8 h g 2 f CSE 373 SP 8 - KASEY CHAMPION 2 Warm
More informationCSE 373: Data Structures and Algorithms. Binary Search Trees. Autumn Shrirang (Shri) Mare
CSE 373: Data Structures and Algorithms Binary Search Trees Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber,
More informationMinimum Spanning Tree
CSE 373: Data Structures and Algorithms Minimum Spanning Tree Autumn 018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber,
More informationHash Tables: Handling Collisions
CSE 373: Data Structures and Algorithms Hash Tables: Handling Collisions Autumn 208 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty,
More informationFloyd s buildheap, Sorting
CSE 7: Data Structures and Algorithms Floyd s buildheap, Sorting Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie
More informationCSE 373: Data Structures and Algorithms. Hash Tables. Autumn Shrirang (Shri) Mare
CSE 373: Data Structures and Algorithms Hash Tables Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker
More informationCSE 373: Data Structures and Algorithms. Asymptotic Analysis. Autumn Shrirang (Shri) Mare
CSE 373: Data Structures and Algorithms Asymptotic Analysis Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber,
More informationBinary Heaps. Autumn Shrirang (Shri) Mare CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Binary Heaps Autumn 018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker
More informationTesting and Debugging
CSE 373: Data Structures and Algorithms Testing and Debugging Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Whitaker
More informationCSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare
CSE 373: Data Structures and Algorithms Memory and Locality Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber,
More informationCSE 373: Data Structures and Algorithms. Final Review. Autumn Shrirang (Shri) Mare
CSE 373: Data Structures and Algorithms Final Review Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker
More informationCSC263 Week 11. Larry Zhang.
CSC263 Week 11 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements A2 due next Tuesday Course evaluation: http://uoft.me/course-evals ADT: Disjoint Sets What does it store? What operations are supported?
More informationCSE 373: Data Structures and Algorithms. Graph Traversals. Autumn Shrirang (Shri) Mare
SE 373: ata Structures and lgorithms Graph Traversals utumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey hampion, en Jones, dam lank, Michael Lee, Evan Mcarty, Robbie Weber, Whitaker
More informationCOP 4531 Complexity & Analysis of Data Structures & Algorithms
COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 8 Data Structures for Disjoint Sets Thanks to the text authors who contributed to these slides Data Structures for Disjoint Sets Also
More informationMinimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7
Minimum Spanning Trees and Union Find CSE 101: Design and Analysis of Algorithms Lecture 7 CSE 101: Design and analysis of algorithms Minimum spanning trees and union find Reading: Section 5.1 Quiz 1 is
More informationCSE 373: Data Structures and Algorithms. Graphs. Autumn Shrirang (Shri) Mare
SE 373: Data Structures and lgorithms Graphs utumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey hampion, en Jones, dam lank, Michael Lee, Evan Mcarty, Robbie Weber, Whitaker rand, Zora
More informationLecture 1: CSE 373. Data Structures and Algorithms
Lecture 1: CSE 373 Data Structures and Algorithms Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Whitaker Brand, Stuart Reges, Zora Fung, Justin Hsia, and many others for sample
More informationAnnouncements Problem Set 5 is out (today)!
CSC263 Week 10 Announcements Problem Set is out (today)! Due Tuesday (Dec 1) Minimum Spanning Trees The Graph of interest today A connected undirected weighted graph G = (V, E) with weights w(e) for each
More informationCS 161: Design and Analysis of Algorithms
CS 161: Design and Analysis of Algorithms Greedy Algorithms 2: Minimum Spanning Trees Definition The cut property Prim s Algorithm Kruskal s Algorithm Disjoint Sets Tree A tree is a connected graph with
More informationBuilding a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)
Chapter 5. Greedy algorithms Minimum spanning trees Building a network Properties of the optimal solutions Suppose you are asked to network a collection of computers by linking selected pairs of them.
More informationChapter 5. Greedy algorithms
Chapter 5. Greedy algorithms Minimum spanning trees Building a network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem
More informationDisjoint Sets. Andreas Klappenecker [using notes by R. Sekar]
Disjoint Sets Andreas Klappenecker [using notes by R. Sekar] Equivalence Classes Frequently, we declare an equivalence relation on a set S. So the elements of the set S are partitioned into equivalence
More informationUnion Find and Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 8
Union Find and Greedy Algorithms CSE 101: Design and Analysis of Algorithms Lecture 8 CSE 101: Design and analysis of algorithms Union find Reading: Section 5.1 Greedy algorithms Reading: Kleinberg and
More informationCSE 100 Disjoint Set, Union Find
CSE 100 Disjoint Set, Union Find Union-find using up-trees The union-find data structure 1 0 2 6 7 4 3 5 8 Perform these operations: Find(4) =! Find(3) =! Union(1,0)=! Find(4)=! 2 Array representation
More informationDisjoint Sets. Based on slides from previous iterations of this course.
Disjoint Sets Based on slides from previous iterations of this course Today s Topics Exam Discussion Introduction to Disjointed Sets Disjointed Set Example Operations of a Disjointed Set Types of Disjointed
More informationCSE 373 MAY 10 TH SPANNING TREES AND UNION FIND
CSE 373 MAY 0 TH SPANNING TREES AND UNION FIND COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend HW5 out tomorrow
More informationLecture Summary CSC 263H. August 5, 2016
Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.
More informationCOMP Analysis of Algorithms & Data Structures
COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Disjoin Sets and Union-Find Structures CLRS 21.121.4 University of Manitoba 1 / 32 Disjoint Sets Disjoint set is an abstract data type
More informationGraphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1
Graphs Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1 Warmup Discuss with your neighbors: Come up with as many kinds of relational data as you can (data that can be represented with a graph).
More informationCSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015
CSE33: Data Abstractions Lecture 5: Minimum Spanning Trees Ruth Anderson via Conrad Nied Winter 05 A quick note about Gradescope 3/06/05 Today s XKCD 3/06/05 3 You guys are awesome 3/06/05 4 Do you still
More informationComplexity of Prim s Algorithm
The main loop is: Complexity of Prim s Algorithm while ( not ISEMPTY(Q) ): u = EXTRACT-MIN(Q) if p[u]!= NIL: A = A U {(p[u],u)} for v in adjacency-list[u]: if v in Q and w(u,v) < priority[v] : DECREASE-PRIORITY(v,
More informationGreedy Approach: Intro
Greedy Approach: Intro Applies to optimization problems only Problem solving consists of a series of actions/steps Each action must be 1. Feasible 2. Locally optimal 3. Irrevocable Motivation: If always
More informationCS 6783 (Applied Algorithms) Lecture 5
CS 6783 (Applied Algorithms) Lecture 5 Antonina Kolokolova January 19, 2012 1 Minimum Spanning Trees An undirected graph G is a pair (V, E); V is a set (of vertices or nodes); E is a set of (undirected)
More informationCMSC 341 Lecture 20 Disjointed Sets
CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements
More informationDisjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50
Disjoint Sets (Disjoint Sets) Data Structures and Programming Spring 2017 1 / 50 Making a Good Maze What s a Good Maze? Connected Just one path between any two rooms Random The Maze Construction Problem
More informationCSE 100: GRAPH ALGORITHMS
CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue
More informationCMSC 341 Lecture 20 Disjointed Sets
CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements
More informationUnion/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445
CS 5 Union/Find Aka: Disjoint-set forest Alon Efrat Problem definition Given: A set of atoms S={1, n E.g. each represents a commercial name of a drugs. This set consists of different disjoint subsets.
More informationSolutions to Exam Data structures (X and NV)
Solutions to Exam Data structures X and NV 2005102. 1. a Insert the keys 9, 6, 2,, 97, 1 into a binary search tree BST. Draw the final tree. See Figure 1. b Add NIL nodes to the tree of 1a and color it
More informationPartha Sarathi Manal
MA 515: Introduction to Algorithms & MA5 : Design and Analysis of Algorithms [-0-0-6] Lecture 20 & 21 http://www.iitg.ernet.in/psm/indexing_ma5/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.
More informationCSE 100 Minimum Spanning Trees Prim s and Kruskal
CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:
More informationCMSC 341 Lecture 20 Disjointed Sets
CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements
More informationAlgorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1
Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1 Minimum Spanning Tree Problem Given: Undirected connected weighted graph (G, W ) Output: An MST of G We have already
More informationMinimum Trees. The problem. connected graph G = (V,E) each edge uv has a positive weight w(uv)
/16/015 Minimum Trees Setting The problem connected graph G = (V,E) each edge uv has a positive weight w(uv) Find a spanning graph T of G having minimum total weight T is a tree w T = w uv uv E(T) is minimal
More informationMinimum Spanning Trees My T. UF
Introduction to Algorithms Minimum Spanning Trees @ UF Problem Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication
More informationDisjoint set (Union-Find)
CS124 Lecture 6 Spring 2011 Disjoint set (Union-Find) For Kruskal s algorithm for the minimum spanning tree problem, we found that we needed a data structure for maintaining a collection of disjoint sets.
More informationChapter 23. Minimum Spanning Trees
Chapter 23. Minimum Spanning Trees We are given a connected, weighted, undirected graph G = (V,E;w), where each edge (u,v) E has a non-negative weight (often called length) w(u,v). The Minimum Spanning
More informationRepresentations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs
Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects
More informationThe minimum spanning tree problem
The minimum spanning tree problem MST is a minimum cost connection problem on graphs The graph can model the connection in a (hydraulic, electric, telecommunication) network: the nodes are the points that
More informationAlgorithms (IX) Yijia Chen Shanghai Jiaotong University
Algorithms (IX) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Shortest paths in the presence of negative edges Negative edges Dijkstra s algorithm works in part because the shortest
More informationCS6140: Advanced Programming Lab Assignment 3
CS6140: Advanced Programming Lab Assignment 3 Lead TAs: Gaurav Singh and Dhanasekaran. Posted: September 19, 2013 Due: September 26, 2013 In this lab assignment, you are required to implement Kruskal s
More informationLecture 4 Greedy algorithms
dvanced lgorithms loriano Zini ree University of ozen-olzano aculty of omputer Science cademic Year 203-20 Lecture Greedy algorithms hess vs Scrabble In chess the player must think ahead winning strategy
More informationOutline. Disjoint set data structure Applications Implementation
Disjoint Data Sets Outline Disjoint set data structure Applications Implementation Data Structures for Disjoint Sets A disjoint-set data structure is a collection of sets S = {S 1 S k }, such that S i
More informationMinimum Spanning Trees
Minimum Spanning Trees 1 Minimum- Spanning Trees 1. Concrete example: computer connection. Definition of a Minimum- Spanning Tree Concrete example Imagine: You wish to connect all the computers in an office
More informationLecture 10. Elementary Graph Algorithm Minimum Spanning Trees
Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo
More information6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro
6,8:15 MA/CSSE 473 Day 37 Student Questions Kruskal data structures Disjoint Set ADT Complexity intro Data Structures for Kruskal A sorted list of edges (edge list, not adjacency list) Edge e has fields
More information6.1 Minimum Spanning Trees
CS124 Lecture 6 Fall 2018 6.1 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties
More informationCSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015
CSE: Data Structures & Algorithms Lecture : Implementing Union-Find Lauren Milne Spring 05 Announcements Homework due in ONE week Wednesday April 9 th! TA sessions Catie will be back on Monday. The plan
More informationDisjoint Sets. Maintaining Disjoint Sets Complexity Analysis
Disjoint Sets Maintaining Disjoint Sets Complexity Analysis Disjoint Sets - Definition Set representatives called canonical elements. Elements are integers beween 1 and n. Each element can be accessed
More informationAlgorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University
Algorithms (VI) Greedy Algorithms Guoqiang Li School of Software, Shanghai Jiao Tong University Review of the Previous Lecture Lengths on Edges BFS treats all edges as having the same length. It is rarely
More informationMinimum Spanning Trees
CSMPS 2200 Fall Minimum Spanning Trees Carola Wenk Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk 11/6/ CMPS 2200 Intro. to Algorithms 1 Minimum spanning trees Input: A
More informationCSE 548: (Design and) Analysis of Algorithms
Intro Aggregate Charging Potential Table resizing Disjoint sets 1 / 33 CSE 548: (Design and) Analysis of Algorithms Amortized Analysis R. Sekar Intro Aggregate Charging Potential Table resizing Disjoint
More informationMinimum Spanning Trees
CS124 Lecture 5 Spring 2011 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties
More informationMinimum spanning trees
Minimum spanning trees [We re following the book very closely.] One of the most famous greedy algorithms (actually rather family of greedy algorithms). Given undirected graph G = (V, E), connected Weight
More informationLecture 10: Introduction to Hash Tables
Lecture 10: Introduction to Hash Tables CSE 373: Data Structures and Algorithms CSE 373 19 WI - KASEY CHAMPION 1 Warm Up CSE 373 SP 18 - KASEY CHAMPION 2 Administrivia HW 2 part 1 grades went out HW 2
More informationCSE 5095 Notes. Author : Michael Tedford. Class Date : 18th February 2014
CSE 5095 Notes Author : Michael Tedford Class Date : 18th February 2014 Union-Find: Consider the n singleton sets {1}, {2},..., {n}. Consider a sequence of Union and Find operations performed on these
More informationDisjoint-set data structure: Union-Find. Lecture 20
Disjoint-set data structure: Union-Find Lecture 20 Disjoint-set data structure (Union-Find) Problem: Maintain a dynamic collection of pairwise-disjoint sets S = {S 1, S 2,, S r }. Each set S i has one
More informationlecture24: Disjoint Sets
lecture24: Largely based on slides by Cinda Heeren CS 225 UIUC 22nd July, 2013 Announcements mp6 due tonight mp7 out soon! mt3 tomorrow night (7/23) Optional review instead of lab tomorrow Code Challenge
More informationCSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader
CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, 2015 Problem Topic Points Possible Points Earned Grader 1 The Basics 40 2 Application and Comparison 20 3 Run Time Analysis 20 4 C++ and Programming
More informationCSE 202: Design and Analysis of Algorithms Lecture 3
CSE 202: Design and Analysis of Algorithms Lecture 3 Instructor: Kamalika Chaudhuri Announcement Homework 1 out Due on Mon April 11 in class No late homeworks will be accepted Greedy Algorithms Direct
More informationWeek 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.
Week 9 Student Responsibilities Reading: hapter 3.3 3. (Tucker),..5 (Rosen) Mat 3770 Spring 01 Homework Due date Tucker Rosen 3/1 3..3 3/1 DS & S Worksheets 3/6 3.3.,.5 3/8 Heapify worksheet ttendance
More informationmanaging an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm
Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing
More informationPriority Queues and Heaps
Priority Queues and Heaps Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1 Warm Up We have seen several data structures that can implement the Dictionary ADT so far: Arrays, Binary (AVL) Trees,
More informationContext: Weighted, connected, undirected graph, G = (V, E), with w : E R.
Chapter 23: Minimal Spanning Trees. Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Definition: A selection of edges from T E such that (V, T ) is a tree is called a spanning
More information1. AVL Trees (10 Points)
CSE 373 Spring 2012 Final Exam Solution 1. AVL Trees (10 Points) Given the following AVL Tree: (a) Draw the resulting BST after 5 is removed, but before any rebalancing takes place. Label each node in
More informationMinimum Spanning Trees. CSE 373 Data Structures
Minimum Spanning Trees CSE 373 Data Structures Reading Chapter 3 Section 3.7 MSTs 2 Spanning Tree Given (connected) G(V,E) a spanning tree T(V,E ): Spans the graph (V = V) Forms a tree (no cycle); E has
More information2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016
CS125 Lecture 2 Fall 2016 2.1 Greedy Algorithms We will start talking about methods high-level plans for constructing algorithms. One of the simplest is just to have your algorithm be greedy. Being greedy,
More informationSorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min
Binary Search Trees FRIDAY ALGORITHMS Sorted Arrays Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min 6 10 11 17 2 0 6 Running Time O(1) O(lg n) O(1) O(1)
More information15 211: RECITATION7, SECTION M. Hui Han Chin,
15 211: RECITATION7, SECTION M Hui Han Chin, hchin@cmu.edu NOTICE! Homework theory is due tomorrow Lab 3 review Make sure your code compiles. Will provide a dummy test harness. You should try compressing
More informationLecture 6 Basic Graph Algorithms
CS 491 CAP Intro to Competitive Algorithmic Programming Lecture 6 Basic Graph Algorithms Uttam Thakore University of Illinois at Urbana-Champaign September 30, 2015 Updates ICPC Regionals teams will be
More informationCSE Table Doubling 11.1
Table Doubling 11.1 Dynamic Arrays Allocate an array of size 10. What if you try to insert 11 elements in the array? Need to reallocate the array. Reallocate to size 11. Insert element. Reallocate to size
More informationAmortized Analysis and Union-Find , Inge Li Gørtz
Amortized Analysis and Union-Find 02283, Inge Li Gørtz 1 Today Amortized analysis 3 different methods 2 examples Union-Find data structures Worst-case complexity Amortized complexity 2 Amortized Analysis
More informationReview of course COMP-251B winter 2010
Review of course COMP-251B winter 2010 Lecture 1. Book Section 15.2 : Chained matrix product Matrix product is associative Computing all possible ways of parenthesizing Recursive solution Worst-case running-time
More informationCSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms
CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms Professor Henry Carter Fall 2016 Recap Greedy algorithms iterate locally optimal choices to construct a globally optimal solution
More informationCSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees
CSE 33 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 0 August 3, 0 CSE 33 Data Abstractions, Summer 0 Making Connections You have a set of nodes (numbered -9)
More informationLecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29
Lecture 13 Connectedness in graphs Spanning trees in graphs Finding a minimal spanning tree Time costs of graph problems and NP-completeness Finding a minimal spanning tree: Prim s and Kruskal s algorithms
More informationN objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label
CS 0 Theory of Algorithms Supplement Disjoint Set Structures Disjoint Set Structures N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label of the set Example:
More informationCSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)
Name: Sample Solution Email address: CSE 373 Autumn 0: Midterm # (closed book, closed notes, NO calculators allowed) Instructions: Read the directions for each question carefully before answering. We may
More informationCSE 326: Data Structures B-Trees and B+ Trees
Announcements (2/4/09) CSE 26: Data Structures B-Trees and B+ Trees Midterm on Friday Special office hour: 4:00-5:00 Thursday in Jaech Gallery (6 th floor of CSE building) This is in addition to my usual
More informationCSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013
CSE373: Data Structures & Algorithms Lecture 7: Minimum Spanning Trees Dan Grossman Fall 03 Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such
More informationCS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee
CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee 2 Today s Topics: 1. Binary tree 2. Heap Priority Queue Emergency Department waiting room operates as a priority queue: patients are sorted
More informationCourse Review for Finals. Cpt S 223 Fall 2008
Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,
More informationLecture 1: Welcome! CSE 373: Data Structures and Algorithms CSE WI - KASEY CHAMPION 1
Lecture 1: Welcome! CSE 373: Data Structures and Algorithms 1 Agenda -Introductions -Syllabus -Dust off data structure cob webs -Meet the ADT -What is complexity? 2 Waitlist/ Overloads -There are no overloads
More informationCSE 21 Mathematics for Algorithm and System Analysis
CSE 21 Mathematics for Algorithm and System Analysis Unit 4: Basic Concepts in Graph Theory Section 3: Trees 1 Review : Decision Tree (DT-Section 1) Root of the decision tree on the left: 1 Leaves of the
More informationHomework Assignment #3 Graph
CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution
More informationFinal Examination CSE 100 UCSD (Practice)
Final Examination UCSD (Practice) RULES: 1. Don t start the exam until the instructor says to. 2. This is a closed-book, closed-notes, no-calculator exam. Don t refer to any materials other than the exam
More informationData Structures for Disjoint Sets
Data Structures for Disjoint Sets Manolis Koubarakis 1 Dynamic Sets Sets are fundamental for mathematics but also for computer science. In computer science, we usually study dynamic sets i.e., sets that
More informationLecture 18: Implementing Graphs
Lecture 18: Implementing Graphs CS 373: Data Structures and Algorithms CS 373 19 WI - KASY CHAMPION 1 Administrivia HW 5 Part due Friday, last day to turn in Monday Optional: HW 3 regrade to be turned
More informationlooking ahead to see the optimum
! Make choice based on immediate rewards rather than looking ahead to see the optimum! In many cases this is effective as the look ahead variation can require exponential time as the number of possible
More information