Chapter 5 Data Structures -...
Transcript of Chapter 5 Data Structures -...
![Page 1: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/1.jpg)
Chapter 5
Data Structures
Algorithm TheoryWS 2018/19
Fabian Kuhn
![Page 2: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/2.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 2
Kruskal Algorithm
3
144
6
1
10
13
23
21
31
825
20
1118
17
16
199
12
7 228
1. Start with anempty edge set
2. In each step:Add minimumweight edge ๐such that ๐ doesnot close a cycle
![Page 3: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/3.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 3
Implementation of Kruskal Algorithm
1. Go through edges in order of increasing weights
2. For each edge ๐:
if ๐ does not close a cycle then
add ๐ to the current solution
![Page 4: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/4.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 4
Union-Find Data Structure
Also known as Disjoint-Set Data Structureโฆ
Manages partition of a set of elements
โข set of disjoint sets
Operations:
โข ๐ฆ๐๐ค๐_๐ฌ๐๐ญ(๐): create a new set that only contains element ๐ฅ
โข ๐๐ข๐ง๐(๐): return the set containing ๐ฅ
โข ๐ฎ๐ง๐ข๐จ๐ง(๐, ๐): merge the two sets containing ๐ฅ and ๐ฆ
![Page 5: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/5.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 5
Implementation of Kruskal Algorithm
1. Initialization:For each node ๐ฃ: make_set(๐ฃ)
2. Go through edges in order of increasing weights:Sort edges by edge weight
3. For each edge ๐ = {๐ข, ๐ฃ}:
if ๐๐ข๐ง๐ ๐ โ ๐๐ข๐ง๐(๐) then
add ๐ to the current solution
๐ฎ๐ง๐ข๐จ๐ง(๐, ๐)
![Page 6: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/6.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 6
Managing Connected Components
โข Union-find data structure can be used more generally to manage the connected components of a graph
โฆ if edges are added incrementally
โข make_set(๐ฃ) for every node ๐ฃ
โข find(๐ฃ) returns component containing ๐ฃ
โข union(๐ข, ๐ฃ) merges the components of ๐ข and ๐ฃ(when an edge is added between the components)
โข Can also be used to manage biconnected components
![Page 7: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/7.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 7
Basic Implementation Properties
Representation of sets:
โข Every set ๐ of the partition is identified with a representative, by one of its members ๐ฅ โ ๐
Operations:
โข make_set(๐ฅ): ๐ฅ is the representative of the new set {๐ฅ}
โข find(๐ฅ): return representative of set ๐๐ฅ containing ๐ฅ
โข union(๐ฅ, ๐ฆ): unites the sets ๐๐ฅ and ๐๐ฆ containing ๐ฅ and ๐ฆ and
returns the new representative of ๐๐ฅ โช ๐๐ฆ
![Page 8: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/8.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 8
Observations
Throughout the discussion of union-find:
โข ๐: total number of make_set operations
โข ๐: total number of operations (make_set, find, and union)
Clearly:
โข ๐ โฅ ๐
โข There are at most ๐ โ 1 union operations
Remark:
โข We assume that the ๐ make_set operations are the first ๐operationsโ Does not really matterโฆ
![Page 9: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/9.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 9
Linked List Implementation
Each set is implemented as a linked list:
โข representative: first list element (all nodes point to first elem.)in addition: pointer to first and last element
โข sets: 1,5,8,12,43 , 7,9,15 ; representatives: 5, 9
5 12 8 43 1
9 15 7
![Page 10: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/10.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 10
Linked List Implementation
๐ฆ๐๐ค๐_๐ฌ๐๐ญ(๐):
โข Create list with one element:
time: ๐ถ ๐
๐๐ข๐ง๐(๐):
โข Return first list element:
time: ๐ถ(๐)
๐ฅ
๐ฆ ๐ ๐ฅ ๐
![Page 11: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/11.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 11
Linked List Implementation
๐ฎ๐ง๐ข๐จ๐ง(๐, ๐):
โข Append list of ๐ฆ to list of ๐ฅ:
Time: ๐ถ ๐ฅ๐๐ง๐ ๐ญ๐ก ๐จ๐ ๐ฅ๐ข๐ฌ๐ญ ๐จ๐ ๐
๐ ๐ ๐ฅ ๐ ๐ ๐ ๐ฆโช
๐ ๐ ๐ฅ ๐ ๐ ๐ ๐ฆ
![Page 12: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/12.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 12
Cost of Union (Linked List Implementation)
Total cost for ๐ โ 1 union operations can be ฮ(๐2):
โข make_set ๐ฅ1 , make_set ๐ฅ2 , โฆ ,make_set(๐ฅ๐),union ๐ฅ๐โ1, ๐ฅ๐ , union ๐ฅ๐โ2, ๐ฅ๐โ1 , โฆ , union ๐ฅ1, ๐ฅ2
![Page 13: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/13.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 13
Weighted-Union Heuristic
โข In a bad execution, average cost per union can be ฮ(๐)
โข Problem: The longer list is always appended to the shorter one
Idea:
โข In each union operation, append shorter list to longer one!
Cost for union of sets ๐๐ฅ and ๐๐ฆ: ๐ min ๐๐ฅ , ๐๐ฆ
Theorem: The overall cost of ๐ operations of which at most ๐ are make_set operations is ๐ถ(๐+ ๐ ๐ฅ๐จ๐ ๐).
![Page 14: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/14.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 14
Weighted-Union Heuristic
Theorem: The overall cost of ๐ operations of which at most ๐are make_set operations is ๐ถ(๐+ ๐ ๐ฅ๐จ๐ ๐).
Proof:
![Page 15: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/15.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 15
โข Represent each set by a tree
โข Representative of a set is the root of the tree
๐
Disjoint-Set Forests
โ ๐
๐
๐
๐
๐
๐
๐
๐ ๐ฅ
๐ฆ
๐ข
๐ฃ
๐
![Page 16: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/16.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 16
Disjoint-Set Forests
๐ฆ๐๐ค๐_๐ฌ๐๐ญ(๐ฑ): create new one-node tree
๐๐ข๐ง๐(๐): follow parent point to root(parent pointer to itself)
๐ฎ๐ง๐ข๐จ๐ง(๐, ๐): attach tree of ๐ฅ to tree of ๐ฆ
๐ฅ
๐
๐
๐ ๐
๐ฆ
๐ข
๐ฃ
๐
๐ฅ
๐
๐ฆ ๐
๐
โช ๐
๐ฅ
๐
๐ฆ ๐
๐
![Page 17: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/17.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 17
Bad Sequence
Bad sequence leads to tree(s) of depth ฮ(๐)
โข make_set ๐ฅ1 , make_set ๐ฅ2 , โฆ ,make_set(๐ฅ๐),union ๐ฅ1, ๐ฅ2 , union ๐ฅ1, ๐ฅ3 , โฆ , union ๐ฅ1, ๐ฅ๐
![Page 18: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/18.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 18
Union-By-Size Heuristic
Union of sets ๐บ๐ and ๐บ๐:
โข Root of trees representing ๐1 and ๐2: ๐1 and ๐2โข W.l.o.g., assume that ๐1 โฅ |๐2|
โข Root of ๐1 โช ๐2: ๐1 (๐2 is attached to ๐1 as a new child)
Theorem: If the union-by-size heuristic is used, the worst-case cost of a ๐๐ข๐ง๐-operation is ๐ถ(๐ฅ๐จ๐ ๐)
Proof:
Similar Strategy: union-by-rank
โข rank: essentially the depth of a tree
![Page 19: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/19.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 19
Union-Find Algorithms
Recall: ๐ operations, ๐ of the operations are make_set-operations
Linked List with Weighted Union Heuristic:
โข make_set: worst-case cost ๐ 1
โข find : worst-case cost ๐(1)
โข union : amortized worst-case cost ๐(log ๐)
Disjoint-Set Forest with Union-By-Size Heuristic:
โข make_set: worst-case cost ๐ 1
โข find : worst-case cost ๐(log ๐)
โข union : worst-case cost ๐(log ๐)
Can we make this faster?
![Page 20: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/20.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 20
Path Compression During Find Operation
๐๐ข๐ง๐(๐):
1. if ๐ โ ๐. ๐๐๐๐๐๐ก then
2. ๐. ๐๐๐๐๐๐ก โ find ๐. ๐๐๐๐๐๐ก
3. return ๐. ๐๐๐๐๐๐ก
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
๐
![Page 21: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/21.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 21
Complexity With Path Compression
When using only path compression (without union-by-rank):
๐: total number of operations
โข ๐ of which are find-operations
โข ๐ of which are make_set-operations at most ๐ โ 1 are union-operations
Total cost: ๐ ๐+ ๐ โ ๐ฅ๐จ๐ ๐+ เต๐ ๐
๐ = ๐ถ ๐+ ๐ โ ๐ฅ๐จ๐ ๐+ ฮค๐ ๐๐
![Page 22: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/22.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 22
Union-By-Size and Path Compression
Theorem:
Using the combined union-by-rank and path compression heuristic, the running time of ๐ disjoint-set (union-find) operations on ๐ elements (at most ๐ make_set-operations) is
๐ฏ ๐ โ ๐ถ ๐,๐ ,
Where ๐ผ ๐, ๐ is the inverse of the Ackermann function.
![Page 23: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/23.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 23
Ackermann Function and its Inverse
Ackermann Function:
For ๐, โ โฅ 1,
๐จ ๐, โ โ เต
๐โ, ๐ข๐ ๐ = ๐, โ โฅ ๐๐จ ๐ โ ๐, ๐ , ๐ข๐ ๐ > ๐, โ = ๐
๐จ ๐ โ ๐,๐จ ๐, โ โ ๐ , ๐ข๐ ๐ > ๐, โ > ๐
Inverse of Ackermann Function:
๐ถ ๐,๐ โ ๐ฆ๐ข๐ง ๐ โฅ ๐ | ๐จ ๐, ฮค๐ ๐ > ๐ฅ๐จ๐ ๐ ๐
![Page 24: Chapter 5 Data Structures - uni-freiburg.deac.informatik.uni-freiburg.de/.../05_DataStructures_p3.pdfData Structures Algorithm Theory WS 2018/19 Fabian Kuhn Algorithm Theory, WS 2018/19](https://reader034.fdocuments.us/reader034/viewer/2022050501/5f93ff550c67e37fbf1f3fc5/html5/thumbnails/24.jpg)
Algorithm Theory, WS 2018/19 Fabian Kuhn 24
Inverse of Ackermann Function
โข ๐ผ ๐, ๐ โ min ๐ โฅ 1 | ๐ด ๐, ฮค๐ ๐ > log2 ๐
๐ โฅ ๐ โน ๐ด ๐, ฮค๐ ๐ โฅ ๐ด ๐, 1 โน ๐ผ ๐, ๐ โค min ๐ โฅ 1|๐ด ๐, 1 > log ๐
โข ๐ด 1, โ = 2โ, ๐ด ๐, 1 = ๐ด(๐ โ 1,2),
๐ด ๐, โ = ๐ด ๐ โ 1, ๐ด ๐, โ โ 1