13 A: External Algorithms; Disjoint Sets; Java API Support CS1102S: - - PowerPoint PPT Presentation

13 a external algorithms disjoint sets java api support
SMART_READER_LITE
LIVE PREVIEW

13 A: External Algorithms; Disjoint Sets; Java API Support CS1102S: - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

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 14th April, 2010, 10:00 CS1102S: Data Structures and Algorithms 13 A: External Algorithms; Disjoint Sets; Java API Support 1

slide-2
SLIDE 2

External Search Trees: B-Trees External Sorting Disjoint Sets Java 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

slide-3
SLIDE 3

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

1

External Search Trees: B-Trees Motivation Definition of B-Trees Insertion 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

slide-4
SLIDE 4

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-5
SLIDE 5

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 fast data bus, this is a coarse simplification

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

slide-6
SLIDE 6

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 fast data bus, this is a coarse simplification Complicating factors very fast memory on the CPU: registers cache hierarchy: layers of larger and slower memory units between CPU and main memory chips virtual memory: disk memory used when required memory exceeds physically available main memory

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

slide-7
SLIDE 7

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-8
SLIDE 8

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-9
SLIDE 9

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-10
SLIDE 10

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-11
SLIDE 11

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-12
SLIDE 12

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-13
SLIDE 13

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 per second,

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

slide-14
SLIDE 14

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 per second, a factor of 10,000,000 faster than disks!

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

slide-15
SLIDE 15

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-16
SLIDE 16

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-17
SLIDE 17

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-18
SLIDE 18

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 very fast, once the disk head has reached the correct location

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

slide-19
SLIDE 19

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 very fast, once the disk head has reached the correct location Reading and writing in blocks Reading and writing is typically done in large blocks to take advantage of this feature of disk storage

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

slide-20
SLIDE 20

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Search Trees—Revisited

Setup We would like to quickly find out if a given data item is included in a collection.

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

slide-21
SLIDE 21

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Search Trees—Revisited

Setup We would like to quickly find out if a given data item is included in a collection. Example In an underground carpark, a system captures the licence plate numbers of incoming and outgoing cars.

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

slide-22
SLIDE 22

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Search Trees—Revisited

Setup We would like to quickly find out if a given data item is included in a collection. Example In an underground carpark, a system captures the licence plate numbers 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

slide-23
SLIDE 23

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-24
SLIDE 24

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 V and the right subtree only contains items larger than V.

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

slide-25
SLIDE 25

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 V and the right subtree only contains items larger than V. Search can then proceed top-down, starting at the root. If the search item is smaller than the item at the root, go down to the left, and if it is larger, go right.

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

slide-26
SLIDE 26

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Example

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

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

slide-27
SLIDE 27

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Insertion

Idea Proceed like in search. If item is found, do nothing. If not, insert it in the last visited position. Example

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

slide-28
SLIDE 28

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Deletion

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

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

slide-29
SLIDE 29

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Deletion

Idea Proceed like in search. If item is not found, do nothing. If item is found, 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

slide-30
SLIDE 30

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Deletion

Idea Proceed like in search. If item is not found, do nothing. If item is found, 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

slide-31
SLIDE 31

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Average-case Analysis

Average Depth If all insertion sequences are equally likely, the average depth

  • f any node is O(log N)

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

slide-32
SLIDE 32

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Average-case Analysis

Average Depth If all insertion sequences are equally likely, the average depth

  • f 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

slide-33
SLIDE 33

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-34
SLIDE 34

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 by at most one (Adelson-Velskii and Landis), using rebalancing if necessary

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

slide-35
SLIDE 35

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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 by at most one (Adelson-Velskii and Landis), using rebalancing if necessary 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 than log N.

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

slide-36
SLIDE 36

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Search Trees with External Storage

Main issue When data does not fit in main memory, the number of block accesses needs to be minimized

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

slide-37
SLIDE 37

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Search Trees with External Storage

Main issue When data does not fit in main memory, the number of block accesses needs to be minimized Overall idea Put more data into each node; use n − ary trees instead of binary trees

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

slide-38
SLIDE 38

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Example of 5-ary Tree

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

slide-39
SLIDE 39

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

B-tree Definition

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

1

Data items are stored at leaves

2

Nonleaf nodes store up to M − 1 keys to guide search; key i represents smallest key in subtree i + 1

3

Root is either a leaf or has between two and M children

4

Non-leaf non-root nodes have between ⌈M/2⌉ and M children

5

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

slide-40
SLIDE 40

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Example of B-Tree of Order 5

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

slide-41
SLIDE 41

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

B-Tree Before Insertion of 57

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

slide-42
SLIDE 42

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

B-Tree After Insertion of 57

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

slide-43
SLIDE 43

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Insertion of 55 Causes Split

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

slide-44
SLIDE 44

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Insertion of 40 Causes Two Splits

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

slide-45
SLIDE 45

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-46
SLIDE 46

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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

slide-47
SLIDE 47

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion 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-tree can gain height

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

slide-48
SLIDE 48

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

Before Deletion of 99

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

slide-49
SLIDE 49

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Motivation Definition of B-Trees Insertion and Deletion

