Top Banner
Chapter 5 Data Structures Algorithm Theory WS 2018/19 Fabian Kuhn
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

Aug 05, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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