Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
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
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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 1
Solution 1
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..
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
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)
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
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.
• 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
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).
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.
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.
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