/
Cse  373 October 27 th   – Priority Cse  373 October 27 th   – Priority

Cse 373 October 27 th – Priority - PowerPoint Presentation

sandsomber
sandsomber . @sandsomber
Follow
344 views
Uploaded On 2020-07-01

Cse 373 October 27 th – Priority - PPT Presentation

QUeues Today HW 2 grades went out yesterday Many of the problems seemed to be a problem with rigor In proofs justify what youre saying and make as much explicit as you can Today HW 2 grades went out yesterday ID: 791270

heap priority tree queue priority heap queue tree left property data binary parent search heaps bst insert array trees

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Cse 373 October 27 th – Priority" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Cse 373

October 27

th

– Priority

QUeues

Slide2

Today

HW 2 grades went out yesterday

Many of the problems seemed to be a problem with rigor

In proofs, justify what you’re saying and make as much explicit as you can

Slide3

Today

HW 2 grades went out yesterday

Many of the problems seemed to be a problem with rigor

In proofs, justify what you’re saying and make as much explicit as you can

https://catalyst.uw.edu/webq/survey/ejmcc/

340863

Slide4

Today

B+-Trees

Conclusion and important info

New ADT – Priority Queue

Slide5

B-tree example

Let M (the number of children from a signpost) be 3 and let L (the number of

k,v

pairs in a leaf) be 1

This is a 3-1 tree (uncommon, but useful for demonstration).

Slide6

B-trees

B-Trees do not receive a benefit unless their nodes are page aligned

If the nodes overlap a page boundary, we are doubling the number of potential disk accesses

Slide7

B-trees

B-Trees do not receive a benefit unless their nodes are page aligned

If the nodes overlap a page boundary, we are doubling the number of potential disk accesses

Because of this, B-trees are not implemented in Java.

Slide8

B-trees

Designed based on our knowledge of memory architecture

If a disk access brings a whole page into memory (or cache), make sure that we get the maximum amount of information.

When we bring in a signpost, we can use fast in-memory binary search to find the correct child

Slide9

B-Trees

Important things to remember

Signposts v. Leaves

Performing a find

Runtime analysis

Inserting in simple cases

Calculating M and L

Slide10

B-tree

Conclusion

Slide11

B-tree

Conclusion

Good data structure for working with and understanding memory and the disk

Slide12

B-tree

Conclusion

Good data structure for working with and understanding memory and the disk

More complicated analysis, but comes after recognizing that

bigO

assumes equal memory access

Slide13

B-tree

Conclusion

Good data structure for working with and understanding memory and the disk

More complicated analysis, but comes after recognizing that

bigO

assumes equal memory access

Computer architecture constraints have real-world impacts that can be corrected for

Slide14

B-tree

Conclusion

Good data structure for working with and understanding memory and the disk

More complicated analysis, but comes after recognizing that

bigO

assumes equal memory access

Computer architecture constraints have real-world impacts that can be corrected for

Theory is great, but it has its limitations

Slide15

Priority Queue

New ADT

Slide16

Priority Queue

New ADT

Objects in the priority queue have:

Data

Priority

Slide17

Priority Queue

New ADT

Objects in the priority queue have:

Data

Priority

Conditions

Lower priority items should

dequeue

first

Should be able to change priority of an item

FIFO for equal priority?

Slide18

Priority Queue

insert(K key,

int

priority)

Insert the key into the PQ with given priority

findMin

()

Return the key that currently has lowest priority in the PQ (min-heap)

deleteMin

()

Return and remove the key with lowest priority

changePriority

(K key,

int

newPri

)

Assign a new priority to the object key

Slide19

Priority Queue

Applications?

Slide20

Priority Queue

Applications?

Hospitals

CSE course overloads

Etc…

Slide21

Priority Queue

How to implement?

Slide22

Priority Queue

How to implement?

Array?

Slide23

Priority Queue

How to implement?

Array?

Must keep sorted

Inserting into the middle is costly

(must move other items)

Slide24

Priority Queue

How to implement?

Keep data sorted (somehow)

Array?

Inserting into the middle is costly

(must move other items)

Linked list?

Must iterate through entire list to find place

Cannot move backward if priority changes

Slide25

Priority Queue

These

data structures

will all give us the behavior we want as far as the ADT, but they may be poor design decisions

Any other data structures to try?

Slide26

Priority QUeue

Priority queue

i

mplementations?

Slide27

Priority QUeue

Priority queue

i

mplementations?

Binary search tree?

Slide28

Priority QUeue

Priority queue

i

mplementations?

Binary search tree?

Faster insert

Slide29

Priority QUeue

Priority queue

i

mplementations?

Binary search tree?

Faster insert

Find? Always deleting the smallest (left-most) element

Slide30

Priority QUeue

Priority queue

i

mplementations?

Binary search tree?

Faster insert

Find? Always deleting the smallest (left-most) element

Changing

priority?

Slide31

Priority QUeue

Want the speed of trees (but not BST)

Priority Queue has unique demands

Slide32

Priority QUeue

Want the speed of trees (but not BST)

Priority Queue has unique demands

Other types of trees?

Slide33

Priority QUeue

Want the speed of trees (but not BST)

Priority Queue has unique demands

Other types of trees?

Review BST first

Slide34

Properties (BST)

Tree

Slide35

Properties (BST)

Tree (Binary)

Root

(Two)

C

hildren

No cycles

Slide36

Properties (BST)

