Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

DAA

Book: Introduction to Algorithms, by:

Thomas H. Cormen
Charles E. Leiserson
Ronald L. Rivest
Clifford Stein

Shashank Dwivedi 1
Lecture Contents (objectives)
 Algorithm Definition
 Insertion sort
 Analysis of insertion sort
 Example of insertion sort
 (Best, Worst and Average) case analysis
 Merge sort
 The divide-and-conquer approach
 Analyzing merge sort
 Example of merge sort
 Recursion tree

Computer Sciences Department 2


Algorithms

 Informally, an algorithm is any well-defined


computational procedure that takes some value, or
set of values, as input and produces some value, or
set of values, as output.

 An algorithm is thus a sequence of computational


steps that transform the input into the output.

Computer Sciences Department 3


Design and Analysis of Algorithms
• Analysis: predict the cost of an algorithm in
terms of resources and performance

• Design: design algorithms which minimize the


cost

Computer Sciences Department 4


 An algorithm is said to be correct if, for every input instance,
it halts with the correct output

Computer Sciences Department 5


Algorithms as a technology

 Would you have any reason to study algorithms? YES.


 If computers were infinitely fast, any correct method for
solving a problem would do.
 Computers may be fast, but:
- memory may be cheap, but it is not free. Computing
time is therefore a bounded resource, and so is space in
memory. These resources should be used wisely, and
algorithms that are efficient in terms of time or space
will help you do so.
Computer Sciences Department 6
Very important

 Which computer/ algorithm is


faster?

Computer Sciences Department 7


Computer Sciences Department 8
Computer Sciences Department 9
Getting Started

Computer Sciences Department 10


Running time

• The running time depends on the input: an


already sorted sequence is easier to sort.
• Major Simplifying Convention:
Parameterize the running time by the size of
the input, since short sequences are easier to
sort than long ones.
TA(n) = time of A on length n inputs
• Generally, seek upper bounds on the
running time, to have a guarantee of
performance.
Computer Sciences Department 11
Kinds of analyses
Worst-case: (usually)
• T(n) = maximum time of algorithm on any input
of size n. (upper bound on the running time)

Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs
of size n.

Best-case:
• T(n) = minimum time of algorithm ((fastest
time to complete, with optimal inputs chosen)
(lower bound on the running time))
Computer Sciences Department 12
Worst-case and average-case analysis

 The best case, in which the input array was already


sorted, and the worst case, in which the input array
was reverse sorted.
 The worst-case running time of an algorithm is an
upper bound on the running time for any input.

How long does it take to determine where in sub-array


A[1 . . j − 1] to insert element A[ j ]?
Computer Sciences Department 13
Average case

 Suppose that we randomly choose n numbers and apply


insertion sort.
How long does it take to determine where in sub-array A[1 . .
j − 1] to insert element A[ j ]?

 On average, half the elements in A[1 . . j − 1] are less than


A[ j ], and half the elements are greater.
 On average, therefore, we check half of the sub-array
A[1 . . j − 1], so t j = j/2.
Computer Sciences Department 14
Start
Insertion sort
One move / comparing / insert it into the correct position

6 5 3 1 8 7 2 4

The numbers that we wish to sort are also


known as the keys
Insertion sort, is an efficient algorithm for
sorting a small number of elements.
Computer Sciences Department 15
Insertion sort (cont’d)

Computer Sciences Department 16


Insertion sort (cont’d) “Pseudo-code conventions”

1 i j n
A:
Computer Sciences Department
sorted
17 key Length[A]=n
Analyzing algorithms

 predicting the resources that the algorithm requires.


 random-access machine (RAM)- Instructions are
(executed one after another, with no concurrent
operations)
 The data types in the RAM model are integer and
floating point and limit on the size of each word of data.
 Is exponentiation a constant time instruction? “shift
left” instruction.

Computer Sciences Department 18


Analysis of insertion sort
 The time taken by the INSERTION-SORT procedure depends
on the input.
 INSERTION SORT can take different amounts of time:
- to sort two input sequences of the same size
depending on how nearly sorted they already
are.
- to sort thousand numbers or three numbers.

 The running time of an algorithm on a particular input is the


