Luleå University of Technology

LTU courses · Python · Algorithms

D0012E.

Algorithms and data structures, with proofs and measurements. Sorting with nothing but two stacks, finding three numbers where x + y = z in quadratic time, and a binary search tree that rebuilds parts of itself to stay shallow. Each lab was done in a group of three, with a written report for each.

Python Linked lists Recursion Divide and conquer Search trees Analysis

Course labs

L1
Sorting a stack
A stack on a linked list, and a sort that only uses two stacks and two extra values. We proved the worst case is n(3n − 1)/2 pushes, n(3n − 1)/2 pops and n(n − 1) comparisons, and it happens when the input is already sorted. The counts from our runs match it exactly.
L2
Recursion and divide and conquer
Do three of the numbers satisfy x + y = z? First in Θ(n³), then in Θ(n²) by keeping the numbers sorted as they arrive. Then the maximum subarray sum in O(n), by returning four values from each half. Only indexing, arithmetic and while loops were allowed.
L3
A tree that stays balanced
A binary search tree where no subtree may hold more than c of its parent's nodes. When one does, that part is rebuilt as a perfectly balanced tree. We measured the cost for six values of c on five kinds of input, up to 8,000 keys.

Try each lab

The numbers start in stack U and end sorted in stack R, smallest on top. Anything in the way goes back onto U. Step through it, or pick another input. Lab 1's code, ported to JavaScript.

U
R
x·
y·
0
Pushes
0
Pops
0
Comparisons
Worst case for these 8: n(3n − 1)/2 pushes

Our run with 4,000 numbers
InputPushesComparisonsTime
Random11,973,1167,980,7415.39 s
Sorted (the worst case)23,998,00015,996,00010.59 s
Reverse (the best case)7,9993,9990.0035 s

23,998,000 is exactly 4000 × (3 × 4000 − 1) / 2, the worst case we proved.

Type some different numbers. Both versions answer the same question. The second keeps the numbers it has seen sorted, so each new number needs one pass instead of a check against every pair.

Θ(n³)

Θ(n²)

Max subarray, O(n)

Insert keys one at a time. When a subtree gets more than c of its parent's nodes, that part is rebuilt perfectly balanced and flashes. A plain search tree with the same keys is shown in numbers next to it.

0
Keys
0
Height of D
0
Height of a plain tree
0
Rebuilds
0
Keys moved by rebuilds
Our run with 8,000 keys: work per insert (comparisons and keys moved)
InputPlain treec = 0.55c = 0.7c = 0.8c = 0.99
Random15.518.913.412.915.1
Ascending3,999.562.733.228.575.0
Almost sorted2,449.062.432.727.957.1

A low c rebuilds too often and a high c lets the tree get deep. Around 0.8 was cheapest in every input we tried.

The lab files as we handed them in. The demos above are ports of these.

What I learned

The worst case is not always the messy input.

For the stack sort, a sorted input is the slowest and a reversed one is the fastest. We proved the exact counts first, and the program then counted the same numbers to the last push.

Keeping something true saves a whole factor of n.

If the numbers seen so far stay sorted, each new number can be checked with two pointers in one pass. That turns Θ(n³) into Θ(n²).

Return more than you were asked for.

The maximum subarray becomes linear when each half also returns its total, its best start and its best end, so the two halves can be joined without looking at them again.

Expensive steps can be cheap on average.

Rebuilding a subtree costs a lot once, but it doesn't happen often. The constant c decides how often, and the measurements showed where the balance is.