After Deletion of 99

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

slide-50
SLIDE 50

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

1

External Search Trees: B-Trees

2

External Sorting Model for External Sorting The Simple Algorithm Multiway 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

slide-51
SLIDE 51

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Tapes as Storage

Similar to disks Access time many orders of magnitude slower than main memory

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

slide-52
SLIDE 52

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Tapes as Storage

Similar to disks Access time many orders of magnitude slower than main memory 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

slide-53
SLIDE 53

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Tapes as Storage

Similar to disks Access time many orders of magnitude slower than main memory 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

slide-54
SLIDE 54

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Tapes as Storage

Similar to disks Access time many orders of magnitude slower than main memory 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

slide-55
SLIDE 55

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

External Sorting

Main idea Use tapes sequentially, and read one block from each input tape tape

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

slide-56
SLIDE 56

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

External Sorting

Main idea Use tapes sequentially, and read one block from each input tape tape Merge blocks Sort the blocks Use merge procedure from mergesort to merge

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

slide-57
SLIDE 57

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway 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

slide-58
SLIDE 58

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway 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 to

  • utput tapes

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

slide-59
SLIDE 59

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway 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 to

  • utput tapes

Continue, writing larger runs Read two runs from each “output” tape, and merge them on the fly, writing alternatively to “input” tapes

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

slide-60
SLIDE 60

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway 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 to

  • utput tapes

Continue, writing larger runs Read two runs from each “output” tape, and merge them on the fly, writing alternatively to “input” tapes Continue until one tape has all sorted data

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

slide-61
SLIDE 61

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Multiway Merge

Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge

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

slide-62
SLIDE 62

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Multiway Merge

Why only four tapes? If we have more than four tapes, we can take advantage of them 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

slide-63
SLIDE 63

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Multiway Merge

Why only four tapes? If we have more than four tapes, we can take advantage of them 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

slide-64
SLIDE 64

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Multiway Merge

Why only four tapes? If we have more than four tapes, we can take advantage of them 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

slide-65
SLIDE 65

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Multiway Merge

Why only four tapes? If we have more than four tapes, we can take advantage of them by using multiway merge How finding the smallest element during merge? Priority queue! Each iteration of inner loop deleteMin to find smallest element insert new element from tape from which element was deleted

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

slide-66
SLIDE 66

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Polyphase Merge and Replacement Selection

Polyphase merge: main idea Make use of fewer tapes, by re-using tapes for reading and writing

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

slide-67
SLIDE 67

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Polyphase Merge and Replacement Selection

Polyphase merge: main idea Make use of fewer tapes, by re-using tapes for reading and writing Leading to tape organization using kth order Fibonacci numbers

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

slide-68
SLIDE 68

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Model for External Sorting The Simple Algorithm Multiway Merge

Polyphase Merge and Replacement Selection

Polyphase merge: main idea Make use of fewer tapes, by re-using tapes for reading and writing Leading to tape organization using kth order Fibonacci numbers Replacement selection: main idea Make use of input tape as output tape, reusing the tapes “on the fly”

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

slide-69
SLIDE 69

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

1

External Search Trees: B-Trees

2

External Sorting

3

Disjoint Sets Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-70
SLIDE 70

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Equivalence Relations

Definition An equivalence relation is a relation R that satisfies three properties:

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

slide-71
SLIDE 71

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Equivalence Relations

Definition An equivalence relation is a relation R that satisfies three properties:

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

slide-72
SLIDE 72

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-73
SLIDE 73

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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, so that find(a) = find(b)

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

slide-74
SLIDE 74

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-75
SLIDE 75

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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] to repres[a]

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

slide-76
SLIDE 76

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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] to repres[a] Fast Union, Reasonable Find Union/find data structure

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

slide-77
SLIDE 77

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-78
SLIDE 78

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-79
SLIDE 79

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-80
SLIDE 80

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-81
SLIDE 81

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Example

Initial setup:

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

slide-82
SLIDE 82

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Example

Initial setup: After union(4, 5)

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

slide-83
SLIDE 83

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Example

After union(4, 5)

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

slide-84
SLIDE 84

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Example

After union(4, 5) After union(6, 7)

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

slide-85
SLIDE 85

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Example

After union(6, 7)

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

slide-86
SLIDE 86

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Example

After union(6, 7) After union(4, 6)

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

slide-87
SLIDE 87

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Representation

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

slide-88
SLIDE 88

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-89
SLIDE 89

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-90
SLIDE 90

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Variants

Problem How to choose root for union? Bad choice can lead to long paths

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

slide-91
SLIDE 91

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Variants

Problem How to choose root for union? Bad choice can lead to long paths 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

slide-92
SLIDE 92

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Variants

Problem How to choose root for union? Bad choice can lead to long paths 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

slide-93
SLIDE 93

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

Variants

Problem How to choose root for union? Bad choice can lead to long paths 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 N

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

slide-94
SLIDE 94

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-95
SLIDE 95

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-96
SLIDE 96

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-97
SLIDE 97

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-98
SLIDE 98

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-99
SLIDE 99

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

A Very Slowly Growing Function

