SlideShare uma empresa Scribd logo
1 de 70
Baixar para ler offline
Algorithms

Sandeep Kumar Poonia
Head Of Dept. CS/IT
B.E., M.Tech., UGC-NET
LM-IAENG, LM-IACSIT,LM-CSTA, LM-AIRCC, LM-SCIEI, AM-UACEE
Algorithms
Introduction
Proof By Induction
Asymptotic notation
Pre Computer Era:
•

In the pre computer era various models of
computation were proposed. The models worked on a
predefined set of operations. The primary focus in
this era was on “What can be computed and what
cannot be computed?” This field of study was
called Computability Theory.

Sandeep Kumar Poonia
Post Computer Era:
• With ultimate computability in terms of Turing
Machines, the primary focus in the post
computer era was Complexity Theory. People
were more interested to find out “How well
can it be computed?”

Sandeep Kumar Poonia
Sandeep Kumar Poonia
Sandeep Kumar Poonia
Sandeep Kumar Poonia
As a corollary to the above mentioned result we can easily see that a number n
must be halved floor(log2n) + 1 times to reach 0.

Sandeep Kumar Poonia
Algorithms
•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.

Sandeep Kumar Poonia
Algorithms

Sandeep Kumar Poonia
Algorithms

Sandeep Kumar Poonia
Properties of Algorithms

Sandeep Kumar Poonia
Steps to develop an algorithm

Sandeep Kumar Poonia
Algorithms as a technology
•Suppose computers were infinitely fast and
•computer memory was free.

Would you have any reason to study algorithms?......YES(See an
example)
let us assume a faster computer (computer A) running insertion sort
against a slower computer (computer B) running merge sort. They each
must sort an array of one million numbers. Suppose that computer A
executes one billion instructions per second and computer B executes
only ten million instructions per second, so that computer A is 100
times faster than computer B in raw computing power.
insertion sort, takes time roughly equal to c1n2 to sort n items,
merge sort, takes time roughly equal to c2n lg n
Sandeep Kumar Poonia
Algorithms as a technology
To sort one million numbers,
• Computer A takes

• Computer B takes

By using an algorithm whose running time grows more
slowly, even with a poor compiler, computer B runs 20
times faster than computer A
Sandeep Kumar Poonia
Review: Induction
• Suppose
 S(k) is true for fixed constant k
o Often k = 0

 S(n)  S(n+1) for all n >= k

• Then S(n) is true for all n >= k

Sandeep Kumar Poonia
Proof By Induction
• Claim:S(n) is true for all n >= k
• Basis:
 Show formula is true when n = k

• Inductive hypothesis:
 Assume formula is true for an arbitrary n

• Step:
 Show that formula is then true for n+1

Sandeep Kumar Poonia
Induction Example:
Gaussian Closed Form
• Prove 1 + 2 + 3 + … + n = n(n+1) / 2
 Basis:
o If n = 0, then 0 = 0(0+1) / 2

 Inductive hypothesis:
o Assume 1 + 2 + 3 + … + n = n(n+1) / 2

 Step (show true for n+1):
1 + 2 + … + n + n+1 = (1 + 2 + …+ n) + (n+1)
= n(n+1)/2 + n+1 = [n(n+1) + 2(n+1)]/2
= (n+1)(n+2)/2 = (n+1)(n+1 + 1) / 2

Sandeep Kumar Poonia
Induction Example:
Geometric Closed Form
• Prove a0 + a1 + … + an = (an+1 - 1)/(a - 1) for
all a  1

 Basis: show that a0 = (a0+1 - 1)/(a - 1)
a0 = 1 = (a1 - 1)/(a - 1)

 Inductive hypothesis:
o Assume a0 + a1 + … + an = (an+1 - 1)/(a - 1)

 Step (show true for n+1):
a0 + a1 + … + an+1 = a0 + a1 + … + an + an+1
= (an+1 - 1)/(a - 1) + an+1 = (an+1+1 - 1)/(a - 1)

