CS 234 Module 9: Improvements

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/79

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:38 AM on 7/28/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

80 Terms

1
New cards

Average case

A calculation of running time in which the representative value for each instance size k is determined by taking the sum over all instances of that size of the product of the probability of the instance and its running time

2
New cards

What is the formula for average-case running time?

I∈S Pr[I] ∙ Cost(I)

  • I ∈ S: Possible data items

  • Pr[I]: Probability of a particular data item being sought

  • Cost(I): Cost for searching for a particular data item

3
New cards

Uniform Distribution

  • All probabilities are equal

  • Probability for each item = 1/n

  • For successful search, n is the number of data items stored

  • For unsuccessful search, n is the number of data items that might be sought

4
New cards

Cost of Finding a Data Item

At most n data items need to be examined to find any data item, that is: Cost[I] ≤ n for each data item I

5
New cards

Upper Bound of Linear Search

Total = O(n)

I∈S Pr[I] ∙ Cost(I) ≤ ∑I∈S 1/n ∙ n ∈ O(n)

6
New cards

Lower Bound of Linear Search

Total cost: Ω(n),

using the fact that there are at least ⌊n/2⌋ data items in the second half of the array, so for each I∈T, Cost(I) ≥ ⌊n/2⌋

I∈S Pr[I] ∙ Cost(I) ≥ ∑I∈T Pr[I] ∙ Cost(I) = ∑I∈T 1/n ∙ Cost(I) ≥ ∑I∈T 1/n ∙ ⌊n/2⌋ ≥ ⌊n/2⌋ ∙ 1/n ∙ ⌊n/2⌋ ∈ Ω(n)

7
New cards

Cost of Searching A Node in Tree

Cost[I] ≤ log n

Cost of searching is linear in tree depth; since the height of the tree is logarithmic, no data item is in a node of depth greater than logarithmic.

8
New cards

Upper Bound of Binary Search

Total = O(log n)

I∈S Pr[I] ∙ Cost(I) ≤ ∑I∈S 1/n ∙ log n ∈ O(log n)

9
New cards

Lower Bound of Binary Search

Total cost: Ω(log n),

Using the line we drew, each node below the line is at least halfway down the tree; each data item in T requires at least ⌊(log n)/2⌋ time, so Cost(I) ≥ ⌊(log n)/2⌋

Since at least half the data is in T, |T| ≥ ⌊(log n)/2⌋.

Multiplying roughly half by roughly half = roughly a quarter, which is in Ω(log n)

I∈S Pr[I] ∙ Cost(I) ≥ ∑I∈T Pr[I] ∙ Cost(I) = ∑I∈T 1/n ∙ Cost(I) ≥

I∈T 1/n ∙ ⌊(log n)/2⌋ ≥ ⌊n/2⌋ ∙ 1/n ∙ ⌊(log n)/2⌋ ∈ Ω(log n)

10
New cards

What is the Worst-Case Cost of Linear Search?

θ(n)

11
New cards

What is the Worst-Case Cost of Binary Search?

θ(log n)

12
New cards

Improved Binary Search

At each phase of binary search in array A:

  • Consider positions Low through High

  • Mid = Low + 0.5(High - Low)

  • At each phase, Data is compared to A[Mid]

  • If Data is bigger:

    • Update Low = Mid + 1

  • Otherwise:

    • Update High = Mid - 1

13
New cards

Interpolation Search

At each phase of binary search in array A:

  • Consider positions Low through High

  • Mid = Low + (Data-A[Low])/(A[High]-A[Low]) ∙ (High - Low)

    • Low + weight x size of interval

  • At each phase, Data is compared to A[Mid]

  • If Data is bigger:

    • Update Low = Mid + 1

  • Otherwise:

    • Update High = Mid - 1

14
New cards

What is the average-case runtime of Interpolation Search?

O(loglog n), when the data is evenly spaced

15
New cards

What is the worst-case runtime of Interpolation Search?

θ(n)
In the worst case, we could choose a value of Mid that splits the interval into a piece of size one and a piece of size one less than the original interval, and end up having to search in the bigger piece.
This can happen in every phase

16
New cards

Heuristic

A method that may not guarantee bounds on running time or correctness

