/
Cse   373 October 9 th  – Cse   373 October 9 th  –

Cse 373 October 9 th – - PowerPoint Presentation

classyshadow
classyshadow . @classyshadow
Follow
342 views
Uploaded On 2020-07-01

Cse 373 October 9 th – - PPT Presentation

Amortized Analysis Today Master Theorem Amortized Analysis Binary Search Trees Review Algorithm Analysis Asymptotic behavior Review Algorithm Analysis Asymptotic behavior Loops and iterations ID: 791271

case analysis bst implementations analysis case implementations bst node tree height worst deleting array elements delete children left key

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Cse 373 October 9 th –" 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 9

th

Amortized Analysis

Slide2

Today

Master

Theorem

Amortized

Analysis

Binary Search Trees

Slide3

Review

Algorithm Analysis

Asymptotic behavior

Slide4

Review

Algorithm Analysis

Asymptotic behavior

Loops and iterations

Slide5

Review

Algorithm Analysis

Asymptotic behavior

Loops and iterations

Recursive functions

Slide6

Review

Algorithm Analysis

Asymptotic behavior

Loops and iterations

Recursive functions

Recurrence relations

Slide7

Analysis

On

Friday,

we showed the formal recurrence approach

Break into recursive, non-recursive

Compute non-recursive computation time

Produce the recurrence

Roll out the recurrence and produce the closed form

Upper-bound the closed form with

bigO

notation

Slide8

Analysis

While this process is important, we can save some steps if all we care about is the upper bound

bigO

notation eliminates the need for constants

Lots of our messing around with c

0

and c

1

doesn’t come through to the solution

Slide9

Analysis

Master theorem

These recurrences all follow a similar pattern

Slide10

Analysis

Master theorem

These recurrences all follow a similar pattern

Therefore, if you can produce a recurrence, there is actually a procedural way to produce solutions

Slide11

Analysis

Master theorem

These recurrences all follow a similar pattern

Therefore, if you can produce a recurrence, there is actually a procedural way to produce solutions

If T(n) = a*T(n/b)+

n

c

for n > n

0

and if the base case is a constant

Slide12

Analysis

Master theorem

These recurrences all follow a similar pattern

Therefore, if you can produce a recurrence, there is actually a procedural way to produce solutions

If T(n) = a*T(n/b)+

n

c

for n > n

0

and if the base case is a constant

Case 1:

log

b

(a) < c: T(n) = O(

n

c

)

Case

2:

log

b

(a)

=

c

: T(n) = O(

n

c

lg

n)

Case

3:

log

b

(a)

>

c

: T(n) = O(

n

log

a

)

Slide13

Analysis

Recurrences come up all the time

Slide14

Analysis

Recurrences come up all the time

Analyze methods and iterative approaches through the normal methods

Recursive functions use a recurrence

Possible to get to

bigO

solution quickly

Usually for worst-case analysis

Slide15

Analysis

Final analysis type

Slide16

Analysis

Final analysis type

Worst-case

Slide17

Analysis

Final analysis type

Worst-case

Consider adding to an unsorted array

Slide18

Analysis

Final analysis type

Worst-case

Consider adding to an unsorted array

Resizing is the costly O(n) operation

Slide19

Analysis

Final analysis type

Worst-case

Consider adding to an unsorted array

Resizing is the costly O(n) operation

This occurs in predictable ways

Slide20

Analysis

Final analysis type

Worst-case

Consider adding to an unsorted array

Resizing is the costly O(n) operation

This occurs in predictable ways

Do these types of operations really slow down the function?

Slide21

Amortized Analysis

Adding to unsorted array

Slide22

Amortized Analysis

Adding to unsorted array

How long does it take to add

n

elements into the array?

Slide23

Amortized Analysis

Adding to unsorted array

How long does it take to add

n

elements into the array?

Let’s say the array is full with

n

elements and we add

n

more

Slide24

Amortized Analysis

Adding to unsorted array

How long does it take to add

n

elements into the array?

Let’s say the array is full with

n

elements and we add

n

more

It takes n-1*O(1) + 1*O(n) = O(n)

Slide25

Amortized Analysis

Adding to unsorted array

How long does it take to add

n

elements into the array?