Sandeep Kumar Poonia
Induction
•
•

We’ve been using weak induction
Strong induction also holds
 Basis: show S(0)
 Hypothesis: assume S(k) holds for arbitrary k <= n
 Step: Show S(n+1) follows

•

Another variation:
 Basis: show S(0), S(1)
 Hypothesis: assume S(n) and S(n+1) are true
 Step: show S(n+2) follows

Sandeep Kumar Poonia
Asymptotic Performance
• Here, we care most about asymptotic
performance
 How does the algorithm behave as the problem
size gets very large?
o Running time
o Memory/storage requirements
o Bandwidth/power requirements/logic gates/etc.

Sandeep Kumar Poonia
Asymptotic Notation
• By now you should have an intuitive feel for
asymptotic (big-O) notation:
 What does O(n) running time mean? O(n2)?
O(n lg n)?
 How does asymptotic running time relate to
asymptotic memory usage?

• Our first task is to define this notation more
formally and completely

Sandeep Kumar Poonia
Analysis of Algorithms
• Analysis is performed with respect to a
•

computational model
We will usually use a generic uniprocessor
random-access machine (RAM)
 All memory equally expensive to access
 No concurrent operations
 All reasonable instructions take unit time
o Except, of course, function calls

 Constant word size
o Unless we are explicitly manipulating bits
Sandeep Kumar Poonia
Input Size
• Time and space complexity
 This is generally a function of the input size
o E.g., sorting, multiplication

 How we characterize input size depends:
o Sorting: number of input items
o Multiplication: total number of bits
o Graph algorithms: number of nodes & edges Etc

Sandeep Kumar Poonia
Running Time
• Number of primitive steps that are executed
 Except for time of executing a function call most
statements roughly require the same amount of
time
oy=m*x+b
o c = 5 / 9 * (t - 32 )
o z = f(x) + g(y)

• We can be more exact if need be

Sandeep Kumar Poonia
Analysis
• Worst case
 Provides an upper bound on running time
 An absolute guarantee

• Average case
 Provides the expected running time
 Very useful, but treat with care: what is “average”?
o Random (equally likely) inputs
o Real-life inputs

Sandeep Kumar Poonia
An Example: Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
30

10

40

20

1

2

3

4

i =  j =  key = 
A[j] = 
A[j+1] = 

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
30

10

40

20

1

2

3

4

i=2 j=1
A[j] = 30

key = 10
A[j+1] = 10

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
30

30

40

20

1

2

3

4

i=2 j=1
A[j] = 30

key = 10
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
30

30

40

20

1

2

3

4

i=2 j=1
A[j] = 30

key = 10
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
30

30

40

20

1

2

3

4

i=2 j=0
A[j] = 

key = 10
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
30

30

40

20

1

2

3

4

i=2 j=0
A[j] = 

key = 10
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=2 j=0
A[j] = 

key = 10
A[j+1] = 10

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=3 j=0
A[j] = 

key = 10
A[j+1] = 10

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=3 j=0
A[j] = 

key = 40
A[j+1] = 10

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=3 j=0
A[j] = 

key = 40
A[j+1] = 10

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=3 j=2
A[j] = 30

key = 40
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=3 j=2
A[j] = 30

key = 40
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=3 j=2
A[j] = 30

key = 40
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=4 j=2
A[j] = 30

key = 40
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=4 j=2
A[j] = 30

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=4 j=2
A[j] = 30

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=4 j=3
A[j] = 40

key = 20
A[j+1] = 20

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

20

1

2

3

4

i=4 j=3
A[j] = 40

key = 20
A[j+1] = 20

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

40

1

2

3

4

i=4 j=3
A[j] = 40

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

40

1

2

3

4

i=4 j=3
A[j] = 40

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

40

1

2

3

4

i=4 j=3
A[j] = 40

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

40

1

2

3

4

i=4 j=2
A[j] = 30

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

40

40

1

2

3

4

i=4 j=2
A[j] = 30

