77
Design and Analysis of Algorithms Instructor : Reena Pagare

Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Embed Size (px)

Citation preview

Page 1: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Design and Analysis of Algorithms

Instructor : Reena Pagare

Page 2: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Caution

Please finish your assignments independently.

Power off (or ring off) your mobile phone. Keep your mobile phones in the bag and not on desk.

Feedback (positive or negative) is encouraged and welcome, and IMPORTANT. (ask questions!)

Thinking, before, in and after class is encouraged and IMPORTANT.

Reena Pagare , MAEER's MITCOE , SPPU

Page 3: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Why Study this Course?

Donald E. Knuth stated “Computer Science is the study of algorithms”

Cornerstone of computer science. Programs will not exist without

algorithms.

Basic for any Computer Science Degree

Applications :

Computational Primitives – CG – Geometric Algorithms

Communication Network – Shortest path Algorithms

Genome Structure in Bioinformatics – Dynamic Programming

Search engines – Page Rank Algorithm by Google

Challenging (i.e. Good for brain !!!)

Real Blend of Creativity and Precision

Very interesting if you can concentrate on this course

Reena Pagare , MAEER's MITCOE , SPPU

Page 4: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Course Prerequisite

Data Structure

Discrete Mathematics

C, Java or other programming languages

Advanced Mathematics

Reena Pagare , MAEER's MITCOE , SPPU

Page 5: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Program Educational Objectives

Industry-ready and knowledgeable engineers: To prepare graduates to

acquire a fulfilling professional knowledge and knowledge of management

practices for employment in industry or academia, and postgraduate study

in engineering

Productive and modern engineers: Develop competence in applying

knowledge of mathematics, science, and engineering; enabling graduates to

solve engineering problems in a modern technological society as valuable

productive engineers

Innovative thinkers and researchers: To provide an individual with an

academic environment and to motivate and inculcate innovative thinking

and research capability using modern tools and techniques.

Reena Pagare , MAEER's MITCOE , SPPU

Page 6: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Programme Outcomes

a. Graduates will demonstrate an ability to apply knowledge of mathematics, science and

engineering

b. Graduates will demonstrate an ability to design and conduct experiments, as well as to

analyze and interpret data

c. Graduates will demonstrate an ability to design a system, component, or process to

meet desired needs within realistic constraints such as economic, environmental, social,

political, ethical, health and safety, manufacturability, and sustainability

e. Graduates will demonstrate an ability to identify, formulate, and solve complex

engineering problems.

Program Specific Outcomes• PSO-2 Ability to innovate in computing methodology for optimized solution

for socio-economic benefit

Reena Pagare , MAEER's MITCOE , SPPU

Page 7: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Objectives & Outcomes

. Course Outcomes

CO-I Use different computational models to analyze the algorithms

CO-II Design and analysis of algorithm strategies

CO-III Ability to differentiate between different computational problems

CO-IV To solve problems for multicore or distributed or

concurrent/parallel/embedded environments

Course Objectives:•To develop problem solving abilities using mathematical theories

•To apply algorithmic strategies while solving problems

•To develop time and space efficient algorithms

•To study algorithmic examples in distributed, concurrent and parallel environments

Reena Pagare , MAEER's MITCOE , SPPU

Page 8: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Expected Outcomes

Student should be able to

Define algorithm formally and informally.

Explain the idea of different algorithms

Describe the process of algorithm design and analysis

Design recursive and non-recursive algorithms

Reena Pagare , MAEER's MITCOE , SPPU

Page 9: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Course Assessment

Assessment type CO-I CO-II CO-III CO-IV

Assignment-I 40% 40% 30%

Assignment-II 30% 30% 30% 40%

Tutorial

Quiz

End- Term exam 10% 10% 20% 30%

Class test

Lab session 20% 20% 20% 30%

Project

Seminar

Special(*)

Total 100% 100% 100% 100%

Reena Pagare , MAEER's MITCOE , SPPU

Page 10: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Algorithm: A brief History

