Skip to content

Changing a number in a heap

storing

Level 2 of 3

Also called Decrease-key, Increase-key, Change-key and Priority update.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

The heap drawn was made by putting in 16, 29, 23, 47, 35, 40 and 31. Then 23 changes to 50. At which place does 50 stop?

123456716292347354031a16b29c23d47e35f40g31
the answer, and why

the answer is B

23 stands at place 3, over 40 and 31. 50 is bigger than both, so it swaps with the smaller, 31, down to place 7, which has no child.

  1. step 16 of 22

    16292347354031

    23 stands in place 3. The heap keeps a note of where each number stands, so this is one look and no comparison.

    comparisons: 0

  2. step 20 of 22

    16293147354050

    Swap them: 50 sinks below 31.

    comparisons: 2

  3. step 21 of 22

    16293147354050

    Nothing is below 50, so it stays.

    comparisons: 2

23 at place 3 becomes 50, sinks below 31, the smaller child, and stops. 2 comparisons.

if you chose another

  • That swaps with 40, the first child. A sink swaps with the smaller child, 31.
  • That leaves 50 over 40 and 31, both smaller than it.
  • Place 2 is not below place 3. Place 3's children are places 6 and 7.

question 2

On an empty heap, put 6, 7, 8, 9 and 10, change 10 to 1, then change 9 to 2. How many comparisons does the site count?

the answer, and why

the answer is D

The puts compare 4 times. 1 climbs from place 5 to the top, 2 comparisons. 2 climbs from place 4 above 6, then stops under 1, 2 more.

  • 627189100

    after the puts; 10 at place 5 becomes 1 and climbs 2 levels

  • 16897

    after that; 9 at place 4 becomes 2, climbs above 6 and stops under 1

The heap after the puts, then after 10 becomes 1. In all, 4 + 2 + 2 = 8.

if you chose another

  • That counts the choices of climb or sink, 1 against 10 and 2 against 9. They compare new with old, so the site leaves them out.
  • That counts the puts only. The changes climb too.
  • That leaves out the comparison that stops 2 under 1.

question 3

Each list puts 11 to 17 into an empty heap, smallest first, then makes one change; 11->20 means change 11 to 20. Which list makes the most comparisons?

the answer, and why

the answer is A

With 11->20, the top made 20 sinks two levels, comparing both children at each, 4 comparisons; the others cost 2. With the puts' 6, 11->20 costs 6 + 4 = 10, the others 6 + 2 = 8.

11121314151617111122133144155166177
Places 1, 7, 4, 3 change: 11->20, 17->1, 14->2, 13->30.

if you chose another

  • 17 is at the last place. Made 1, it climbs two levels, one comparison each.
  • 14 is at place 4. Made 2, it climbs two levels too, one comparison each.
  • 13 is at place 3. Made 30, it sinks one level, then stops at place 6, which has no child.

question 4

A route search puts 1,000 distances in, smallest first, then makes 1,000 changes, each picking a distance at random and making it the smallest. Which way of keeping them makes the fewest comparisons and looks?

the answer, and why

the answer is C