key = 20
A[j+1] = 40

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

30

40

1

2

3

4

i=4 j=2
A[j] = 30

key = 20
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

30

40

1

2

3

4

i=4 j=2
A[j] = 30

key = 20
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

30

40

1

2

3

4

i=4 j=1
A[j] = 10

key = 20
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

30

30

40

1

2

3

4

i=4 j=1
A[j] = 10

key = 20
A[j+1] = 30

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

20

30

40

1

2

3

4

i=4 j=1
A[j] = 10

key = 20
A[j+1] = 20

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
An Example: Insertion Sort
10

20

30

40

1

2

3

4

i=4 j=1
A[j] = 10

key = 20
A[j+1] = 20

InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Done!

Sandeep Kumar Poonia
Insertion Sort
What is the precondition
InsertionSort(A, n) {
for this loop?
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

Sandeep Kumar Poonia
Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
How many times will
}
this loop execute?

Sandeep Kumar Poonia
Insertion Sort
Statement

Effort

InsertionSort(A, n) {
for i = 2 to n {
c1n
key = A[i]
c2(n-1)
j = i - 1;
c3(n-1)
while (j > 0) and (A[j] > key) {
c4T
A[j+1] = A[j]
c5(T-(n-1))
j = j - 1
c6(T-(n-1))
}
0
A[j+1] = key
c7(n-1)
}
0
}
T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith
for loop iteration
Sandeep Kumar Poonia
Analyzing Insertion Sort
•

•

T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1)
= c8T + c9n + c10

What can T be?
 Best case -- inner loop body never executed
o ti = 1  T(n) is a linear function

 Worst case -- inner loop body executed for all
previous elements
o ti = i  T(n) is a quadratic function

Sandeep Kumar Poonia
Analysis
• Simplifications
 Ignore actual and abstract statement costs
 Order of growth is the interesting measure:
o Highest-order term is what counts



Sandeep Kumar Poonia

Remember, we are doing asymptotic analysis
As the input size grows larger it is the high order term that
dominates
Upper Bound Notation
• We say InsertionSort’s run time is O(n2)
 Properly we should say run time is in O(n2)
 Read O as “Big-O” (you’ll also hear it as “order”)

• In general a function
 f(n) is O(g(n)) if there exist positive constants c
and n0 such that f(n)  c  g(n) for all n  n0

• Formally

 O(g(n)) = { f(n):  positive constants c and n0 such
that f(n)  c  g(n)  n  n0
Sandeep Kumar Poonia
Lower Bound Notation
• We say InsertionSort’s run time is (n)
• In general a function
 f(n) is (g(n)) if  positive constants c and n0 such
that 0  cg(n)  f(n)  n  n0

• Proof:

 Suppose run time is an + b
o Assume a and b are positive (what if b is negative?)

 an  an + b

Sandeep Kumar Poonia
Asymptotic Tight Bound
• A function f(n) is (g(n)) if  positive
constants c1, c2, and n0 such that
c1 g(n)  f(n)  c2 g(n)  n  n0

• Theorem
 f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n))

Sandeep Kumar Poonia
Practical Complexity
250

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2

f(n) = n^3
f(n) = 2^n

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Sandeep Kumar Poonia
Practical Complexity
500

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Sandeep Kumar Poonia
Practical Complexity
1000

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1

Sandeep Kumar Poonia

3

5

7

9

11

13

15

17

19
Practical Complexity
5000

4000
f(n) = n
f(n) = log(n)

3000

f(n) = n log(n)
f(n) = n^2
2000

f(n) = n^3
f(n) = 2^n

1000

0
1

Sandeep Kumar Poonia

3

5

7

9

11

13

15

17

19
Practical Complexity
10000000
1000000
100000
10000
1000
100
10
1
1

Sandeep Kumar Poonia

4

16

64

256

1024

4096

16384 65536
Other Asymptotic Notations
• A function f(n) is o(g(n)) if  positive
•
•

