125
External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler 13 A: External Algorithms; Disjoint Sets; Java API Support CS1102S: Data Structures and Algorithms Martin Henz April 14, 2010 Generated on Wednesday 14 th April, 2010, 10:00 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 1

13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

  • Upload
    trananh

  • View
    245

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

13 A: External Algorithms; Disjoint Sets; JavaAPI Support

CS1102S: Data Structures and Algorithms

Martin Henz

April 14, 2010

Generated on Wednesday 14th April, 2010, 10:00CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 1

Page 2: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

1 External Search Trees: B-Trees

2 External Sorting

3 Disjoint Sets

4 Java API Support for Data Structures

5 Another Puzzler

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 2

Page 3: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

1 External Search Trees: B-TreesMotivationDefinition of B-TreesInsertion and Deletion

2 External Sorting

3 Disjoint Sets

4 Java API Support for Data Structures

5 Another Puzzler

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 3

Page 4: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Internal Storage

Assumption so far: random-access memory

Memory can be read and written at a speed O(1)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 4

Page 5: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Internal Storage

Assumption so far: random-access memory

Memory can be read and written at a speed O(1)

Abstraction

Even for main memory accessible to the CPU through a fastdata bus, this is a coarse simplification

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 5

Page 6: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Internal Storage

Assumption so far: random-access memory

Memory can be read and written at a speed O(1)

Abstraction

Even for main memory accessible to the CPU through a fastdata bus, this is a coarse simplification

Complicating factors

very fast memory on the CPU: registers

cache hierarchy: layers of larger and slower memory unitsbetween CPU and main memory chips

virtual memory: disk memory used when required memoryexceeds physically available main memory

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 6

Page 7: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 7

Page 8: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 8

Page 9: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

Disk storage

Access time depends on speed of disk, e.g. 7,200 RPM

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 9

Page 10: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

Disk storage

Access time depends on speed of disk, e.g. 7,200 RPM

How many disk accesses possible per second?

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 10

Page 11: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

Disk storage

Access time depends on speed of disk, e.g. 7,200 RPM

How many disk accesses possible per second?

120 accesses per second

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 11

Page 12: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

Disk storage

Access time depends on speed of disk, e.g. 7,200 RPM

How many disk accesses possible per second?

120 accesses per second

How many instructions are executed per minute?

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 12

Page 13: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

Disk storage

Access time depends on speed of disk, e.g. 7,200 RPM

How many disk accesses possible per second?

120 accesses per second

How many instructions are executed per minute?

With 1.2-GIPS processor, we have 1,2 billion instructions persecond,

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 13

Page 14: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

External Storage

Internal vs external storage

Internal storage is governed by electricity;external storage is governed by mechanics

Disk storage

Access time depends on speed of disk, e.g. 7,200 RPM

How many disk accesses possible per second?

120 accesses per second

How many instructions are executed per minute?

With 1.2-GIPS processor, we have 1,2 billion instructions persecond, a factor of 10,000,000 faster than disks!

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 14

Page 15: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Another Characteristics of Disk Storage

We know already

Access time is limited by mechanics

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 15

Page 16: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Another Characteristics of Disk Storage

We know already

Access time is limited by mechanics

How about access size?

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 16

Page 17: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Another Characteristics of Disk Storage

We know already

Access time is limited by mechanics

How about access size?

defined by operating system/hardware design;

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 17

Page 18: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Another Characteristics of Disk Storage

We know already

Access time is limited by mechanics

How about access size?

defined by operating system/hardware design;typically large “chunks”, called blocks, of data can be read veryfast, once the disk head has reached the correct location

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 18

Page 19: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Another Characteristics of Disk Storage

We know already

Access time is limited by mechanics

How about access size?

defined by operating system/hardware design;typically large “chunks”, called blocks, of data can be read veryfast, once the disk head has reached the correct location

Reading and writing in blocks

Reading and writing is typically done in large blocks to takeadvantage of this feature of disk storage

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 19

Page 20: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Search Trees—Revisited

Setup

We would like to quickly find out if a given data item is includedin a collection.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 20