Tree (Binary)

Root

(Two)

C

hildren

No cycles

Search

Slide37

Properties (BST)

Tree (Binary)

Root

(Two)

C

hildren

No cycles

Search

Comparable data

Left child data < parent data

Slide38

Properties (BST)

Tree (Binary)

Root

(Two)

C

hildren

No cycles

Search

Comparable data

Left child data < parent data

Smallest child is at the left most node

Slide39

Properties (BST)

Binary tree may be useful

Search property doesn’t help

Slide40

Properties (BST)

Binary tree may be useful

Search property doesn’t help

Always deleting min

Slide41

Properties (BST)

Binary tree may be useful

Search property doesn’t help

Always deleting min

Put min on top!

Slide42

Heap-order property

Still a binary tree

Slide43

Heap-order property

Still a binary tree

Instead of search (left < parent),

Slide44

Heap-order property

Still a binary tree

Instead of search (left < parent),

parent should be less than children

Slide45

Heap-order property

Still a binary tree

Instead of search (left < parent),

parent should be less than children

How to implement?

Insert and delete are different than BST

Slide46

Heap-order property

Still a binary tree

Instead of search (left < parent),

parent should be less than children

How to implement?

Insert and delete are different than BST

Slide47

Heaps

The Priority Queue is the ADT

The Heap is the Data Structure

Slide48

Heap Example

Only looking at priorities

Insert something priority 4

Slide49

Heap Example

4

Slide50

Heap Example

4

Now insert priority 6?

Slide51

Heap Example

4

Now insert priority 6?

Should come after 4, but no preference right over left?

Slide52

Heap Example

4

Now insert priority 6?

Should come after 4, but no preference right over left?

Solution: fill the tree from top to bottom left to right.

Slide53

Heap Example

4

6

null

Now insert 2.

Slide54

Heap Example

2

6

4

Now insert 2.

Slide55

Heap Example

2

6

4

Could easily have been 4 on the left, but our left to right top to bottom strategy determines this solution

Slide56

Completeness

Slide57

Completeness

Filling left to right and top to bottom is another property - completeness

Slide58

Heaps

Heap property (parents < children)

Complete tree property (left to right, bottom to top

)

Slide59

Review

15

30

80

20

10

Is this a heap?

Slide60

Review

15

30

80

20

10

Is this a heap?

No. Why?

Slide61

Review

Is this a heap?

450

3

1

75

50

8

6

0

10

10

Slide62

Review

Is this a heap?

No. Why

450

3

1

75

50

8

6

0

10

10

Slide63

Review

Is this a heap?

No. Why

450

3

1

75

50

8

6

0

10

10

Slide64

Review

Is this a heap?

99

60

40

80

20

10

50

700

85

Slide65

Review

Is this a heap?

Yes, Heap

+ Complete

99

60

40

80

20

10

50

700

85

Slide66

Heaps

Heap property (parents < children)

Complete tree property (left to right, bottom to top)

How does this help?

Array implementation

Slide67

Heaps

0

1

2

3

4

Insert into array from left to right

For any parent at index

i

,

children at 2*i+1 and 2*i+2

Slide68

Review

Array

property (with 1 indexing)

G

E

D

C

B

A

J

K

H

I

F

L

Slide69

Review

Array property

G

E

D

C

B

A

J

K

H

I

F

L

7

1

2

3

4

5

6

9

8

10

11

12

Slide70

Review

Array property

G

E

D

C

B

A

J

K

H

I

F

L

7

1

2

3

4

5

6

9

8

10

11

12

A

B

C

D

E

F

G

H

I

J

K

L

0

1

2

3

4

5

6

7

8

9

10

11

12

13

Slide71

Heaps

https://www.cs.usfca.edu/~galles/visualization/

Heap.html

Another visualizer

Slide72

Heaps

How to maintain heap property then?

Slide73

Heaps

How to maintain heap property then?

Parent must be higher priority than children

Slide74

Heaps

How to maintain heap property then?

Parent must be higher priority than children

Two functions – percolate up and percolate down

Slide75

Heap Functions

Percolate up

When a new item is inserted

:

Place the item at the next position to preserve completeness

Swap the item up the tree until it is larger than its parent

Slide76

Heap Functions

Percolate down

When an item is deleted

:

Remove the root of the tree (to be returned)

Move the last object in the tree to the root

Swap the moved piece down while it is larger than it’s smallest child

Only swap with the smallest child

Slide77

Heaps as Arrays

Because heaps are complete, they can be represented as arrays without any gaps in them.

Naïve implementation:

Left child: 2*i+1

Right child: 2*

i

+ 2

Parent: (i-1)/2

Slide78

Heaps as Arrays

Alternate (common) implementation:

Put the root of the array at index 1

Leave index 0 blank

Calculating children/parent becomes:

Left child: 2*

i

Right child: 2*

i

+ 1

Parent:

i

/2

Slide79

Heaps as Arrays

Why do an array at all?

Slide80

Heaps as Arrays

Why do an array at all?

+ Memory efficiency

+ Fast accesses to data

+ Forces log n depth

- Needs to resize

- Can waste space

Slide81

Heaps as Arrays

Why do an array at all?

+ Memory efficiency

+ Fast accesses to data

+ Forces log n depth

- Needs to resize

- Can waste space

Almost always

done through an array

Slide82

Next week

Analysis of the heap

buildHeap

()—a unique case and analysis