DSA1 | Week 10
Heaps
insert(S,x): insert an element x into S.
Goal
findmin(S): report the smallest element in S.
deletemin(S): remove the smallest element in S.
Maintain a set S with support for the following operations.
insert(S,x): O(1)
Goal
findmin(S): O(n)
deletemin(S): O(n)
Maintain a set S with support for the following operations.
insert(S,x): O(n)
Goal
findmin(S): O(1)
deletemin(S): O(1)
Maintain a set S with support for the following operations.
insert(S,x): O(log n)
Goal
findmin(S): O(1)
deletemin(S): O(log n)
Maintain a set S with support for the following operations.
Intuition
For findmin to happen in O(1) time,
the minimum element needs to be in
an easy-to-access location.
For deletemin to happen in O(log n) time,
the second minimum element needs to be
reasonably easy to find.
how hard is it to find the secondmin?
very easy
tedious
how hard is it to find the secondmin?
too organized
too messy
🔑 Idea
Each element has two pointers
to two other elements.
These pointers are hints
within the data structure to
help you navigate the data.
In particular,
the global min points to two locations.
How can we ensure
that both are candidates for secondmin?
Invariant
If an element (a) points to another one (b),
directly or indirectly,
then a is smaller than b.
Invariant
If an element (a) points to another one (b),
directly or indirectly,
then a is smaller than b.
Invariant
If an element (a) points to another one (b),
directly or indirectly,
then a is smaller than b.
Invariant
If an element (a) points to another one (b),
directly or indirectly,
then a is smaller than b.
Invariant
If an element (a) points to another one (b),
directly or indirectly,
then a is smaller than b.
Invariant
If an element (a) points to another one (b),
directly or indirectly,
then a is smaller than b.
&
all elements point to two other elements!
5
15
32
42
64
100
999
5
15
32
42
64
100
999
15
32
42
64
100
999
15
32
42
64
100
999
15
32
42
64
100
999
15
32
42
64
100
999
15
32
42
64
100
999
DSA 1 | Week 12
By Neeldhara Misra
DSA 1 | Week 12
- 409