Page 21: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Search Trees—Revisited

Setup

We would like to quickly find out if a given data item is includedin a collection.

Example

In an underground carpark, a system captures the licence platenumbers of incoming and outgoing cars.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 21

Page 22: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Search Trees—Revisited

Setup

We would like to quickly find out if a given data item is includedin a collection.

Example

In an underground carpark, a system captures the licence platenumbers of incoming and outgoing cars.Problem: Find out if a particular car is in the carpark.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 22

Page 23: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Binary Search

Setup

Keep items in a tree. Each node holds one data item.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 23

Page 24: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Binary Search

Setup

Keep items in a tree. Each node holds one data item.

Idea

The left subtree of a node V only contains items smaller than Vand the right subtree only contains items larger than V .

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 24

Page 25: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Binary Search

Setup

Keep items in a tree. Each node holds one data item.

Idea

The left subtree of a node V only contains items smaller than Vand the right subtree only contains items larger than V .

Search

can then proceed top-down, starting at the root. If the searchitem is smaller than the item at the root, go down to the left, andif it is larger, go right.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 25

Page 26: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Example

Both trees are binary trees, but only the left tree is a searchtree.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 26

Page 27: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Insertion

Idea

Proceed like in search. If item is found, do nothing. If not, insertit in the last visited position.

Example

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 27

Page 28: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Deletion

Idea

Proceed like in search. If item is not found, do nothing. If item isfound, take action depending on node.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 28

Page 29: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Deletion

Idea

Proceed like in search. If item is not found, do nothing. If item isfound, take action depending on node.

Leaf

If the node is leaf, delete it from parent.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 29

Page 30: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Deletion

Idea

Proceed like in search. If item is not found, do nothing. If item isfound, take action depending on node.

Leaf

If the node is leaf, delete it from parent.

One child

If the node has one child, move the child to parent.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 30

Page 31: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Average-case Analysis

Average Depth

If all insertion sequences are equally likely, the average depthof any node is O(log N)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 31

Page 32: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Average-case Analysis

Average Depth

If all insertion sequences are equally likely, the average depthof any node is O(log N)

Deletion introduces imbalance

Deletion favours right subtree, and therefore trees become“left-heavy” on the long run.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 32

Page 33: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

A Cure: AVL Trees

Worst-case depth

We want to restrict all operations to O(log N) in the worst case.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 33

Page 34: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

A Cure: AVL Trees

Worst-case depth

We want to restrict all operations to O(log N) in the worst case.

AVL Trees

Make sure that the height of the subtrees of any node differ byat most one (Adelson-Velskii and Landis), using rebalancing ifnecessary

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 34

Page 35: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

A Cure: AVL Trees

Worst-case depth

We want to restrict all operations to O(log N) in the worst case.

AVL Trees

Make sure that the height of the subtrees of any node differ byat most one (Adelson-Velskii and Landis), using rebalancing ifnecessary

Bound

The height of an AVL tree is at most 1.44 log(N + 2) − 1.328,thus O(log N). In practice, the height is only slightly more thanlog N.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 35

Page 36: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Search Trees with External Storage

Main issue

When data does not fit in main memory, the number of blockaccesses needs to be minimized

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 36

Page 37: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Search Trees with External Storage

Main issue

When data does not fit in main memory, the number of blockaccesses needs to be minimized

Overall idea

Put more data into each node; use n − ary trees instead ofbinary trees

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 37

Page 38: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Example of 5-ary Tree

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 38

Page 39: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

B-tree Definition

A B-tree of order M is an M-ary tree with the followingproperties:

1 Data items are stored at leaves2 Nonleaf nodes store up to M − 1 keys to guide search; key

i represents smallest key in subtree i + 13 Root is either a leaf or has between two and M children4 Non-leaf non-root nodes have between ⌈M/2⌉ and M

children5 Leaves are at same depth, have between ⌈L/2⌉ and L

children

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 39

Page 40: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Example of B-Tree of Order 5

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 40

Page 41: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

B-Tree Before Insertion of 57

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 41

Page 42: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

B-Tree After Insertion of 57

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 42

