Unit I
Unit I
Unit I
Introduction
1-0
Algorithm
An Algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.
1-1
Notion of algorithm
problem
algorithm
input
computer
output
Algorithmic solution
Design and Analysis of Algorithms Unit I
1-2
PSEUDOCODE
Pseudocode (pronounced SOO-doh-kohd) is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language rather than in a programming language. It is sometimes used as a detailed step in the process of developing a program. It allows programmers to express the design in great detail and provides programmers a detailed template for the next step of writing code in a specific programming language.
Design and Analysis of Algorithms Unit I
1-3
1-4
1-5
Some Keywords
As verbs, use the words generate, Compute, Process, Set, reset, increment, calculate, add, sum, multiply, ... print, display, input, output, edit, test , etc.
1-6
M-1 M-2
M-3
1-7
Euclids Algorithm
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) = ? Euclids 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) = gcd(24,12) = gcd(12,0) = 12
1-8
1-10
Is this an algorithm?
1-11
Sieve of Eratosthenes
Input: Integer n 2 Output: List of primes less than or equal to n for p 2 to n do A[p] p for p 2 to n do if A[p] { 0 //p hasnt been previously eliminated from the list j p* p while j n do A[j] j
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Design and Analysis of Algorithms Unit I
1-12
1-13
1-14
1-15
1-16
1-17
1-18
1-19
1-20
1-22
1-24
1-25
1-26
1-27
1-28
1-29
Sorting
The sorting problem asks us to rearrange the items of a given list in ascending order. we usually need to sort lists of numbers, characters from an alphabet, character strings, records similar to those maintained by schools about their students, libraries about their holdings, companies about their employees.
Design and Analysis of Algorithms Unit I
1-30
Searching
The searching problem deals with finding a given value, called a search key, in a given set (or a multiset, which key, permits several elements to have the same value).
1-31
String Processing
A string is a sequence of characters from an alphabet. String of particular interest: 1. Text string comprises letters, numbers, and special characters 2. Bit string comprises zeros and ones 3. Gene sequence Mainly string matching problem: searching for a given problem: word in a text
1-32
Graph Problems
A graph can be thought of as a collection of points called vertices, some of which are connected by line segments called edges. Used for modeling a wide variety of real-life applications. realBasic graph algorithms include: 1. Graph traversal algorithms - How can one visit all the points in a network? 2. Shortest-path algorithms - What is the best Introduction Shortestroute between two cities? 3. Topological sorting for graphs with directed edges
1-33
Combinatorial Problems
combinatorial problems: problems that ask (explicitly or implicitly) to find a combinatorial objectsuch as a object permutation, a combination, or a subsetthat satisfies subset certain constraints and has some desired property (e.g., maximizes a value or minimizes a cost). 1. Combinatorial grows extremely fast with problem size 2. No known algorithm solving most such problems exactly in an acceptable amount of time.
1-34
Geometric Problems
Geometric algorithms deal with geometric objects such as points, lines, and polygons. 2 class problems: The closest pair problem: given n points in the plane, find problem: the closest pair among them. The convex hull problem asks to find the smallest convex polygon that would include all the points of a given set. If
1-35
1-36
Numerical Problems
Numerical problems, another large special area of problems, applications, are problems that involve mathematical objects of continuous nature: solving equations and systems of equations, computing definite integrals, evaluating functions, and so on.
1-37
1-38
Analysis of algorithms
Issues: correctness time efficiency space efficiency optimality Approaches: theoretical analysis empirical analysis
Design and Analysis of Algorithms Unit I
1-39
T(n) copC(n)
running time execution time for basic operation or cost Number of times basic operation is executed
Matrix dimensions or Multiplication of total number of two numbers elements nsize = number of digits (in binary representation) Division Visiting a vertex or traversing an edge 1-41
Typical graph #vertices and/or problem edges Design and Analysis of Algorithms Unit I
1-42
Efficiencies
Worst Case Efficiency: Is its efficiency for the worst case input of size n, which is an input of size n for which the algorithm runs the longest among all possible inputs of that size Cworst(n) BestBest-case efficiency: Is its efficiency for the worst case input of size n, which is an input of size n for which the algorithm runs the fastest among all possible inputs of that size Cbest(n)
1-43
Amortized efficiency
It applies not to a single run of an algorithm, but rather to a sequence of operations performed on the same data structure
1-44
BestBest-case, average-case, worst-case averageworstFor some algorithms, efficiency depends on form of input: Worst case: Cworst(n) maximum over inputs of size n Best case: Cbest(n) minimum over inputs of size n Average case: Cavg(n) average over inputs of size n Number of times the basic operation will be executed on typical input NOT the average of worst and best case Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all possible inputs. So, avg = expected under uniform distribution.
Design and Analysis of Algorithms Unit I
1-45
1-47
Order of growth
Most important: Order of growth within a constant multiple as n Example: How much faster will algorithm run on computer that is twice as fast? How much longer does it take to solve problem of double input size?
1-48
1-49
Asymptotic Notations
O (Big-Oh)-notation (Big-Omega) -notation (Big-Theta) -notation
1-50
1-51
O-notation
Definition: A function t(n) is said to be in O(g(n)), denoted t(n) t( O(g t(n) O(g(n)) is bounded above by some constant multiple of g(n) for all large n, i.e., there exist positive constant c and nonnonnegative integer n0 such that f(n) c g(n) for every n n0 g(
1-52
BigBig-oh
1-53
;-notation
Formal definition A function t(n) is said to be in ;(g(n)), denoted t(n) ;(g(n)), if t(n) is bounded below by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that t(n) u cg(n) for all n u n0
1-54
BigBig-omega
1-55
5-notation
Formal definition A function t(n) is said to be in 5(g(n)), denoted t(n) 5(g(n)), if t(n) is bounded both above and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constant c1 and c2 and some nonnegative integer n0 such that c2 g(n) e t(n) e c1 g(n) for all n u n0
1-56
BigBig-theta
1-57
Theorem
If t1(n) O(g1(n)) and t2(n) O(g2(n)), then t1(n) + t2(n) O(max{g1(n), g2(n)}). The analogous assertions are true for the ;-notation and 5-notation. Proof. There exist constants c1, c2, n1, n2 such that t1(n) e c1*g1(n), for all n u n1 t2(n) e c2*g2(n), for all n u n2 Define c3 = c1 + c2 and n3 = max{n1,n2}. Then t1(n) + t2(n) e c3*max{g1(n), g2(n)}, for all n u n3
1-58
c > 0 order of growth of T(n) = order of growth of g(n) order of growth of T(n) > order of growth of g(n)
1-60
f(n) g(n)
lim
npg
f (n) g (n)
1-61
All polynomials of the same degree k belong to the same class: aknk + ak-1nk-1 + + a0 5(nk) Exponential functions an have different orders of growth for different as order log n < order nE (E>0) < order an < order n! < order nn
Design and Analysis of Algorithms Unit I
1-62
1-64
1-66
1-70
1-72
M(n) = 2M(n-1) + 1
1-74
n-2 2 1 1
...
1
...
...
2 1
1-76
A(n) = A( n / 2 ) + 1, A(1) = 0 A(2 k ) = A( 2 k 1 + 1, A( 2 0 = 1 (using the Smoothness Rule) ) ) = (A( 2 k 2) + 1) + 1 = A( 2 k 2) + 2 = A(2 k i) + i = A( 2 k k) + k = k + 0 = log 2 n
Design and Analysis of Algorithms Unit I
1-77