17
New cards

Uniform Distribution

A probability distribution in which all probabilities are equal

18
New cards

What is the calculation for the average-case cost of a successful search, given that:

  • we have a uniform distribution, and

  • the cost of finding an item in position 𝔦 is the number of items that need to be read (𝔦 + 1)

(n+1)/2

19
New cards

What is the average cost of a search if the data items are stored in the order ape, cat, dog, and pig?

Cost(Ape)*Pr(Ape) + Cost(Cat)*Pr(Cat) + Cost(Dog)*Pr(Dog) + Cost(Pig)*Pr(Pig)

= 1 × 1/8 + 2 × 1/4 + 3 × 1/8 + 4 × 1/2
= 1/8 + 1/2 + 3/8 + 2
= 3

<p>Cost(Ape)*Pr(Ape) + Cost(Cat)*Pr(Cat) + Cost(Dog)*Pr(Dog) + Cost(Pig)*Pr(Pig)</p><p>= 1 × 1/8 + 2 × 1/4 + 3 × 1/8 + 4 × 1/2 <br>= 1/8 + 1/2 + 3/8 + 2 <br>= 3</p>
20
New cards

How does cost change in linear search on an array?

The cost of linear search in slot 0 is 1, and increases by 1 as slot number increases by 1

21
New cards

What strategy should be used to obtain the smallest worst-case cost for linear search?

Put the items in nonincreasing order of probability. Doing so results in higher probabilities being multiplied by smaller costs.

22
New cards

Self-organizing Heuristic

Use information about past accesses to rearrange data items

23
New cards

Move to Front Herustic

Based on the assumption that if a data item is accessed, it is likely to be accessed again. Rearrangements are hoped to help bring down the cost of future searches of searched items.

  • Linear search for the item

  • After a successful search, move the item that was sought to the front of the list

  • Consequently, each data item before that sought item is moved one position closer to the back of the list

  • Most-searched element moves to the front quickly, slipping back each time another item is accessed

  • Least-searched element moves one closer to the back each time an element after it is accessed

24
New cards

Transpose Heuristic

A less disruptive heuristic than Move to Front

  • Linear search for the item

  • After a successful search, exchange the found data item with the preceding data item, if any

  • The most-searched element moves slowly to the front, and the least-used element moves slowly to the back

25
New cards

When does Transpose outperform Move to Front?

Transpose does better than Move to Front unless:

  • there are only two items

  • all items are equally probable to be sought

26
New cards

Digital Data

A way of ADTs treating data items, using functions that extract information from data items

27
New cards

Bit Vector

A data structure consisting of a single bit for each data item, taken from a fixed range of integers. Data items are stored as 1s.

<p><span>A data structure consisting of a single bit for each data item, taken from a fixed range of integers. Data items are stored as 1s.</span></p>
28
New cards

What is the runtime for search in a bit vector?

It takes constant time due to random access.

29
New cards

Hashing

A general way of mapping keys to integers in the range, aka distributing keys into buckets.

30
New cards

What does all methods of hashing have in common?

Each method specifies a hash function ⨍, and a method for collision resolution.

31
New cards

Hash Function ⨍

Maps keys (or data items) to buckets.

Each key is mapped to a value in the range from 0 to N-1, where N is the number of buckets

32
New cards

Collision

Occurs when 2 different keys are mapped by the hash function ⨍ to the same bucket

33
New cards

Types of Conflict Resolution

Separate chaining and open addressing.

34
New cards

Separate Chaining

Make each bucket big enough to store all data items that are mapped to it; by storing all data items k with ⨍(k) = i in bucket i

35
New cards

Algorithm for ADT Dictionary CREATE for Hashing

Initializes any data structure (typically an array) used in hashing.

36
New cards

Algorithm for ADT Dictionary IS_EMPTY for Hashing

Require checking all array entries, unless an implementation includes a variable used to keep track of the number of data items stored.

37
New cards

Algorithm for ADT Dictionary LOOK_UP for Hashing

Varies substantially depending on the type of hashing.

38
New cards

Algorithm for ADT Dictionary ADD for Hashing

