You are on page 1of 130

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
Part II Sorting and Order Statistics

I Record : Collection of data


I Key : Value to be sorted
I Satellite data
I If satellite data is large, we permute an array of pointers to
the records.

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.

3
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.
I MERGE procedure does not operate in place.

3
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.
I MERGE procedure does not operate in place.
I Ch 6 : Heapsort that uses a data structure called heap.

3
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.
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.

3
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.

4
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.

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

I Leaf nodes are at the same depth and all internal nodes have
2 child nodes

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

I Leaf nodes are at the same depth and all internal nodes have
2 child nodes

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

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

I All levels are completely filled except possibly the last level,
which is filled from the left

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

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

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

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

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

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

I A.length,

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

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

I A.length, A.heap-size,

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Heap : nearly 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]

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

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

I Either max-heaps or min-heaps

10
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]

10
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]

10
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


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

I Min heap

11
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]

11
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

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.

12
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.

12
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.

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.

13
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.

13
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

13
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

13
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

13
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

13
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)

13
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
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.

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: Maintaining the heap property

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

17
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

17
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

17
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 =


17
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
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

18
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 a+1
For b > a > 0 , <
b b+1

18
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 a+1
For b > a > 0 , <
b b+1
2h − 1
<
2h − 1 + 2h−1

18
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 a+1
For b > a > 0 , <
b b+1
2h − 1 2h
<
2h − 1 + 2h−1 2h + 2h−1

18
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 a+1
For b > a > 0 , <
b b+1
2h − 1 2h
<
2h − 1 + 2h−1 2h + 2h−1
2 × 2h−1
=
(2 + 1) × 2h−1

18
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 a+1
For b > a > 0 , <
b b+1
2h − 1 2h
<
2h − 1 + 2h−1 2h + 2h−1
2 × 2h−1 2
= =
(2 + 1) × 2h−1 3

18
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 a+1
For b > a > 0 , <
b b+1
2h − 1 2h
<
2h − 1 + 2h−1 2h + 2h−1
2 × 2h−1 2
= =
(2 + 1) × 2h−1 3
2n
Maximum size of child subtree <
3

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

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

I T (n) ≤ T (2n/3) + Θ(1)

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

I T (n) ≤ T (2n/3) + Θ(1)


log 3 1
nlogb a = n 2 = Θ(1)

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

I T (n) ≤ T (2n/3) + Θ(1)


log 3 1
nlogb a = n 2 = Θ(1)
logb a
Θ(n ) = Θ(f (n))

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

I T (n) ≤ T (2n/3) + Θ(1)


log 3 1
nlogb a = n 2 = Θ(1)
logb a
Θ(n ) = Θ(f (n))
logb a
Soln. Θ(n lg n)

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

I T (n) ≤ T (2n/3) + Θ(1)


log 3 1
nlogb a = n 2 = Θ(1)
logb a
Θ(n ) = Θ(f (n))
logb a
Soln. Θ(n lg n) = Θ(lg n)

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

I T (n) ≤ T (2n/3) + Θ(1)


log 3 1
nlogb a = n 2 = Θ(1)
logb a
Θ(n ) = Θ(f (n))
logb a
Soln. Θ(n lg n) = Θ(lg n)
T (n) = O(lg n)

20
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

21
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

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

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

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

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

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

Running time:

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

Running time: O(n lg n)

23
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 }

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

24
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

24
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,

24
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

24
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

24
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

24
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

24
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,

24
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
24
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
24
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
24
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

25
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

25
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

25
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

25
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.

25
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.

25
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

25
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

25
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

25
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

25
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
25
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
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
BUILD-MAX-HEAP : Running time

26
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

26
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

26
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

26
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

26
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
26
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
26
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
26
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

26
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)

26
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)

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

27
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 + . . .

27
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

27
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

27
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

27
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

27
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.

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

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

I Running time : O(n lg n)

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

I Running time : O(n lg n)


I Operation (P. 161)

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

I Heap data structure can be used to construct efficient priority


queue

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling
I Two forms : max-priority queue and min priority-queue

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling
I Two forms : max-priority queue and min priority-queue
I Max-priority queue operations:

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling
I Two forms : max-priority queue and min priority-queue
I Max-priority queue operations:
1. Insert(S,x)

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling
I Two forms : max-priority queue and min priority-queue
I Max-priority queue operations:
1. Insert(S,x)
2. Maximum(S)

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling
I Two forms : max-priority queue and min priority-queue
I Max-priority queue operations:
1. Insert(S,x)
2. Maximum(S)
3. Extract-Max(S)

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

I Heap data structure can be used to construct efficient priority


queue
I Job scheduling
I Two forms : max-priority queue and min priority-queue
I Max-priority queue operations:
1. Insert(S,x)
2. Maximum(S)
3. Extract-Max(S)
4. Increase-Key(S,x,k)

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

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

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

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


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

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

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

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

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

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


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

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

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

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

34
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Priority Queue

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


O(lg n) time.

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

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

You might also like