number of primitive operations or “steps” executed.
Computer Sciences Department 19
Analysis of insertion sort (cont’d)

Best case

Worst case
Computer Sciences Department 20
Analysis of insertion sort (cont’d)

Computer Sciences Department 21


t i on
lana
Exp

a b

c =an2+bn-c
Computer Sciences Department 22
Example of insertion sort
8 1 4 9 3 7

Computer Sciences Department 23


Example of insertion sort
8 1 4 9 3 7

Computer Sciences Department 24


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7

Computer Sciences Department 25


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7

Computer Sciences Department 26


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7

Computer Sciences Department 27


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7

Computer Sciences Department 28


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7
1 4 8 9 3 7

Computer Sciences Department 29


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7
1 4 8 9 3 7

Computer Sciences Department 30


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7
1 4 8 9 3 7
1 3 4 8 9 7

Computer Sciences Department 31


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7
1 4 8 9 3 7
1 3 4 8 9 7

Computer Sciences Department 32


Example of insertion sort
8 1 4 9 3 7
1 8 4 9 3 7
1 4 8 9 3 7
1 4 8 9 3 7
1 3 4 8 9 7
1 3 4 7 8 9
Computer Sciences Department 33
The divide-and-conquer approach

 Divide the n-element sequence to be sorted into two


subsequences of n/2 elements each.
 The merge sort algorithm closely follows the divide-and-
conquer paradigm.

Computer Sciences Department 34


Computer Sciences Department 35
MERGE-SORT

MERGE-SORT A[1 . . n]
1. If n = 1, done.
2. Recursively sort A[ 1 . . n/2 ]
and A[ n/2+1 . . n ] .
3. “Merge” the 2 sorted lists.

Computer Sciences Department 36


Analyzing merge sort
T(n) MERGE-SORT A[1 . . n]
Q(1) 1. If n = 1, done.
2T(n/2) 2. Recursively sort A[ 1 . . n/2 ]
and A[ n/2+1 . . n ] .
Q(n) 3. “Merge” the 2 sorted lists

Recurrence for merge sort


Q(1) if n = 1;
T(n) =
2T(n/2) + Q(n) if n > 1.
Computer Sciences Department 37
Computer Sciences Department 38
Merge sort
 MERGE(A, p, q, r), where A is an array and p, q, and r
are indices numbering elements of the array such
that p ≤ q < r.

Computer Sciences Department 39


Computer Sciences Department 40
Merging two sorted arrays
20 12
13 11
7 9
2 1

Computer Sciences Department 41


Merging two sorted arrays
20 12
13 11
7 9
2 1

Computer Sciences Department 42


Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2

Computer Sciences Department 43


Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2

1 2

Computer Sciences Department 44


Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2

1 2

Computer Sciences Department 45


Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2

1 2 7

Computer Sciences Department 46


Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7

Computer Sciences Department 47


Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7 9

Computer Sciences Department 48


Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7 9

Computer Sciences Department 49


Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11

Computer Sciences Department 50


Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11

Computer Sciences Department 51


Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11 12

Computer Sciences Department 52


Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11 12
Time = Q(n) to merge a total of n elements (linear
time).

Computer Sciences Department 53


Merge sort - “Pseudo-code conventions”

Computer Sciences Department 54


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Computer Sciences Department 55


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Computer Sciences Department 56


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n)

Computer Sciences Department 57


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
T(n/2) T(n/2)

Computer Sciences Department 58


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

Computer Sciences Department 59


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2

cn/4 cn/4 cn/4 cn/4


Q(1)

Computer Sciences Department 60


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4

Q(1)

Computer Sciences Department 61


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4

Q(1)

Computer Sciences Department 62


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4

Q(1)

Computer Sciences Department 63


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn


Q(1)

Computer Sciences Department 64


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn


Q(1) #leaves = n Q(n)

Computer Sciences Department 65


Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn


Q(1) #leaves = n Q(n)
Total = Q(n lg n)
Computer Sciences Department 66
Conclusions
• Q(n lg n) grows more slowly than Q(n2).

• Therefore, merge sort asymptotically


beats insertion sort in the worst case.

• In practice, merge sort beats insertion


sort for n > 30 or so.

Computer Sciences Department 67

You might also like