Consists of the same algorithm as LOOK_UP, followed by either

  • replacing the element (if a data item with the key being sought has been found), or

  • inserting the new data item in the appropriate location

39
New cards

Algorithm for ADT Dictionary DELETE for Hashing

Consists of the same algorithm as LOOK_UP, but unlike in the case of adding a data item, additional steps might be required to ensure that future LOOK_UP operations will work properly.

40
New cards

How to measure the cost of search for a data item?

As the number of probes needed to find data item. Where each probe is the examination of a data item or location

41
New cards

Load Factor

The ratio of the number of data items to the number of buckets

The average number of probes needed for a search is compared to this for a particular type of hashing

42
New cards

Bit

0 or 1

43
New cards

What are the goals for hash functions?

  1. Quick to compute, since we need to compute the function every time we search for a data item

  2. Spread keys evenly among buckets to reduce collisions

44
New cards

Modular Arithmetic

a system of arithmetic where numbers wrap around after reaching a certain value, called the modulus.

<p>a system of arithmetic where numbers <strong>wrap around</strong> after reaching a certain value, called the <strong>modulus</strong>.</p>
45
New cards

Hash Function Example 1: f(k) = ⌈log N⌉ Leading/Trailing bits in k as binary number

  • A quick-to-compute function by extracting bits using division

  • Doesn’t spread keys evenly among buckets.

    • If keys are strings, this hash function may categorize strings by the first/last letter. Not all letters appear with equal frequency in a language, resulting in an uneven distribution of keys in buckets

46
New cards

Hash Function Example 2: Modular Arithmetic

  • f(k) = k mod N

  • A quick-to-compute function

  • Not necessarily spread keys evenly among buckets

    • If N (number of buckets) is a power of 2, this is the same as Example 1 with strings as keys

47
New cards

Hash Function Example 3: f(k) = (ak + b) mod N for each value of k

Uses randomness to try to break up any patterns in the data.
a and b are selected at random, and then the same values of a and b are reused

48
New cards

Goals of Hashing

  1. The hash function distributes keys among buckets as evenly as possible.

  2. The hash function does not take a lot of time to compute.

  3. Collision resolution minimizes the average number of probes required.

  4. Collision resolution does not take a lot of time to compute.

49
New cards

Probes

The examination of a data item or location in hashing

50
New cards

Dictionary Hashing using Separate Chaining Linked List Implementation

Data structures:

  • Array in which each slot stores the null pointer or a pointer to a linked list of nodes, each storing a data item and a pointer Next to the next linked node in the list, if any, and otherwise the null pointer

Definition:

  • The linked list for slot i forms bucket i.

<h4 id="3bb3c7fe-2fba-413a-b8df-1d88d5448607" data-toc-id="3bb3c7fe-2fba-413a-b8df-1d88d5448607" collapsed="false" seolevelmigrated="true" style="text-align: left;"><span style="line-height: 1.4em; font-size: inherit;"><strong>Data structures:</strong></span></h4><ul><li><p>Array in which each slot stores the null pointer or a pointer to a linked list of nodes, each storing a data item and a pointer <em>Next</em> to the next linked node in the list, if any, and otherwise the null pointer</p></li></ul><h4 id="17b36487-b247-48d8-ad1f-e2b48df886d4" data-toc-id="17b36487-b247-48d8-ad1f-e2b48df886d4" collapsed="false" seolevelmigrated="true" style="text-align: left;"><span style="line-height: 1.4em; font-size: inherit;"><strong>Definition:</strong></span></h4><ul><li><p>The linked list for slot i forms bucket i.</p></li></ul><p></p>
51
New cards

LOOK_UP(D, Key) under Dictionary Hashing using Separate Chaining Linked List Implementation

Worst-case cost: θ(n)

  1. Use the hash function on Key to determine which slot Key belongs in

  2. Linear search of linked nodes until Key or the last linked node is found

    1. Return the element stored with Key

    2. Return false otherwise

52
New cards

ADD(D, Key, Element) under Dictionary Hashing using Separate Chaining Linked List Implementation, where Key is guaranteed to use a key that isn’t used for any data item.

Worst-case cost: θ(1)

  1. Use the hash function on Key to determine which slot Key belongs in

  2. Allocate a new linked node Node storing Key and Element

  3. Add Node to the beginning of the linked list