Let’s say the array is full with

n

elements and we add

n

more

It takes n-1*O(1) + 1*O(n) = O(n)

Amortized over the whole set of operations, each one is only O(1) time

Slide26

Amortized Analysis

Adding to unsorted array

How long does it take to add

n

elements into the array?

Let’s say the array is full with

n

elements and we add

n

more

It takes n-1*O(1) + 1*O(n) = O(n)

Amortized over the whole set of operations, each one is only O(1) time

What does this depend on?

Slide27

Amortized Analysis

Adding to unsorted array

How long does it take to add

n

elements into the array?

Let’s say the array is full with

n

elements and we add

n

more

It takes n-1*O(1) + 1*O(n) = O(n)

Amortized over the whole set of operations, each one is only O(1) time

What does this depend on?

Doubling the array

Slide28

Amortized Analysis

Adding to unsorted array

What if we only add some constant number to the array?

Let’s resize and add 10,000 elements every time

How long does it take to add

n

elements?

n

-n/10,000*

O(1) +

n/10,000*

O(n)

Slide29

Amortized Analysis

Adding to unsorted array

What if we only add some constant number to the array?

Let’s resize and add 10,000 elements every time

How long does it take to add

n

elements?

n

-n/10,000*

O(1) +

n/10,000*

O(n)

= O(n

2

)

This is for any constant, regardless of how large

Slide30

Amortized Analysis

Amortization the average runtime over repeated calls to the same function

Slide31

Amortized Analysis

Amortization the average runtime over repeated calls to the same function

If the worst case happens in predictable ways (i.e. every

n

inserts), then the costly operation doesn’t increase the total asymptotic runtime of multiple operations

Slide32

Amortized Analysis

Amortization the average runtime over repeated calls to the same function

If the worst case happens in predictable ways (i.e. every

n

inserts), then the costly operation doesn’t increase the total asymptotic runtime of multiple operations

Over

n

operations, remember to divide the total runtime by

n

Slide33

Dictionaries

Back to the dictionary problem

Can we apply these analytical tools to some simple implementations?

Slide34

Implementations

Simple implementations

Slide35

Implementations

Simple implementations

insert find delete

Unsorted linked-list

Unsorted array

Sorted linked list

Sorted array

*

Because we need to check for duplicates

O(n)*

O(n)

O(n)

O(n)*

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(log n)

O(n)

Slide36

Implementations

Other implementations?

Slide37

Implementations

Other implementations?

Binary Search Tree (BST)

Slide38

Implementations

Other implementations?

Binary Search Tree (BST)

Sort based on keys (which have to be comparable)

Slide39

Implementations

Other implementations?

Binary Search Tree (BST)

Sort based on keys (which have to be comparable)

How do we implement this?

Slide40

BINARY SEARCH TREE

Review

Slide41

BINARY SEARCH TREE

Review

What is a binary search tree?

Slide42

BINARY SEARCH TREE

Review

What is a binary search tree?

A rooted tree, where each node has at most two children

Slide43

BINARY SEARCH TREE

Review

What is a binary search tree?

A rooted tree, where each node has at most two children

All elements less than the root are in the left

subtree

and all elements larger than the root are in the right

subtree

Slide44

BINARY SEARCH TREE

Review

What is a binary search tree?

A rooted tree, where each node has at most two children

All elements less than the root are in the left

subtree

and all elements larger than the root are in the right

subtree

All,

subtrees

must also be binary search trees

Slide45

BINARY SEARCH TREE

Review

What is a binary search tree?

A rooted tree, where each node has at most two children

All elements less than the root are in the left

subtree

and all elements larger than the root are in the right

subtree

All,

subtrees

must also be binary search trees

With this property, all binary search trees have sorted in-order traversals

Slide46

Implementations

Other implementations?

Binary Search Tree (BST)

Sort based on keys (which have to be comparable)

How do we implement this?

What changes need to be made?

Slide47

Implementations

BST Node:

Before:

Slide48

Implementations

BST Node:

Before:

Node left

Node right

Value data

Slide49

Implementations

BST Node:

Before:

Node left

Node right

Value data

Now?

Slide50

Implementations

BST Node:

Before:

Node left

Node right

Value data

Now?

Node left

