0% found this document useful (0 votes)
5 views

dsa 4

The document discusses various data structures and algorithms, focusing on sorting algorithms, binary trees, and heaps. It explains in-place sorting, the properties of binary trees, and the characteristics of max-heaps and min-heaps. Additionally, it covers the MAX-HEAPIFY procedure and the relationship between the height of a heap and the number of elements it contains.

Uploaded by

ciceja2514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

dsa 4

The document discusses various data structures and algorithms, focusing on sorting algorithms, binary trees, and heaps. It explains in-place sorting, the properties of binary trees, and the characteristics of max-heaps and min-heaps. Additionally, it covers the MAX-HEAPIFY procedure and the relationship between the height of a heap and the number of elements it contains.

Uploaded by

ciceja2514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 139

1

Data Structures and Algorithms

BITS-Pilani K. K. Birla Goa Campus

1
Material for the presentation taken from Cormen, Leiserson, Rivest and
Stein, Introduction to Algorithms, Third Edition; 1
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Sorting algorithms

I In place sorting : If at any time only a constant number of


elements are stored outside the array.

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Sorting algorithms

I In place sorting : If at any time only a constant number of


elements are stored outside the array.
Auxilliary space complexity is

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Sorting algorithms

I In place sorting : If at any time only a constant number of


elements are stored outside the array.
Auxilliary space complexity is Θ(1)

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Sorting algorithms

I In place sorting : If at any time only a constant number of


elements are stored outside the array.
Auxilliary space complexity is Θ(1)
I MERGE procedure does not operate in place.

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Sorting algorithms

I In place sorting : If at any time only a constant number of


elements are stored outside the array.
Auxilliary space complexity is Θ(1)
I MERGE procedure does not operate in place.
I Ch 6 : Heapsort that uses a data structure called heap.

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Sorting algorithms

I In place sorting : If at any time only a constant number of


elements are stored outside the array.
Auxilliary space complexity is Θ(1)
I MERGE procedure does not operate in place.
I Ch 6 : Heapsort that uses a data structure called heap.
I Heapsort sorts n elements in place in O(n lg n) time.

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary tree

I Binary tree is a tree data structure where each node can have
at most two child nodes : left child node and right child node.

3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Full Binary tree

I Each node is either a leaf node or has two child nodes.

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Complete Binary tree

I A complete binary tree is a binary tree in which all levels are


completely filled except possibly the last level, which is filled
from left to right.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Complete Binary tree

I A complete binary tree is a binary tree in which all levels are


completely filled except possibly the last level, which is filled
from left to right.

5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap data structure : a complete binary tree

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap data structure : a complete binary tree

I The last level is not completely filled.

6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : a complete binary tree

I Each node of the heap corresponds to an element of the array.

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : a complete binary tree

I Each node of the heap corresponds to an element of the array.

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : a complete binary tree

I Each node of the heap corresponds to an element of the array.

I A.length,

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : a complete binary tree

I Each node of the heap corresponds to an element of the array.

I A.length, A.heap-size,

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : a complete binary tree

I Each node of the heap corresponds to an element of the array.

I A.length, A.heap-size, Root : A[1]

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : parent, left child, right child

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Either max-heaps or min-heaps

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Either max-heaps or min-heaps


I Max-heap property: A[PARENT (i)] ≥ A[i]

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Either max-heaps or min-heaps


I Max-heap property: A[PARENT (i)] ≥ A[i]

9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Either max-heaps or min-heaps


I Max-heap property: A[PARENT (i)] ≥ A[i]

I A[1] contains the maximum element


9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Min heap

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Min heap
I Min-heap property: A[PARENT (i)] ≤ A[i]

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary Heaps

I Min heap
I Min-heap property: A[PARENT (i)] ≤ A[i]
I A[1] will contain the smallest element

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap

I Height of a node : number of edges on the longest simple


downward path from the node to a leaf.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap

I Height of a node : number of edges on the longest simple