53
New cards

DELETE(D, Key) under Dictionary Hashing using Separate Chaining Linked List Implementation

Worst-case cost: θ(n)

  1. Use the hash function on Key to determine which slot Key belongs in

  2. Linear search of linked nodes until Key is found

  3. Remove the node storing Key

54
New cards

Worst-case cost of CREATE() under Dictionary Hashing using Separate Chaining Linked List Implementation

The array can be allocated in constant time. Each slot can be initialized in constant time, for a total in θ(N) time.

N = number of buckets

55
New cards

Worst-case cost of CREATE() under Dictionary Hashing using Separate Chaining Linked List Implementation

Each of the N slots can be read in constant time, for a total in θ(N) time.

N = number of buckets

56
New cards

ADD(D, Key, Element) under Dictionary Hashing using Separate Chaining Linked List Implementation, where Key might be used by data item that is currently stored in the ADT.

Worst-case cost: θ(n)

  1. Use the hash function on Key to determine which slot Key belongs in

  2. Linear search of linked nodes until Key or the last linked node is found

    1. If Key is found, update the pair to store Element

    2. If Key is not found,

      1. Allocate a new linked node Node storing Key and Element

      2. Add Node to the beginning of the linked list

57
New cards

What counts as a probe for separate chaining?

Each read of a slot in the array and each read of a linked node counts as a probe.

58
New cards

Open Addressing

  • Each bucket is associated with a slot in an array.

  • Store at most one item in each bucket.

  • A data item k might not be stored in the bucket f(k).

  • ADD, LOOK_UP, and DELETE search through slots in a particular order for a particular data item.

59
New cards

Probe Sequence for a Data Item

The order of buckets the algorithm examines until it finds the key or an empty slot.

60
New cards

Algorithm of LOOK_UP for a Probe Sequence

  1. Checking buckets in the order of the probe sequence

  2. In a successful search: search stops when a data item is found

  3. In an unsuccessful search: search stops as soon as an empty bucket is found

61
New cards

Algorithm of ADD for a Probe Sequence

  1. Checking buckets in the order of the probe sequence

  2. If a data item with the sought key is found:

    1. Stops search

    2. Update element

  3. If the key is not found:

    1. Allocate a new data item

    2. Insert the new item in the first empty bucket we found

62
New cards

Why the Algorithm of DELETE for a Probe Sequence is so complex and outside of the scope of this course.

When a data item α is stored later in the probe sequence after a data item is deleted. If the deleted item leaves an empty bucket, a future search for α will stop at that empty bucket rather than continue searching for α.

63
New cards

What are the different methods of hashing that uses open addressing by specifying different rules about how to define the probe sequences for data items?

  • Linear probing

  • Quadratic probing

  • Double hashing

64
New cards

Linear Probing

When adding a new data item with key κ,

  1. if slot ⨍(κ) is full

  2. we look at the next slot

  3. and the slot after that

  4. and so on…
    (including wrapping back to the beginning of the array ~ mod N)

  5. stopping when we found an empty slot

  6. Placing the new data item in the slot

Probe Sequence for κ: ⨍(κ), (⨍(κ)+1) mod N, (⨍(κ)+2) mod N,(⨍(κ)+3) mod N, so on

<p>When adding a new data item with key κ,</p><ol><li><p>if slot ⨍(κ) is full</p></li><li><p>we look at the next slot</p></li><li><p>and the slot after that</p></li><li><p>and so on… <br>(including wrapping back to the beginning of the array ~ mod N)</p></li><li><p>stopping when we found an empty slot</p></li><li><p>Placing the new data item in the slot</p></li></ol><p>Probe Sequence for κ: ⨍(κ), (⨍(κ)+1) mod N, (⨍(κ)+2) mod N,(⨍(κ)+3) mod N, so on</p><p></p>
65
New cards

Clustering

A phenomenon in which once a collision occurs, more collisions tend to pile up in the same place, like a snowball growing bigger and bigger.

<p><span>A phenomenon in which once a collision occurs, more collisions tend to pile up in the same place, like a snowball growing bigger and bigger.</span></p>
66
New cards

