week_10
week_10
Shahid Hussain
Week 10: October 21 – October 26, 2024: Fall 2024
1
Greedy Algorithms
Greedy Algorithms
2
Binary Heaps
3
Binary Heaps (cont.)
• The binary tree (for binary heaps) can be easily represented using an array
• The nodes of tree have a natural ordering: row by row, starting at the root
and moving from the left to the right within each level. The array has
positions 1, . . . , n corresponding to the considered nodes
• One can show that the node at location j in the array has the parent at
location ⌊j/2⌋ has two of its child nodes at locations 2j and 2j + 1
4
Binary Heaps (cont.)
2 3
4 5 6 7
8 9 10 11 12 13 14 15
5
Binary Heaps (cont.): Insert Key
• To insert a new element, we place it at the bottom of the tree (in the first
available position)
• If the key of new element is less than the key of its parent, then we should
swap these elements, etc
• The number of swaps is at most the depth of the tree, which is ⌊log2 n⌋ where
n is the number of elements
2 1
2 3 2 2
4 5 4 Insert 4 5 4 3
−−−−−→1
6
Binary Heaps (cont.) Delete-Min
• To delete the min element (which is always the root of the heap) we return
the element attached to the root
• Then we remove this element from the heap, take the element attached to the
last node in the tree (in the rightmost position in the bottom level) and place
it at the root
• If the key of this element is bigger than any of the root’s child nodes, we swap
these elements, etc. The number of swaps is at most ⌊log2 n⌋
2 4 2
2 3 2 3 4 3
4 5 4 Delete-min 4 5 −→ 4 5
−−−−−−−→
7
Binary Heaps (cont.) Build-Heap
8
Single Source Shortest Paths:
Dijkstra’s Algorithm
Single Source Shortest Paths: Dijkstra’s Algorithm
10
Single Source Shortest Paths: Dijkstra’s Algorithm (cont.)
3
1
1
s 1 1
3
2
11
Single Source Shortest Paths: Dijkstra’s Algorithm (cont.)
• During each step the algorithm adds new node v to the set S, computes the
value d(v) for this node and forms the path Pv from s to v with the help of
values predecessor (·)
• Let us show by induction on |S| that Pv is the shortest path from s to v
• From here it follows that d(v) is the minimum length of a path from s to v
• Let |S| = 1, in this case S = {s} and d(s) = 0, the statement holds in this case
• Assume that the statement holds when |S| = k for some value of k ≥ 1
12
Single Source Shortest Paths: Dijkstra’s Algorithm (cont.)
• So during the step number k + 1 the algorithm chooses the node v, and
e = (u, v) be the final edge in the path Pv
• Let us consider an arbitrary path P from s to v
• Since v ∈
/ S, there is an edge e′ = (x, y) in this path such that x ∈ S and y ∈
/S
• By the inductive hypothesis, the length of any path from s to x is at least d(x)
• Therefore l(P ) ≥ d(x) + le′ ≥ d(u) + le = l(Pv ). The inequality
d(x) + le′ ≥ d(u) + le follows from the description of the algorithm
• Therefore l(P ) ≥ l(Pv ), and Pv is the shortest path from s to v
13
Single Source Shortest Paths: Dijkstra’s Algorithm (cont.)
• To decrease the key for w we should find w in the heap (in the corresponding
array)
• To this end, we will assume that all n nodes in G are numbered by numbers
1, . . . , n and we have an array in which in the t-th position we have the
current value of the position of t-th node in the heap
• We can store in this array values d(u) and predecessor (u)
• The decrease-key operation can be used at most once per edge, when the
initial node of the edge is added to S
• Let |V | = n and |E| = m
• The algorithm makes one operation of make-queue in time O(n log n), at most
n operations of delete-min in time O(n log n), and at most m operations of
decrease-key in time O(m log n)
• Thus, the overall time for the implementation is O((m + n) log n)
15
Minimum Spanning Trees
Minimum Spanning Trees: Prim’s Algorithm
16
Minimum Spanning Trees: Prim’s Algorithm
3
1 7
2
s 1 5
2 1
4
17
Minimum Spanning Trees: Prim’s Algorithm
• Let us prove by induction on |S| that partial spanning tree constructed during
the step number |S| is a part of minimum spanning tree
• If |S| = 1 then the partial spanning tree is empty, and the considered
statement holds. Let for some k ≥ 1 this statement hold: |S| = k and the
constructed partial spanning tree Tk is a part of some minimum spanning
tree T
18
Minimum Spanning Trees: Prim’s Algorithm
19
Minimum Spanning Trees: Prim’s Algorithm
20
Minimum Spanning Trees: Prim’s Algorithm
21
Minimum Spanning Trees: Prim’s Algorithm
22