downward path from the node to a leaf.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap

I Height of a node : number of edges on the longest simple


downward path from the node to a leaf.

I Height of a heap is the height of its root.

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

2h

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

2h ≤ n

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

2h ≤ n ≤ 2h+1 − 1

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

2h ≤ n ≤ 2h+1 − 1
2h ≤ n < 2h+1

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

2h ≤ n ≤ 2h+1 − 1
2h ≤ n < 2h+1
h ≤ lg n < (h + 1)

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Height of a heap

I Suppose a heap has n elements and has a height of h.

2h ≤ n ≤ 2h+1 − 1
2h ≤ n < 2h+1
h ≤ lg n < (h + 1)
h = blg nc
12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Maintaining the heap property

I MAX-HEAPIFY(A,i) : binary trees rooted at LEFT(i) and


RIGHT(i) satisfy max-heap property.

13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Maintaining the heap property

I MAX-HEAPIFY(A,i) : binary trees rooted at LEFT(i) and


RIGHT(i) satisfy max-heap property.
https://visualgo.net/en/heap

13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Maintaining the heap property

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Maintaining the heap property

15
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

I Let the tree rooted at i have n nodes. The child subtree will
have a size at most 2n/3

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

I Let the tree rooted at i have n nodes. The child subtree will
have a size at most 2n/3

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

I Let the tree rooted at i have n nodes. The child subtree will
have a size at most 2n/3

h = height of node i
Maximum size of child subtree = 2h − 1

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

I Let the tree rooted at i have n nodes. The child subtree will
have a size at most 2n/3

h = height of node i
Maximum size of child subtree = 2h − 1

Maximum fraction of nodes =


16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

I Let the tree rooted at i have n nodes. The child subtree will
have a size at most 2n/3

h = height of node i
Maximum size of child subtree = 2h − 1
2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
A useful fractional inequality:
a a+1
For 0 < a < b , <
b b+1

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
A useful fractional inequality:
a a+1
For 0 < a < b , <
b b+1
h
2 −1
<
2h − 1 + 2h−1

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
A useful fractional inequality:
a a+1
For 0 < a < b , <
b b+1
h
2 −1 2h
<
2h − 1 + 2h−1 2h + 2h−1

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
A useful fractional inequality:
a a+1
For 0 < a < b , <
b b+1
h
2 −1 2h
<
2h − 1 + 2h−1 2h + 2h−1
2 × 2h−1
=
(2 + 1) × 2h−1

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
A useful fractional inequality:
a a+1
For 0 < a < b , <
b b+1
h
2 −1 2h
<
2h − 1 + 2h−1 2h + 2h−1
2 × 2h−1 2
= h−1
=
(2 + 1) × 2 3

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Finding recurrence

2h − 1
Maximum fraction of nodes =
2h − 1 + 2h−1
A useful fractional inequality:
a a+1
For 0 < a < b , <
b b+1
h
2 −1 2h
<
2h − 1 + 2h−1 2h + 2h−1
2 × 2h−1 2
= h−1
=
(2 + 1) × 2 3
2n
Maximum size of child subtree <
3

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c
log 3 1
nlogb a = n 2 =1

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c
log 3 1
nlogb a = n 2 =1
logb a
c = Θ(n lgk n)

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c
log 3 1
nlogb a = n 2 =1
logb a
c = Θ(n lgk n)
= Θ(1) , for k = 0

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c
log 3 1
nlogb a = n 2 =1
logb a
c = Θ(n lgk n)
= Θ(1) , for k = 0
Soln. Θ(nlogb a lgk+1 n)

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c
log 3 1
nlogb a = n 2 =1
logb a
c = Θ(n lgk n)
= Θ(1) , for k = 0
Soln. Θ(nlogb a lgk+1 n) = Θ(lg n)

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
MAX-HEAPIFY: Running time

