Вы находитесь на странице: 1из 25

DESIGN AND ANALYSIS OF ALGORITHMS

CSE 408

UNIT 1

•ALGORITHMS
•FUNDAMENTAL OF ALGORITHMIC PROBLEM SOLVING
•FUNDAMENTAL DATA STRUCTURE
•FUNDAMENTALS OF ANALYSIS OF ALGORITHMIC EFFICIENCY
•ASYMPTOTIC NOTATIONS
•USING LIMITS FOR COMPARING ORDERS OF GROWTH

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
DESIGN AND ANALYSIS OF ALGORITHMS

PROBLEM

NUMBER OF SOLUTIONS (ALGORITHMS) (DESIGN)

SELECT THE OPTIMAL ONE (ANALYSIS)

IMPLEMENT IN SOME PROGRAMMING LANGUAGE

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
ALGORITHMS

 Definition
 Properties
 Example (gcd)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
ALGORITHM

SIMPLY,
It is any well defined computational procedure that takes some
value or set of values, as input and produce some value , or set
of values as output.

OR

A set of computational steps that transform the input into the


output.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate(justified) input in a finite
amount of time.
problem

algorithm

input “computer” output

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
* *unambiguous: not open to more than one interpretation
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
REQUIREMENTS/PROPERTIES
An algorithm has the following requirements:
1. Finiteness
Algorithm must complete after a finite number of instructions has been
executed.
2. Definiteness
• Every step must be clearly defined, having only one interpretation
• It simply means each instruction is clear and unambiguous.
• Instructions such as : “compute x/0” or “subtract 7 or 6 or x” are not
permitted.
3. Clearly specified input
Number and types of valid inputs are clearly specified
4. Clearly specified/expected output
correct output should be produced for a valid input
5. Effectiveness
every instruction must be so clear that we would implement it with pen
and paper. It should not be complex.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Example of computational problem: sorting

 Statement of problem:
• Input: A sequence of n numbers <a1, a2, …, an>

• Output: A reordering of the input sequence <a´1, a´2, …, a´n> so that a´i
≤ a´j whenever i < j

 Instance: The sequence <5, 3, 2, 8, 3>

 Algorithms:
• Selection sort
• Insertion sort
• Merge sort
• (many others)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Problem: Greatest common divisor

Problem: Find gcd(m,n), the greatest common divisor of two


nonnegative, not both zero integers m and n

Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?

Solutions introduced are:


• Euclid’s Algorithm
• Consecutive integer checking algorithm
• Middle-school procedure

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Solution 1

Euclid’s algorithm is based on repeated application of equality


gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the problem trivial.

Example: gcd(60,24)
Here m=60 and n=24
Step 1: gcd(m,n) = gcd(n, m mod n)
gcd(24, 60mod24) = gcd(24,12)
Since second number is not zero so again..

Step 2: gcd(m,n) = gcd(n, m mod n)


gcd(12, 24mod12) = gcd(12,0)
Since second number is 0 so, Answer is 12
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
nd ed., Ch. 1
Euclid’s algorithm

Step 1 If n = 0, return m and stop; otherwise go to Step 2


Step 2 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.

while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Complexity is O(log n)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Solution 2

Consecutive integer checking algorithm


Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return t and stop;
otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2

Complexity: O(n)

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Solution 3

Middle-school procedure
Step 1 Find the prime factorization of m
Step 2 Find the prime factorization of n
Step 3 Find all the common prime factors
Step 4 Compute the product of all the common prime factors
and return it as gcd(m,n)

Time complexity: O(sqrt(n))

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING

 Algorithmic design and analysis process


 Basic Algorithm design techniques
 Analyzing algorithms

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Algorithmic design and analysis process

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
1. Understanding the problem:
• Understand the given problem completely.
• It will helps to understand how such an algo works and to
know its strengths and weaknesses, especially when you have
to choose among several algorithms.
• An input to an algorithm specifies an instance of the problem
the algorithm solves.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
2. Decide on:
• Computational means: platform an algo requires. Whether it
is sequential algorithm or parallel algorithm.

• exact vs approximate solving:


exact algorithms are algorithms that always solve an
optimization problem to optimality .
approximation algorithms are efficient algorithms that find
approximate solutions or the solutions which are close to
optimal.

• Data structure
• Algorithmic design technique
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
nd ed., Ch. 1
ALGORITHM DESIGN TECHNIQUES
For a given problem, there are many ways to design algorithms
for it.
Following is the list of several popular design techniques:
 Divide and Conquer
 Greedy approach
 Dynamic Programming
 Randomized Algorithms
 Backtracking Algorithms
etc.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Divide and Conquer

• Divide the original problem into a set of sub problems.


• Solve every sub problem individually, recursively.
• Combine the solution of the sub problems into a solution of the
whole original problem.

Example: merge sort

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Greedy approach
Greedy algorithms solve problems by making the choice that
seems best at the particular moment.
At each phase:
• We take the best we can get right now, without regard for
future consequences.
• We hope that by choosing a local optimum at each step, we
will end up at a global optimum.
• It may not always provide optimal solution but the solution will
be close to optimal.
Example: Prim’s and Kruskal’s Algorithm

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Dynamic Programming
• It is a problem solving technique like D & C, solves problems
by dividing them into sub problems.
• Dynamic programming is used when the sub problems are not
independent.
• Same sub problem will not be solved multiple times but the
prior result will be used to optimize the solution. e.g.
In Fibonacci series:
Fib(4) = Fib(3) + Fib(2)
(Fib(2) + Fib(1)) + Fib(2)
((Fib(1) + Fib(0)) + Fib(1)) + Fib(2)
((Fib(1) + Fib(0)) + Fib(1)) + (Fib(1) + Fib(0))
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
• Here, call to Fib(1) and Fib(0) is made multiple times.
• In the case of Fib(100) these calls would be count for million
times.
• Hence there is lots of wastage of resouces(CPU cycles &
Memory for storing information on stack).

Example: Matrix Chain multiplication


Longest common subsequence

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Randomized algorithms
An algorithm that uses random numbers to decide what to do next
anywhere in its logic.
Example: Randomized quick sort

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Backtracking algorithms
• It try each possibility until they find the right one.
• During the search, if an alternative doesnot work, the vearch
backtracks to the choice point, the place which presented
different alternatives, and tries the next alternative.

Example: N queen problem


Hamilton circuit problem

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
3. Design an algorithm
• A pseudo code is preferred, it is a mixture of a natural language
and programming language.

4. Proving its correctness


• Once an algorithm has been specified, correctness has to be
proved i.e. an algo should yields a required result for every
legitimate input in a finite amount of time.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
5. Analyzing an algorithm
• After correctness, the most important thing is efficiency.
• There are two kinds of algorithm efficiency:
Time efficiency : It indicates how fasts the algorithm runs.
Space efficiency: It indicates how much memory the algorithm
needs to run to completion.

6. Coding an algorithm

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1

Вам также может понравиться