Node right

Slide51

Implementations

BST Node:

Before:

Node left

Node right

Value data

Now?

Node left

Node right

Key k

Value v

Slide52

Implementations

BST Changes:

Insert() and find() remain similar

Slide53

Implementations

BST Changes:

Insert() and find() remain similar

Key is the primary comparison

Slide54

Implementations

BST Changes:

Insert() and find() remain similar

Key is the primary comparison

Value is attached to the key

Slide55

Implementations

BST Changes:

Insert() and find() remain similar

Key is the primary comparison

Value is attached to the key

Dictionary fact: All values have an associated key

Slide56

Implementations

BST Changes:

Insert() and find() remain similar

Key is the primary comparison

Value is attached to the key

Dictionary fact: All values have an associated key

All keys are unique, i.e. each key only has one value

Slide57

Implementations

BST Analysis:

What is our time for the three functions?

Slide58

Implementations

BST Analysis:

What is our time for the three functions?

Insert()? Delete()? Find()?

Consider

best and worst-case.

What are the inputs for best and worst-case?

Slide59

Implementations

BST Analysis:

Insert():

Slide60

Implementations

BST Analysis:

Insert():

Worst case:

Slide61

Implementations

BST Analysis:

Insert():

Worst case: O(n)

Slide62

Implementations

BST Analysis:

Insert():

Worst case: O(n).

What is this worst case?

Slide63

Implementations

BST Analysis:

Insert():

Worst case: O(n)

Best case:

Slide64

Implementations

BST Analysis:

Insert():

Worst case: O(n)

Best case: O

(1)

What is the general case here?

What does the runtime for a particular insert depend on

?

Height of the tree

Slide65

Height review

Height

Slide66

Height review

Height

In this class, we set the height of an empty tree to be equal to -1

Slide67

Height review

Height

In this class, we set the height of an empty tree to be equal to -1

This makes the height of a single node 0

Slide68

Height review

Height

In this class, we set the height of an empty tree to be equal to -1

This makes the height of a single node 0

How do you calculate the height of a large tree?

Slide69

Height review

Height

In this class, we set the height of an empty tree to be equal to -1

This makes the height of a single node 0

How do you calculate the height of a large tree?

Height = 1 + max(height(left),height(right))

Slide70

Implementations

BST Analysis:

Find():

Slide71

Implementations

BST Analysis:

Find():

Worst-case:

Slide72

Implementations

BST Analysis:

Find():

Worst-case: O(n)

Slide73

Implementations

BST Analysis:

Find():

Worst-case: O(n)

What is this case?

Slide74

Implementations

BST Analysis:

Find():

Worst-case: O(n)

What is this case?

When the tree is linear

Slide75

Implementations

BST Analysis:

Find():

Worst-case: O(n)

What is this case?

When the tree is linear

Best-case: O(1)

Slide76

Implementations

BST Analysis:

Find():

Worst-case: O(n)

What is this case?

When the tree is linear

Best-case: O(1)

When the item is the root

Slide77

Implementations

BST Analysis:

Find():

Worst-case: O(n)

What is this case?

When the tree is linear

Best-case: O(1)

When the item is the root

Generally, however: O(log n) when the tree is balanced

Slide78

Implementations

BST Analysis:

Delete():

Slide79

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Slide80

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Slide81

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Slide82

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Slide83

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Case 1: The key is a leaf in the tree

Slide84

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Case 1: The key is a leaf in the tree

Remove the pointer to that node

Slide85

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Case 1: The key is a leaf in the tree

Remove the pointer to that node

Case 2: The node has one child

Slide86

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Case 1: The key is a leaf in the tree

Remove the pointer to that node

Case 2: The node has one child

Replace that node with its child

Slide87

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Case 1: The key is a leaf in the tree

Remove the pointer to that node

Case 2: The node has one child

Replace that node with its child

Case 3: The node has two children

Slide88

Implementations

BST Analysis:

Delete():

What are some strategies for deleting?

Are there any cases where deleting is easy?

Case 0: The element is not in the data structure

Don’t change the data, possibly throw an exception

Case 1: The key is a leaf in the tree

Remove the pointer to that node

Case 2: The node has one child

Replace that node with its child

Case 3: The node has two children