Quadratic Probing

Using squares for ‘step’ sizes, the probe sequence for κ:
⨍(κ), (⨍(κ)+1²) mod N, (⨍(κ)+2²) mod N, (⨍(κ)+3²) mod N, so on

67
New cards

What is the tradeoff in choosing probe sequences?

  • Making probe sequences differ for different keys might reduce the chances of clustering.

  • Making probe sequences differ for different keys might add to the cost of determining sequences.

68
New cards

Double Hashing

Forms different probe sequences even for keys where ⨍ maps to the same bucket by using a second hash function 𝔤(κ), the probe sequence for κ:

⨍(κ), (⨍(κ)+𝔤(k)) mod N, (⨍(κ)+2𝔤(k)) mod N, (⨍(κ)+3𝔤(k)) mod N, so on

69
New cards

How to choose 𝔤(k) to prevent some buckets from never appearing in any probe sequence?

  • Make the number of slots (N) a prime number

  • If and are not relatively prime, then and N have a common divisor d≠1.

  • If there is such a d, then
    (⨍(κ)+(N/d) × 𝔤(k)) mod N = (⨍(κ)+ N × 𝔤(k)/d) mod N = ⨍(κ)

  • Our probe sequence starts to repeat after only N/d steps, so we don't reach all the buckets

70
New cards

What are the differences of Open Addressing to Separate Chaining

Open Addressing

  • uses array directly

  • no extra space for pointer

  • losing the flexibility of linked data

  • fixed number of slots and a limit on the number of data items we can store

71
New cards

How to fix the problem where deleting an item leaves a gap, where linear search might stop before all data items in the probe sequence are checked?

We could solve that problem by viewing each slot as storing compound data, where one of the fields is a single bit that indicates whether or not the slot had ever contained a data item. That would us allow us to refill the slot, and in the interim to ensure that searches would not stop prematurely.

72
New cards

Bit Vector Algorithm

  • Initializes each entry in the bit vector to 0.

  • For each data item i, enters a 1 in index i.

  • Creates a sequence by linear search through the slots, adding the index of each slot that contains a 1.

73
New cards

What is the worst-case running time of bit vector algorithm?

θ(n + r)

n = number of data items

r = range

74
New cards

ADT Bucket

Supports adding data items and returning contents in a bucket.

75
New cards

ADD_TO_BUCKET(B, Data)

Adds Data to bucket B

76
New cards

RETURN_CONTENTS(B)

  • Returns all data items in bucket B

  • Empties all contents in B

77
New cards

Bucket Sort

Using an array of r buckets named B0 through Br-1:

  • Initializes each entry in an array of length r to empty in θ(r).

  • For each data item (i, Value), executes ADD_TO_BUCKET(Bi, Value) in θ(n).

  • For each bucket in order, concatenates the results of RETURN_CONTENTS(Bi) in θ(n + r).

<p>Using an array of <strong>r </strong>buckets named B<sub>0</sub> through B<sub>r-1</sub>:</p><ul><li><p>Initializes each entry in an array of length<strong> </strong>r to empty in θ(r).</p></li><li><p>For each data item (i, Value), executes <span style="line-height: 1.4em; font-size: inherit; color: inherit;"><em>ADD_TO_BUCKET</em></span><em>(B<sub>i</sub>, Value)</em> in θ(n).</p></li><li><p>For each bucket in order, concatenates the results of <span style="line-height: 1.4em; font-size: inherit; color: inherit;"><em>RETURN_CONTENTS</em></span><em>(B<sub>i</sub>) </em>in θ(n + r).</p></li></ul><p></p>
78
New cards

What are the problems that arise with large values of r?

  1. If r > n log n, there is no improvement over Heapsort’s runtime of θ(n log n)

  2. If r is really large as f(n), the number of bits per value (log r) is very large.

    • Finding a slot will not be considered a θ(1) operation

79
New cards

Stable Sort

A sorting algorithm that does not change the order of equal-valued items.

80
New cards

A Pass

One complete traversal of the data during which the algorithm performs its intended comparisons and, if necessary, swaps or moves elements.
e.g.

  • Sort by day of the month

  • Sort by month

  • Sort by year