Você está na página 1de 292

PART I

Gate- ADA & DSA by Nitesh Dubey

What is Data Structure?


The logical and mathematical model of a particular
organization of data is called a Data Structure.
Data Structure generally specify the following things:
a).Organization of Data

b).Accessing Methods
c).Degree of Associative
d).Processing Alternatives for Information.

Gate- ADA & DSA by Nitesh Dubey

Classification of Data Structure


Major Classification is:
Linear Data Structure
Non-Linear Data Structure

Linear :
the values are arranged in linear fashion.
that means in sequence
Eg.-Arrays, linked lists, stacks, queues, etc.

Non-Linear :
this type is opposite to linear.
The data values are not arranged in order.
Eg- Trees, Graphs, Table, Sets, etc.
Gate- ADA & DSA by Nitesh Dubey

Other Classifications..
Homogenous

Non-homogenous
Dynamic
Static

Gate- ADA & DSA by Nitesh Dubey

Data Types
The Data Structure is a collection of different data types.

Major Classification of data types is:

Primitive Data type


Non- Primitive Data type

Gate- ADA & DSA by Nitesh Dubey

Classification of Data Types


Primitive: These are the basic data types defined by a
computer language itself. It generally represents a single
valued data.
w.r.f. of C/ C++, there are 4 primitive data types:
Integer
Floating Points
Characters
Void

Non-Primitive: These are basically derived and User


defined data types.
Ex- Arrays, Structure, Union, Class, Files, Stack,
Queue, Graphs, Trees,etc.
Gate- ADA & DSA by Nitesh Dubey

Array
An Array is a collection of elements having following
properties:
1.
2.
3.
4.

It is collection of similar data type.


It is consecutive (sequential) set of memory locations.
Referred by single variable name.
Elements are accessed by its index value.

Types of array:

Single Dimensional, and


Multidimensional.
Gate- ADA & DSA by Nitesh Dubey

Array..
Advantages:
Retrieval of stored elements is efficient using index
value.
Searching technique is very simply.
Disadvantages:
Insertion & deletion at random location are
complicated.
For storing data, large continuous free block of
memory is required.
Memory fragmentation occurs if remove the elements
randomly.

Gate- ADA & DSA by Nitesh Dubey

Structure
A structure is a group of items in which item is identified
by its own identifier, each of which is known as a member
of the structure.
Thus structure is a collection of different items of various
data types under a unique name.
Syntax of structure in C is as under:
struct name
{
type1 data1;
type2 data2;
.
.
typen datan; };

The memory requirement of a structure is the summation of size of


all data members.
Gate- ADA & DSA by Nitesh Dubey

Union
Unions are very similar to structures except the way of
member data is stored.
In union, the members are sharing the common memory
location. Thus, it is used to save memory.
Syntax of union in C is as under:
union name
{
type1 data1;
type2 data2;
.
.
typen datan;
};

The memory requirement of a union is the size of largest


data member.
Gate- ADA & DSA by Nitesh Dubey

Union..
DOS.h

union REGS
{

struct WORDREGS x;
struct BYTEREGS h;
};
Gate- ADA & DSA by Nitesh Dubey

Function
A function is a set instruction to carryout a particular
task.
After its execution it returns a single value.

We can also pass some parameters to a function.


Classification:
Standard Functions (library / built-in)
User-defined Functions

Gate- ADA & DSA by Nitesh Dubey

Algorithm

Gate- ADA & DSA by Nitesh Dubey

Algorithm
An algorithm is a computational method for solving a
problem.
It is a sequence of steps that take us from the input to
the output.
An algorithm must be
Correct: It should provide a correct solution according to the
specifications.
Finite: It should terminate.
General: It should work for every instance of a problem
Efficient: It should use few resources (such as time or
memory).
Gate- ADA & DSA by Nitesh Dubey

Analysis of Algorithms
It is the study of their efficiency.
It determine the amount of resources necessary
to execute it.
Most algorithms are designed to work with
inputs of arbitrary length.
Quantifying the resources required.

Gate- ADA & DSA by Nitesh Dubey

Analysis of Algorithms.
Measures of resource utilization (efficiency):
Execution time
Memory space

time complexity
space complexity

Observation :
The larger the input data the more the resource
requirement:
Complexities are functions of the amount of
input data (input size).
Gate- ADA & DSA by Nitesh Dubey

Space Complexity
Space complexity is defined as the amount of
memory a program needs to run to completion.
Space Complexity is= Instruction space +
Data space +
Stack space

Gate- ADA & DSA by Nitesh Dubey

Time Complexity
Time complexity is the amount of computer time a
program needs to run.
How do we measure?
1. Count a particular operation (operation counts)

2. Count the number of steps (step counts)


3. Asymptotic Notation
Gate- ADA & DSA by Nitesh Dubey

Asymptotic Notation
Describes the behavior of the time or space
complexity for large instance characteristic
Major Notations are
Big Oh (O) notation provides an upper bound for the
function
Omega () notation provides a lower-bound
Theta ( ) notation is used when an algorithm can be
bounded both from above and below by the same
function
Gate- ADA & DSA by Nitesh Dubey

Upper Bounds- Big Oh (O)


Time complexity T(n) is a function of the problem size n.
The value of T(n) is the running time of the algorithm in the worst
case, i.e., the number of steps it requires at most with an arbitrary
input.

The order is denoted by a complexity class using the Big Oh (O)


notation.

Definition: f(n) = O(g(n)) (read as f(n) is Big Oh of g(n)),


iff positive constants c and n0 exist such
that f(n) cg(n) for all n, n n0.
That is, O(g) comprises all functions f, for which there exists a
constant c and a number n0, such that f(n) is smaller or equal
to cg(n) for all n, n n0.
Gate- ADA & DSA by Nitesh Dubey

Big Oh (O).

g(n) is an asymptotic upper bound for f(n).


Gate- ADA & DSA by Nitesh Dubey

Big Oh Examples
Bubble Sort :T(n) =O(n2).
Linear Search: T(n) =O(n).
2n2 =O(n2).

7n2 +5n+1000 =O(n2).


9n3 +100n !=O(n2).
Gate- ADA & DSA by Nitesh Dubey

Lower Bounds- Omega ()


for the problem the lower bound is,
a certain number of steps that every algorithm
has to execute at least in order to solve the problem.

Definition: f(n) = (g(n)) (read as f(n) is omega of g(n))


iff positive constants c and
n0 exist such that f(n) cg(n) for all n, n n0.
That is, (g) comprises all functions f, for which there
exists a constant c and a number n0, such that f(n) is
greater or equal to cg(n) for all n n0.
Gate- ADA & DSA by Nitesh Dubey

Omega ()..

g(n) is an asymptotic lower bound for f(n).


Gate- ADA & DSA by Nitesh Dubey

Omega () Examples

Linear Search : T(n) = (1).

Bubble Sort :T(n) = (n).

Gate- ADA & DSA by Nitesh Dubey

Tightly Bound- Theta ( )


Used when the function f can be bounded both from
above and below by the same function g.
Definition: f(n) = (g(n)) (read as f(n) is theta of g(n)) iff
positive constants c1, c2 and n0 exist such that c1g(n) f(n)
c2g(n) for all n, n n0.
That is, f lies between c1 times the function g and c2
times the function g, except possibly when n is smaller
than n0.

Theta ( ) Examples:
Find Max / Min: T(n) =

(n)

Matrix Multiplication : T(n)=


Gate- ADA & DSA by Nitesh Dubey

(n3)

Theta ( )

g(n) is an asymptotically tight bound for f(n).


Gate- ADA & DSA by Nitesh Dubey

Relations b/w ,

,O

Theorem : For any two functions g(n) and f(n),


f(n) = (g(n)) iff
f(n) = O(g(n)) and f(n) = (g(n)).
Gate- ADA & DSA by Nitesh Dubey

Relations b/w ,

i.e.,

,O..

(g(n)) = O(g(n)) (g (n))

In practice,
asymptotically tight bounds are obtained from
asymptotic upper and lower bounds.

Gate- ADA & DSA by Nitesh Dubey

Common Growth Rate Functions


1 (constant): growth is independent of the problem size n.
log2N (logarithmic): growth increases slowly compared to the
problem size (binary search)
N (linear): directly proportional to the size of the problem.
N * log2N (n log n): typical of some divide and conquer approaches
(merge sort)

N2 (quadratic): typical in nested loops


N3 (cubic): more nested loops

2N (exponential): growth is extremely rapid and possibly impractical.


Gate- ADA & DSA by Nitesh Dubey

Practical Complexities
logn

nlogn

0
1
2
3
4
5
7

1
2
4
8
16
32
100

0
2
8
24
64
160
700

n2

n3

2n

1
1
2
4
8
4
16
64
16
64
512
256
256
4096
65536
1024 32768
4294967296
10000 1000000 1267650600228
2294014967032
05376

Gate- ADA & DSA by Nitesh Dubey

Complexity of Some Major Algorithm

Gate- ADA & DSA by Nitesh Dubey

Recursion

Gate- ADA & DSA by Nitesh Dubey

Recursive Function
A method of programming whereby a function directly or
indirectly calls itself.
Recursion is often presented as an alternative to iteration.

A function performed a task by calling itself repeatedly.


We can use the recursive function only where we want to
perform some task with the help of same set of statements
repeatedly.
The data structure used by recursion is stack.

Gate- ADA & DSA by Nitesh Dubey

Example of Recursion
Recursion for finding factorial of given number.
int fact(int x)
{
if(x==0)
return(1);
else
return(x * fact(x-1));
}

Gate- ADA & DSA by Nitesh Dubey

Recursion how it works?


To see how the recursion works, lets break down
the factorial function to solve factorial(3)

Gate- ADA & DSA by Nitesh Dubey

Recursion Tree
A tree representation of recursion calls.
A method to analyze the complexity of an
algorithm by diagramming the recursive
function calls.

Gate- ADA & DSA by Nitesh Dubey

How to Build a Recursion Tree ?


root = the initial call
Each node = a particular call
Each new call becomes a child of the node
that called it
A tree branch (solid line) = a call-return path
between any 2 call instances

Gate- ADA & DSA by Nitesh Dubey

Example - Fibonacci Numbers


F (n) = F (n-1) + F (n-2)
Fibonacci (n)
IF (n <= 1)
RETURN n
ELSE
RETURN Fibonacci (n-1) + Fibonacci (n-2)

Gate- ADA & DSA by Nitesh Dubey

Recursion Tree showing Fibonacci calls

Gate- ADA & DSA by Nitesh Dubey

Types of Recursive Functions


Direct Recursion.
Indirect Recursion.
Linear Recursion

Tail Recursion
Binary Recursion

Exponential Recursion
Nested Recursion

Mutual Recursion
Gate- ADA & DSA by Nitesh Dubey

Direct Recursion
int factorial (int x)
{
if (x==0)
return(1);
else
return (x * factorial(x-1));
}
Gate- ADA & DSA by Nitesh Dubey