Page 43: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Insertion of 55 Causes Split

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 43

Page 44: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Insertion of 40 Causes Two Splits

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 44

Page 45: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

What if a Split Reaches the Root?

Splitting root is allowed

Create a new root, and have the two halves as children

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 45

Page 46: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

What if a Split Reaches the Root?

Splitting root is allowed

Create a new root, and have the two halves as children

Exception in definition makes sense

Root can have between 2 and M children

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 46

Page 47: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

What if a Split Reaches the Root?

Splitting root is allowed

Create a new root, and have the two halves as children

Exception in definition makes sense

Root can have between 2 and M children

Growing B-trees

Splitting root as result of insertion is the only way that a B-treecan gain height

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 47

Page 48: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

Before Deletion of 99

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 48

Page 49: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

MotivationDefinition of B-TreesInsertion and Deletion

After Deletion of 99

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 49

Page 50: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

1 External Search Trees: B-Trees

2 External SortingModel for External SortingThe Simple AlgorithmMultiway Merge

3 Disjoint Sets

4 Java API Support for Data Structures

5 Another Puzzler

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 50

Page 51: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Tapes as Storage

Similar to disks

Access time many orders of magnitude slower than mainmemory

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 51

Page 52: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Tapes as Storage

Similar to disks

Access time many orders of magnitude slower than mainmemory

Additional characteristics

Large amounts of data can be read sequentially quite efficiently

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 52

Page 53: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Tapes as Storage

Similar to disks

Access time many orders of magnitude slower than mainmemory

Additional characteristics

Large amounts of data can be read sequentially quite efficiently

Access of previous locations

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 53

Page 54: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Tapes as Storage

Similar to disks

Access time many orders of magnitude slower than mainmemory

Additional characteristics

Large amounts of data can be read sequentially quite efficiently

Access of previous locations

is extremely slow, as it requires re-winding the tape!

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 54

Page 55: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

External Sorting

Main idea

Use tapes sequentially, and read one block from each inputtape tape

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 55

Page 56: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

External Sorting

Main idea

Use tapes sequentially, and read one block from each inputtape tape

Merge blocks

Sort the blocksUse merge procedure from mergesort to merge

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 56

Page 57: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

The Simple Algorithm: Overview

Four tapes

Two input tapes; two output tapes

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 57

Page 58: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

The Simple Algorithm: Overview

Four tapes

Two input tapes; two output tapes

Read and write runs

Read runs from input tape, sort them and write alternatively tooutput tapes

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 58

Page 59: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

The Simple Algorithm: Overview

Four tapes

Two input tapes; two output tapes

Read and write runs

Read runs from input tape, sort them and write alternatively tooutput tapes

Continue, writing larger runs

Read two runs from each “output” tape, and merge them on thefly, writing alternatively to “input” tapes

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 59

Page 60: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

The Simple Algorithm: Overview

Four tapes

Two input tapes; two output tapes

Read and write runs

Read runs from input tape, sort them and write alternatively tooutput tapes

Continue, writing larger runs

Read two runs from each “output” tape, and merge them on thefly, writing alternatively to “input” tapes

Continue

until one tape has all sorted dataCS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 60

Page 61: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Multiway Merge

Why only four tapes?

If we have more than four tapes, we can take advantage ofthem by using multiway merge

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 61

Page 62: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Multiway Merge

Why only four tapes?

If we have more than four tapes, we can take advantage ofthem by using multiway merge

How finding the smallest element during merge?

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 62

Page 63: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Multiway Merge

Why only four tapes?

If we have more than four tapes, we can take advantage ofthem by using multiway merge

How finding the smallest element during merge?

Priority queue!

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 63

Page 64: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Multiway Merge

Why only four tapes?

If we have more than four tapes, we can take advantage ofthem by using multiway merge

How finding the smallest element during merge?

Priority queue!

Each iteration of inner loop

deleteMin to find smallest element

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 64

Page 65: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Multiway Merge

Why only four tapes?

If we have more than four tapes, we can take advantage ofthem by using multiway merge

How finding the smallest element during merge?