Definition log∗ N is the number of times log needs to be applied to N until N ≤ 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

slide-100
SLIDE 100

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-101
SLIDE 101

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Equivalence Relations The Dynamic Equivalence Problem Basic Data Structure Variants

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

slide-102
SLIDE 102

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

1

External Search Trees: B-Trees

2

External Sorting

3

Disjoint Sets

4

Java API Support for Data Structures Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

5

Another Puzzler

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

slide-103
SLIDE 103

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

The Top-level Collection Interface

public interface Collection <Any> extends Iterable <Any> { int size ( ) ; boolean isEmpty ( ) ; void clear ( ) ; boolean contains ( Any x ) ; boolean add ( Any x ) ; / / sic boolean remove ( Any x ) ; / / sic java . 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

slide-104
SLIDE 104

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

The List Interface in Collection API

public interface List <Any> extends Collection <Any> { Any get ( int idx ) ; Any set ( int idx , Any newVal ) ; void add ( int idx , Any x ) ; void remove ( int idx ) ; 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 ( int pos ) ; }

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

slide-105
SLIDE 105

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

ArrayList and LinkedList

public class ArrayList <Any> implements List <Any> { . . . } public class LinkedList <Any> implements List <Any> { . . . }

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

slide-106
SLIDE 106

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

Iterators

public interface 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

slide-107
SLIDE 107

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

ListIterators

public interface 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 previous ( ) ; void add ( Any x ) ; void set ( Any newVal ) ; }

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

slide-108
SLIDE 108

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

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

slide-109
SLIDE 109

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

AbstractMap<K,V>

Basic operations V get(K key): Returns the value to which the specified key is mapped. V put(K key, V value): Associates the specified value with the specified key in this map.

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

slide-110
SLIDE 110

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

AbstractMap<K,V>

Basic operations V get(K key): Returns the value to which the specified key is mapped. V put(K key, V value): Associates the specified value with the 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

slide-111
SLIDE 111

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

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

slide-112
SLIDE 112

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

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

slide-113
SLIDE 113

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

HashSet

Implements Collection using HashMap

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

slide-114
SLIDE 114

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

PriorityQueue

Implements Collection Efficient implementation of heap data structure Operation names:

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

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

slide-115
SLIDE 115

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

Sorting

Generic sorting supported by class Collections

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

slide-116
SLIDE 116

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

Sorting

Generic sorting supported by class Collections Uses mergesort in order to minimize number of comparisons

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

slide-117
SLIDE 117

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

Sorting

Generic sorting supported by class Collections Uses mergesort in order to minimize number of comparisons Sorting of built-in numerical types supported by class Arrays

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

slide-118
SLIDE 118

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler Collections, Lists, Iterators Trees Hashing PriorityQueue Sorting

Sorting

Generic sorting supported by class Collections Uses mergesort in order to minimize number of comparisons Sorting of built-in numerical types supported by class Arrays Uses efficient implementation of quicksort, to take advantage of tight inner loop.

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

slide-119
SLIDE 119

External Search Trees: B-Trees External Sorting Disjoint Sets Java 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

slide-120
SLIDE 120

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler

Remember Lecture 2 A: Parameter Passing

Java uses pass-by-value parameter passing. public static void tryChanging ( int a ) { a = 1; return ; } . . . int 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

slide-121
SLIDE 121

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler

Remember Lecture 2 A: Parameter Passing with Objects

public static void tryChanging ( SomeObject obj ) {

  • bj . someField = 1;
  • bj = new SomeObject ( ) ;
  • bj . someField = 2;

return ; } . . . SomeObject someObj = new SomeObject ( ) ; tryChanging ( 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

slide-122
SLIDE 122

External Search Trees: B-Trees External Sorting Disjoint Sets Java 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 appears first, followed by the second smallest etc, finally followed by the largest element

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

slide-123
SLIDE 123

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler

Will This Work?

public static <AnyType extends Comparable<? super An void mergeSort ( AnyType [ ] a ) { AnyType [ ] r e t = . . . . ; / / declare helper array . . . . / / here goes a program that places . . . . / / the element

  • f

” a ” i n t o ” r e t ” so . . . . / / that ” r e t ” i s sorted a = r e t ; return ; } . . . Integer [ ] myArray = . . . ; IterativeMergeSort . mergeSort ( myArray ) ;

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

slide-124
SLIDE 124

External Search Trees: B-Trees External Sorting Disjoint Sets Java API Support for Data Structures Another Puzzler

Will This Work?

public static <AnyType extends Comparable<? super An void mergeSort ( AnyType [ ] a ) { AnyType [ ] r e t = . . . . ; / / declare helper array . . . . / / here goes a program that places . . . . / / the element

  • f

” a ” i n t o ” r e t ” so . . . . / / that ” r e t ” i s sorted a = r e t ; return ; } . . . Integer [ ] myArray = . . . ; IterativeMergeSort . mergeSort ( myArray ) ; Answer: No! The assignment a = ret ; has no effect on myArray!

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

slide-125
SLIDE 125

External Search Trees: B-Trees External Sorting Disjoint Sets Java 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