Indirect Recursion
void fun1()
{
static i=0;
if (i<5)
fun2();
}
void fun2()
{
printf ("Recursion from fun2 to fun1 which is indirect
recursion\n");
fun1();
}
main()
{
fun1();
}
Gate- ADA & DSA by Nitesh Dubey

Linear Recursion
A linear recursive function is a function that
only makes a single call to itself each time the
function runs.
(as opposed to one that would call itself multiple
times during its execution).

thus if we were to draw out the recursive calls,


we would see a straight, or linear, path.
The factorial function is a good example of
linear recursion.
Gate- ADA & DSA by Nitesh Dubey

Linear Recursion - Example


//C++
int factorial (int n)
{
if ( n == 0 )
return 1;
return
n * factorial(n-1);
}

// or factorial(n-1) * n

Gate- ADA & DSA by Nitesh Dubey

Linear Recursion - Example

Gate- ADA & DSA by Nitesh Dubey

Tail Recursion
A recursive procedure where the recursive call is the last action to
be taken by the function.
Tail recursive functions are generally easy to transform into
iterative functions.

Tail recursion is a form of linear recursion.


Often, the value of the recursive call is returned.
A tail recursive function is one where every recursive call is the last
thing done by the function before returning and thus produces the
functions value.

Gate- ADA & DSA by Nitesh Dubey

Tail Recursion..
As such, tail recursive functions can often be easily
implemented in an iterative manner;
by taking out the recursive call and replacing it with a
loop, the same effect can generally be achieved.
A good compiler can recognize tail recursion and
convert it to iteration in order to optimize the
performance of the code.

Gate- ADA & DSA by Nitesh Dubey

Tail Recursion.
to compute the GCD, or Greatest Common Denominator, of two
numbers:

//C++
int gcd(int m, int n)
{
int r;
if (m < n)
return gcd(n,m);
r = m%n;
if (r == 0)
return(n);
else
return(gcd(n,r));
}
Gate- ADA & DSA by Nitesh Dubey

Tail Recursion.
Is the factorial method a tail recursive method?
int fact(int x)
{
if (x==0)
return 1;
else
return x*fact(x-1);
}
When returning back from a recursive call, there is still one
pending operation, multiplication.
Therefore, factorial is a non-tail recursive method.
Gate- ADA & DSA by Nitesh Dubey

Tail Recursion.
Is this method tail recursive?
void fun1(int i)
{
if (i>0)
{
printf(%d , i);
fun1(i-1);
}
It is tail recursive.

Gate- ADA & DSA by Nitesh Dubey

Tail Recursion.
Is the following program tail recursive?
void prog(int i) {
if (i>0) {
prog(i-1);
printf(%d , i);
prog(i-1);
}
}
No, because there is an earlier recursive call, other than the last one,
In tail recursion, the recursive call should be the last statement, and
there should be no earlier recursive calls whether direct or indirect.
Gate- ADA & DSA by Nitesh Dubey

Tail Recursion.
Advantage of Tail Recursive Method
Tail Recursive methods are easy to convert to iterative.
void tail(int i){
if (i>0)
{
printf(%d , i);
tail(i-1)
}
}

void iterative (int i)


{
for (; i>0 ; i--)
printf(%d , i);
}

Smart compilers can detect tail recursion and convert it to


iterative to optimize code
Used to implement loops in languages that do not support loop
structures explicitly (e.g. prolog)
Gate- ADA & DSA by Nitesh Dubey

Binary Recursion
A recursive function which calls itself twice during
the course of its execution.
The mathematical combinations operation is a good
example of a function that can quickly be
implemented as a binary recursive function. The
number of combinations, often represented as nCk
where we are choosing n elements out of a set of k
elements.

Gate- ADA & DSA by Nitesh Dubey

Binary Recursion - Example


//C++
int choose (int n, int k)
{
if (k == 0 || n == k)
return(1);
else
return(choose(n-1,k) + choose(n-1,k-1));
}

Gate- ADA & DSA by Nitesh Dubey

Exponential Recursion
Recursion where more than one call is made to the
function from within itself. This leads to exponential
growth in the number of recursive calls.
An exponential recursive function is one that, if you
were to draw out a representation of all the function
calls, would have an exponential number of calls in
relation to the size of the data set
(exponential meaning if there were n elements, there would be
O(an) function calls where a is a positive number).

A good example an exponentially recursive function is


a function to compute all the permutations of a data
set.
Gate- ADA & DSA by Nitesh Dubey

Exponential Recursion - Example


void print_array(int arr[], int n)
{
int i;
void print_permutations(int arr[], int n, int i)
for(i=0; i<n; i++)
{
printf("%d ", arr[i]); int j, swap;
printf("\n");
print_array(arr, n);
}
for(j=i+1; j<n; j++)
{
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
print_permutations(arr, n, i+1);
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
Gate- ADA & DSA by Nitesh Dubey

Nested Recursion
In nested recursion, one of the arguments to the
recursive function is the recursive function itself.
These functions tend to grow extremely fast.

A good example is the classic mathematical function,


Ackermann's function.
It grows very quickly (even for small values of x and y,
Ackermann(x,y) is extremely large) and it cannot be
computed with only definite iteration (a completely
defined for() loop for example); it requires indefinite
iteration (recursion, for example).
Gate- ADA & DSA by Nitesh Dubey

Nested Recursion.. Example


//C++
int ackerman(int m, int n)
{
if (m == 0)
return(n+1);
else
if (n == 0)
return(ackerman(m-1,1));
else
return(ackerman(m-1,ackerman(m,n-1)));
}
Gate- ADA & DSA by Nitesh Dubey

Mutual Recursion
A recursive function doesn't necessarily need to call
itself.
Some recursive functions work in pairs or even larger
groups.

For example, function A calls function B which calls


function C which in turn calls function A.
A simple example of mutual recursion is a set of
function to determine whether an integer is even or
odd. How do we know if a number is even? Well, we
know 0 is even. And we also know that if a number n
is even, then n - 1 must be odd. How do we know if a
number is odd? It's not even!
Gate- ADA & DSA by Nitesh Dubey

Mutual Recursion
//C++
int is_even(unsigned int n)
{
if (n==0)
return 1;
else
return(is_odd(n-1));
}
int is_odd(unsigned int n)
{
return (!is_even(n));
}
Gate- ADA & DSA by Nitesh Dubey

Recursions.
Recursion is a powerful problem-solving technique that
often produces very clean solutions to even the most
complex problems.
Recursive solutions can be easier to understand and to
describe than iterative solutions.
Recursion works the best when the algorithm and/or data
structure that is used naturally supports recursion.
One such data structure is the tree, One such algorithm is
the binary search algorithm.

Gate- ADA & DSA by Nitesh Dubey

Recursion..
Recursive solutions may
because they use calls.

involve

extensive

overhead

When a call is made, it takes time to build a stackframe


and push it onto the system stack.
Conversely, when a return is executed, the stackframe
must be popped from the stack and the local variables reset
to their previous values this also takes time.
In general, recursive algorithms run slower than their
iterative counterparts.
Also, every time we make a call, we must use some of the
memory resources to make room for the stackframe.
Gate- ADA & DSA by Nitesh Dubey

Recursion..
Therefore, if the recursion is deep, say, factorial(1000),
we may run out of memory.
Because of this, it is usually best to develop iterative
algorithms when we are working with large numbers.

Gate- ADA & DSA by Nitesh Dubey

About Recursion final Points


PROS

Clearer logic
Often more compact code
Often easier to modify
Allows for complete analysis of runtime
performance

CONS
Overhead costs
Time Consuming
Additional Memory Requirement.
Gate- ADA & DSA by Nitesh Dubey

Sample Question-1
Consider following recursive function in C/ C++
int func (int n)
{
static int i=1;
if (n>=5) return n;
n=n + 1;
i++;
int x = func (n);
Stmt : return n;
}
Gate- ADA & DSA by Nitesh Dubey

Questionlinked
1.

The value returned by func(1) isA.


B.
C.
D.

2.

How many times does the Stmt will execute in the


above code?
A.
B.
C.
D.

3.

5
6
7
2

1
5
4
0

What will be the final value of i.


A.
B.
C.
D.

1
5
4
0

Gate- ADA & DSA by Nitesh Dubey

Questionlinked
4.

What would be the final value of i if we remove the


keyword static from the code.
A.
B.
C.
D.
E.

5.

1
2
3
4
5

The value returned by func(5) isA.


B.
C.
D.

5
6
7
2

Gate- ADA & DSA by Nitesh Dubey

Questionlinked- Answers
1.

2 (D)

2.

4 (C)

3.

5 (B)

4.

2 (B)

5.

5 (A)

Gate- ADA & DSA by Nitesh Dubey

Sample Question-2
Consider following recursive function in C/ C++
int func(int a, int b)
{
if(b==0)
return(a);
else
return(1+ func (a, b-1));
}

Gate- ADA & DSA by Nitesh Dubey

Questions..
1.

The value returned by func(2,3) isA.


B.
C.
D.
E.

2.

5
1
3
2
None

The value returned by func(1,2) isA.


B.
C.
D.
E.

5
1
3
2
none

Gate- ADA & DSA by Nitesh Dubey

1.

5 (A)

2.

3(C)

Sample Question-3
Find the output of following code of C/ C++
void f(void)
{
int s = 0;
s++;
if (s == 10)
return;
printf("%d ", s);
f( ); }

1 1 Infinite

int main(void)
{
f( );
return 0;
}
Gate- ADA & DSA by Nitesh Dubey

Sample Question-4
Find the output of following code of C/ C++
void f(void)
{
static int s = 0;
s++;
if (s == 10)
return;
printf("%d ", s);
f( ); }
int main(void)
{
f( );
return 0;
}

123456789

Gate- ADA & DSA by Nitesh Dubey

Sample Question-5
Find the output of following code of C/ C++
void f(int i)
{
if( i < 10)
{
f( i + 1 );
printf("%d ", i);
}
}
int main(void)
{
f( 0 );
return 0;
}

9876543210

Gate- ADA & DSA by Nitesh Dubey

Sample Question-6
Find the output of following code of C++
void func6()
{
char ch;
cout << "Enter a character ('.' to end program): ";
cin >> ch;
if (ch != '.')
{
func6();
For Input Chars : a b c d e f .
cout << ch;
}}
Output Chars: fedcba
void main()
{
func6();
cout << "\n";
}

Gate- ADA & DSA by Nitesh Dubey

Sample Question-7

In Tower of Hanoi problem, how many disk


moves are required to shift 5 disks.
A.
B.
C.
D.

27
32
31
28

31 (C)

Gate- ADA & DSA by Nitesh Dubey

The Towers of Hanoi Problem


Legend has it that there were three diamond
needles set into the floor of the temple of Brahma
in Hanoi.

Stacked upon the leftmost needle were 64 golden


disks, each a different size, stacked in concentric
order:
Gate- ADA & DSA by Nitesh Dubey

The Towers of Hanoi


The priests were to transfer the disks from the
first needle to the second needle, using the third
as necessary.

But they could only move one disk at a time, and could
never put a larger disk on top of a smaller one.
When they completed this task, the world would
end!
Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Design


Basis: What is an instance of the problem that is
trivial?

for n == 1

Since this base case could occur when the disk is


on any needle, we simply output the instruction
to move the top disk from src to dest.
Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Design.


Basis: What is an instance of the problem that is
trivial?

For n == 1

Since this base case could occur when the disk


is on any needle, we simply output the
instruction to move the top disk from src to
dest.
Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Design..


Induction Step: n > 1
How can recursion help us out?

a. Recursively move n-1 disks from src to aux.

Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Design..


Induction Step: n > 1
How can recursion help us out?

b. Move the one remaining disk from src to


dest.
Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Design..


Induction Step: n > 1
How can recursion help us out?

c. Recursively move n-1 disks from aux to


dest...
Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Design.


Induction Step: n > 1
How can recursion help us out?

d. Were done!

Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Algorithm


We can combine these steps into the following
algorithm:
0. Receive n, src, dest, aux.
1. If n > 1:
a. Move(n-1, src, aux, dest);
b. Move(1, src, dest, aux);
c. Move(n-1, aux, dest, src);
Else
Display Move the top disk from , src, to , dest.
End if.

Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Analysis


Lets see how many moves it takes to solve this problem,
as a function of n, the number of disks to be moved.
n
Number of disk-moves required
1
1
2
3
3
7
4
15
5
31
...
i
2i-1
64
264-1 (a big number)
Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Analysis..


How big?
Suppose that our computer and super-printer can generate and
print 1,048,576 (220) instructions/second.
There are 264 instructions to print.
Then it will take 264/220 = 244 seconds to print them.
Then it will take @ 244 / 26 = 238 minutes to print them.
Then it will take @ 238 / 26 = 232 hours to print them.
Then it will take @ 232 / 25 = 227 days to print them.
Then it will take @ 227 / 29 = 218 years to print them.

1 century == 100 years.


Then it will take @ 218 / 27 = 211 centuries to print them.
1 millenium == 10 centuries.
Then it will take @ 211 / 24 = 27 = 128 millenia
Gate- ADA & DSA by Nitesh Dubey

Some Simple Algorithms

Gate- ADA & DSA by Nitesh Dubey

Linear Search
Searching is the process of determining whether or not a
given value exists in a data structure or a storage media.
We discuss two searching methods on one-dimensional
arrays: linear search and binary search.
The linear (or sequential) search algorithm on an array is:
Sequentially scan the array, comparing each array item with the searched value.

If a match is found; return the index of the matched element; otherwise return 1.

Note: linear search can be applied to both sorted and unsorted


arrays.
Gate- ADA & DSA by Nitesh Dubey

Linear Search..
//Function
public static int linearSearch(Object[] array, Object key)
{
for(int k = 0; k < array.length; k++)

if(array[k].equals(key))
return k;
return -1;
}

Gate- ADA & DSA by Nitesh Dubey

Linear Search
/* Linear search */
for ( i=0; i < N ; i++)
{
if( keynum == array[i] )
{
found = 1;
break;
}
}
if ( found == 1)
printf("SUCCESSFUL SEARCH\n");
else
Gate- ADA & DSA by Nitesh Dubey
printf("Search is FAILED\n

Linear Search
Data structure -Array

Worst case performance O(n)


Best case performance O(1)

Average case performance O(n/2)

Gate- ADA & DSA by Nitesh Dubey

Bubble Sort
Sorting takes an unordered collection and makes it an ordered one.
Bubble sort algorithm:
Compare adjacent elements. If the first is greater than the second, swap
them.
Do this for each pair of adjacent elements, starting with the first two and
ending with the last two. At this point the last element should be the
greatest.
Repeat the steps for all elements except the last one.
Keep repeating for one fewer element each time, until you have no more
pairs to compare

Gate- ADA & DSA by Nitesh Dubey

Bubble Sort..
for i = 1:n,
swapped = false
for j = n:i+1,
if a[j] < a[j-1],
swap a[j,j-1]
swapped = true
break if not swapped
end

Gate- ADA & DSA by Nitesh Dubey

Bubble Sort
Data structure -Array
Worst case performance O(n2)
Best case performance O(n)

Average case performance O(n2)


Worst case space complexity O(1) auxiliary

Gate- ADA & DSA by Nitesh Dubey

Bubble Sort
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, j, N, temp;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
printf("Enter the elements one by
one\n");
for(i=0; i<N ; i++)
scanf("%d",&array[i]);
printf("Input array is\n");
for(i=0; i<N ; i++)
printf("%d\n",array[i]);

/* Bubble sorting begins */


for(i=0; i< N ; i++)
{
for(j=0; j< (N-i-1) ; j++)
{
if(array[j] > array[j+1])
{
temp
= array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
Gate- ADA & DSA by Nitesh Dubey
}

Selection Sort
Algorithm:
Pass through elements sequentially;
In the ith pass, we select the element with the

lowest value in A[i] through A[n], then swap the


lowest value with A[i].

Gate- ADA & DSA by Nitesh Dubey

Selection Sort..
for i = 1:n,
k=i
for j = i+1:n,
if a[j] < a[k],
k=j
invariant: a[k] smallest of a[i..n]
swap a[i,k]
invariant: a[1..i] in final position
end

Gate- ADA & DSA by Nitesh Dubey

Selection Sort..
Not stable

O(1) extra space


(n2) comparisons

(n) swaps
Not adaptive

Gate- ADA & DSA by Nitesh Dubey

Insertion Sort
Algorithm:
Start with the result being the first element of the input;

Loop over the input array until it is empty, "removing" the first
remaining (leftmost) element;
Compare the removed element against the current result, starting from
the highest (rightmost) element, and working left towards the lowest
element;
If the removed input element is lower than the current result element,
copy that value into the following element to make room for the new
element below, and repeat with the next lowest result element;
Otherwise, the new element is in the correct location; save it in the cell
left by copying the last examined result up, and start again from step 2
with the next input element.
Gate- ADA & DSA by Nitesh Dubey

Insertion Sort
for i = 2:n,
for (k = i; k > 1 and a[k] < a[k-1]; k--)
swap a[k,k-1]
invariant: a[1..i] is sorted
end

Gate- ADA & DSA by Nitesh Dubey

Insertion Sort.

Stable
O(1) extra space
O(n2) comparisons and swaps
Adaptive: O(n) time when nearly sorted
Very low overhead

Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer

Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer
The divide-and-conquer strategy solves a problem by:
1. Breaking it into sub-problems that are themselves smaller
instances of the same type of problem.
2. Recursively solving these sub-problems
3. Appropriately combining their answers
The name "divide and conquer" is sometimes applied also to
algorithms that reduce each problem to only one sub problem,
such as the binary search algorithm.

Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer- Advantages
Solving difficult problems
Algorithm efficiency

Parallelism
Memory access
Roundoff control

Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer- Implementation Issues


Recursion

Explicit stack
Stack size
Choosing the base cases
Sharing Repeated subproblems

Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer- Examples
Problems Solved by Divide & Conquer :
Binary Search
Merge Sort

Quick Sort
Max-Min Problem
Matrix Multiplication
..etc.
Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer- Recurrence Relation


also called as Difference Equation.
Is a numerical way to represent the Computation
time & other parameters of analysis.
Recurrence Relation for Divide & Conquer :

T(1)

n=1

T(n) =
aT(n/b) + f(n)

n>1

Where a and b are known constant,


T(1) is known,
n is a power of b ( i.e. n= bk )
Gate- ADA & DSA by Nitesh Dubey

Some Examples

Gate- ADA & DSA by Nitesh Dubey

Max-Min Problem

Gate- ADA & DSA by Nitesh Dubey

Examples 1 : Max-Min Problem


Performs two recursive calls on partitions of roughly
half of the total size of the list, and
then makes two further comparisons to sort out the
max/min for the entire list.

Recurrence Relation :

T(n) = 2T(n/2) + 2
Where T(1) = 0

Gate- ADA & DSA by Nitesh Dubey

Examples 2 : Binary Search


Primary Requirement :
Elements should be in SORTED order.
much more Efficient than Linear Search.

Recurrence Relation :
T(n) = T(n/2) + O(1)

Gate- ADA & DSA by Nitesh Dubey

Binary Search

Gate- ADA & DSA by Nitesh Dubey

Examples 2 : Binary Search


Algorithm.

Step 1: get sorted data as Input of size N.


Step 2: initialize low:=1; high:=N;
Step 3: if (low > high) print ("Value Not Found");
goto Step 7
Step 4: find mid:=(low+ high) div 2
Step 5: if ( v==a[mid] ) then print ("Value Found")
goto Step 7.
Step 5: if ( v<a[mid] ) then high:=mid-l
else
low:=mid+1
Step 6: goto step 3
Step 7: End
Gate- ADA & DSA by Nitesh Dubey

Examples 2 : Binary Search - Analysis


Following tree structure describe the way of
division...

Gate- ADA & DSA by Nitesh Dubey

Examples 2 : Binary Search - Analysis


Therefore, (in worst case),

Complexity = Number of Comparison


= Number of level of Binary tree + 1
=k+1
We know that,
n = 2k
log n = k . log 2
k = log n / log 2
i.e. k = log2n
Number of Comparison =( log2n + 1 ) log2n
Complexity = O(log2n)
Gate- ADA & DSA by Nitesh Dubey

Merge Sort

Gate- ADA & DSA by Nitesh Dubey

Examples 3 : Merge Sort


The merge sort algorithm closely follows the divide
and-conquer paradigm.
It is based on Two-way Merge Sort.
specially for External Sorting.
Intuitively, it operates as follows.
Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each.
Conquer: Sort the two subsequences recursively using
merge sort.

Combine: Merge the two sorted subsequences to produce


the sorted answer.
Gate- ADA & DSA by Nitesh Dubey

Examples 3 : Merge Sort : Working by example

Divide

Conquer

Gate- ADA & DSA by Nitesh Dubey

Examples 3 : Merge Sort : Analysis


When we have n > 1 elements, we break down the
running time as follows..
Divide: The divide step just computes the middle of the
subarray, which takes constant time. Thus, D(n) = (1).
Conquer: We recursively solve two subproblems, each of
size n/2, which contributes Q(n) =2T (n/2) to the running
time.
Combine: We uses the merge procedure on an nelement subarray that takes time (n), so C(n) = (n).

Gate- ADA & DSA by Nitesh Dubey

Examples 3 : Merge Sort : analysis


Therefore, T(n) = D(n) + C(n) + Q(n)
= (1)+ (n) + 2T (n/2)
= (n) + 2T (n/2)
gives the recurrence for the worst-case running time
T (n) of merge sort:

(1)

,if n = 1

T(n)=
2T (n/2) +
i.e. Complexity = O(n log2n)

(n)

,if n > 1

: by solving above relation

Gate- ADA & DSA by Nitesh Dubey

Quick Sort

Gate- ADA & DSA by Nitesh Dubey

Examples 4 : Quick Sort

most efficient Internal Sorting method.

it works as under:
1. select a value as a pivot element from given array
A[1], . . . , A[n]
2. Divide the list into 2 half based on pivot value such that,
(elements of first half) < pivot < (elements of second half)

3. Repeat from step-1 for both halves if they still divisible into
two.

we hope the pivot is near the median key value in the array, so
that it produce nearly equal size of halves.

Gate- ADA & DSA by Nitesh Dubey

Quick Sort..
Simple Steps-

1. if left < right:


1.1. Partition a[left...right] such that:
all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Partitioning


A key step in the Quicksort algorithm is
partitioning the array
We choose some (any) number p in the array to use
as a pivot
We partition the array into three parts:
p

numbers
less than p

numbers greater than


or equal to p

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Algorithm


Choose an array value (say, the first) to use as the pivot
Starting from the left end, find the first element that is
greater than or equal to the pivot
Searching backward from the right end, find the first
element that is less than the pivot
Interchange (swap) these two elements
Repeat, searching from where we left off, until done

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Example


choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
search:
436924312189356
swap:
433924312189656
search:
433924312189656
swap:
433124312989656
search:
433124312989656
swap:
433122314989656
search:
433122314989656
right)

swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6


Gate- ADA & DSA by Nitesh Dubey

(left >

Quick Sort- Analysis (Best case)


Suppose each partition operation divides the array
almost exactly in half.
Then the depth of the recursion in log2n

Because thats how many times we can halve n

However, there are many recursions!


How can we figure this out?
We note that
Each partition is linear over its subarray
All the partitions at one level cover the array

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Analysis (Best case)


Way of Partitioning
n

n/2

n/2

n/4

n/4

n/8

.
.
.
.

.
.
.
.
.
.
Gate- ADA & DSA by Nitesh Dubey
.
.

.
.
.
.

n/8

Quick Sort- Analysis (Best case)


We cut the array size in half each time
So the depth of the recursion in log2n
At each level of the recursion, all the partitions at
that level do work that is linear in n
O(log2n) * O(n) = O(n log2n)
Hence in the average case, quick sort has time
complexity O(n log2n)
Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Analysis (Worst case)


In the worst case, partitioning always divides the size n
array into these three parts:
A length one part, containing the pivot itself
A length zero part, and
A length n-1 part, containing everything else
We dont recur on the zero-length part

Recurring on the length n-1 part requires (in the worst


case) recurring to depth n-1

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Analysis (Worst case)..

Way of Partitioning
n/2
n-1
n-2

n-3

.
.

Gate- ADA & DSA by Nitesh Dubey

.
.

.
.
.
.
.
.
.
.

Quick Sort- Analysis (Worst case)..


In the worst case, recursion may be n levels deep
(for an array of size n).
But the partitioning work done at each level is still n.
O(n) * O(n) = O(n2)
So worst case for Quicksort is O(n2)

When does this happen?


When the array is sorted to begin with!
Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Analysis..


If the array is sorted to begin with, Quicksort is
terrible: O(n2)
However, Quicksort is usually O(n log2n)
The constants are so good that Quicksort is
generally the fastest algorithm known.
Most real-world sorting is done by Quicksort.

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- How to improve TC ?


Almost anything you can try to improve Quicksort will
actually slow it down.

One good tweak is to switch to a different sorting method


when the subarrays get small (say, 10 or 12).
Quicksort has too much overhead for small array sizes
For large arrays, it might be a good idea to check
beforehand if the array is already sorted.
Picking a better pivot.

Gate- ADA & DSA by Nitesh Dubey

Quick Sort- Picking a better pivot.


For every iteration pick the Pivot from random position
of array.

We could do an optimal quicksort (guaranteed


O(n log n)) if we always picked a pivot value that
exactly cuts the array in half
Such a value is called a median: pick it as Pivot
value.
Take the median (middle value) of three randomly
selected values and consider it as pivot.

Gate- ADA & DSA by Nitesh Dubey

Example 5 : Matrix Multiplication


Basic Matrix Multiplication
Suppose we want to multiply two matrices of size N x
N: for example A x B = C.

Therefore,
C11 = a11b11 + a12b21
C12 = a11b12 + a12b22
C21 = a21b11 + a22b21

2x2 matrix multiplication can be


accomplished in 8 multiplication.
(2log28 =23)

C22 = a21b12 + a22b22Gate- ADA & DSA by Nitesh Dubey

Matrix Multiplication

Gate- ADA & DSA by Nitesh Dubey

Basic Matrix Multiplication..


void matrix_mult ()
{

Algorithm
for (i = 1; i <= N; i++)

{ for (j = 1; j <= N; j++)


{ compute Ci,j;

}}

Time analysis

Ci , j

ai ,k bk , j
k 1
N

Thus T ( N )

c
i 1 j 1 k 1

Gate- ADA & DSA by Nitesh Dubey

cN 3

O( N 3 )

Matrix Multiplication using Divide and Conquer

A0

A1

B0

B1

A2

A3

B2

B3

R
A0 B0+A1 B2

A0 B1+A1 B3

A2 B0+A3 B2

A2 B1+A3 B3

Divide matrices into sub-matrices: A0 , A1, A2 etc

Divide the matrices till its size reduced to 2x2


Use blocked matrix multiply equations
Recursively multiply sub-matrices
Gate- ADA & DSA by Nitesh Dubey

Basic Matrix Multiplication using Divide & Conque


Min size of matrices is 2x2.
Each of which requires 8 multiplications.

n2 number of Addition operation will be required. (for n x n)


Therefore, the recurrence relation will be :

,if n = 2

T(n)=

8T (n/2) + cn2 ,if n > 2


i.e., T(n) = O(n3)
Gate- ADA & DSA by Nitesh Dubey

Strassenss Matrix Multiplication


Volker Strassen published the Strassen algorithm in
1969.
Strassen showed that 2x2 matrix multiplication can be
accomplished in 7 multiplication and 18 additions
or subtractions. (2log27 =22.807)
This reduce can be done by Divide and Conquer
Approach.

Gate- ADA & DSA by Nitesh Dubey

Strassenss Matrix Multiplication

Where,

C11 = P1 + P4 - P5 + P7
C12 = P3 + P5
C21 = P2 + P4
C22 = P1 + P3 - P2 + P6

And,

P1 = (A11+ A22)(B11+B22)
P2 = (A21 + A22) * B11
P3 = A11 * (B12 - B22)
P4 = A22 * (B21 - B11)
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
Gate- ADA & DSA by Nitesh Dubey
P7 = (A12 - A22) * (B21 + B22)

Strassenss Matrix Multiplication


C11 = P1 + P4 - P5 + P7
= (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) * B22+
(A12 - A22) * (B21 + B22)
= A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 A22 B11 A11 B22 -A12 B22 + A12 B21 + A12 B22 A22 B21 A22 B22

= A11 B11 + A12 B21

Gate- ADA & DSA by Nitesh Dubey

Strassenss Matrix Multiplication- Time Analysis


Min size of matrices is 2x2.
Each of which requires 7 multiplications.

n2 number of Addition operation will be required. (for n x n)


Therefore, the recurrence relation will be :

,if n = 2

T(n)=

7T (n/2) + cn2 ,if n > 2


i.e., T(n) = O(n2.81)
Gate- ADA & DSA by Nitesh Dubey

Towers of Hanoi

Gate- ADA & DSA by Nitesh Dubey

Towers of Hanoi

Gate- ADA & DSA by Nitesh Dubey

Tower of Hanoi Algorithm


We can combine these steps into the
following algorithm:
0. Receive n, src, dest, aux.
1. If n > 1:
a. Move(n-1, src, aux, dest);
b. Move(1, src, dest, aux);
c. Move(n-1, aux, dest, src);
Else
Display Move the top disk from , src, to
, dest.
Gate- ADA & DSA by Nitesh Dubey
End if.

Tower of Hanoi Algorithm


Let T[N] be the minimum number of moves needed to
solve the puzzle with N disks.
The recursive solution above involves moving twice (N1) disks from one peg to another and making one
additional move in between.
It then follows that (for n>0)
T[N] = 2T[N-1]+1
T[1] = 1 and T[0] = 0
i.e.,

T[N] = O(2^N)
O(2n)
Gate- ADA & DSA by Nitesh Dubey

Hashing

Gate- ADA & DSA by Nitesh Dubey

Hashing
Hashing is function that maps each key to a location
in memory.
A keys location does not depend on other elements,
and does not change after insertion.
unlike a sorted list

A good hash function should be easy to compute.


With such a hash function, the dictionary operations
can be implemented in O(1) time.

Gate- ADA & DSA by Nitesh Dubey

Hash Tables
a hash table is an array of size Tsize
has index positions 0 .. Tsize-1

Two types of hash tables


open hash table
array element type is a <key, value> pair
all items stored in the array

chained hash table


element type is a pointer to a linked list of nodes containing
<key, value> pairs
items are stored in the linked list nodes

keys are used to generate an array index


home address (0 .. Tsize-1)
Gate- ADA & DSA by Nitesh Dubey

Hash Function
a hash function is used to put data in the hash table.
Hash function is used to implement the hash table.
The integer returned by the hash function is called
Hash key.
Each position of the hash table is called Bucket.
Home bucket is Actual bucket of a value.
by applying the hash function to the key we perform
insert,
retrieve,
update,
delete operations.
Gate- ADA & DSA by Nitesh Dubey

Some Hash functions


Division Method
H(key)= key % Tsize

Mid Square Method


Multiplicative Hash Method
H(key)= floor( p * (key * A) % 1)
where p is integer constant and A is Constant (0< A <1).
Donald Knuth suggested A=0.61803398987

Digit Folding
Shift folding
Boundary folding (reverse the ith part)

Digit Analysis
when all identifiers are known in advance

Gate- ADA & DSA by Nitesh Dubey

Some hash functions.


How to apply Hash function:
if key (data) type is integer
- key % Tsize

if key (data) type is a string


- convert it into an integer and then % Tsize

Goals for a hash function:


Fast to compute
Even distribution
cannot guarantee no collisions unless all key values are
known in advance
Gate- ADA & DSA by Nitesh Dubey

An Open Hash Table


Hash (key) produces
an index in the range
0 to 6. That index is
the home address

Some insertions:
K1 --> 3
K2 --> 5
K3 --> 2

0
1
2
3
4
5
6

K3

K3info

K1

K1info

K2

K2info

key value
Gate- ADA & DSA by Nitesh Dubey

Hash Collisions
Collisions occur when different elements are mapped to the
same cell.

Some more insertions:


K4 --> 3
K5 --> 2
K6 --> 4

Linear probing collision


resolution strategy

0
1
2
3
4
5
6

Gate- ADA & DSA by Nitesh Dubey

K6

K6info

K3

K3info

K1

K1info

K4

K4info

K2

K2info

K5

K5info

Collisions Resolution techniques


Chaining - using linked list

Linear probing
Linear probing with Chaining using array
Linear probing with Chaining (with replacement)
Quadratic probing
Double Hashing

Gate- ADA & DSA by Nitesh Dubey

Linear Probing (insert 12)


12 = 1 x 11 + 1
12 mod 11 = 1

Gate- ADA & DSA by Nitesh Dubey

Search with linear probing (Search 15)


15 = 1 x 11 + 4
15 mod 11 = 4

NOT FOUND !

Gate- ADA & DSA by Nitesh Dubey

Deletion with linear probing: LAZY (Delete 9)


9 = 0 x 11 + 9
9 mod 11 = 9

FOUND !

Gate- ADA & DSA by Nitesh Dubey

Search Performance

0
1
2
3
4
5
6

K6

K6info

Average number of probes needed


to retrieve the value with key K?
K

K3

K3info

K1

K1info

K4

K4info

K2

K2info

K5

K5info

hash(K)
K1
3
K2
5
K3
2
K4
3
K5
2
K6
4

#probes
1
1
1
2
5
4

14/6 = 2.33 (successful)


Gate- ADA & DSA by Nitesh Dubey

Chaining
0

1
2
3
4

Gate- ADA & DSA by Nitesh Dubey

A Chained Hash Table

insert keys:
K1 --> 3
K2 --> 5
K3 --> 2
K4 --> 3
K5 --> 2
K6 --> 4

0
1
2
3
4
5
6

Gate- ADA & DSA by Nitesh Dubey


linked lists of synonyms

K3 K3info K5 K5info
K1 K1info K4 K4info
K6 K6info
K2 K2info

Insertion: insert 53

53 = 4 x 11 + 9
53 mod 11 = 9

0
1
2
3
4
5
6
7
8
9
10

23
24
36

56

14

16
17
7

29

31

20

42

0
1
2
3
4
5
6
7
8
9
10

23
24
36

14

16
17
7

29

53

20

Gate- ADA & DSA by Nitesh Dubey

31

56

42

Search Performance
0
1
2
3
4
5
6

Average number of probes needed


to retrieve the value with key K?
K
K3 K3info
K1 K1info
K6 K6info
K2 K2info

K5 K5info
K4 K4info

hash(K)
K1
3
K2
5
K3
2
K4
3
K5
2
K6
4

#probes
1
1
1
2
2
1

8/6 = 1.33 (successful)


Gate- ADA & DSA by Nitesh Dubey

Quadratic Probing
Solves the clustering problem in Linear Probing

Check H(x)
If collision occurs
If collision occurs
If collision occurs
If collision occurs
...

check
check
check
check

H(x)
H(x)
H(x)
H(x)

+
+
+
+

1
4
9
16

H(x) + i2

Gate- ADA & DSA by Nitesh Dubey

Quadratic Probing (insert 12)


12 = 1 x 11 + 1
12 mod 11 = 1
12 mod 11 = 1
(12+1) mod 11 = 2
(12+4) mod 11 = 5
(12+9) mod 11 = 10
(12+16) mod 11 = 6
(12+25) mod 11 = 4

Gate- ADA & DSA by Nitesh Dubey

Double Hashing
When collision occurs use a second hash function
Hash2 (x) = R (x mod R)
R: greatest prime number smaller than table-size
Inserting 12
H2(x) = 7 (x mod 7) = 7 (12 mod 7) = 2
Check H(x)
If collision occurs check H(x) + 2
If collision occurs check H(x) + 4
If collision occurs check H(x) + 6
If collision occurs check H(x) + 8

H(x) + i * H2(x)
Gate- ADA & DSA by Nitesh Dubey

Double Hashing (insert 12)


12 = 1 x 11 + 1
12 mod 11 = 1
7 12 mod 7 = 2

Gate- ADA & DSA by Nitesh Dubey

Factors affecting Search Performance


quality of hash function
how uniform?
depends on actual data

collision resolution strategy used


load factor of the HashTable
N/Tsize
the lower the load factor the better the search
performance

Gate- ADA & DSA by Nitesh Dubey

Traversal
Visit each item in the hash table

Open hash table


O(Tsize) to visit all n items

Tsize is larger than n

Chained hash table


O(Tsize + n) to visit all n items

Items are not visited in order of key value

Gate- ADA & DSA by Nitesh Dubey

Rehashing
If table gets too full, operations will take too long.
Build another table, twice as big (and prime).
Eg.- Next prime number after 11 x 2 is 23

Insert every element again to this table

Rehash after a percentage of the table becomes full


(70% for example)

Gate- ADA & DSA by Nitesh Dubey

Question-1
An Advantage of chained hash table (external hashing)
over the open addressing scheme isA. Worst case complexity of search operations is less
B. Space used is less
C. Deletion is easier
D. None of the above

Ans:- (C)

Gate- ADA & DSA by Nitesh Dubey

Question-2
A chained hash table has an array size of 512. What is
the maximum number of entries that can be placed in
the table?
A. 256
B. 511
C. 512
D. 1024
E. There is no maximum.

Ans:- (E)

Gate- ADA & DSA by Nitesh Dubey

Question-3
The hash function hash : = key mod size, and linear
probing are used to insert the keys37, 38, 72, 48, 98, 11, 56
into the hash table with indices 0 ... 6. The order of the
keys in the array are given byA.
B.
C.
D.
E.

72,
11,
98,
98,
11,

11,
48,
11,
56,
37,

37,
37,
37,
37,
48,

38,
38,
38,
38,
38,

56,
72,
72,
72,
72,

98,
98,
56,
11,
98,

48
56
48
48
56

Gate- ADA & DSA by Nitesh Dubey

Ans:- (D)

Question-4
An internal hash table has 5 buckets, numbered 0, 1, 2,
3, 4. Keys are integers, and the hash function
h(i) = i mod 5
is used, with linear resolution of collisions. If elements
with keys 13, 8, 24, 10, and 3 are inserted, in that
order, into an initially blank hash table, then the
content of the bucket numbered 2 isA. 3
B. 8
C. 10
D. 13
E. 24
Gate- ADA & DSA by Nitesh Dubey

Ans:- (A)

Question-5
Suppose there is an open (external) hash table with four buckets,
numbered 0,1,2,3, and integers are hashed into these buckets
using hash function h(x) = x mod 4, If the sequence of perfect
squares 1,4,9,.. ,l2, ... is hashed into the table, then, as the total
number of entries in the table grows, what will happen?
A. Two of the buckets will each get approximately half the entries,
and the other two will remain empty.
B. All buckets will receive approximately the same number of
entries.
C. All entries will go into one particular bucket.
D. All buckets will receive entries, but the difference between the
buckets with smallest and largest number of entries will grow.
E. Three of the buckets will each get approximately one-third of the
entries, and the fourth bucket will remain empty.

Ans:- (A)

Gate- ADA & DSA by Nitesh Dubey

Question-6
Insert the characters of string K R P C S N Y T J M into a
hash table of size 10. Use the hash function
h(x) = (ord(x) ord(a + 1) mod 10.
Use linear probing to resolve collisions. Get the answer
of followingA. Which insertions cause collisions?
B. Display the final hash table.

Ans:-

Collisions at J M
Table Contents- T K J C N Y P M R S
Gate- ADA & DSA by Nitesh Dubey

Question-7
A hash table implementation uses function of (% 7) and
linear probing to resolve collision. What is the ratio of
numbers in the following series with out collision and
with collision if 7 buckets are used:
32, 56, 87, 23, 65, 26, 93
A. 2/5
B. 3/4
C. 4/3
D. 5/2
Ans:- (C)

Gate- ADA & DSA by Nitesh Dubey

some more Asymptotic Notations

Gate- ADA & DSA by Nitesh Dubey

Asymptotic Notation
little Oh (o) : o-notation is used to denote an upper
bound that is asymptotically tight.
f(n) = o(g(n)) ,
for any positive constants c >0 and n0 >0 such that
0 f(n) < c.g(n) for all n, n n0.
The function f(n) becomes insignificant relative to g(n) as
n approaches infinity, i.e.

For Example : 2n = o(n2)


2n2 != o(n2)
Gate- ADA & DSA by Nitesh Dubey

Asymptotic Notation
little Omega (w) : w-notation is used to denote an lower
bound that is asymptotically tight.
f(n) = w (g(n)) ,
for any positive constants c >0 and n0 >0 such that
0 c.g(n) < f(n) for all n, n n0.
The function f(n) becomes tends to infinity relative to g(n)
as n approaches infinity, i.e.

For Example : 2n2 = w (n)


2n2 != w (n2)
Gate- ADA & DSA by Nitesh Dubey

Asymptotic Notations Relationship


If f(n) = O(g(n)), i.e. f(n) grows no faster than g(n).
If f(n) = o(g(n)), i.e. f(n) grows slower than g(n).

If f(n) = (g(n)) i.e. f(n) grows no slower than g(n).


If f(n) = w (g(n)) i.e. f(n) grows faster than g(n).

If f(n) = (g(n)) i.e. f(n) and g(n) grow at the same rate.

Gate- ADA & DSA by Nitesh Dubey

Asymptotic Notations Relationship

Gate- ADA & DSA by Nitesh Dubey

Algorithm Design Techniques

Gate- ADA & DSA by Nitesh Dubey

Algorithm Design Techniques


Some Designing Techniques are:
Divide-and-Conquer
Greedy approach,

Dynamic programming,
Backtracking
Branch n Bound
Lower Bound Theory
Parallel Computing
Gate- ADA & DSA by Nitesh Dubey

Algorithm Design Techniques


Divide-and-Conquer: Breaking problem into number of subproblems.

Greedy approach :optimization technique


Dynamic

programming:

optimization technique

Dynamic

programming

is

an

Backtracking: Backtracking is a systematic method for


generating all (or subsets of) combinortial objects.

Branch n Bound : It is a rather general optimization technique

that applies where the greedy method and dynamic programming


fail.

Lower Bound Theory : Deriving good lower bounds.


Parallel Computing : does parallel processing.

Gate- ADA & DSA by Nitesh Dubey

Greedy Approach

Gate- ADA & DSA by Nitesh Dubey

Greedy approach
Optimization technique.

A greedy algorithm is any algorithm that follows the


problem solving heuristic of making the locally optimal
choice at each stage with the hope of finding the global
optimum.
The greedy method is a general algorithm design paradigm,
built on the following elements:

Configurations: different choices, collections, or values to


find.
Objective Function: a score assigned to configurations,
which we want to either maximize or minimize.
Gate- ADA & DSA by Nitesh Dubey

Greedy approach
Optimization technique.

A greedy algorithm is any algorithm that follows the


problem solving heuristic of making the locally
optimal choice at each stage with the hope of finding
the global optimum.
Types:
Pure greedy algorithms
Orthogonal greedy algorithms
Relaxed greedy algorithms

Gate- ADA & DSA by Nitesh Dubey

Greedy Algorithm
Problems exhibit optimal substructure.

Problems also exhibit the greedy-choice property.


When we have a choice to make, make the one that looks best right
now.
Make a locally optimal choice in hope of getting a globally
optimal solution.

Gate- ADA & DSA by Nitesh Dubey

Greedy Algorithm..
We need to find a feasible solution that either maximizes or
minimizes a given objective function.
A feasible solution that does this is called Optimal solution.
A feasible solution is not necessarily be an optimal solution.
Working:
Greedy method devise an algorithm
(considering one input at a time).

that

works

in

stages

At each stage, a decision is made regarding whether a particular


input is in an optimal solution.
This is done by considering the input in an order determined by
some selection procedure.

If the inclusion of the next input into the partially constructed


optimal solution will result in an infeasible solution, then this
input is not added to the partial solution.
Gate- ADA & DSA by Nitesh Dubey

Otherwise it is added.

Greedy Algorithm
The selection procedure itself is based on some optimization
measure.
This measure may be the objective function.
Subset Paradigm : Generally we get an algorithm that generate
suboptimal solutions. This version of the greedy technique is
called subset paradigm.
Ordering Paradigm : Some problems do not optimized the
subset. The greedy method make decision by considering the
inputs in some order. Each decision is made using an
optimization criterion that can be computed using decisions
already made. This version of G M is called as ordering
paradigm.

Gate- ADA & DSA by Nitesh Dubey

Greedy Algorithm for Subset Paradigm


Algorithm Greedy (a, n)
// a[1:n] contains the n inputs.
{
solution = 0; //Initialize the solution
for I = 1 to n do
{
x = Select (a);
if Feasible (solution, x) then
solution = Union (solution,
x);
}
return ( solution );
}
Gate- ADA & DSA by Nitesh Dubey

Some Problems

Gate- ADA & DSA by Nitesh Dubey

The Fractional Knapsack Problem


Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight

Goal: Choose items with maximum total benefit but with weight
at most W.

If we are allowed to take fractional amounts, then this is the


fractional knapsack problem.
In this case, we let xi denote the amount we take of item i
Objective:
maximize

bi ( xi / wi )
i S

Subject to :

xi

i SGate- ADA & DSA by Nitesh Dubey

The Fractional Knapsack Problem


Largest-profit strategy: (Greedy method)
Pick always the object with largest profit.
If the weight of the object exceeds the remaining Knapsack
capacity, take a fraction of the object to fill up the Knapsack.

Largest profit-weight ratio strategy (steps):


Order profit-weight ratios of all objects.
Pi/wi >= (pi+1)/(wi+1) for 1 <= i <= n-1
Pick the object with the largest p/w If the weight of the
object exceeds the remaining knapsack capacity, take a
fraction of the object.

Gate- ADA & DSA by Nitesh Dubey

The Fractional Knapsack - Example


Solution:

Let we have five items.


Weights & Profits are as under :

knapsack
Solution:

Items:
Weight:
Profit:
Value:

4 ml

8 ml

2 ml

6 ml

1 ml

$12

$32

$40

$30

$50

20

50

1
2
6
1

ml
ml
ml
ml

of
of
of
of

5
3
4
2

10 ml

Step -1: Sort the items as per Profit/Weight in decreasing order.


i.e.; P/W = { 12/4, 32/8, 40/2, 30/6, 50/1}
= { 3, 4, 20, 5, 50}
Therefore order of items is- < I5, I3, I4, I2, I1 >
Step -2: Select the items as per their sorted order and get the profit gain
& weight utilization.
At last we will get : X= < 0, 1/8, 1, 1, 1 >
Profit Gain= 124
Gate- ADA & DSA by Nitesh Dubey

The Fractional Knapsack Algorithm


Greedy choice: Keep taking item
with highest value (benefit to weight
ratio)
Use a heap-based priority queue to
store the items, then the time complexity
is O(n log n).

Correctness: Suppose there is a


better solution
there is an item i with higher value than
a chosen item j (i.e., vj<vi) , if we replace
some j with i, we get a better solution
Thus, there is no better solution than
the greedy one

Algorithm
fractionalKnapsack(S, W)
Input: set S of items w/ benefit
bi
and weight wi; max. weight
W

Output: amount xi of each item


i
to maximize benefit with
weight at most W
for each item i in S
xi
0
Step 1: Sort pi/wi into non-increasing order.
vi
bi / wi
{value}
Step 2: Put the objects into the knapsack according
w
0
{current total
to the sorted sequence as possible as we can.
weight}
while w < W
remove item i with highest vi
xi
min{wi , W w}
Gate- ADA & DSA by Nitesh Dubey
w
w + min{wi , W w}

The Fractional Knapsack Problem.

Complexity - ???????

Gate- ADA & DSA by Nitesh Dubey

Job sequencing with Deadlines


Problem:
Let we have n jobs, S={1, 2, , n},
each job i has a deadline di 0 and,
a profit pi

0.

We need one unit of time to process each job and we


can do at most one job each time.
We can earn the profit pi if job i is completed by its
deadline

Gate- ADA & DSA by Nitesh Dubey

Job sequencing with deadlines- Algorithm


Step 1: Sort pi into non-increasing order. After sorting p1
pi.

p2

p3

Step 2: Add the next job i to the solution set if i can be completed
by its deadline.
Assign i to time slot [r-1, r], where r is the largest integer
such that 1 r di and [r-1, r] is free.
Step 3: Stop if all jobs are examined. Otherwise, go to step 2.
Time complexity: O(n2)
Gate- ADA & DSA by Nitesh Dubey

Job sequencing with deadlines- Example


Let N=5, P= { 10, 20 , 5, 1, 15}
D= { 1, 2, 3, 3, 2}
Step 1:-Sort the jobs as per their Profit in decreasing order.
Therefore order of items is- < J2, J5, J1, J3, J4 >
Step 2:-Add the next job i to the solution set from sorted list if i can be
completed by its deadline in following manner:
Add i at di deadline if di deadline is free.
Otherwise go for next di-1 deadlines until get a free deadline slot.
Otherwise discard that job.

Step 3:- Stop if all jobs are examined. Otherwise, go to step 2.


Solution:
solution = {5, 2, 3}
total profit =15 + 20 + 5 =40

pi

di

20

assign to [1, 2]

15

assign to [0, 1]

10

reject

assign to [2, 3]

reject

Gate- ADA4
& DSA by Nitesh
1 Dubey

Job sequencing with deadlines.

Time complexity:
O(n2)

Gate- ADA & DSA by Nitesh Dubey

What is a Minimum Spanning Tree.

Let G = (V, E) be a simple, connected, undirected graph that is not


edge-weighted.

A spanning tree of G is a free tree (i.e., a tree with no root) with | V | 1 edges that connects all the vertices of the graph.

Thus a minimum spanning tree for G is a graph, T = (V, E) with the


following properties:
V = V
T is connected
T is acyclic.

A spanning tree is called a tree because every acyclic undirected graph


can be viewed as a general, unordered tree. Because the edges are
undirected, any vertex may be chosen to serve as the root of the tree.

Gate- ADA & DSA by Nitesh Dubey

Constructing Minimum Spanning Trees

Any traversal of a connected, undirected graph visits all the


vertices in that graph. The set of edges which are traversed
during a traversal forms a spanning tree.
(a) Graph G

For example, Fig:(b) shows the spanning tree obtained from a


breadth-first traversal starting at vertex b.
(b) Breadth-first
spanning tree of
G rooted at b

Similarly, Fig:(c) shows the spanning tree obtained from a


depth-first traversal starting at vertex c.

Gate- ADA & DSA by Nitesh Dubey

(c) Depth-first
spanning tree of
G rooted at c

What is a Minimum-Cost Spanning Tree

For an edge-weighted , connected, undirected graph, G, the total cost of G is the


sum of the weights on all its edges.
A minimum-cost spanning tree for G is a minimum spanning tree of G that has the
least total cost.
Example: The graph

Has 16 spanning trees. Some are:

The graph has two minimum-cost spanning trees, each with a cost of 6:

Gate- ADA & DSA by Nitesh Dubey

Applications of Minimum-Cost Spanning Trees


Minimum-cost spanning trees have many applications. Some are:

Building cable networks that join n locations with minimum cost.

Building a road network that joins n cities with minimum cost.

Obtaining an independent set of circuit equations for an electrical network.

In pattern recognition minimal spanning trees can be used to find noisy


pixels.

Gate- ADA & DSA by Nitesh Dubey

Constructing Minimum Spanning Trees


Solution:
Using greedy method.
Two algorithms:
Prim's algorithm.
Kruskal's algorithm.

Gate- ADA & DSA by Nitesh Dubey

Prims Algorithm

Prims algorithm finds a minimum cost spanning tree by selecting


edges from the graph one-by-one as follows:
It starts with a tree, T, consisting of the starting vertex, x.
Then, it adds the shortest edge emanating from x that connects T to
the rest of the graph.
It then moves to the added vertex and repeats the process.

Consider a graph G=(V, E);


Let T be a tree consisting of only the starting vertex x;
while (T has fewer than IVI vertices)
{
find a smallest edge connecting T to G-T;
add it to T;
}

Gate- ADA & DSA by Nitesh Dubey

Minimum Cost Spanning Trees.


Prims algorithm
T = {};
TV = {0};
/* start with vertex 0 and no edge*/
while (T contains fewer than n-1 edges) {
let (u,v) be a least cost edge such that
u TV and v TV;
if (there is no such edge)
break;
add v to TV;
add (u,v) to T;
}
if (T contains fewer than n-1 edges)
printf(no spanning tree\n);

Gate- ADA & DSA by Nitesh Dubey


Networking Laboratory

Minimum Cost Spanning Trees.

10

5
25

3
(a)

10
2

10
2

5
25

4
3
(b)

stages in Prims algorithm


Gate- ADA & DSA by Nitesh Dubey

1
6

4
22

3
(c)

Minimum Cost Spanning Trees.

10

5
25

10
2

12
22
(d)

0
1

5
25

16
2

12
22

3
(e)

stages in Prims algorithm


Gate- ADA & DSA by Nitesh Dubey

10

14

5
25

16
2

12
22

3
(f)

Example
Trace Prims algorithm starting at vertex a:

The resulting minimum-cost spanning tree is:

Gate- ADA & DSA by Nitesh Dubey

Minimum Cost Spanning Trees.


Kruskals algorithm
select the edges for inclusion in T in non-decreasing
order of their cost
an edge is added to T if it does not form a cycle with
the edges that are already in T
exactly n-1 edges are selected

Gate- ADA & DSA by Nitesh Dubey

Minimum Cost Spanning Trees.


Kruskals algorithm
T = {};
while (T contains less than n-1 edges && E is not
empty) {
choose a least cost edge (v,w) from E;
delete (v,w) from E;
if ((v,w) does not create a cycle in T)
add (v,w) to T;
else
discard (v,w);
}
if (T contains fewer than n-1 edges)
printf(no spanning tree\n);

Gate- ADA & DSA by Nitesh Dubey

Minimum Cost Spanning Trees.


choosing a least cost edge(v,w) from E
a min heap
determine and delete the next least cost edge: O(log2e)
construction of the heap: O(e)

checking that the new edge,(v,w), does not form


a cycle in T
use the union-find operations

Gate- ADA & DSA by Nitesh Dubey

Minimum Cost Spanning Trees.

0
10
5
25

28
1

14
6

24
22

(a)

16
2

18

12

10
2

4
3
(b)

stages in Kruskals algorithm


Gate- ADA & DSA by Nitesh Dubey

3
(c)

Minimum Cost Spanning Trees.

10

12
3

(d)

10

0
1

14

10
2

12
3
(e)

stages in Kruskals algorithm


Gate- ADA & DSA by Nitesh Dubey

14

16
2

12
3
(f)

Minimum Cost Spanning Trees.

0
10

0
1

14

16
2

12
22

10

14

5
25

12
22

(g)

3
(h)

stages in Kruskals algorithm


Gate- ADA & DSA by Nitesh Dubey

16

Minimum Cost Spanning Trees.


edge
----(0,5)
(2,3)
(1,6)
(1,2)
(3,6)
(3,4)
(4,6)
(4,5)
(0,1)

weight
----10
12
14
16
18
22
24
25
28

result
initial
added to tree
added
added
added
discarded
added
discarded
added
not considered

figure
(b)
(c)
(d)
(e)
(f)
(g)
(h)

summary of the Kruskals algorithm


Gate- ADA & DSA by Nitesh Dubey

Optimal Storage on Tapes

Gate- ADA & DSA by Nitesh Dubey

Optimal Storage on Tapes


Minimize Mean Retrieval Time (MRT)

Time complexity:
an efficient sorting method

O(nlogn)

Gate- ADA & DSA by Nitesh Dubey

Optimal Merge Patterns

Gate- ADA & DSA by Nitesh Dubey

Optimal Merge Patterns


Merge two sorted
files each has n &
m
elements,
respectively: takes
O (n+m).
Problem
Statement:
Given n sorted
files.
Merge n files in a
minimum amount
of time.

Gate- ADA & DSA by Nitesh Dubey

Huffman Coding
Take the characters and their frequencies, and sort
list by increasing frequency.

this

All the characters are vertices of the tree.


Take the first 2 vertices from the list and make them
children of a vertex having the sum of their frequencies.
Insert the new vertex into the sorted list of vertices
waiting to be put into the tree.
If there are at least 2 vertices in the list, go to step 3.

Read the Huffman code from the tree.


Gate- ADA & DSA by Nitesh Dubey

Huffman Coding

Final codes are :


b0
a 11
c 101
d 1001
e 1000

Gate- ADA & DSA by Nitesh Dubey

Single Source Shortest Path


Given:
Given a weighted digraph G= (V,E) where the
weights are >0.
A source vertex, vo V.

Problem Statement :
Find the shortest path from vo to all other nodes
in G.
Shortest paths are generated in increasing order:
1,2,3, ..

Gate- ADA & DSA by Nitesh Dubey

Single Source Shortest Path..


Lemma 24.1: Let p = v1, v2, , vk be a SP from v1 to vk.
Then,
pij = vi, vi+1, , vj is a SP from vi to vj, where 1 i j k.
So, we have the optimal-substructure property.
Bellman-Fords algorithm uses dynamic programming.
Dijkstras algorithm uses the greedy approach.
Let (u, v) = weight of SP from u to v.

Gate- ADA & DSA by Nitesh Dubey

Single Source Shortest Path..


Algorithm:
Assumes no negativeweight edges.

Maintains a set S of
vertices whose SP from
s has been determined.
Repeatedly selects u in
VS with minimum SP
estimate
(greedy
choice).
Store VS in priority
queue Q.

T(V) = O(V2)
Gate- ADA & DSA by Nitesh Dubey

Single Source Shortest Path..


Dijkstras Algorithm
Initialize(G, s);
S := ;
Q := V[G];
while Q
do
u := Extract-Min(Q);
S := S {u};
for each v Adj[u] do
Relax(u, v, w)
od
od

Gate- ADA & DSA by Nitesh Dubey

Dynamic Programming

Gate- ADA & DSA by Nitesh Dubey

Dynamic Programming
Dynamic programming is an optimization technique.

It solves complex problems by breaking them down


into simpler sub-problems.
It is applicable to problems exhibiting the properties
of overlapping subproblems which are only slightly
smaller, and optimal substructure (described below).
the method takes far less time than general methods.

Gate- ADA & DSA by Nitesh Dubey

Dynamic Programming..
Dynamic programming is a method for efficiently solving a
broad range of search and optimization problems which
exhibit the characteristics of overlapping sub problems
and optimal substructure.
Richard Bellman described the way of solving problems
where you need to find the best decisions one after
another.
In 1982 David Kohler used dynamic programming to
analyses the best way to play the game of darts.
When applicable, the method takes much less time than
naive methods.

Gate- ADA & DSA by Nitesh Dubey

Dynamic Programming
Top-down dynamic programming simply means storing
the results of certain calculations, which are then re-used
later because the same calculation is a sub-problem in a
larger calculation.
Top-Down method is often called Memorization.
Bottom-up dynamic programming involves formulating a
complex calculation as a recursive series of simpler
calculations.
In Bottom Up Dynamic Programming, we start from
smaller cases and store the calculated values in a table
for future use, an effective strategy to most dependencybased problems.
This avoids calculating the subproblem twice.
Gate- ADA & DSA by Nitesh Dubey

Dynamic Programming v/s Divide &


Conquer:
Divide-and-conquer
S.
No.
1
Divide-and-conquer
algorithms split a problem
into separate subproblems,
solve the subproblems, and
combine the results for a
solution to the original
problem.
Example:
Quicksort,
Mergesort, Binary search
2

Divide-and-conquer
algorithms can be thought
of as top-down algorithms

Dynamic Programming
Dynamic Programming split
a
problem
into
subproblems,
some
of
which are common, solve
the
subproblems,
and
combine the results for a
solution to the original
problem.
Example: Multistage graph,
Matrix Chain Multiplication
Dynamic programming can
be thought of as bottom-up

Gate- ADA & DSA by Nitesh Dubey

Dynamic Programming v/s Divide &


Conquer:
Divide-and-conquer
S.
No.
3 In divide and conquer,
subproblems
are
independent.

Dynamic Programming
In Dynamic Programming,
subproblems
are
not
independent.

Divide & Conquer solutions


are simple as compared to
Dynamic programming .

Dynamic
programming
solutions can often be quite
complex and tricky.

Divide & Conquer can be


used for any kind of
problems.
Only
one
decision
sequence is ever generated

Dynamic programming is
generally
used
for
Optimization Problems.
Many decision sequences
may be generated.

Gate- ADA & DSA by Nitesh Dubey

Some Problems

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem


A multi-stage graph is a directed graph G = < V, E >, in
which the nodes are partitioned into k 2 disjoint sets Vi,
1 i k. Also, every edge (u, v) in E has u in Vi and v in Vi +
1, for some i.
This means that we can visualize the graph as a number of
layers or stages, defined by Vi and the edges go from one
stage to the next.
A typical practical example arises in project management, where,
in order to complete a project, we have to perform activities in
several stages and at each stage we have to select a particular
action from the several alternatives available.
The resource allocation problem can be described as a multistage
graph.
Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem

Forward
Method
Backward
Method

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem


To find a shortest path in a multi-stage graph
3

Apply the greedy method :


the shortest path from S to T :
1+2+5=8

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem


e.g.

1
11

18

16

13

The greedy method can not be applied to this case:


(S, A, D, T)

1+4+18 = 23.

The real shortest path is:


(S, C, F, T) 5+2+2 = 9.

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem


Two Approaches :
Forward Method

cost (i, j )

min {c( j , l )
l Vi 1
j ,l
E

cost (i 1, l )}

Backward Method

bcost (i, j)

min {bcost (i 1, l) c(l, j)}


l Vi 1
j,l
E

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem


Dynamic programming approach (forward approach):
4

1
11

16

13

d(B, T)

d(A, T)

18

9
5

d(C, T)

d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}

d(A,T) = min{4+d(D,T), 11+d(E,T)} A


= min{4+18, 11+13} = 22.
Gate- ADA & DSA by Nitesh Dubey

11

D
E

d(D, T)

T
d(E, T)

Multistage Graph Problem -

forward approach

Solving:
cost(k-1,j) = c(j,t) if <j,t> E, otherwise
Then computing cost(k-2,j) for all j Vk-2
Then computing cost(k-3,j) for all j Vk-3

Finally computing cost(1,s)

Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem

Example:

k=5

Stage 5
cost(5,12) = 0.0
Stage 4
cost(4,9) = min {4+cost(5,12)} = 4
cost(4,10) = min {2+cost(5,12)} = 2
cost(4,11) = min {5+cost(5,12)} = 5
Stage 3
cost(3,6) = min {6+cost(4,9), 5+cost(4,10)} = 7
cost(3,7) = min {4+cost(4,9), 3+cost(4,10)} = 5
cost(3,8) = min {5+cost(4,10), 6+cost(4,11)} = 7

Gate- ADA & DSA by Nitesh Dubey

forward approach

Multistage Graph Problem -

Stage 2
cost(2,2)
cost(2,3)
cost(2,4)
cost(2,5)
Stage 1
cost(1,1)

=
=
=
=

min
min
min
min

forward approach

{4+cost(3,6), 2+cost(3,7), 1+cost(3,8)} = 7


{2+cost(3,6), 7+cost(3,7)} = 9
{11+cost(3,8)} = 18
{11+cost(3,7), 8+cost(3,8)} = 15

= min {9+cost(2,2), 7+cost(2,3), 3+cost(2,4), 2+cost(2,5)} = 16

Important notes: avoiding the recomputation of cost(3,6), cost(3,7), and


cost(3,8) in computing cost(2,2)
Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem -

forward approach

Recording the path


d(i,j) = value of l (l is a vertex) that minimizes
c(j,l)+cost(i+1,l) in equation
In Figure
d(3,6)=10; d(3,7)=10; d(3,8)=10
d(2,2)=7; d(2,3)=6; d(2,4)=8; d(2,5)=8
d(1,1)=2

When letting the minimum-cost path 1,v2,v3,,vk-1,t,

v2 = d(1,1) = 2
v3 = d(2,d(1,1)) = 7
v4 = d(3,d(2,d(1,1))) = d(3,7) = 10
So the solution (minimum-cost path) is 1271012 and
its cost is 16
Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem - algorithm


Forward Approach:
void Fgraph (graph G, int k, int n, int p[] )
// The input is a k-stage graph G = (V,E) with n vertices indexed in
order
// of stages. E is a set of edges and c[i][j] is the cost of <i, j>.
// p[1 : k] is a minimum-cost path.
{
float cost[MAXSIZE]; int d[MAXSIZE], r;
cost[n] = 0.0;
for (int j=n-1; j >= 1; j--) { // Compute cost[j].
let r be a vertex such that <j, r> is an edge
of G and c[j][r] + cost[r] is minimum;
cost[j] = c[j][r] + cost[r];
d[j] = r;
}
// Find a minimum-cost path.
p[1] = 1; p[k] =n ;
for ( j=2; j <= k-1; j++) p[j] = d[ p[ j-1 ] ];
}
Gate- ADA & DSA by Nitesh Dubey

Multistage Graph Problem - complexity

Time complexity:
O(|V|+|E|)

Gate- ADA & DSA by Nitesh Dubey

Reliability Design

Gate- ADA & DSA by Nitesh Dubey

Reliability Design
Want to design a system that is composed of
several devices connected in Series.

where 1 i n
Given

Reliability of each device; ri for device Di

where 0 ri 1
Multiple copies of same device type are connected in parallel.
Cost of single unit of device Di is ci
Maximum allowable cost of entire system, c
Gate- ADA & DSA by Nitesh Dubey

Reliability Design

Problem Statement : Design a system having


maximum Reliability by maintaining the
upper limit of overall system cost, i.e.-

Gate- ADA & DSA by Nitesh Dubey

Reliability Design
If stage i contains mi copies of device Di, then reliability of stage i
becomes

Solution :
Find the maximum possible instance of a device with reference
to the overall system cost (c) by following equation :

where 1 mi ui

Apply following function for optimization.

Gate- ADA & DSA by Nitesh Dubey

Reliability Design

Solution :

Gate- ADA & DSA by Nitesh Dubey

Reliability Design

Time complexity:
just find out

Gate- ADA & DSA by Nitesh Dubey

All Pair Shortest Path

Gate- ADA & DSA by Nitesh Dubey

All Pair Shortest Path


The problem that considers finding the
shortest path between all pairs of vertices on
a graph.
The algorithm runs in O(|V|3) time and
negatively weighed edges may be present,
however negatively weighted cycles cause
problems with the algorithm

Gate- ADA & DSA by Nitesh Dubey

All Pair Shortest Path


Given :
Let G= <V, E> be a directed graph with n
vertices.
Let cost(i, j) be a cost adjacency matrix for G
such that,
cost(i, i)=0.
cost(i, j)= , if (i, j) not E(G).
cost(i, j)= w , where w is weight of edge (i, j) E(G).
Problem Statement :
All Pair Shortest Path problem is to determine a
matrix A such that A(i, j) is the length of a shortest path from i to
j.
Gate- ADA & DSA by Nitesh Dubey

All Pair Shortest Path


One can solve this problem by applying n times single
source shortest path algorithm.
It will take O(n3) time.
The Floyd-Warshall algorithm is an efficient algorithm to
find all-pairs shortest paths on a graph.
It is a Dynamic programming approach.

Apply following formula to get the solution-

where,

clearly,

Apply above equation n times to get matrices of A.


Gate- ADA & DSA by Nitesh Dubey

All Pair Shortest Path - algorithm

Gate- ADA & DSA by Nitesh Dubey

All Pair Shortest Path- Example


Example :

For following
given graph find the all pair
shortest path.

Solution : After applying


mentioned equation we
will get following matrices
as solution.

where A0 is original cost matrix.


A1 is cost of vertices traveling via
vertex 1.
A2 is cost of vertices traveling via
vertex 2.
A3 is cost of vertices traveling via
vertex 3 (i.e. final solution).
Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem

Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


We are given a knapsack of capacity c and a set of n objects
numbered 1,2,,n. Each object i has weight wi and profit pi.
Let v = [v1, v2,, vn] be a solution vector in which vi = 0 if object i
is not in the knapsack, and vi = 1 if it is in the knapsack.
The goal is to find a subset of objects to put into the knapsack so
that

(that is, the objects fit into the knapsack) and

is maximized (that is, the profit is maximized).


Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


The naive method is to consider all 2n possible subsets of the n
objects and choose the one that fits into the knapsack and
maximizes the profit.
Let F[i,x] be the maximum profit for a knapsack of capacity x using
only objects {1,2,,i}. The DP formulation is:

Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight

Goal: Choose items with maximum total benefit but with weight
at most W.
Here we are not allowed to select the items fractionally.
i.e. x = 0 or x = 1 for all items.

Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


Solution :
Let an fj(y) be the value of an optimal solution to KNAP(1, j, y).
Since the principal of optimality holds, we obtain

For arbitrary fi(y) , i > 0, above equation generalizes to

Solve the equation for fn(m) by beginning with the knowledge


f0(y)=0 for all y and fi(y) = -, for y < 0.
Then f1 , f2 , f3 , fn can be successively computed using above
equation.
Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


Solution :
We can use following ordered set to represent fj(y) -

Si = { (fi( yj ) , yj ) : 1 j k }
Si is a pair of (P, W), where P = fi( yj ) and W = yj .

Note that S0={(0, 0)}.


We can compute Si+1 from Si by first computing -

Now, Si+1 can be computed by merging the pair in Si and Si


together.

Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


Discarding Rule :

If Si+1 contains two pairs (Pj, Wj) and (Pk, Wk) with
the property that Pj Pk and Wj Wk , then the pair
(Pj, Wj) can be discarded.
It is also called as Purging rules.

Gate- ADA & DSA by Nitesh Dubey

0/1 Knapsack Problem


Example : Consider the knapsack instance n=3, (w1,w2,w3)= (2,3,4),
(p1,p2, p3) = (1,2,5), and m=6. Find the solution.

Solution : after computation we get following ordered pair. Here we


can discard the pair (3,5) because of Purging rule.

The pair (6,6) will get selected. Hence optimal solution is


(x1,x2,x3)= (1,0,1).
Gate- ADA & DSA by Nitesh Dubey

Single Source Shortest Path

Gate- ADA & DSA by Nitesh Dubey

Single Source Shortest Path


Given: A single source vertex in a weighted,
directed graph.
Want to compute a shortest path for each
possible destination.
Similar to BFS.

We will assume either


no negative-weight edges, or
no reachable negative-weight cycles.

Algorithm will compute a shortest-path tree.


Similar to BFS tree.

Gate- ADA & DSA by Nitesh Dubey

Bellman-Ford Algorithm -

Single Source Shortest Path

Can have negative-weight edges.


Will detect reachable negative-weight cycles.
Initialize(G, s);
for i := 1 to |V[G]| 1 do
for each (u, v) in E[G] do
Relax(u, v, w)
Time
od
Complexity
od;
is O(VE).
for each (u, v) in E[G] do
if d[v] > d[u] + w(u, v) then
return false
fi
od;
return true
Gate- ADA & DSA by Nitesh Dubey

Design Tech. & Algorithm

Gate- ADA & DSA by Nitesh Dubey

Recurrence Relation

Gate- ADA & DSA by Nitesh Dubey

Recurrence Relation
A recurrence relation for the sequence {an} is an
equation that expresses an in terms of one or more of the
previous terms a0; a1; .. ; an-1, for all integers n with n
>= n0.

Many sequences can be a solution for the same


recurrence relation.
an = 2an-1 - an-2;
for n>= 2
The following sequences are solutions of this recurrence
relation:
an = 3n,
for all n >= 0,
an = 5,
for all n >= 0.
Gate- ADA & DSA by Nitesh Dubey

Divide-and-Conquer- Recurrence Relation


also called as Difference Equation.
Is a numerical way to represent the Computation time &
other parameters of analysis.
Recurrence Relation for Divide & Conquer :

T(1)

n=1

T(n) =
aT(n/b) + f(n)

n>1

Where a and b are known constant,


T(1) is known,

n is a power of b ( i.e. n= bk )
Gate- ADA & DSA by Nitesh Dubey

Recurrence Relations
In other words, a recurrence relation is like a
recursively defined sequence, but without specifying
any initial values (initial conditions).
Therefore, the same recurrence relation can have (and
usually has) multiple solutions.
If both the initial conditions and the recurrence
relation are specified, then the sequence is uniquely
determined.

Gate- ADA & DSA by Nitesh Dubey

Recurrence Relation..
The initial conditions for a sequence specify the terms
before n0 (before the recurrence relation takes effect).
The recurrence relations together with the initial
conditions uniquely determines the sequence.
For the example above, the initial conditions are: a0 = 0,
a1 = 3; and a0 = 5, a1 = 5; respectively.

Gate- ADA & DSA by Nitesh Dubey

Recurrence Relation..
Some more examples of recurrence relation are:
tn = tn-1 + 1 (Sequential search)
tn = tn-1 + n (Selection sort)

tn = tn/2 + 1 (Binary search)


tn = 2tn/2 + n (Merge sort)

Gate- ADA & DSA by Nitesh Dubey

Modeling with Recurrence Relations


Example:
Someone deposits $10,000 in a savings account
at a bank yielding 5% per year with interest
compounded annually. How much money will be in the
account after 30 years?
Solution:
Let Pn denote the amount in the account after n
years.
How can we determine Pn on the basis of Pn-1?

Gate- ADA & DSA by Nitesh Dubey

Modeling with Recurrence Relations.


We can derive the following recurrence relation:
Pn = Pn-1 + 0.05Pn-1
= 1.05Pn-1.
The initial condition is P0 = 10,000.
Then we have:
P1 = 1.05P0
P2 = 1.05P1 = (1.05)2P0
P3 = 1.05P2 = (1.05)3P0

Pn = 1.05Pn-1 = (1.05)nP0
We now have a formula to calculate Pn for any natural
Gate- ADA
& DSA
by Nitesh Dubey
number n and can avoid
the
iteration.

Modeling with Recurrence Relations.


Let us use this formula to find P30 under the initial
condition P0 = 10,000:
P30 = (1.05)30 10,000 = 43,219.42
After 30 years, the account contains $43,219.42.

Gate- ADA & DSA by Nitesh Dubey

Complexity of Some Recurrence Relation

Gate- ADA & DSA by Nitesh Dubey

Methods of Solving Recurrence Relations


The Master Theorem
Substitution method
Draw the recursion tree, think about it
Guess at an upper bound, prove it
Accounting method

Gate- ADA & DSA by Nitesh Dubey

Master Theorem
Consider following R.R
T(n) = a T(n/b) + f(n)
Ignore floors and ceilings for n/b
constants a >= 1 and b>1

f(n) any function


Let e = logba

CASE 1:
If f(n) = O(ne-c) for constant c>0, then

T(n) =

(ne)

Gate- ADA & DSA by Nitesh Dubey

Master Theorem.
CASE 2:
If f(n) =

(ne), then

T(n) =
CASE 3:
If f(n) =

(f(n).log n)

(ne+c) for constant c>0, then

T(n) =

(f(n))

Gate- ADA & DSA by Nitesh Dubey

Master Theorem.
Example:
T(n) = 4 T(n/2) + n
a=
b=
nlog_ba =

f(n) =

Gate- ADA & DSA by Nitesh Dubey

Complexity of Some Major Algorithm

Gate- ADA & DSA by Nitesh Dubey

End of Part -I

Gate- ADA & DSA by Nitesh Dubey

Você também pode gostar