What are some possible strategies?

Slide89

Implementations

Deleting nodes with 2 children

How do we delete 12?

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide90

Implementations

Deleting nodes with 2 children

How do we delete 12?

Can we replace 12 with one of it’s children?

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide91

Implementations

Deleting nodes with 2 children

How do we delete 12?

Can we replace 12 with one of it’s children?

Need to find candidate to replace 12

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide92

Implementations

Deleting nodes with 2 children

If a node has 2 children, then we can “delete” it by over writing the node with a different <key, value> pair

Slide93

Implementations

Deleting nodes with 2 children

If a node has 2 children, then we can “delete” it by over writing the node with a different <key, value> pair

In order to avoid changing the shape and doing too much work, it must be either the predecessor (the element just before it in sorted order) or the successor (the element just after it in sorted order)

Slide94

Implementations

Deleting nodes with 2 children

What are the predecessor and successor of 12?

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide95

Implementations

Deleting nodes with 2 children

What are the predecessor and successor of 12?

What is unique about these elements?

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide96

Implementations

Deleting nodes with 2 children

What are the predecessor and successor of 12?

What is unique about these elements?

They have at most one child! Easy deletion

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide97

Implementations

Deleting nodes with 2 children

What are the predecessor and successor of 12?

What is unique about these elements?

They have at most one child! Easy deletion

8

4

2

1

3

6

5

7

12

10

9

11

14

13

15

Slide98

Implementations

Deleting nodes with 2 children

What are the predecessor and successor of 12?

What is unique about these elements?

They have at most one child! Easy deletion

8

4

2

1

3

6

5

7

13

10

9

11

14

15

Slide99

Implementations

BST Analysis:

Delete():

Worst case(): O(n)

Slide100

Implementations

BST Analysis:

Delete():

Worst case(): O(n), finding the predecessor/successor takes time

Slide101

Implementations

BST Analysis:

Delete():

Worst case(): O(n), finding the predecessor/successor takes time.

What is this case?

Slide102

Implementations

BST Analysis:

Delete():

Worst case(): O(n), finding the predecessor/successor takes time.

What is this case?

Best case(): O(1) if we’re deleting the root from a degenerate tree

Slide103

Implementations

BST Analysis:

Delete():

Worst case(): O(n), finding the predecessor/successor takes time.

What is this case?

Best case(): O(1) if we’re deleting the root from a degenerate tree

“Degenerate” trees are those that are very unbalanced.

Slide104

Analysis

Height

Slide105

Analysis

Height

Many of our worst cases are when trees are poorly balanced

Slide106

Analysis

Height

Many of our worst cases are when trees are poorly balanced

Can we enforce this balance?

Slide107

Analysis

Height

Many of our worst cases are when trees are poorly balanced

Can we enforce this balance?

What are some possible balance conditions?

Slide108

Analysis

Height

Many of our worst cases are when trees are poorly balanced

Can we enforce this balance?

What are some possible balance conditions?

Number of elements on the left = number on right?

Slide109

Analysis

Height

Many of our worst cases are when trees are poorly balanced

Can we enforce this balance?

What are some possible balance conditions?

Number of elements on the left = number on right?

What we really care about though is the height of the tree

Slide110

Analysis

Height

Many of our worst cases are when trees are poorly balanced

Can we enforce this balance?

What are some possible balance conditions?

Number of elements on the left = number on right?

What we really care about though is the height of the tree

Height of the left = height on the right?

Slide111

Analysis

This doesn’t help much

Slide112

Analysis

This doesn’t help much

Need the definition to be recursive

Let height(left) = height(right) for all nodes

Slide113

Analysis

Now what’s wrong?

Slide114

Analysis

Now what’s wrong?

Only perfect trees (with 2

k

children) can exist

Slide115

Analysis

For each node in the tree, the height of its left and right

subtrees

can differ by at most one

Slide116

Analysis

For each node in the tree, the height of its left and right

subtrees

can differ by at most one

|(height(left) – height(right)|

<

1

Slide117

Analysis

For each node in the tree, the height of its left and right

subtrees

can differ by at most one

|(height(left) – height(right)|

<

1

This is the AVL property, and we can use it to create self balancing trees