Priority queue!

Each iteration of inner loop

deleteMin to find smallest elementinsert new element from tape from which element was deleted

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 65

Page 66: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Polyphase Merge and Replacement Selection

Polyphase merge: main idea

Make use of fewer tapes, by re-using tapes for reading andwriting

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 66

Page 67: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Polyphase Merge and Replacement Selection

Polyphase merge: main idea

Make use of fewer tapes, by re-using tapes for reading andwritingLeading to tape organization using k th order Fibonaccinumbers

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 67

Page 68: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Model for External SortingThe Simple AlgorithmMultiway Merge

Polyphase Merge and Replacement Selection

Polyphase merge: main idea

Make use of fewer tapes, by re-using tapes for reading andwritingLeading to tape organization using k th order Fibonaccinumbers

Replacement selection: main idea

Make use of input tape as output tape, reusing the tapes “onthe fly”

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 68

Page 69: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

1 External Search Trees: B-Trees

2 External Sorting

3 Disjoint SetsEquivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

4 Java API Support for Data Structures

5 Another Puzzler

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 69

Page 70: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Equivalence Relations

Definition

An equivalence relation is a relation R that satisfies threeproperties:

1 (Reflexive) aRa, for all a ∈ S.2 (Symmetric) aRb if and only if bRa.3 (Transitive) aRb and bRc implies aRc.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 70

Page 71: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Equivalence Relations

Definition

An equivalence relation is a relation R that satisfies threeproperties:

1 (Reflexive) aRa, for all a ∈ S.2 (Symmetric) aRb if and only if bRa.3 (Transitive) aRb and bRc implies aRc.

Examples

Electrical connectivity (metal wires between points)

Cities belonging to same country

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 71

Page 72: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

The Dynamic Equivalence Problem

Initial setup

Collection of N disjoint sets, each with one element

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 72

Page 73: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

The Dynamic Equivalence Problem

Initial setup

Collection of N disjoint sets, each with one element

Operations

find(a): return the set of which x is element

union(a, b): merge the sets to which a and b belong, sothat find(a) = find(b)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 73

Page 74: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Strategies

Fast Find, Slow Union

Use array repres to store equivalence class for each element

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 74

Page 75: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Strategies

Fast Find, Slow Union

Use array repres to store equivalence class for each element

find(a): return repres[a]

union(a, b): if repres[x ] = repres[b] then set repres[x ] torepres[a]

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 75

Page 76: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Strategies

Fast Find, Slow Union

Use array repres to store equivalence class for each element

find(a): return repres[a]

union(a, b): if repres[x ] = repres[b] then set repres[x ] torepres[a]

Fast Union, Reasonable Find

Union/find data structure

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 76

Page 77: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Basic Data Structure

Idea

Maintain forest corresponding to equivalence relation

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 77

Page 78: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Basic Data Structure

Idea

Maintain forest corresponding to equivalence relation

Union

Merge trees

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 78

Page 79: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Basic Data Structure

Idea

Maintain forest corresponding to equivalence relation

Union

Merge trees

Find

Return root of tree

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 79

Page 80: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Basic Data Structure

Idea

Maintain forest corresponding to equivalence relation

Union

Merge trees

Find

Return root of tree

Observe

Only upward direction needed!

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 80

Page 81: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Example

Initial setup:

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 81

Page 82: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Example

Initial setup:

After union(4, 5)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 82

Page 83: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Example

After union(4, 5)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 83

Page 84: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Example

After union(4, 5)

After union(6, 7)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 84

Page 85: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Example

After union(6, 7)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 85

Page 86: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Example

After union(6, 7)

After union(4, 6)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 86

Page 87: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Representation

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 87

Page 88: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Representation

Idea

Remember parent node only; mark root with −1

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 88

Page 89: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Representation

Idea

Remember parent node only; mark root with −1

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 89

Page 90: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Variants

Problem

How to choose root for union? Bad choice can lead to longpaths

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 90

Page 91: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Variants

Problem

How to choose root for union? Bad choice can lead to longpaths

Union-by-size