I T (n) ≤ T (2n/3) + c
log 3 1
nlogb a = n 2 =1
logb a
c = Θ(n lgk n)
= Θ(1) , for k = 0
Soln. Θ(nlogb a lgk+1 n) = Θ(lg n)
T (n) = O(lg n)

18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

I Convert an unordered array into a max-heap using


MAX-HEAPIFY

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

I Convert an unordered array into a max-heap using


MAX-HEAPIFY

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

I Convert an unordered array into a max-heap using


MAX-HEAPIFY

What will be the parent node of the last element in array A?

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

I Convert an unordered array into a max-heap using


MAX-HEAPIFY

What will be the parent node of the last element in array A?


bA.length/2c

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Building a heap

20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

Running time:

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

Running time: O(n lg n)

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

Running time: O(n lg n) { Not asymptotically tight }

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1

h = 0,

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
 
10
h = 0,
20+1

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
 
10
h = 0, = 5;
20+1

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
 
10
h = 0, = 5; h = 1,
20+1

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
   
10 10
h = 0, = 5; h = 1, =3
20+1 21+1

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
   
10 10
h = 0, = 5; h = 1, =3
20+1 21+1

h = 2,

22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
   
10 10
h = 0, = 5; h = 1, =3
20+1 21+1
 
10
h = 2, = 2;
22+1
22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
   
10 10
h = 0, = 5; h = 1, =3
20+1 21+1
 
10
h = 2, = 2; h = 3,
22+1
22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2

l n m
I There are at most nodes having a height h.
2h+1
   
10 10
h = 0, = 5; h = 1, =3
20+1 21+1
   
10 10
h = 2, = 2; h = 3, =1
22+1 23+1
22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm
Number of nodes having zero height =
2

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.
Let us construct a new heap T 0 by removing all leaf nodes
from T

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.
Let us construct a new heap T 0 by removing all leaf nodes
from T
0
kh = kh−1

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.
Let us construct a new heap T 0 by removing all leaf nodes
from T
n0
 
0
kh = kh−1 ≤ (h−1)+1
2

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.
Let us construct a new heap T 0 by removing all leaf nodes
from T
n0
   
0 b(n/2)c
kh = kh−1 ≤ (h−1)+1 = (h−1)+1
2 2

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.
Let us construct a new heap T 0 by removing all leaf nodes
from T
n0
   
0 b(n/2)c
kh = kh−1 ≤ (h−1)+1 = (h−1)+1
2 2
 
(n/2)

2h
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
l n m
Nodes with height h ≤ h+1
2
I Proof by Induction
I Base case: h = 0 lnm l n m
Number of nodes having zero height = ≤ h+1
2 2
I Inductive step: l n m
Assume that there are at most (h−1)+1 number of nodes
2
of height h − 1.
Let kh be the number of nodes of height h in the binary heap
T.
Let us construct a new heap T 0 by removing all leaf nodes
from T
n0
   
0 b(n/2)c
kh = kh−1 ≤ (h−1)+1 = (h−1)+1
2 2
  l
(n/2) n m
≤ =
2h 2h+1
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

blg nc l
X n m
h+1
O(h)
2
h=0

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n
2h+1 2h
h=0 h=0

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n
2h+1 2h
h=0 h=0

X
kx k
k=0

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n
2h+1 2h
h=0 h=0

X x
kx k =
(1 − x)2
k=0

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n
2h+1 2h
h=0 h=0

X x
kx k =
(1 − x)2
k=0

X
k(1/2)k
k=0
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n
2h+1 2h
h=0 h=0

X x
kx k =
(1 − x)2
k=0

X 1/2
k(1/2)k =
(1 − (1/2))2
k=0
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n
2h+1 2h
h=0 h=0

X x
kx k =
(1 − x)2
k=0

X 1/2
k(1/2)k = =2
(1 − (1/2))2
k=0
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n 
2h+1 2h
h=0 h=0

!
X h
=O n
2h
h=0

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n 
2h+1 2h
h=0 h=0