Muhammad ibn Musa al-Khwarizmi, one

of the most influential mathematicians in 9th

century in Baghdad, wrote a textbook in Arabic

about adding, multiplying, dividing numbers,

and extracting square roots and computing π.

www.lib.virginia.edu/science/parshall/khwariz.html

Many centuries later, decimal system was

adopted in Europe, and the procedures in Al

Khwarizmi’s book were named after him as

“Algorithms”. (image of Al Khwarizmi from

http://jeff560.tripod.com/)

Reena Pagare , MAEER's MITCOE , SPPU

Page 11: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Notion: Algorithm

You all have learned the course DATA

STRUCTURE or DISCRETE MATHEMATICS,

and have your own sense on algorithm.

to your opinion, what is algorithm?

Reena Pagare , MAEER's MITCOE , SPPU

Page 12: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Notion: Algorithms

“computer”

problem

algorithm

input output

An algorithm is a sequence of unambiguous instructions for solving acomputational problem, i.e., for obtaining a required output for anylegitimate input in a finite amount of time.

Reena Pagare , MAEER's MITCOE , SPPU

Page 13: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Notion: Algorithm

More precisely, an algorithm is a method or process to solve a

problem satisfying the following properties:

Finiteness

– terminates after a finite number of steps

Definiteness

– Each step must be rigorously and unambiguously specified

Input

– Valid inputs must be clearly specified.

Output

– can be proved to produce the correct output given a valid input.

Effectiveness

– Steps must be sufficiently simple and basic.

Reena Pagare , MAEER's MITCOE , SPPU

Page 14: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Notion: Algorithm*

According to the definition of Algorithms,

“almost all” problems are unsolvable. For

example:

Determine if a polynomial with n (n > 1) variables has

an integral solution.

Determine if there is a ‘1111111’ pattern in ’s

decimal format.

Halting Problem:

– Determine if a program halts on an input

Reena Pagare , MAEER's MITCOE , SPPU

Page 15: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Examples of Algorithms – Computing the Greatest Common Divisor of Two Integers

gcd(m, n): the largest integer that divides both m and n.

First try -- Euclid’s algorithm: gcd(m, n) = gcd(n, m mod n)

Step1: If n = 0, return the value of m as the answer and stop; otherwise,

proceed to Step 2.

Step2: Divide m by n and assign the value of the remainder to r.

Step 3: Assign the value of n to m and the value of r to n. Go to Step 1.

Reena Pagare , MAEER's MITCOE , SPPU

Page 16: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Methods of Specifying an Algorithm

Natural language

Ambiguous

“Mike ate the sandwich on a bed.”

Pseudocode

A mixture of a natural language and programming language-like structures

Precise and succinct.

Pseudocode in this course

– omits declarations of variables

– use indentation to show the scope of such statements as for, if, and while.

– use for assignment

Reena Pagare , MAEER's MITCOE , SPPU

Page 17: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Pseudocode of Euclid’s Algorithm

Algorithm Euclid(m, n)

//Computes gcd(m, n) by Euclid’s algorithm

//Input: Two nonnegative, not-both-zero integers m and n

//Output: Greatest common divisor of m and n

while n ≠ 0 do

r m mod n

m n

n r

return m

Questions:

Finiteness: how do we know that Euclid’s algorithm actually comes to a stop?

Definiteness: nonambiguity

Effectiveness: effectively computable.

Reena Pagare , MAEER's MITCOE , SPPU

Page 18: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Second Try for gcd(m, n)

Consecutive Integer Algorithm

Step1: Assign the value of min{m, n} to t.

Step2: Divide m by t. If the remainder of this division is 0, go to Step3;otherwise, go to Step 4.

Step3: Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step4.

Step4: Decrease the value of t by 1. Go to Step2.

Questions Finiteness

Definiteness

Effectiveness

Which algorithm is faster, the Euclid’s or this one?

Reena Pagare , MAEER's MITCOE , SPPU

Page 19: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Third try for gcd(m, n)

Middle-school procedure Step1: Find the prime factors of m.

Step2: Find the prime factors of n.

Step3: Identify all the common factors in the two prime expansions found in Step1 and Step2. (If p is a common factor occurring Pm and Pn times in m and n, respectively, it should be repeated in min{Pm, Pn} times.)

Step4: Compute the product of all the common factors and return it as the gcd of the numbers given.

Reena Pagare , MAEER's MITCOE , SPPU

Page 20: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

What can we learn from the previous 3 examples?

Each step of an algorithm must be unambiguous.

The same algorithm can be represented in several different ways.

(different pseudocodes)

There might exists more than one algorithm for a certain problem.

Algorithms for the same problem can be based on very different ideas

and can solve the problem with dramatically different speeds.

Reena Pagare , MAEER's MITCOE , SPPU

Page 21: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Fundamentals of Algorithmic Problem Solving

Understanding the problem Asking questions, do a few examples by hand, think about special

cases, etc.

Deciding on Exact vs. approximate problem solving

Appropriate data structure

Design an algorithm

Proving correctness

Analyzing an algorithm Time efficiency : how fast the algorithm runs

Space efficiency: how much extra memory the algorithm needs.

Coding an algorithm

Reena Pagare , MAEER's MITCOE , SPPU

Page 22: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Algorithm Design and Analysis Process

Reena Pagare , MAEER's MITCOE , SPPU

Page 23: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Two main issues related to algorithms

How to design algorithms

How to analyze algorithm efficiency

Reena Pagare , MAEER's MITCOE , SPPU

Page 24: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Algorithm Design Techniques/Strategies

Brute force

Divide and conquer

Space and time

tradeoffs

Greedy approach

Dynamic programming

Backtracking

Branch and bound

Reena Pagare , MAEER's MITCOE , SPPU

Page 25: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Analysis of Algorithms

How good is the algorithm?

time efficiency

space efficiency

Does there exist a better algorithm?

lower bounds

optimality

Reena Pagare , MAEER's MITCOE , SPPU

Page 26: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Example: Fibonacci NumberFibonacci’s original question: Suppose that you are given a newly-born pair of rabbits,

one male, one female.

Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits.

Suppose that our rabbits never die.

Suppose that the female always produces one new pair (one male, one female) every month.

Question: How many pairs will there be in one year? 1. In the beginning: (1 pair)

2. End of month 1: (1 pair) Rabbits are ready to mate.

3. End of month 2: (2 pairs) A new pair of rabbits are born.

4. End of month 3: (3 pairs) A new pair and two old pairs.

5. End of month 4: (5 pairs) ...

6. End of month 5: (8 pairs) ...

7. after 12 months, there will be 233 rabits

(image of Leonardo Fibonacci from

http://www.math.ethz.ch/fibonacci)

Reena Pagare , MAEER's MITCOE , SPPU

Page 27: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Recurrence Relation of Fibonacci Number fib(n):

{0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …}

Reena Pagare , MAEER's MITCOE , SPPU

Page 28: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Algorithm: Fibonacci

Problem: What is fib(200)? What about fib(n), where n is any

positive integer?

Algorithm 1 fib(n)

if n = 0 then

return (0)

if n = 1then

return (1)

return (fib(n − 1) + fib(n − 2))

Questions that we should ask

ourselves.

1. Is the algorithm correct?

2. What is the running time of our

algorithm?

3. Can we do better?

Reena Pagare , MAEER's MITCOE , SPPU

Page 29: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Answer of Questions

Is the algorithm correct? Yes, we simply follow the definition of Fibonacci numbers

How fast is the algorithm? If we let the run time of fib(n) be T(n), then we can formulate

T(n) = T(n − 1) + T(n − 2) + 3 1.6n

T(200) 2139

The world fastest computer BlueGene/L, which can run 248 instructions per second, will take 291 seconds to compute. (291 seconds = 7.85 × 1019 years )

Can Moose’s law, which predicts that CPU get 1.6 times faster each year, solve our problem?

No, because the time needed to compute fib(n) also have the same “growth” rate

– if we can compute fib(100) in exactly a year,

– then in the next year, we will still spend a year to compute fib(101)

– if we want to compute fib(200) within a year, we need to wait for 100 years.

Reena Pagare , MAEER's MITCOE , SPPU

Page 30: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Improvement

Can we do better?

Yes, because many computations in the previous algorithm

arerepeated.

Algorithm 2: fib(n)

comment: Initially we create an array A[0: n]

A[0] ← 0,A[1] ← 1

for i = 2 to n do

A[i] = A[i − 1] + A[i − 2]

return (A[n])

Reena Pagare , MAEER's MITCOE , SPPU

Page 31: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Important problem types

sorting

searching

string processing

graph problems

combinatorial problems

geometric problems

numerical problems

Reena Pagare , MAEER's MITCOE , SPPU

Page 32: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

ASYMPTOTICS

Used to formalize that an algorithm has running

time or storage requirements that are ``never

more than,'' ``always greater than,'' or ``exactly''

some amount

Reena Pagare , MAEER's MITCOE , SPPU

Page 33: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

ASYMPTOTICS NOTATIONSO-notation (Big Oh)

Asymptotic Upper Bound

For a given function g(n), we denote O(g(n)) as

the set of functions:

O(g(n)) = { f(n)| there exists positive

constants c and n0 such that

0 ≤ f(n) ≤ c g(n) for all n ≥ n0 }

Reena Pagare , MAEER's MITCOE , SPPU

Page 34: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

ASYMPTOTICS NOTATIONSΘ-notation

Asymptotic tight bound

Θ (g(n)) represents a set of functions such that:

Θ (g(n)) = {f(n): there exist positive

constants c1, c2, and n0 such

that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n)

for all n≥ n0}

Reena Pagare , MAEER's MITCOE , SPPU

Page 35: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

ASYMPTOTICS NOTATIONSΩ-notation

Asymptotic lower bound

Ω (g(n)) represents a set of functions such that:

Ω(g(n)) = {f(n): there exist positive

constants c and n0 such that

0 ≤ c g(n) ≤ f(n) for all n≥ n0}

Reena Pagare , MAEER's MITCOE , SPPU

Page 36: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

O-notation ------------------Less than equal to (“≤”)

Θ-notation ------------------Equal to (“=“)

Ω-notation ------------------Greater than equal to

(“≥”)

Reena Pagare , MAEER's MITCOE , SPPU

Page 37: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Mappings for n2

Reena Pagare , MAEER's MITCOE , SPPU

Page 38: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Bounds of a Function

Reena Pagare , MAEER's MITCOE , SPPU

Page 39: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

c1 , c2 & n0 -> constants

T(n) exists between c1n & c2n

Below n0 we do not plot T(n)

T(n) becomes significant only above n0

Reena Pagare , MAEER's MITCOE , SPPU

Page 40: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Examples of algorithms for sorting techniques and their complexities

Insertion sort : O(n2)

Selection sort : O(n2)

Quick sort : O(n logn)

Merge sort : O(n

logn)

Reena Pagare , MAEER's MITCOE , SPPU

Page 41: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Examples Big Oh (O - Notation)

1

10

100

1,000

10,000

1 10 100 1,000

n

3n 2n+10 n

Example: 2n + 10 is O(n)2n + 10 cn

(c 2) n 10

n 10/(c 2)

Pick c = 3 and n0 = 10

Reena Pagare , MAEER's MITCOE , SPPU

Page 42: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Example: the function

n2 is not O(n)

n2 cn

n c

The above inequality

cannot be satisfied since

c must be a constant

1

10

100

1,000

10,000

100,000

1,000,000

1 10 100 1,000

n

n^2 100n

10n n

Reena Pagare , MAEER's MITCOE , SPPU

Page 43: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

7n-2 is O(n)

need c > 0 and n0 1 such that 7n-2 c•n for n n0

this is true for c = 7 and n0 = 1

3n3 + 20n2 + 5 is O(n3)

need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n n0

this is true for c = 4 and n0 = 21

3 log n + 5 is O(log n)

need c > 0 and n0 1 such that 3 log n + 5 c•log n for n n0

this is true for c = 8 and n0 = 2

Reena Pagare , MAEER's MITCOE , SPPU

Page 44: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Big-Oh Rules

If is f(n) a polynomial of degree d, then f(n) is

O(nd), i.e.,

1. Drop lower-order terms

2. Drop constant factors

Use the smallest possible class of functions

Say “2n is O(n)” instead of “2n is O(n2)”

Use the simplest expression of the class

Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”

Reena Pagare , MAEER's MITCOE , SPPU

Page 45: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Prefix Averages (Quadratic)

The following algorithm computes prefix averages in

quadratic time by applying the definitionAlgorithm prefixAverages1(X, n)

Input array X of n integers

Output array A of prefix averages of X #operations

A new array of n integers n

for i 0 to n 1 do n

s X[0] n

for j 1 to i do 1 + 2 + …+ (n 1)

s s + X[j] 1 + 2 + …+ (n 1)

A[i] s / (i + 1) n

return A 1

Reena Pagare , MAEER's MITCOE , SPPU

Page 46: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Arithmetic Progression

The running time of prefixAverages1 is

O(1 + 2 + …+ n)

The sum of the first n integers is n(n + 1) / 2

There is a simple visual proof of this fact

Thus, algorithm prefixAverages1 runs in O(n2) time

Reena Pagare , MAEER's MITCOE , SPPU

Page 47: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Arithmetic Progression

The running time of

prefixAverages1 is

O(1 + 2 + …+ n)

The sum of the first n

integers is n(n + 1) / 2

There is a simple visual

proof of this fact

Thus, algorithm

prefixAverages1 runs in

O(n2) time 0

1

2

3

4

5

6

7

1 2 3 4 5 6

Reena Pagare , MAEER's MITCOE , SPPU

Page 48: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Prefix Averages (Linear)

The following algorithm computes prefix averages in linear time by

keeping a running sum

Algorithm prefixAverages2(X, n)

Input array X of n integers

Output array A of prefix averages of X #operations

A new array of n integers n

s 0 1

for i 0 to n 1 do n

s s + X[i] n

A[i] s / (i + 1) n

return A 1

Algorithm prefixAverages2 runs in O(n) time

Reena Pagare , MAEER's MITCOE , SPPU

Page 49: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Math you need to Review

properties of logarithms:

logb(xy) = logbx + logby

logb (x/y) = logbx - logby

logbxa = alogbx

logba = logxa/logxb

properties of exponentials:

a(b+c) = aba c

abc = (ab)c

ab /ac = a(b-c)

b = a logab

bc = a c*logab

Reena Pagare , MAEER's MITCOE , SPPU

Page 50: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Example Uses of the Relatives of Big-Oh

5n2 is (n2)f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1 such

that f(n) c•g(n) for n n0

let c = 5 and n0 = 1

5n2 is (n)f(n) is (g(n)) if there is a constant c > 0 and an integer constant n0 1 such

that f(n) c•g(n) for n n0

let c = 1 and n0 = 1

5n2 is (n2)

f(n) is (g(n)) if it is (n2) and O(n2). We have already seen the former, for the latter recall that f(n) is O(g(n)) if there is a constant c > 0 and an integer constant n0 1 such that f(n) < c•g(n) for n n0

Let c = 5 and n0 = 1

Reena Pagare , MAEER's MITCOE , SPPU

Page 51: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Function of Growth rate

Reena Pagare , MAEER's MITCOE , SPPU

Page 52: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

RECURRENCE RELATIONS

A Recurrence is an equation or inequality that

describes a function in terms of its value on

smaller inputs

Special techniques are required to analyze the

space and time required

Reena Pagare , MAEER's MITCOE , SPPU

Page 53: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

RECURRENCE RELATIONS EXAMPLE

EXAMPLE 1: QUICK SORT

T(n)= 2T(n/2) + O(n)

T(1)= O(1)

In the above case the presence of function of T on both sides of the equation signifies the presence of recurrence relation

(SUBSTITUTION MEATHOD used) The equations are simplified to produce the final result:

……cntd

Reena Pagare , MAEER's MITCOE , SPPU

Page 54: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

T(n) = 2T(n/2) + O(n)

= 2(2(n/22) + (n/2)) + n

= 22 T(n/22) + n + n

= 22 (T(n/23)+ (n/22)) + n + n

= 23 T(n/23) + n + n + n

= n log n

Reena Pagare , MAEER's MITCOE , SPPU

Page 55: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

EXAMPLE 2: BINARY SEARCH

T(n)=O(1) + T(n/2)

T(1)=1

Above is another example of recurrence relation and the way

to solve it is by Substitution.

T(n)=T(n/2) +1

= T(n/22)+1+1

= T(n/23)+1+1+1

= logn

T(n)= O(logn)

Reena Pagare , MAEER's MITCOE , SPPU

Page 56: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

The Recursion Tree

depth T’s size

0 1 n

1 2 n/2

i 2i n/2i

… … …

time

bn

bn

bn

+

=

2if)2/(2

2if )(

nbnnT

nbnT

Draw the recursion tree for the recurrence relation and look

for a pattern:

Total time = bn + bn log n

(last level plus all previous levels)

Reena Pagare , MAEER's MITCOE , SPPU

Page 57: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Master Method (Appendix)

+

=

dnnfbnaT

dncnT

if)()/(

if )(

Many divide-and-conquer recurrence equations have the form:

.1 somefor )()/( provided

)),((is)(then),(is)(if 3.

)log(is)(then),log(is)(if 2.

)(is)(then),(is)(if 1.

log

1loglog

loglog

+

+

nfbnaf

nfnTnnf

nnnTnnnf

nnTnOnf

a

kaka

aa

b

bb

bb

The Master Theorem:

Reena Pagare , MAEER's MITCOE , SPPU

Page 58: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Master Method, Example 1The form:

The Master Theorem:

Example:

+

=

dnnfbnaT

dncnT

if)()/(

if )(

.1 somefor )()/( provided

)),((is)(then),(is)(if 3.

)log(is)(then),log(is)(if 2.

)(is)(then),(is)(if 1.

log

1loglog

loglog

+

+

nfbnaf

nfnTnnf

nnnTnnnf

nnTnOnf

a

kaka

aa

b

bb

bb

nnTnT += )2/(4)(Solution: logba=2, so case 1 says T(n) is O(n2).

Reena Pagare , MAEER's MITCOE , SPPU

Page 59: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Master Method, Example 2

The form:

The Master Theorem:

Example:

+

=

dnnfbnaT

dncnT

if)()/(

if )(

.1 somefor )()/( provided

)),((is)(then),(is)(if 3.

)log(is)(then),log(is)(if 2.

)(is)(then),(is)(if 1.

log

1loglog

loglog

+

+

nfbnaf

nfnTnnf

nnnTnnnf

nnTnOnf

a

kaka

aa

b

bb

bb

3)3/(9)( nnTnT +=Solution: logba=2, so case 3 says T(n) is O(n3).

Reena Pagare , MAEER's MITCOE , SPPU

Page 60: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Master Method, Examples

2

( ) = 3 ( / 4) .

( ) 9 ( / 3) .

( ) (2 / 3) 1.

( ) 3 ( / 4) log .

( ) 7 ( / 2) ( ).

( ) 2 ( / 2) log .

( ) ( /

Examples: solve these recurrences

T n T n n

T n T n n

T n T n

T n T n n n

T n T n n

T n T n n n

T n T n

+

= +

= +

= +

= +

=

= +

3) (2 / 3) .T n n+ +

Reena Pagare , MAEER's MITCOE , SPPU

Page 61: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

A general divide-and-conquer algorithm

Step 1: If the problem size is small, solve this

problem directly; otherwise, split the

original problem into 2 sub-problems with

equal sizes.

Step 2: Recursively solve these 2 sub-problems

by applying this algorithm.

Step 3: Merge the solutions of the 2 sub-

problems into a solution of the original

problem.

Reena Pagare , MAEER's MITCOE , SPPU

Page 62: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Given an instance of a problem, the

method works as follows:

DAQ( )

is sufficiently small

solve it directly

divide-and-conquer

Divide and Conquer

x

x

x

function

if then

1

divide into smaller subinstances , … , ;

1 DAQ( );

combine these 's to obtain a solution for ;

( )

k

i i

i

x x x

i k y x

y y x

y

else

for to do

return

Reena Pagare , MAEER's MITCOE , SPPU

Page 63: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

1

0

Typically, , … , are of the same size, say .

In that case, the time complexity of DAQ, ( ), satisfies

a recurrence:

if ( )

Analysis of Divide-and-Conquer

kx x n b

T n

c n nT n

kT n b

=

+ 0

0

( ) if

Where ( ) is the running time of dividing and

combining 's.

What is ?

What is ?

i

f n n n

f n x

y

c

n

Reena Pagare , MAEER's MITCOE , SPPU

Page 64: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Example -- Maxima in set

finding the maximum of a set S of n numbers

Reena Pagare , MAEER's MITCOE , SPPU

Page 65: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Time complexity: Maxima

T(n): # of comparisons

Calculation of T(n):

Assume n = 2k,

T(n) = 2T(n/2)+1

= 2(2T(n/4)+1)+1

= 4T(n/4)+2+1

:

=2k-1T(2)+2k-2+…+4+2+1

=2k-1+2k-2+…+4+2+1

=2k-1 = n-1

Reena Pagare , MAEER's MITCOE , SPPU

Page 66: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Binary Search

e.g. 2 4 5 6 7 8 9

search 7: needs 3 comparisons

time: O(log n)

The binary search can be used only if the

elements are sorted and stored in an array.

Reena Pagare , MAEER's MITCOE , SPPU

Page 67: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Binary Search – cont..

Input: A sorted sequence of n elements stored in an array.

Output: The position of x (to be searched).

Step 1: If only one element remains in the array, solve it directly.

Step 2: Compare x with the middle element of the array.

Step 2.1: If x = middle element, then output it and stop.

Step 2.2: If x < middle element, then recursively solve the problem with x and the left half array.

Step 2.3: If x > middle element, then recursively solve the problem with x and the right half array.

Reena Pagare , MAEER's MITCOE , SPPU

Page 68: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Algorithm for BS

// a[]: sorted sequence in nondecreasing order

// low, high: the bounds for searching in a []

// x: the element to be searched

// If x = a[j], for some j, then return j else return –1

if (low > high) then return –1 // invalid range

if (low = high) then // if small P

if (x == a[i]) then return i

else return -1

else // divide P into two smaller subproblems

mid = (low + high) / 2

if (x == a[mid]) then return mid

else if (x < a[mid]) then

return BinSearch(a, low, mid-1, x)

else return BinSearch(a, mid+1, high, x)

Reena Pagare , MAEER's MITCOE , SPPU

Page 69: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

mergesort [ .. ]

// Sort [ .. ]//

Sort an array [1.. ]

// base

( ) 2

mergesort [ .. ]

mergesort [ 1.. ]

merge [

procedure

if th

..

en return case //

],

Mergesort:

A i j

A i j

i j

m i j

A i m

A m j

A i m

A n

=

+

+

divide and con

[ 1.. ] ( [ .. ]

Initial call: mergesort [1.. ]

quer

A m j A i m

A n

+

Reena Pagare , MAEER's MITCOE , SPPU

Page 70: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Let ( ) denote the running time of mergesorting an

array of size .

( ) satisfies the recurrence:

if 1 ( )

2 2 ( ) if 1

Solving the rec

Analysis of Mergesort

T n

n

T n

c nT n

T n T n n n

=

+ +

urrence yields:

( ) ( log )

We will learn how to solve such recurrences.

T n n n=

Reena Pagare , MAEER's MITCOE , SPPU

Page 71: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

MERGE SORT – Linked List

mergesort ,

// Sort [ .. ]. Initially, [ ]

function

globa

= 0, 1 .//

[1.. ],

// base case

l

/

[1.. ]

if then retu ( )

rn /

Linked-List Version of Mergesort

i j

A i j link k k n

A n link n

i j i=

( ) 2

1 mergesort ,

2 mergesort 1,

divide

merge

and conquer

1,

return

2

) (

m i j

ptr i m

ptr m j

ptr ptr ptr

ptr

+

+

Reena Pagare , MAEER's MITCOE , SPPU

Page 72: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Merge Sort – Linked List

Suppose a function ( ) satisfies the recurrence

if 1 ( )

3 4 if 1

where is a positive constant.

Wish to obtain a function ( ) such that ( )

Solving Recurrences

T n

c nT n

T n n n

c

g n T n

=

+

= ( ( )).

Will solve it using various methods: Iteration Method,

Recurrence Tree, Guess and Prove, and Master Method

g n

Reena Pagare , MAEER's MITCOE , SPPU

Page 73: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Merge Sort – Linked List

2 3 3

Assume is a power of 4. Say, 4 . Then,

( ) 3 ( / 4)

= 3 / 4 3 ( / 16)

= 3( / 4) 9 ( / 16) 3 ( / 64)

= (3 / 4) (3 / 4) 3 ( / 4 )

3 3 = 1

4

Iteration Method

mn n

T n n T n

n n T n

n n n T n

n n n T n

n

=

= +

+ +

+ + +

+ + +

+ +

2 13

34 4 4

= (1) ( )

So, ( ) ( | a power of 4) ( ) ( ). (Why

( )

) ?

m

m

m

n

nT

n O n

T n n n T n n

+ + +

+

= =

Reena Pagare , MAEER's MITCOE , SPPU

Page 74: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Quick Sort

Sort into nondecreasing order[26 5 37 1 61 11 59 15 48 19]

[26 5 19 1 61 11 59 15 48 37]

[26 5 19 1 15 11 59 61 48 37]

[11 5 19 1 15] 26 [59 61 48 37]

[11 5 1 19 15] 26 [59 61 48 37]

[ 1 5] 11 [19 15] 26 [59 61 48 37]

1 5 11 15 19 26 [59 61 48 37]

1 5 11 15 19 26 [59 37 48 61]

1 5 11 15 19 26 [48 37] 59 [61]

1 5 11 15 19 26 37 48 59 61Reena Pagare , MAEER's MITCOE , SPPU

Page 75: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Algorithm Quicksort

Input: A set S of n elements.

Output: The sorted sequence of the inputs in

nondecreasing order.

Step 1: If |S|2, solve it directly.

Step 2: (Partition step) Use a pivot to scan all

elements in S. Put the smaller elements in S1, and

the larger elements in S2.

Step 3: Recursively solve S1 and S2.

Reena Pagare , MAEER's MITCOE , SPPU

Page 76: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Time complexity of Quicksort

time in the worst case:

(n-1)+(n-2)+...+1 = n(n-1)/2= O(n2)

time in the best case:

In each partition, the problem is always divided

into two subproblems with almost equal size.

... ...

...

log2n

n

×2 = nn2

×4 = nn4

Reena Pagare , MAEER's MITCOE , SPPU

Page 77: Design and Analysis of Algorithms · PDF fileDescribe the process of algorithm design and analysis ... Assignment-I 40% 40% 30% ... Appropriate data structure Design an algorithm

Time complexity of the best case

T(n): time required for sorting n elements

T(n)≦ cn+2T(n/2), for some constant c.

≦ cn+2(c.n/2 + 2T(n/4))

≦ 2cn + 4T(n/4)

≦ cnlog2n + nT(1) = O(nlogn)

Reena Pagare , MAEER's MITCOE , SPPU