constants c and n0 such that
f(n) < c g(n)  n  n0
A function f(n) is (g(n)) if  positive
constants c and n0 such that
c g(n) < f(n)  n  n0
Intuitively,
 o() is like <
 O() is like 

Sandeep Kumar Poonia

 () is like >
 () is like 

 () is like =

Mais conteúdo relacionado

Destaque

The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
Kamran Ashraf
 

Destaque (20)

Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
2. the grid
2. the grid2. the grid
2. the grid
 
Linear time sorting algorithms
Linear time sorting algorithmsLinear time sorting algorithms
Linear time sorting algorithms
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Graph
GraphGraph
Graph
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Augmenting Data Structures
Augmenting Data StructuresAugmenting Data Structures
Augmenting Data Structures
 
Avl tree
Avl treeAvl tree
Avl tree
 
An improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmAn improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithm
 
Network flow problems
Network flow problemsNetwork flow problems
Network flow problems
 
Red black tree
Red black treeRed black tree
Red black tree
 
Tabu search
Tabu searchTabu search
Tabu search
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Lecture29
Lecture29Lecture29
Lecture29
 
Whale optimizatio algorithm
Whale optimizatio algorithmWhale optimizatio algorithm
Whale optimizatio algorithm
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
 

Semelhante a Introduction to algorithms

Lecture 2
Lecture 2Lecture 2
Lecture 2
sajinsc
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
Gary Short
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 

Semelhante a Introduction to algorithms (20)

Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
D&AA Lecture 2 insertion sort.ppt
D&AA Lecture 2 insertion sort.pptD&AA Lecture 2 insertion sort.ppt
D&AA Lecture 2 insertion sort.ppt
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Dr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistDr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabist
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
ppt02.ppt
ppt02.pptppt02.ppt
ppt02.ppt
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
insertion sort.ppt
insertion sort.pptinsertion sort.ppt
insertion sort.ppt
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Lecture1
Lecture1Lecture1
Lecture1
 
Asymptotic Analysis
Asymptotic AnalysisAsymptotic Analysis
Asymptotic Analysis
 

Mais de Dr Sandeep Kumar Poonia

Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
Dr Sandeep Kumar Poonia
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...
Dr Sandeep Kumar Poonia
 

Mais de Dr Sandeep Kumar Poonia (20)

Soft computing
Soft computingSoft computing
Soft computing
 
Modified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmModified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithm
 
Enhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmEnhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithm
 
RMABC
RMABCRMABC
RMABC
 
Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
 
Improved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmImproved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithm
 
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmComparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
 
A novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmA novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithm
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmSunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithm
 
New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm
 
A new approach of program slicing
A new approach of program slicingA new approach of program slicing
A new approach of program slicing
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...
 
Enhanced abc algo for tsp
Enhanced abc algo for tspEnhanced abc algo for tsp
Enhanced abc algo for tsp
 
Database aggregation using metadata
Database aggregation using metadataDatabase aggregation using metadata
Database aggregation using metadata
 
Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...
 
Lecture28 tsp
Lecture28 tspLecture28 tsp
Lecture28 tsp
 
Lecture27 linear programming
Lecture27 linear programmingLecture27 linear programming
Lecture27 linear programming
 
Lecture26
Lecture26Lecture26
Lecture26
 
