Daa Unit 1
Daa Unit 1
DAA UNIT 1
UNIT – 1
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.
An algorithm, named after the ninth century scholar Abu Jafar Muhammad Ibn Musu Al-
Khowarizmi, is defined as follows:
An algorithm is a set of rules for carrying out calculation either by hand or on a machine.
An algorithm is a finite step-by-step procedure to achieve a required result.
An algorithm is a sequence of computational steps that transform the input into the
output.
An algorithm is a sequence of operations performed on data that have to be organized in
data structures.
An algorithm is an abstraction of a program to be executed on a physical machine (model
of Computation).
Characteristics of an Algorithm:
1. Finiteness. An algorithm must always terminate after a finite number of steps.
2. Definiteness. Each step of an algorithm must be precisely defined; the actions to be
carried out must be rigorously and unambiguously specified for each case.
3. Input. An algorithm has zero or more inputs, i.e, quantities which are given to it initially
before the algorithm begins.
4. Output. An algorithm has one or more outputs i.e, quantities which have a specified
relation to the inputs.
5. Effectiveness. An algorithm is also generally expected to be effective. This means that all
of the operations to be performed in the algorithm must be sufficiently basic that they can
in principle be done exactly and in a finite length of time.
Page 1 of 20
Algorithm Specification:
Algorithm can be described in three ways.
1. Natural language like English: When this way is choused care should be taken, we should
ensure that each & every statement is definite.
2. Graphic representation called flowchart: This method will work well when the algorithm
is small& simple.
3. Pseudo-code Method: In this method, we should typically describe algorithms as program,
which resembles language like c,c++,java etc.
3. An identifier begins with a letter. The data types of variables are not explicitly declared.
Node. Record
{
data type –1 data-1;
.
.
.
data type –n data –n;
node * link;
}
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
6. There are two Boolean values TRUE and FALSE.
Page 2 of 20
As an example, the following algorithm fields & returns the maximum of ‘n’ given numbers:
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local variables.
Types of Algorithms:
1. Brute Force
Brute force is a straightforward approach to solve a problem based on the problem’s
statement and definitions of the concepts involved. It is considered as one of the easiest
approach to apply and is useful for solving small – size instances of a problem.
Some examples of brute force algorithms are: Selection sort, Bubble sort
2. Greedy Algorithms "take what you can get now" strategy
The solution is constructed through a sequence of steps, each expanding a partially
constructed solution obtained so far. At each step the choice must be locally optimal – this
is the central point of this technique.
Examples: Minimal spanning tree, Shortest distance in graphs
3. Divide-and-Conquer, Decrease-and-Conquer
These are methods of designing algorithms that (informally) proceed as follows:
Page 3 of 20
Given an instance of the problem to be solved, split this into several smaller sub-instances
(of the same problem), independently solve each of the sub-instances and then combine
the sub-instance solutions so as to yield a solution for the original instance.
With the divide-and-conquer method the size of the problem instance is reduced by a
factor (e.g. half the input size), while with the decrease-and-conquer method the size is
reduced by a constant.
Examples of divide-and-conquer algorithms:
Binary search in a sorted array (recursion)
Mergesort algorithm, Quicksort algorithm (recursion)
4. Dynamic Programming
One disadvantage of using Divide-and-Conquer is that the process of recursively solving
separate sub-instances can result in the same computations being performed
repeatedly sinceidentical sub-instances may arise. The idea behind dynamic
programming is to avoid this pathology by obviating the requirement to calculate the
same quantity twice. The method usually accomplishes this by maintaining a table of
sub-instance results. Dynamic Programming is a Bottom-Up Technique in which the
smallest sub-instances areexplicitly solved first and the results of these used to construct
solutions to progressively larger sub-instances.
Examples: Fibonacci numbers computed by iteration.
5. Transform-and-Conquer
These methods work as two-stage procedures. First, the problem is modified to be more
amenable to solution.
1. Example: AVL trees guarantee O(nlogn) search time
6. Backtracking and branch-and-bound: generate and test methods
The method is used for state-space search problems. State-space search problems are
problems, where the problem representation consists of:
a. initial state
b. goal state(s)
c. a set of intermediate states
d. a set of operators that transform one state into another. Each operator has
preconditions and postconditions.
7. Backtracking uses depth-first search usually without cost function. The main
algorithm is as follows:
Store the initial state in a stack
While the stack is not empty, do:
a. Read a node from the stack.
b. While there are available operators do:
i. Apply an operator to generate a child
ii. If the child is a goal state – stop
iii. If it is a new state, push the child into the stack
The utility function is used to tell how close is a given state to the goal state and whether
a given state may be considered a goal state.
If no children can be generated from a given node, then we backtrack – read the next
node from the stack.
8. Genetic Algorithms
Page 4 of 20
Genetic algorithms (GAs) are used mainly for optimization problems for which the exact
algorithms are of very low efficiency.
GAs search for good solutions to a problem from among a (large) number of possible
solutions. The current set of possible solutions is used to generate a new set of possible
solution.
They start with an initial set of possible solutions, and at each step they do the following:
a. evaluate the current set of solutions (current generation)
b. choose the best of them to serve as “parents” for the new generation, and
construct the new generation.
Examples:
The knapsack problem solved with GAs
The traveling salesman problem
Page 5 of 20
Page 6 of 20
1. Divide and Conquer Approach: It is a top-down approach. The algorithms which follow the
divide & conquer techniques involve three steps:
o Greedy Algorithm always makes the choice (greedy criteria) looks best at the moment, to
optimize a given objective.
o The greedy algorithm doesn't always guarantee the optimal solution however it generally
produces a solution that is very close in value to the optimal.
This is particularly helpful when the number of copying subproblems is exponentially large.
Dynamic Programming is frequently related to Optimization Problems.
4. Branch and Bound: In Branch & Bound algorithm a given subproblem, which cannot be
bounded, has to be divided into at least two new restricted subproblems. Branch and Bound
algorithm are methods for global optimization in non-convex problems. Branch and Bound
algorithms can be slow, however in the worst case they require effort that grows exponentially
with problem size, but in some cases we are lucky, and the method coverage with much less
effort.
6. Backtracking Algorithm: Backtracking Algorithm tries each possibility until they find the
right one. It is a depth-first search of the set of possible solution. During the search, if an
Page 7 of 20
alternative doesn't work, then backtrack to the choice point, the place which presented different
alternatives, and tries the next alternative.
7. Randomized Algorithm: A randomized algorithm uses a random number at least once during
the computation make a decision.
Example 2: Trying to factor a large number by choosing a random number as possible divisors.
1. Sorting
2. Searching
3. String processing
4. Graph problems
5. Combinatorial problems
6. Geometric problems
7. Numerical problems
sorting algorithm is an algorithm that puts elements of a list in a certain order. The most- used
orders are numerical order and lexicographical order.
Searching : In computer science, a search algorithm, broadly speaking, is an algorithm for
finding an item with specified properties among a collection of items.
String processing:String searching algorithms are important in all sorts of applications that we
meet everyday. For example the “pbm” graphics format is based on sequences of 1s and 0s. We
could express a task like “find a wide white stripe in the image” as a string searching problem.
Graph problems:Graph algorithms are one of the oldest classes of algorithms.
There are two large classes of graphs:
• undirected graphs
Shortest path (min-cost path). Find the path from B to A with the minimum cost (de- termined
as some simple function of the edges traversed in the path) (Dijkstra’s and Floyd’s algorithms)
• Transitive closure. Determine all pairs of nodes that can reach each other (Floyd’s al- gorithm)
Page 8 of 20
Minimum spanning tree. A spanning three is a set of edges such that every node is reachable
from every other node, and the removal of any edge from the tree eliminates the reachability
property. A minimum spanning tree is the smallest such tree. (Prim’s and Kruskal’s algorithms)
Geometric Problems
Geometric algorithms deal with geometric objects such as points , lines, and polygons. Ancient
Greeks were very much interested in developing procedures for solving a variety of geometric
problems including problems of constructing simple geometric shapes-triangles
Numerical Problems
Numerical problems, another large area of applications are problems that involve mathematical
objects of continuous nature: solving equations and system of equation, computing definite
integrals, evaluating functions and so on. The majority of such mathematical problems can be
solved only approximately.
Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before implementation and
after implementation, as mentioned below −
A priori analysis − This is theoretical analysis of an algorithm. Efficiency of algorithm is
measured by assuming that all other factors e.g. processor speed, are constant and have
no effect on implementation.
A posterior analysis − This is empirical analysis of an algorithm. The selected algorithm
is implemented using programming language. This is then executed on target computer
machine. In this analysis, actual statistics like running time and space required, are
collected.
Algorithm Complexity
Suppose X is an algorithm and n is the size of input data, the time and space used by the
Algorithm X are the two main factors which decide the efficiency of X.
Time Factor − The time is measured by counting the number of key operations such as
comparisons in sorting algorithm
Space Factor − The space is measured by counting the maximum memory space
required by the algorithm.
The complexity of an algorithm f(n) gives the running time and / or storage space required by
the algorithm in terms of n as the size of input data.
Space Complexity
Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle. Space required by an algorithm is equal to the sum of the following
two components −
A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem. For example simple variables & constant used,
program size etc.
A variable part is a space required by variables, whose size depends on the size of the
problem. For example dynamic memory allocation, recursion stack space etc.
Page 9 of 20
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop
Time Complexity
Time Complexity of an algorithm represents the amount of time required by the algorithm to
run to completion. Time requirements can be defined as a numerical function T(n),
where T(n) can be measured as the number of steps, provided each step consumes constant
time.
The Analysis Framework
1. Measuring an Input’s Size
2. Units for Measuring Running Time
3. Orders of Growth
4. Worst-Case, Best-Case, and Average-Case Efficiencies
Page 10 of 20
Asymptotic Analysis
Asymptotic analysis of an algorithm, refers to defining the mathematical
boundation/framing of its run-time performance.
Asymptotic analysis refers to computing the running time of any operation in
mathematical units of computation. For example, running time of one operation is
computed as f(n) and may be for another operation it is computed as g(n2). Which means
first operation running time will increase linearly with the increase in n and running time
of second operation will increase exponentially when n increases. Similarly the running
time of both operations will be nearly same if n is significantly small.
An algorithm falls under three types −
Best Case − Minimum time required for program execution.
Average Case − Average time required for program execution.
Page 11 of 20
Omega Notation, Ω
The Ω(n) is the formal way to express the lower
bound of an algorithm's running time. It measures the
best case time complexity or best amount of time an
algorithm can possibly take to complete.
Theta Notation, θ
The θ(n) is the formal way to express both the lower
bound and upper bound of an algorithm's running time.
It is represented as following −
Page 12 of 20
constant − Ο(1)
logarithmic − Ο(log n)
linear − Ο(n)
quadratic − Ο(n2)
cubic − Ο(n3)
polynomial − nΟ(1)
exponential − 2Ο(n)
Now we will discuss and understand the various notations used for Time Complexity.
1. Big Oh denotes "fewer than or the same as" <expression> iterations.
2. Big Omega denotes "more than or the same as" <expression> iterations.
3. Big Theta denotes "the same as" <expression> iterations.
4. Little Oh denotes "fewer than" <expression> iterations.
5. Little Omega denotes "more than" <expression> iterations.
O(expression) is the set of functions that grow slower than or at the same rate as expression.
Omega(expression)is the set of functions that grow faster than or at the same rate as
expression.
Theta(expression) consist of all the functions that lie in both O(expression) and
Omega(expression).
Recursive Algorithms:
A Recursive function is a function that is defined in terms of itself.
Similarly, an algorithm is said to be recursive if the same algorithm is invoked in the body.
An algorithm that calls itself is Direct Recursive.
Page 13 of 20
Algorithm ‘A’ is said to be Indirect Recursive if it calls another algorithm which in turns calls
‘A’.
1. Each time a function calls itself it should get nearer to the solution.
The recursion algorithm for finding the factorial of a number is given below,
Algorithm: factorial-recursion
Input : n, the number whose factorial is to be found.
Output : f, the factorial of n
Method : if(n=0)
f=1
else
f=factorial(n-1) * n
if end
algorithm ends.
Recursion is a top down approach to problem solving. It divides the problem into pieces or selects
out one key step, postponing the rest.
SOLVING RECURRENCES :
A recurrence relation is an equation that recursively defines a sequence where the next term is a
function of the previous terms (Expressing Fn as some combination of Fi with i<n).
Page 14 of 20
1) Substitution Method: We make a guess for the solution and then we use mathematical
induction to prove the the guess is correct or incorrect.
2) Recurrence Tree Method: In this method, we draw a recurrence tree and calculate the time
taken by every level of tree. Finally, we sum the work done at all levels. To draw the recurrence
tree, we start from the given recurrence and keep drawing till we find a pattern among levels.
The pattern is typically a arithmetic or geometric series.
3) Master Method:
Master Method is a direct way to get the solution. The master method works only for following
type of recurrences or for recurrences that can be transformed to following type.
Page 15 of 20
Linear Search-
Begin
for i = 0 to (n - 1) by 1 do
if (a[i] = item) then
set loc = i
Exit
Page 16 of 20
endif
endfor
set loc = -1
End
Time Complexity Analysis-
Worst Case-
Thus, we have-
Page 17 of 20
Now,
Linear Search algorithm compares element 15 with all the elements of the array one by
one.
It continues searching until either the element 15 is found or all the elements are
searched.
A mathematical puzzle consisting of three towers and more than one ring is
known as Tower of Hanoi.
The rings are of different sizes and are stacked in ascending order, i.e., the
smaller one sits over the larger one. In some of the puzzles, the number of rings
may increase, but the count of the tower remains the same.
The Tower of Hanoi puzzle is solved by moving all the disks to another tower by
not violating the sequence of the arrangements.
Page 18 of 20
In the above 7 step all the disks from peg A will be transferred to C given Condition:
Let T (n) be the total time taken to move n disks from peg A to peg C
1. Moving n-1 disks from the first peg to the second peg. This can be done in T (n-
1) steps.
Page 19 of 20
2. Moving larger disks from the first peg to the third peg will require first one step.
3. Recursively moving n-1 disks from the second peg to the third peg will require
again T (n-1) step.
Page 20 of 20