Always make the smaller tree a subtree of the larger tree

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 91

Page 92: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Variants

Problem

How to choose root for union? Bad choice can lead to longpaths

Union-by-size

Always make the smaller tree a subtree of the larger tree

Analysis

When depth increases, the tree is smaller than the other side.Thus, after union, it is at least twice as large.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 92

Page 93: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Variants

Problem

How to choose root for union? Bad choice can lead to longpaths

Union-by-size

Always make the smaller tree a subtree of the larger tree

Analysis

When depth increases, the tree is smaller than the other side.Thus, after union, it is at least twice as large.

Height

less than or equal to log NCS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 93

Page 94: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Variants

Union-by-height

Always make the shorter tree a subtree of the higher tree

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 94

Page 95: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Variants

Union-by-height

Always make the shorter tree a subtree of the higher tree

Height

As with union-by-size: O(log N)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 95

Page 96: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Path Compression

During find make every node point to root

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 96

Page 97: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Path Compression

During find make every node point to root

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 97

Page 98: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Path Compression

During find make every node point to root

after find(14)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 98

Page 99: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

A Very Slowly Growing Function

Definition

log∗ N is the number of times log needs to be applied to N untilN ≤ 1.

Examples

log∗ 2 = 1

log∗ 4 = 2

log∗ 16 = 3

log∗ 65536 = 4

...

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 99

Page 100: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Runtime

Consider variant

Union-by-height combined with path compression

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 100

Page 101: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Equivalence RelationsThe Dynamic Equivalence ProblemBasic Data StructureVariants

Runtime

Consider variant

Union-by-height combined with path compression

Theorem

The running time of M unions and finds is O(M log∗ N).

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 101

Page 102: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

1 External Search Trees: B-Trees

2 External Sorting

3 Disjoint Sets

4 Java API Support for Data StructuresCollections, Lists, IteratorsTreesHashingPriorityQueueSorting

5 Another PuzzlerCS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 102

Page 103: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

The Top-level Collection Interface

publ ic in te r face Co l l ec t i on <Any>extends I t e r a b l e <Any>

{i n t s ize ( ) ;boolean isEmpty ( ) ;void c l ea r ( ) ;boolean conta ins ( Any x ) ;boolean add ( Any x ) ; / / s i cboolean remove ( Any x ) ; / / s i cjava . u t i l . I t e r a t o r <Any> i t e r a t o r ( ) ;

}

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 103

Page 104: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

The List Interface in Collection API

publ ic in te r face L i s t <Any>extends Co l l ec t i on <Any>

{Any get ( i n t i dx ) ;Any set ( i n t idx , Any newVal ) ;void add ( i n t idx , Any x ) ;void remove ( i n t i dx ) ;

L i s t I t e r a t o r <Any> l i s t I t e r a t o r ( i n t pos ) ;}

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 104

Page 105: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

ArrayList and LinkedList

publ ic class Ar rayL i s t <Any>implements L i s t <Any> { . . . }

publ ic class L inkedL is t <Any>implements L i s t <Any> { . . . }

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 105

Page 106: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

Iterators

publ ic in te r face I t e r a t o r <Any> {boolean hasNext ( ) ;Any next ( ) ;void remove ( ) ;

}

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 106

Page 107: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

ListIterators

publ ic in te r face L i s t I t e r a t o r <Any>extends I t e r a t o r <Any>

{boolean hasPrevious ( ) ;Any prev ious ( ) ;void add ( Any x ) ;void set ( Any newVal ) ;

}

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 107

Page 108: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

TreeSet

Implements Collection

Guarantees O(log N) time for add, remove and contains

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 108

Page 109: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

AbstractMap<K,V>

Basic operations

V get(K key): Returns the value to which the specified keyis mapped.

V put(K key, V value): Associates the specified value withthe specified key in this map.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 109

Page 110: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

AbstractMap<K,V>

Basic operations

V get(K key): Returns the value to which the specified keyis mapped.

V put(K key, V value): Associates the specified value withthe specified key in this map.

Other operations

containsKey(key), containsValue(val), remove(key)

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 110

