Top Banner
DESIGN AND ANALYSIS OF ALGORITHMS Heaps: Updating values, heap sort MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan NPTEL MOOC,JAN-FEB 2015 Week 5, Module 5
18

DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Apr 14, 2018

Download

Documents

phamcong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

DESIGN AND ANALYSIS OF ALGORITHMS Heaps: Updating values, heap sort

MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan

NPTEL MOOC,JAN-FEB 2015 Week 5, Module 5

Page 2: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Heaps

Heaps are a tree implementation of priority queues

insert( ) and delete_max( ) are both O(log N)

heapify( ) builds a heap in O(N)

Tree can be manipulated easily using an array

Page 3: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Recall Dijskstra’s algorithmMaintain two arrays

Visited[ ], initially False for all iDistance[ ], initially ∞ for all i

For ∞, use sum of all edge weights + 1

Set Distance[1] = 0

Repeat, until all vertices are burnt

Find j with minimum Distance

Set Visited[j] = True

Recompute Distance[k] for each neighbour k of j

Page 4: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

BottlenecksFind j with minimum Distance

Naive implementation takes O(n) time

Maintain Distance[] as min-heap, delete_min( ) is O(log n)

Recompute Distance[k] for each neighbour k of j

Use adjacency lists to look up neighbours efficiently

To recompute Distance[k], need to update heap values

Not a basic operation on heaps, as defined

Page 5: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 12 to 44

Increasing a value can create heap violation with parent

Fix violations upwards, to root 10 11

12

33

24

Page 6: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 12 to 44

Increasing a value can create heap violation with parent

Fix violations upwards, to root 10 11

33

24

44

Page 7: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 12 to 44

Increasing a value can create heap violation with parent

Fix violations upwards, to root 10 11

33

44

24

Page 8: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 12 to 44

Increasing a value can create heap violation with parent

Fix violations upwards, to root 10 11

24

44

33

Page 9: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 33 to 9

Decreasing a value can create heap violation with children

Fix violations downwards, to leaves 10 11

24

44

33

Page 10: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 33 to 9

Decreasing a value can create heap violation with children

Fix violations downwards, to leaves 10 11

24

44

9

Page 11: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 33 to 9

Decreasing a value can create heap violation with children

Fix violations downwards, to leaves 10 11

44

24

9

Page 12: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Change 33 to 9

Decreasing a value can create heap violation with children

Fix violations downwards, to leaves 10

44

24

9

11

Page 13: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Update Distance[j]

Where is Distance[j] in heap?

Two additional arrays, NodeToHeap[ ],HeapToNode[ ]

10 11

24

44

33

1 2 3 4 5 6 7 8 97 3 5 2 4 1 6 0 8

0

1 2

3 4 5 6

87

8

6 4

2 5 3 7

1 9

0 1 2 3 4 5 6 7 88 6 4 2 5 3 7 1 9

NodeToHeap

HeapToNode

Page 14: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Update Distance[j]

Where is Distance[j] in heap?

Two additional arrays, NodeToHeap[ ],HeapToNode[ ]

10 11

24

44

9

1 2 3 4 5 6 7 8 97 3 5 2 4 1 6 0 8

0

1 2

3 4 5 6

87

8

6 4

2 5 3 7

1 9

0 1 2 3 4 5 6 7 88 6 4 2 5 3 7 1 9

NodeToHeap

HeapToNode

Page 15: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Update Distance[j]

Where is Distance[j] in heap?

Two additional arrays, NodeToHeap[ ],HeapToNode[ ]

10 11

44

24

9

0

1 2

3 4 5 6

87

8

4

5 3 7

1 9

NodeToHeap

HeapToNode

6

2

1 2 3 4 5 6 7 8 97 1 5 2 4 3 6 0 8

0 1 2 3 4 5 6 7 88 2 4 6 5 3 7 1 9

Page 16: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Updating values

7

5 56

Update Distance[j]

Where is Distance[j] in heap?

Two additional arrays, NodeToHeap[ ],HeapToNode[ ]

10

44

24

9

11

0

1 2

3 4 5 6

87

8

4

5 3 7

1

NodeToHeap

HeapToNode

2

6

9

1 2 3 4 5 6 7 8 97 1 5 2 4 8 6 0 3

0 1 2 3 4 5 6 7 88 2 4 9 5 3 7 1 6

Page 17: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Dijkstra’s algorithm: Complexity

Using heaps with updates

Finding minimum burn time vertex takes O(log n)

With adjacency list, updating burn times take O(log n) each, total O(m) edges

Overall O(n log n + m log n) = O((n+m) log n)

Similar strategy works for Prim’s algorithm for minimum cost spanning tree

Page 18: DESIGN AND ANALYSIS OF ALGORITHMS - Chennai …madhavan/nptel-algorithms-2015/week5/pdf/nptel-wee… · Recall Dijskstra’s algorithm Maintain two arrays Visited[ ], initially False

Heap sortStart 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