Lecture25
Lecture25Lecture25
Lecture25
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Último (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

Introduction to algorithms

  • 1. Algorithms Sandeep Kumar Poonia Head Of Dept. CS/IT B.E., M.Tech., UGC-NET LM-IAENG, LM-IACSIT,LM-CSTA, LM-AIRCC, LM-SCIEI, AM-UACEE
  • 3. Pre Computer Era: • In the pre computer era various models of computation were proposed. The models worked on a predefined set of operations. The primary focus in this era was on “What can be computed and what cannot be computed?” This field of study was called Computability Theory. Sandeep Kumar Poonia
  • 4. Post Computer Era: • With ultimate computability in terms of Turing Machines, the primary focus in the post computer era was Complexity Theory. People were more interested to find out “How well can it be computed?” Sandeep Kumar Poonia
  • 8. As a corollary to the above mentioned result we can easily see that a number n must be halved floor(log2n) + 1 times to reach 0. Sandeep Kumar Poonia
  • 9. Algorithms •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. Sandeep Kumar Poonia
  • 13. Steps to develop an algorithm Sandeep Kumar Poonia
  • 14. Algorithms as a technology •Suppose computers were infinitely fast and •computer memory was free. Would you have any reason to study algorithms?......YES(See an example) let us assume a faster computer (computer A) running insertion sort against a slower computer (computer B) running merge sort. They each must sort an array of one million numbers. Suppose that computer A executes one billion instructions per second and computer B executes only ten million instructions per second, so that computer A is 100 times faster than computer B in raw computing power. insertion sort, takes time roughly equal to c1n2 to sort n items, merge sort, takes time roughly equal to c2n lg n Sandeep Kumar Poonia
  • 15. Algorithms as a technology To sort one million numbers, • Computer A takes • Computer B takes By using an algorithm whose running time grows more slowly, even with a poor compiler, computer B runs 20 times faster than computer A Sandeep Kumar Poonia
  • 16. Review: Induction • Suppose  S(k) is true for fixed constant k o Often k = 0  S(n)  S(n+1) for all n >= k • Then S(n) is true for all n >= k Sandeep Kumar Poonia
  • 17. Proof By Induction • Claim:S(n) is true for all n >= k • Basis:  Show formula is true when n = k • Inductive hypothesis:  Assume formula is true for an arbitrary n • Step:  Show that formula is then true for n+1 Sandeep Kumar Poonia
  • 18. Induction Example: Gaussian Closed Form • Prove 1 + 2 + 3 + … + n = n(n+1) / 2  Basis: o If n = 0, then 0 = 0(0+1) / 2  Inductive hypothesis: o Assume 1 + 2 + 3 + … + n = n(n+1) / 2  Step (show true for n+1): 1 + 2 + … + n + n+1 = (1 + 2 + …+ n) + (n+1) = n(n+1)/2 + n+1 = [n(n+1) + 2(n+1)]/2 = (n+1)(n+2)/2 = (n+1)(n+1 + 1) / 2 Sandeep Kumar Poonia
  • 19. Induction Example: Geometric Closed Form • Prove a0 + a1 + … + an = (an+1 - 1)/(a - 1) for all a  1  Basis: show that a0 = (a0+1 - 1)/(a - 1) a0 = 1 = (a1 - 1)/(a - 1)  Inductive hypothesis: o Assume a0 + a1 + … + an = (an+1 - 1)/(a - 1)  Step (show true for n+1): a0 + a1 + … + an+1 = a0 + a1 + … + an + an+1 = (an+1 - 1)/(a - 1) + an+1 = (an+1+1 - 1)/(a - 1) Sandeep Kumar Poonia
  • 20. Induction • • We’ve been using weak induction Strong induction also holds  Basis: show S(0)  Hypothesis: assume S(k) holds for arbitrary k <= n  Step: Show S(n+1) follows • Another variation:  Basis: show S(0), S(1)  Hypothesis: assume S(n) and S(n+1) are true  Step: show S(n+2) follows Sandeep Kumar Poonia
  • 21. Asymptotic Performance • Here, we care most about asymptotic performance  How does the algorithm behave as the problem size gets very large? o Running time o Memory/storage requirements o Bandwidth/power requirements/logic gates/etc. Sandeep Kumar Poonia
  • 22. Asymptotic Notation • By now you should have an intuitive feel for asymptotic (big-O) notation:  What does O(n) running time mean? O(n2)? O(n lg n)?  How does asymptotic running time relate to asymptotic memory usage? • Our first task is to define this notation more formally and completely Sandeep Kumar Poonia
  • 23. Analysis of Algorithms • Analysis is performed with respect to a • computational model We will usually use a generic uniprocessor random-access machine (RAM)  All memory equally expensive to access  No concurrent operations  All reasonable instructions take unit time o Except, of course, function calls  Constant word size o Unless we are explicitly manipulating bits Sandeep Kumar Poonia
  • 24. Input Size • Time and space complexity  This is generally a function of the input size o E.g., sorting, multiplication  How we characterize input size depends: o Sorting: number of input items o Multiplication: total number of bits o Graph algorithms: number of nodes & edges Etc Sandeep Kumar Poonia
  • 25. Running Time • Number of primitive steps that are executed  Except for time of executing a function call most statements roughly require the same amount of time oy=m*x+b o c = 5 / 9 * (t - 32 ) o z = f(x) + g(y) • We can be more exact if need be Sandeep Kumar Poonia
  • 26. Analysis • Worst case  Provides an upper bound on running time  An absolute guarantee • Average case  Provides the expected running time  Very useful, but treat with care: what is “average”? o Random (equally likely) inputs o Real-life inputs Sandeep Kumar Poonia
  • 27. An Example: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 28. An Example: Insertion Sort 30 10 40 20 1 2 3 4 i =  j =  key =  A[j] =  A[j+1] =  InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 29. An Example: Insertion Sort 30 10 40 20 1 2 3 4 i=2 j=1 A[j] = 30 key = 10 A[j+1] = 10 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 30. An Example: Insertion Sort 30 30 40 20 1 2 3 4 i=2 j=1 A[j] = 30 key = 10 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 31. An Example: Insertion Sort 30 30 40 20 1 2 3 4 i=2 j=1 A[j] = 30 key = 10 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 32. An Example: Insertion Sort 30 30 40 20 1 2 3 4 i=2 j=0 A[j] =  key = 10 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 33. An Example: Insertion Sort 30 30 40 20 1 2 3 4 i=2 j=0 A[j] =  key = 10 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 34. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=2 j=0 A[j] =  key = 10 A[j+1] = 10 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 35. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=3 j=0 A[j] =  key = 10 A[j+1] = 10 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 36. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=3 j=0 A[j] =  key = 40 A[j+1] = 10 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 37. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=3 j=0 A[j] =  key = 40 A[j+1] = 10 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 38. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=3 j=2 A[j] = 30 key = 40 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 39. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=3 j=2 A[j] = 30 key = 40 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 40. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=3 j=2 A[j] = 30 key = 40 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 41. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=4 j=2 A[j] = 30 key = 40 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 42. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=4 j=2 A[j] = 30 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 43. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=4 j=2 A[j] = 30 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 44. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=4 j=3 A[j] = 40 key = 20 A[j+1] = 20 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 45. An Example: Insertion Sort 10 30 40 20 1 2 3 4 i=4 j=3 A[j] = 40 key = 20 A[j+1] = 20 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 46. An Example: Insertion Sort 10 30 40 40 1 2 3 4 i=4 j=3 A[j] = 40 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 47. An Example: Insertion Sort 10 30 40 40 1 2 3 4 i=4 j=3 A[j] = 40 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 48. An Example: Insertion Sort 10 30 40 40 1 2 3 4 i=4 j=3 A[j] = 40 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 49. An Example: Insertion Sort 10 30 40 40 1 2 3 4 i=4 j=2 A[j] = 30 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 50. An Example: Insertion Sort 10 30 40 40 1 2 3 4 i=4 j=2 A[j] = 30 key = 20 A[j+1] = 40 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 51. An Example: Insertion Sort 10 30 30 40 1 2 3 4 i=4 j=2 A[j] = 30 key = 20 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 52. An Example: Insertion Sort 10 30 30 40 1 2 3 4 i=4 j=2 A[j] = 30 key = 20 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 53. An Example: Insertion Sort 10 30 30 40 1 2 3 4 i=4 j=1 A[j] = 10 key = 20 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 54. An Example: Insertion Sort 10 30 30 40 1 2 3 4 i=4 j=1 A[j] = 10 key = 20 A[j+1] = 30 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 55. An Example: Insertion Sort 10 20 30 40 1 2 3 4 i=4 j=1 A[j] = 10 key = 20 A[j+1] = 20 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 56. An Example: Insertion Sort 10 20 30 40 1 2 3 4 i=4 j=1 A[j] = 10 key = 20 A[j+1] = 20 InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Done! Sandeep Kumar Poonia
  • 57. Insertion Sort What is the precondition InsertionSort(A, n) { for this loop? for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } Sandeep Kumar Poonia
  • 58. Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } How many times will } this loop execute? Sandeep Kumar Poonia
  • 59. Insertion Sort Statement Effort InsertionSort(A, n) { for i = 2 to n { c1n key = A[i] c2(n-1) j = i - 1; c3(n-1) while (j > 0) and (A[j] > key) { c4T A[j+1] = A[j] c5(T-(n-1)) j = j - 1 c6(T-(n-1)) } 0 A[j+1] = key c7(n-1) } 0 } T = t2 + t3 + … + tn where ti is number of while expression evaluations for the ith for loop iteration Sandeep Kumar Poonia
  • 60. Analyzing Insertion Sort • • T(n) = c1n + c2(n-1) + c3(n-1) + c4T + c5(T - (n-1)) + c6(T - (n-1)) + c7(n-1) = c8T + c9n + c10 What can T be?  Best case -- inner loop body never executed o ti = 1  T(n) is a linear function  Worst case -- inner loop body executed for all previous elements o ti = i  T(n) is a quadratic function Sandeep Kumar Poonia
  • 61. Analysis • Simplifications  Ignore actual and abstract statement costs  Order of growth is the interesting measure: o Highest-order term is what counts   Sandeep Kumar Poonia Remember, we are doing asymptotic analysis As the input size grows larger it is the high order term that dominates
  • 62. Upper Bound Notation • We say InsertionSort’s run time is O(n2)  Properly we should say run time is in O(n2)  Read O as “Big-O” (you’ll also hear it as “order”) • In general a function  f(n) is O(g(n)) if there exist positive constants c and n0 such that f(n)  c  g(n) for all n  n0 • Formally  O(g(n)) = { f(n):  positive constants c and n0 such that f(n)  c  g(n)  n  n0 Sandeep Kumar Poonia
  • 63. Lower Bound Notation • We say InsertionSort’s run time is (n) • In general a function  f(n) is (g(n)) if  positive constants c and n0 such that 0  cg(n)  f(n)  n  n0 • Proof:  Suppose run time is an + b o Assume a and b are positive (what if b is negative?)  an  an + b Sandeep Kumar Poonia
  • 64. Asymptotic Tight Bound • A function f(n) is (g(n)) if  positive constants c1, c2, and n0 such that c1 g(n)  f(n)  c2 g(n)  n  n0 • Theorem  f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n)) Sandeep Kumar Poonia
  • 65. Practical Complexity 250 f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Sandeep Kumar Poonia
  • 66. Practical Complexity 500 f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Sandeep Kumar Poonia
  • 67. Practical Complexity 1000 f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n 0 1 Sandeep Kumar Poonia 3 5 7 9 11 13 15 17 19
  • 68. Practical Complexity 5000 4000 f(n) = n f(n) = log(n) 3000 f(n) = n log(n) f(n) = n^2 2000 f(n) = n^3 f(n) = 2^n 1000 0 1 Sandeep Kumar Poonia 3 5 7 9 11 13 15 17 19
  • 70. Other Asymptotic Notations • A function f(n) is o(g(n)) if  positive • • constants c and n0 such that f(n) < c g(n)  n  n0 A function f(n) is (g(n)) if  positive constants c and n0 such that c g(n) < f(n)  n  n0 Intuitively,  o() is like <  O() is like  Sandeep Kumar Poonia  () is like >  () is like   () is like =