Page 111: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

TreeMap

Extends AbstractMap

Guarantees O(log N) time for put, get, containsKey,containsValue, remove

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 111

Page 112: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

HashMap

Extends AbstractMap

Uses separate chaining with rehashing

Rehashing is governed by initial capacity and load factor,set in constructor

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 112

Page 113: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

HashSet

Implements Collection using HashMap

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 113

Page 114: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

PriorityQueue

Implements Collection

Efficient implementation of heap data structureOperation names:

deleteMin is called “poll”insert is called “add”

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 114

Page 115: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

Sorting

Generic sorting supported by class Collections

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 115

Page 116: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

Sorting

Generic sorting supported by class Collections

Uses mergesort in order to minimize number ofcomparisons

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 116

Page 117: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

Sorting

Generic sorting supported by class Collections

Uses mergesort in order to minimize number ofcomparisons

Sorting of built-in numerical types supported by classArrays

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 117

Page 118: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Collections, Lists, IteratorsTreesHashingPriorityQueueSorting

Sorting

Generic sorting supported by class Collections

Uses mergesort in order to minimize number ofcomparisons

Sorting of built-in numerical types supported by classArrays

Uses efficient implementation of quicksort, to takeadvantage of tight inner loop.

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 118

Page 119: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

1 External Search Trees: B-Trees

2 External Sorting

3 Disjoint Sets

4 Java API Support for Data Structures

5 Another Puzzler

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 119

Page 120: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Remember Lecture 2 A: Parameter Passing

Java uses pass-by-value parameter passing.

publ ic s t a t i c void t ryChanging ( i n t a ) {a = 1;return ;

}. . .i n t b = 2;tryChanging ( b ) ;System . out . p r i n t l n ( b ) ;

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 120

Page 121: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Remember Lecture 2 A: Parameter Passing withObjects

publ ic s t a t i c void t ryChanging ( SomeObject ob j ) {obj . someField = 1;ob j = new SomeObject ( ) ;ob j . someField = 2;return ;

}. . .SomeObject someObj = new SomeObject ( ) ;t ryChanging ( someObj ) ;System . out . p r i n t l n ( someObj . someField ) ;

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 121

Page 122: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Remember Lecture 7 A: Sorting

Input

Unsorted array of elements

Behavior

Rearrange elements of array such that the smallest appearsfirst, followed by the second smallest etc, finally followed by thelargest element

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 122

Page 123: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Will This Work?

publ ic s t a t i c <AnyType extends Comparable<? super Anvoid mergeSort ( AnyType [ ] a ) {

AnyType [ ] r e t = . . . . ; / / dec lare he lper ar ray. . . . / / here goes a program t h a t places. . . . / / the element o f ” a ” i n t o ” r e t ” so. . . . / / t h a t ” r e t ” i s sor teda = r e t ;return ;

}. . .I n t ege r [ ] myArray = . . . ;I t e ra t i veMergeSor t . mergeSort ( myArray ) ;

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 123

Page 124: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

Will This Work?

publ ic s t a t i c <AnyType extends Comparable<? super Anvoid mergeSort ( AnyType [ ] a ) {

AnyType [ ] r e t = . . . . ; / / dec lare he lper ar ray. . . . / / here goes a program t h a t places. . . . / / the element o f ” a ” i n t o ” r e t ” so. . . . / / t h a t ” r e t ” i s sor teda = r e t ;return ;

}. . .I n t ege r [ ] myArray = . . . ;I t e ra t i veMergeSor t . mergeSort ( myArray ) ;

Answer: No! The assignment a = ret ; has no effect onmyArray!

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 124

Page 125: 13 A: External Algorithms; Disjoint Sets; Java API Supportcs1102s/slides/slides_13_A.color.pdf · External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for

External Search Trees: B-TreesExternal Sorting

Disjoint SetsJava API Support for Data Structures

Another Puzzler

This Week and Beyond

Thursday tutorial: outstanding assignments and labs

Friday lecture: CS1102S summary, outlook; questions?

Next week: Reading week, consultation by appointment

3/5, morning: Final

CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 125