!
X h
=O n
2h
h=0
= O(2n)

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

 
blg nc l blg nc
X n m X h
O(h) = O n 
2h+1 2h
h=0 h=0

!
X h
=O n
2h
h=0
= O(2n) = O(n)

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
P∞ k x
k=0 kx = (1−x)2

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
P∞ k x
k=0 kx = (1−x)2

I 0<x <1, 1 + x + x2 + . . .

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
P∞ k x
k=0 kx = (1−x)2

1
I 0<x <1, 1 + x + x2 + . . . =
1−x

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
P∞ k x
k=0 kx = (1−x)2

1
I 0<x <1, 1 + x + x2 + . . . =
1−x

X 1
I xk =
1−x
k=0

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
P∞ k x
k=0 kx = (1−x)2

1
I 0<x <1, 1 + x + x2 + . . . =
1−x

X 1
I xk =
1−x
k=0
I Differentiating both sides w.r.t x

X 1
kx k−1 =
(1 − x)2
k=0

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
P∞ k x
k=0 kx = (1−x)2

1
I 0<x <1, 1 + x + x2 + . . . =
1−x

X 1
I xk =
1−x
k=0
I Differentiating both sides w.r.t x

X 1
kx k−1 =
(1 − x)2
k=0

I Multiplying x on both sides



X x
kx k =
(1 − x)2
k=0

25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap

I We can construct a max heap (or a min heap) from an


unordered array in O(n) time.

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap

I We can construct a max heap (or a min heap) from an


unordered array in O(n) time.
I What is the running time of Build-Max-Heap procedure in
Θ-notation?

26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heapsort

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heapsort

I Running time : O(n lg n)

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heapsort

I Running time : O(n lg n)


I Operation (P. 161)

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:
1. Maximum(S)

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:
1. Maximum(S)
2. Extract-Max(S)

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:
1. Maximum(S)
2. Extract-Max(S)
3. Increase-Key(S,x,k)

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:
1. Maximum(S)
2. Extract-Max(S)
3. Increase-Key(S,x,k)
4. Insert(S,x)

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:
1. Maximum(S)
2. Extract-Max(S)
3. Increase-Key(S,x,k)
4. Insert(S,x)
I We will use the Heap data structure to construct a
max-priority queue.

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Problem: Data structure for a set of Keys such that the


following operations can be performed in O(lg n) time:
1. Maximum(S)
2. Extract-Max(S)
3. Increase-Key(S,x,k)
4. Insert(S,x)
I We will use the Heap data structure to construct a
max-priority queue.
I Used in Graph algorithms (e.g. Dijkstra’s algorithm)

28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

Running time of Heap-Extract-Max : O(lg n)

29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Increase-Key

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Increase-Key

I Suppose we increase the value at index i to 15.

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Increase-Key

I Suppose we increase the value at index i to 15.


How can we fix the Max-Heap property?

30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Increase-Key

31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Increase-Key

I Running time of Max-Heap-Increase-Key :

31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Increase-Key

I Running time of Max-Heap-Increase-Key : O(lg n)

31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Insert

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Insert

Running time of Max-Heap-Insert :

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Max-Heap-Insert

Running time of Max-Heap-Insert : O(lg n)

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Using a max-heap, all the basic operations can be performed


in O(lg n) time.

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Using a max-heap, all the basic operations can be performed


in O(lg n) time.
I Two forms : max-priority queue and min priority-queue

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Using a max-heap, all the basic operations can be performed


in O(lg n) time.
I Two forms : max-priority queue and min priority-queue
I Min-priority queue can be implemented using a min-heap.

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queues

I Using a max-heap, all the basic operations can be performed


in O(lg n) time.
I Two forms : max-priority queue and min priority-queue
I Min-priority queue can be implemented using a min-heap.
I Min-priority queue operations:
1. Minimum(S)
2. Extract-Min(S)
3. Decrease-Key(S,x,k)
4. Insert(S,x)

33
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms

You might also like