Lecture 6.4 - Using Heaps in Algorithms
Lecture 6.4 - Using Heaps in Algorithms
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 2/9
Dijkstra’s algorithm
def dijkstra(WMat,s):
Maintain two dictionaries with (rows,cols,x) = WMat.shape
vertices as keys infinity = np.max(WMat)*rows+1
(visited,distance) = ({},{})
visited, initially False for all v for v in range(rows):
distance, initially infinity for (visited[v],distance[v]) = (False,infinity)
distance[s] = 0
all v
for u in range(rows):
nextd = min([distance[v] for v in range(rows)
Set distance[s] to 0 if not visited[v]])
nextvlist = [v for v in range(rows)
Repeat, until all reachable vertices if (not visited[v]) and
are visited distance[v] == nextd]
if nextvlist == []:
Find unvisited vertex nextv with break
minimum distance nextv = min(nextvlist)
visited[nextv] = True
Set visited[nextv] to True
for v in range(cols):
Recompute distance[v] for if WMat[nextv,v,0] == 1 and (not visited[v]):
every neighbour v of nextv distance[v] = min(distance[v],distance[nextv]
+WMat[nextv,v,1])
return(distance)
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 3/9
Dijkstra’s algorithm
def dijkstra(WMat,s):
Bottleneck (rows,cols,x) = WMat.shape
infinity = np.max(WMat)*rows+1
Find unvisited vertex j with (visited,distance) = ({},{})
minimum distance for v in range(rows):
(visited[v],distance[v]) = (False,infinity)
Naive implementation requires an distance[s] = 0
O(n) scan for u in range(rows):
nextd = min([distance[v] for v in range(rows)
if not visited[v]])
nextvlist = [v for v in range(rows)
if (not visited[v]) and
distance[v] == nextd]
if nextvlist == []:
break
nextv = min(nextvlist)
visited[nextv] = True
for v in range(cols):
if WMat[nextv,v,0] == 1 and (not visited[v]):
distance[v] = min(distance[v],distance[nextv]
+WMat[nextv,v,1])
return(distance)
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 4/9
Dijkstra’s algorithm
def dijkstra(WMat,s):
Bottleneck (rows,cols,x) = WMat.shape
infinity = np.max(WMat)*rows+1
Find unvisited vertex j with (visited,distance) = ({},{})
minimum distance for v in range(rows):
(visited[v],distance[v]) = (False,infinity)
Naive implementation requires an distance[s] = 0
O(n) scan for u in range(rows):
nextd = min([distance[v] for v in range(rows)
Maintain unvisited vertices as a if not visited[v]])
nextvlist = [v for v in range(rows)
min-heap if (not visited[v]) and
delete min() in O(log n) time distance[v] == nextd]
if nextvlist == []:
break
nextv = min(nextvlist)
visited[nextv] = True
for v in range(cols):
if WMat[nextv,v,0] == 1 and (not visited[v]):
distance[v] = min(distance[v],distance[nextv]
+WMat[nextv,v,1])
return(distance)
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 4/9
Dijkstra’s algorithm
def dijkstra(WMat,s):
Bottleneck (rows,cols,x) = WMat.shape
infinity = np.max(WMat)*rows+1
Find unvisited vertex j with (visited,distance) = ({},{})
minimum distance for v in range(rows):
(visited[v],distance[v]) = (False,infinity)
Naive implementation requires an distance[s] = 0
O(n) scan for u in range(rows):
nextd = min([distance[v] for v in range(rows)
Maintain unvisited vertices as a if not visited[v]])
nextvlist = [v for v in range(rows)
min-heap if (not visited[v]) and
delete min() in O(log n) time distance[v] == nextd]
if nextvlist == []:
But, also need to update distances break
nextv = min(nextvlist)
of neighbours visited[nextv] = True
for v in range(cols):
Unvisited neighbours’ distances
if WMat[nextv,v,0] == 1 and (not visited[v]):
are inside the min-heap distance[v] = min(distance[v],distance[nextv]
Updating a value is not a basic +WMat[nextv,v,1])
return(distance)
heap operation
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 4/9
Updating values in a min-heap
Change 54 to 35
29
38 31
54 47 84 61
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 29
violation with parent
38 31
35 47 84 61
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 29
violation with parent
Swap upwards to restore heap,
similar to insert() 35 31
38 47 84 61
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 29
violation with parent
Swap upwards to restore heap,
similar to insert() 35 31
Change 29 to 71
38 47 84 61
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 71
violation with parent
Swap upwards to restore heap,
similar to insert() 35 31
Change 29 to 71
Increasing a value can create a 38 47 84 61
violation with child
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 31
violation with parent
Swap upwards to restore heap,
similar to insert() 35 71
Change 29 to 71
Increasing a value can create a 38 47 84 61
violation with child
Swap downwards to restore
heap, similar to delete min()
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 31
violation with parent
Swap upwards to restore heap,
similar to insert() 35 61
Change 29 to 71
Increasing a value can create a 38 47 84 71
violation with child
Swap downwards to restore
heap, similar to delete min()
68 59 50
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 31
violation with parent
Swap upwards to restore heap,
similar to insert() 35 61
Change 29 to 71
Increasing a value can create a 38 47 84 71
violation with child
Swap downwards to restore
heap, similar to delete min()
68 59 50
Both updates are O(log n)
Are we done?
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 5/9
Updating values in a min-heap
Change 54 to 35
Reducing a value can create a 31
violation with parent
Swap upwards to restore heap,
similar to insert() 35 61
Change 29 to 71
Increasing a value can create a 38 47 84 71
violation with child
Swap downwards to restore
heap, similar to delete min()
68 59 50
Both updates are O(log n)
Are we done?
0:68 8:59
VtoH 0 1 2 3 4 5 6 7 8
7 3 5 2 4 1 6 0 8
HtoV 0 1 2 3 4 5 6 7 8
7 5 3 1 4 2 6 0 8
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 7/9
Dijkstra’s algorithm
Using min-heaps 7:29
Identifying next vertex to
visit is O(log n)
Updating distance takes 5:38 3:31
O(log n) per neighbour
Adjacency list —
proportionally to degree 1:54 4:47 2:84 6:61
Cumulatively
O(n log n) to identify 0:68 8:59
vertices to visit across n
iterations VtoH 0 1 2 3 4 5 6 7 8
O(m log n) distance 7 3 5 2 4 1 6 0 8
updates overall HtoV 0 1 2 3 4 5 6 7 8
7 5 3 1 4 2 6 0 8
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 7/9
Dijkstra’s algorithm
Using min-heaps 7:29
Identifying next vertex to
visit is O(log n)
Updating distance takes 5:38 3:31
O(log n) per neighbour
Adjacency list —
proportionally to degree 1:54 4:47 2:84 6:61
Cumulatively
O(n log n) to identify 0:68 8:59
vertices to visit across n
iterations VtoH 0 1 2 3 4 5 6 7 8
O(m log n) distance 7 3 5 2 4 1 6 0 8
updates overall HtoV 0 1 2 3 4 5 6 7 8
7 5 3 1 4 2 6 0 8
Overall O((m + n) log n)
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 7/9
Heap sort
Start with an unordered list
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 8/9
Heap sort
Start with an unordered list
Build a heap — O(n)
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 8/9
Heap sort
Start with an unordered list
Build a heap — O(n)
Call delete max() n times to extract elements in descending order — O(n log n)
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 8/9
Heap sort
Start with an unordered list
Build a heap — O(n)
Call delete max() n times to extract elements in descending order — O(n log n)
After each delete max(), heap shrinks by 1
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 8/9
Heap sort
Start with an unordered list
Build a heap — O(n)
Call delete max() n times to extract elements in descending order — O(n log n)
After each delete max(), heap shrinks by 1
Store maximum value at the end of current heap
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 8/9
Heap sort
Start with an unordered list
Build a heap — O(n)
Call delete max() n times to extract elements in descending order — O(n log n)
After each delete max(), heap shrinks by 1
Store maximum value at the end of current heap
In place O(n log n) sort
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 8/9
Summary
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 9/9
Summary
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 9/9
Summary
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 9/9
Summary
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 9/9
Summary
Madhavan Mukund Using Heaps in Algorithms PDSA using Python Week 6 9/9