With a note, finding is one read. A change made the smallest climbs about 8 levels on average, 7,987 for the changes alone (8,986 with the puts' 999), at most 9,000.

this tests how it compares, on the learn page

if you chose another

  • With no note, finding alone averages 500.5 looks a change, 500,500 in all.
  • It finds by the same looks, 500.5 on average.
  • Halving finds it in few looks, but it then walks to the front, 499.5 steps a change on average.

pen and paper

question 5about 10 minutes

Put 4, 5 and 6 into an empty heap, smallest first. Then make three changes, to 3, then 2, then 1, each picking the number it changes at random.

  1. What does a change cost at places 1, 2 and 3?
  2. Why does each change cost the same on average, whatever came before?
  3. What does the whole job cost on average? Check it against section 4 of the learn page.
a hint

Each change makes its number the smallest, so it climbs to the top from wherever it stands.

a worked answer

Place 1 costs 0, places 2 and 3 cost 1 each. Each place is as likely at every change, so a change averages 2 over 3. With the puts' 2, the job averages 2 + 3 × 2/3 = 4. Section 4's formula gives 2 + 0 + 1 + 1, also 4.

placelevels to the toppicked
101 time in 3
211 time in 3
311 time in 3
a change on average2 over 3
The heap of 3, and what a change made the smallest costs at each place.
Each change picks a number at random and makes it the smallest. Why do nn such changes cost Θ(n log⁡2 n)\Theta(n \,\log_2\, n) on average?
  1. the move: every place as likely

    the line:
    each place is pickedwith chance 1n\begin{gathered} \text{each place is picked} \\ \text{with chance } \frac{1}{n} \end{gathered}
    first changesecond changecomparisons
    the topthe top1
    the topplace 22
    place 2the top2
    place 2place 23
    average2
    Put 3 and 4, then two changes, each picking the top or place 2, so 4 picks, each as likely.

    why it is allowed: Each number held is as likely, and each has a place of its own, so each place is as likely, whatever the changes before did.

  2. the move: add up the places

    the line:
    A=(n−1)+n(c(1)×1n+⋯+c(n)×1n)\begin{aligned} A = {} & (n - 1) \\ & + n\bigl(c(1) \times \tfrac{1}{n} + \dots + c(n) \times \tfrac{1}{n}\bigr) \end{aligned}
    placelevels to the top
    10
    21
    31
    42
    in all4
    A heap of 4. A change averages 4 times a quarter, which is 1, and four changes give 4.

    why it is allowed: AA is the average comparisons, and c(p)c(p) the climb from place pp. The puts make n−1n - 1. By line 1, a change averages each climb times 1n\frac{1}{n}. Averages add, by line 2 of question 2 of the page expected cost, so nn changes give nn times that.

  3. the move: cancel

    the line:
    A=(n−1)+c(1)+⋯+c(n)A = (n - 1) + c(1) + \dots + c(n)

    why it is allowed: By line 2, as nn times 1n\frac{1}{n} is 1. For 4, 3 + 0 + 1 + 1 + 2 is 7, the average over all 256 picks.

  4. the move: name the levels

    the line:
    A=(n−1)+⌊log⁡2 1⌋+⋯+⌊log⁡2 n⌋\begin{aligned} A = {} & (n - 1) \\ & + \lfloor \log_2\, 1 \rfloor + \dots + \lfloor \log_2\, n \rfloor \end{aligned}

    why it is allowed: By line 2 of the question on the dearest job, c(p)c(p) is place pp's levels, ⌊log⁡2 p⌋\lfloor \log_2\, p \rfloor by line 7 of that question.

  5. the move: the changes alone

    the line:
    A−(n−1)=⌊log⁡2 1⌋+⋯+⌊log⁡2 n⌋\begin{aligned} A - (n - 1) = {} & \lfloor \log_2\, 1 \rfloor \\ & + \dots + \lfloor \log_2\, n \rfloor \end{aligned}

    why it is allowed: By line 4, taking away the puts' n−1n - 1. For 4, 4.

  6. the move: count the swaps

    the line:
    on average the changes swap⌊log⁡2 1⌋+⋯+⌊log⁡2 n⌋times\begin{gathered} \text{on average the changes swap} \\ \lfloor \log_2\, 1 \rfloor + \dots + \lfloor \log_2\, n \rfloor \\ \text{times} \end{gathered}

    why it is allowed: By line 5, the changes compare that many times. Each comparison of a change made the smallest swaps, by line 2 of the question on the dearest job, and no put does. For 4, 4.

  7. the move: count level by level

    the line:
    A=(n−1)+(n−2+1)+(n−4+1)+…+(n−2h+1)\begin{aligned} A = (n - 1) &+ (n - 2 + 1) \\ &+ (n - 4 + 1) + \dots \\ &+ (n - 2^h + 1) \end{aligned}
    • 0112222

      1 down or more, 6 places

    • 0112222

      2 down or more, 4 places

    A heap of 7, each place showing its levels, 10 in all.
    place1 down or more2 down or morelevels
    1000
    2101
    3101
    4112
    5112
    6112
    7112
    in all6410
    Each row adds to its place's levels, and the columns give the brackets, 6 and 4.

    why it is allowed: By line 4, counting each place once for each level it lies below the top. Level kk starts at place 2 to the power kk. Lines 5 and 7 of the box on halving, inside the box under line 4 of the question on the dearest job, show it with pp for nn. So the places at least kk down run from there to nn.

  8. the move: gather

    the line:
    A=(n−1)+h(n+1)−(2+4+⋯+2h)\begin{aligned} A = {} & (n - 1) + h(n + 1) \\ & - (2 + 4 + \dots + 2^h) \end{aligned}

    why it is allowed: Each of the hh brackets holds n+1n + 1, less a power of 2. For 4, hh is 2, and 3 + 2 × 5, less 2 + 4, is 7.

  9. the move: add 1 and take it away

    the line:
    A=(n−1)+h(n+1)−((1+2+⋯+2h)−1)\begin{aligned} A = {} & (n - 1) + h(n + 1) \\ & - \bigl((1 + 2 + \dots + 2^h) - 1\bigr) \end{aligned}

    why it is allowed: This changes nothing, and the bracket now starts at 1. For 4, 2 + 4 is 1 + 2 + 4, less 1.

  10. the move: use the doubling sum, h for n

    the line:
    A=(n−1)+h(n+1)−((2h+1−1)−1)\begin{aligned} A = {} & (n - 1) + h(n + 1) \\ & - \bigl((2^{h + 1} - 1) - 1\bigr) \end{aligned}
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

      the line:
      S=1+2+4+⋯+2nS = 1 + 2 + 4 + \dots + 2^n

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The box under this line shows a doubling sum from 1 is one less than its next term. For 4, 1 + 2 + 4 is 7, one less than 8.

  11. the move: take away its 1

    the line:
    A=(n−1)+h(n+1)−(2h+1−2)\begin{aligned} A = {} & (n - 1) + h(n + 1) \\ & - (2^{h + 1} - 2) \end{aligned}

    why it is allowed: One less, then 1 less again, is two less. For 4, 2 + 4 is 6.

  12. the move: tidy

    the line:
    A=(n−1)+(n+1)h−2h+1+2A = (n - 1) + (n + 1)h - 2^{h + 1} + 2

    why it is allowed: Taking away 2 less than a number takes the number and gives 2 back. For 4, 3 + 10 + 2 less 8 is 7.

  13. the move: a ceiling from the dearest job

    the line:
    A≤2n log⁡2 nA \le 2n\,\log_2\, n

    why it is allowed: No pick costs more than the dearest job, by line 8 of the question on it, and line 11 there caps that. So neither does the average.

  14. the move: read it as a bound

    the line:
    A∈O(n log⁡2 n)A \in O(n \,\log_2\, n)

    why it is allowed: By line 13, with the fixed multiple 2.

  15. the move: the last level starts in the row

    the line:
    A≥(n−1)+(n+1)h−2n+2A \ge (n - 1) + (n + 1)h - 2n + 2

    why it is allowed: Now a floor, AA above a fixed part of n log⁡2 nn \,\log_2\, n. By line 12. 2h2^h is at most nn, by line 5 of the box on halving inside the box under line 4 of the question on the dearest job. So 2h+12^{h + 1} is at most 2n2n, and taking away 2n2n takes at least as much. At 6, AA is 13 and the right side 9.

  16. the move: gather

    the line:
    A≥(n+1)h−n+1A \ge (n + 1)h - n + 1

    why it is allowed: n−1n - 1, less 2n2n, plus 2, leaves 1 minus nn. At 6, 7 × 2, less 6, plus 1, is 9.

  17. the move: drop what is left over

    the line:
    A≥n(h−1)A \ge n(h - 1)

    why it is allowed: Line 16 is n(h−1)n(h - 1) plus h+1h + 1, which is more than 0. At 6, 6 × 1 + 3 is 9.

  18. the move: a quarter of h + 1

    the line:
    A≥n(h+1)4 for n≥4A \ge \frac{n(h + 1)}{4} \text{ for } n \ge 4

    why it is allowed: From 4 on, hh is at least 2, and a quarter of h+1h + 1 is at most h−1h - 1. With hh at 2, three quarters against 1; each 1 more on hh adds a quarter to the first and 1 to the second.

  19. the move: a simpler floor

    the line:
    A≥n log⁡2 n4 for n≥4A \ge \frac{n \,\log_2\, n}{4} \text{ for } n \ge 4

    why it is allowed: log⁡2 n\log_2\, n is less than h+1h + 1. At 4, lines 19, 18 and 17 give 2, 3 and 4, and AA is 7.

  20. the move: read it as a bound

    the line:
    A∈Ω(n log⁡2 n)A \in \Omega(n \,\log_2\, n)

    why it is allowed: By line 19, with the fixed multiple a quarter.

  21. the move: ceiling and floor together

    the line:
    A∈Θ(n log⁡2 n)A \in \Theta(n \,\log_2\, n)

    why it is allowed: Lines 13 and 19 bound AA by the same growth, n log⁡2 nn \,\log_2\, n, with the fixed multiples 2 and a quarter.

try it on small inputs: over every pick of 4 changes, 256 in all, the average is A=7A = 7

this tests where the formula comes from, on the learn page

what a full answer includes
  • 0 at place 1, and 1 at places 2 and 3.
  • Each place is as likely at every change.
  • 4, as the formula gives at 3.

question 6about 8 minutes

A heap with a note of places holds 31 numbers, a full tree of 5 levels, numbered 0 to 4.

  1. At most how many comparisons can one change that makes a number smaller cost?
  2. And one that makes a number bigger?
  3. With no note, how many looks find a number at random, on average?
a hint

Halve 31 again and again, rounding down, until you reach 1.

a worked answer

31 halves to 15, 7, 3 and 1, four halvings, so place 31 lies 4 levels down. Made smaller, a change costs at most 4, one comparison a level. Made bigger, at most 4 rounds of 2, which is 8; in a full tree the top made the biggest reaches it. With no note, a number at random costs 32 over 2, 16 looks.

numberlog× 2+ 1214283164325314.954153.91372.81231.581100
31 halved, rounding down, reaches 1 in 4 steps, so place 31 is 4 levels down.
One change that makes a number bigger. At most how many comparisons?
  1. the move: a number made bigger sinks

    the line:
    a number made no smallersinks\begin{gathered} \text{a number made no smaller} \\ \text{sinks} \end{gathered}

    why it is allowed: It got no smaller, so the number above it is still no bigger; only a number below can be smaller. The counted job makes no such change.

  2. the move: price a round

    the line:
    a round compares each childwith the smaller so far,then swaps or stops\begin{gathered} \text{a round compares each child} \\ \text{with the smaller so far,} \\ \text{then swaps or stops} \end{gathered}
    1. step 22 of 31

      8331970452652

      8 stands in place 1. The heap keeps a note of where each number stands, so this is one look and no comparison.

      comparisons: 0

    2. step 26 of 31

      19339070452652

      Swap them: 90 sinks below 19.

      comparisons: 2

    3. step 29 of 31

      19332670459052

      Swap them: 90 sinks below 26.

      comparisons: 4

    4. step 30 of 31

      19332670459052

      Nothing is below 90, so it stays.

      comparisons: 4

    The watch page's second example. 8 at the top becomes 90, sinks below 19, then 26, and stops at place 6. 4 comparisons, 2 swaps.
    roundcomparesthen
    133 with 90, 19 with 33swap 90 with 19
    226 with 90, 52 with 26swap 90 with 26
    no roundno child below 90it stays
    Each round of that change. Two rounds of 2, then none.

    why it is allowed: The number itself is the smaller so far at first. So a round makes at most 2 comparisons.

  3. the move: count the rounds

    the line:
    D=2⌊log⁡2 n⌋D = 2\lfloor \log_2\, n \rfloor

    why it is allowed: DD is a ceiling. A round needs a child below, so a sink has at most the last place's levels, by line 4 of the question on the dearest job. Each costs at most 2, by line 2. For 7, 2 × 2 is 4, which the second example reaches.

  4. the move: drop the rounding

    the line:
    D≤2 log⁡2 nD \le 2\,\log_2\, n

    why it is allowed: By line 3, as rounding down never makes a number bigger.

  5. the move: read it as a bound

    the line:
    D∈O(log⁡2 n)D \in O(\log_2\, n)

    why it is allowed: By line 4, with the fixed multiple 2.

try it with a small nn: at n=7n = 7, line 3 gives D=4D = 4, which you can work out by hand

this tests how it compares, on the learn page

what a full answer includes
  • 4, one comparison a level.
  • 8, two comparisons in each of 4 rounds.
  • 16 looks on average.