Professional Documents
Culture Documents
Unit1 Daa
Unit1 Daa
Unit1 Daa
Algorithm Definitions:
A mathematical relation between an observed quantity and a variable used in a
step-by-step mathematical process to calculate a quantity
Algorithm is any well defined computational procedure that takes some value or set of
values as input and produces some value or set of values as output
Criteria to follow:
PSEUDOCODECONVENTIONS
1. Comments begin with // and continue until the end of the line.
2. Blocks are indicated with matching braces {and}.
3. An identifier (= data type) begins with a letter. Simple types are assumed: Integer, float, char,
boolean, and so on. Compound data types can be formed with records:
node = record
{ datatype_1 data_1;
…
datatype_ndata_n;
node *link;
}
In the example, link is a pointer to the record type node.
4. Assignment of values to variables is done using the assignment statement
<variable> := <expression>;
5. There are two Boolean values true and false. In order to produce these values, the logical
operators and, or, and not and the relational operators <,≤, ≠ , =, ≥, > are provided.
6. Elements of multidimensional arrays are accessed using [and]. For example, if A is a
two-dimensional array, the (i,j)th element of the array is denoted A[i,j].
7. The following looping statements are involved: for, while, repeat, until.
The while loop takes the following form:
while<condition> do
{
<statement 1>
…
<statement n>
}
As long as <condition> is true, the statements get executed. When <condition> becomes false,
the loop is exited.
The general form for a for loop is:
for variable:= value1 tovalue2step step do
{
<statement 1>
…
<statement n>
}
Here, value1, value2 and step are arithmetic expressions.
The for loop can be implemented as a while loop.
A repeat-until statement is constructed as follows:
repeat
{
<statement 1>
…
<statement n>
}
Until<condition>
The statements are executed for as long as condition is false.
The instruction break; can be used within any looping instruction to force exit. In case of
nested loops, break; results in the exit of the innermost loop that is part of it. A return statement
within any of the above loops also will result in exiting the loops. A return statement results in
the exit of the function itself.
To turn the algorithm into a pseudocode program, two subtasks remain: Finding the
smallest element.
This task is solved by assuming that a[i] is the minimum, and then comparing a[i] with
a[i+1], a[i+2],..., and whenever a smaller element is found, regarding it as a new minimum.
Eventually a[n] is compared with current minimum and we are done.
Defining “interchange”:
Define t:= a[i]; a[i] = a[j]; a[j]:= t.
1. Algorithm SelectionSort(a,n)
2. // Sort the array a[1:n] into non decreasing order
3. {
4. for i:= 1 to n do
5. {
6. j := i;
7. for k := i+1 to n do
8. if k:= (a[k] < a[j]) then
9. j := k; t:=a[i]; a[i]:=a[j]; a[j]:= t;
10. }
11. }
PROVING CORRECTNESS
Theorem. Algorithm SelectionSort(a,n) correctly sorts a set of n ≥ 1 elements; the result remains
in a[1:n] such that a[1] ≤ a[2] ≤... ≤ a[n].
RECURRENCE RELATIONS
What is recursion?
Recursion is the concept of well-defined self-reference. We use recursion
frequently; consider, for example, the following hypothetical definition of a Jew. (This
definition isn't official - it's just something I heard at a party once.)
Algorithm fact(n)
If n=1
return 1
else
n=n*fact(n-1) n= 5*4*3*2
return n;
Step1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
The Tower of Hanoi puzzle involves moving a pile of deferent size disks from one peg to
another using an intermediate peg. Only one disk at a time can be moved, a disk can only be
moved if it is the top disk on a pile, and a larger disk can never be placed on a smaller one.
Figure shows the initial and goal states of a three-disk problem.
The most straight-forward axiomatization of this problem consists of an operator for
moving each disk between each pair of pegs. For the three-disk problem, this axiomatization
requires 18 operators. Table 1 shows the operator for moving disk C, the largest disk, from peg 1
to 3. The preconditions require that disk C is initially on peg 1 and that neither disk A nor B are
on peg 1 or 3. This representation is far from the most concise one, but it is used in this paper to
simplify the exposition. The basic ideas described in this paper apply to and have been tested on
more compact representations.
Recursive Algorithm
Algorithm Hanoi( n, a, b, c)
// a,b,c are arrays for disk
{
if (n == 1) /* base case */
Move( a, b );
else { /* recursion */
Hanoi( n-1, a, c, b );
Move( a, b );
Hanoi( n-1, c, b, a );
}
}
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
• 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 }
Big-Oh Notation
🞂 To simplify the running time estimation, for a function f(n), we ignore the constants
and lower order terms.
Example: 10n3+4n2-4n+5 is O(n3).
🞂 Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants
c and n0 such that
f(n) ≤ cg(n) for n ≥ n0
🞂 Example: 2n + 10 is O(n)
◦ 2n + 10 ≤ cn
◦ (c − 2) n ≥ 10
◦ n ≥ 10/(c − 2)
◦ Pick c = 3 and n0 = 10
Big-Oh Rules
Mutual Exclusion
Two events E1 and E2 are said to be mutually exclusive if they do not have any common
sample points, that is, if E1 ∩ E2 = Φ .
Example:
Random variable:
Let S be the sample space of an experiment. A random variable on s is a function that
maps the element of S to the set of real numbers. For any Sample point s Є S , X ( s) denotes
the image of s under this mapping. If the range of X, that is, the set of values X can take, is finite
, we say X is discrete.
Let the range of a discrete random variable X be {r1,r2,….,rm }. Then , Prob.[ X=ri ], for an
I, is defined to be the number of sample points whose image is ri divided by the number of
sample points in S.
Always produce the same (correct) output for the same input.
The execution time depends on the output of the randomizer, in general
execution time is characterized as a random variable
Monte Carlo algorithm
Always produce the different output for the same input from run to run.
Consider any problem for which there are only two possible answers, say , yes or no.
Las Vegas algorithm
Algorithm LasVegas()
{
while (true) do
{
i:=Random() mod 2;
if ( i ≥ 1) then return;
}
}
Repeated Element
Consider an array a[ ] of n numbers that has n/2 distinct elements and n/2 copies of
another element. The problem is to identify the repeated element.
Here , the sampling performed is with repetitions; i.e., the first and second elements are
randomly picked from out of the n elements. Thus there is a probability (equal to 1/n) that the
same array element is picked each time. If we just checked for the equality of the two elements
picked, our answer might be incorrect, Therefore, it is essential to make sure that the two array
indices picked are different and two array cells contain the same value.
Repeated Elements algorithm
Algorithm Repeated Elements (a, n)
// Find the repeated element from a [1: n]
{
While (true) do
{i:=Random() mod n + 1;
j:=Random() mod n + 1;
// I and j are random numbers in the range [1, n]
if ( ( i ≠ j ) and (a[ i ] = a[ j ] )) then return i;
}
}
Primality Testing
Any integer greater than one is said to be a prime if its only divisors are 1 and the
integer itself.
n-1
// If a mod n is not 1, n is not a prime.
}
return true;
}
Algorithm Prime1(n)
{
// Specify t;
for i :=1 to t do
{
m:= Power(n,0.5);
j:=Random() mod m+2;
if((n mod j) = 0) then return false;
// if j divides n, n is not prime.
}
return true;
}