Beruflich Dokumente
Kultur Dokumente
Objectives:
At the end of the activity the students should be able to:
1. Design an algorithmic solution for the given problem and implement the same
in c/c++ and measure the time complexity(running time) of the same on two
different computers of different clock speeds.
2. Analyse and find what the given algorithm does and compute the time and
space complexity of the given algorithm.
Assignment Expectations:
The activity has two parts:
1. Part A:The students have to design an algorithmic solution to the given problem and implement
it c/c++ programming language , in any environment of your choice(However LINUX environment
is preferred )using netbeans as the tool and submit the working code to the course instructor
before one week after the discussion of the concepts in the class. (Only the correct code which
works for all possible cases will be considered for evaluation).
The implemented code should address the following points wherever applicable:
a) Use random numbers as array elements.
b) Following test cases should act as input for the below listed sorting algorithms(wherever
applicable).
Fully Random input.
Almost sorted array
Half sorted array.
Completely sorted array.
c) Plot the graph of time taken versus the input size.
d)Record and compare the run time of the implemented algorithm on two different machines of
different clock speeds and interpretate the same.
2. Part B: The students are expected to analyse the given algorithm and answer the following
questions w.r.t the given algorithm:
All these things need to be written in A4 size paper containing the problem statement
and answer to the above mentioned questions and submit the hard copy(Hand
written/typed) to the course-instructor as per guidelines said earlier.The cover page of
submission paper should contain your name, roll number, usn and division.
Problem Statement
Brute-Force Technique
b) Algorithm mode(a,n)
//Input:An array a[0..n-1]
modecount 1
for i 0 to n-2 do
count 1
for j i+1 to n-1 do
if (a[i]=a[j])
count count +1
if(count>modecount)
modecount count
modevalue a[i]
return 0
b) Algorithm BFPE(P[0..n], x)
//Input: Array P[0..n] of the coefficients of a polynomial of degree n,
// stored from the lowest to the highest and a number x
p ← 0.0
for i ← n downto 0 do
power ← 1
for j ← 1 to i do
power ← power * x
p ← p + P[i] * power
return p
for i 0 to n-j-1 do
if (a[i]>=a[i+1]
temp a[i]
a[i] a[i+1]
a[i+1] temp
flag 1
if(flag=0) break;
4. a) Implement more than one brute force algorithm for computing the
value of the polynomial:
p(x) = anxn + an-1xn-1 +… + a1x1 + a0 , at a point x = x0
And compare their efficiency.
b) Algorithm select(T[1…n])
//Input:An array T[1..n]
for i 1 to n-1
minj i; minx T[i]
for j i+1 to n
if T[j]<minx
minj j
minx T[j]
T[minj] T[i]
T[i] minx
10. a) Write a program to sort a given set of elements using Insertion sort
method , Is insertion sort efficient for smaller or large values of n?
b) Algorithm expodc(a,n)
//Input: a and n
if n=1 return a
2
if n is even then return [expodc(a,n/2)]
return a * expodc(a,n-1)
11. a) Write a separate program using adjacency list and matrix to print all the
nodes reachable from a given starting node in a digraph using BFS
method. Compare efficiency of both the methods.
b) Algorithm Insert2 (A[0..n − 1])
for i ← 1 to n − 1 do
j←i−1
while j ≥ 0 and A[j] > A[j + 1] do
swap(A[j], A[j + 1])
j←j–1
12. a) Write a separate program using adjacency list and matrix to print all the
nodes reachable from a given starting node in a digraph using DFS
method. Compare efficiency of both the methods.
b) Algorithm LogFloor (n)
//Input: A positive integer
if n = 1 return 0
else return LogFloor (n/2)+1 // its floor of n
13. a) Write a BFS based program for checking whether the given graph is
bipartite.( In the mathematical field of graph theory, a bipartite graph (or
bigraph) is a graph whose vertices can be divided into two disjoint sets U
and V such that every edge connects a vertex in U to one in V; that is, U
and V are independent sets. Equivalently, a bipartite graph is a graph that
does not contain any odd-length cycles.)
b) Algorithm Permute(n)
//Implements Heap’s algorithm for generating permutations
//Input: A positive integer n and a global array A[1..n]
if n = 1
write A
else
for i ← 1 to n do
Permute(n − 1)
if n is odd
swap A[1] and A[n]
else swap A[i] and A[n]
14. a)Write a program to find the position of largest element using decrease
by one technique and compare the run time with brute force strategy.
b) Algorithm GE(A[0..n − 1, 0..n])
//Input: An n-by-n +1 matrix A[0..n − 1, 0..n] of real numbers
for i ← 0 to n − 2 do
for j ← i +1 to n − 1 do
for k ← i to n do
A[j, k] ← A[j, k] − A[i, k] * A[j, i] / A[i, i]
b) Algorithm horner(a,n,x)
//Input: array a[0..n-1],x
p=a[n]
for i 0 to n-1
p p*x+a[i]
return p
Transform and conquer
18. a) Write a program to sort a given set of elements using the Heap sort
method .Compare heap sort technique with that of the quick sort and also
compare the worst case running time of heap sort with different sorting
techniques.
b) Algorithm PU(a[0…..n-1])
//Input: An unsorted array a[0…n-1]
//Output:Mode of the given array
Sort(a[0….n-1])
i 0
m 0
while i<=n-1 do
{
tl 1
te a[i]
while i+tl<= n-1 and A[i+tl]= te
tl=tl+1;
if tl>m
m tl
me te
i i+tl
}
return me
19. a) Write a program for constructing an AVL tree for a given list of n
distinct integers.
b) Algorithm find(a[1..n])
//Input: array a[1..n]
if (E[1]> E[2])
max=E[1]
second=E[2]
else
max=E[2]
second=E[1]
for i 3 to n
if E[i]>max
second =max
max=E[i]
else second=E[i]
Space and Time tradeoffs
Dynamic Programming
b) Algorithm Tc(a,n)
//Input:An array a[0..n-1]
MergeSort(a,0,n-1)
for i 0 to n-2 do
if (a[i]=a[i+1])
return 1
return -1
if j>frequency
frequency j
modevalue a[i]
i i+j
return modevalue
23. a) Write a program using dynamic programming to find the sum of
contiguous subarray within a one-dimensional array of numbers which
has the largest sum.
b) Algorithm Sort(A[0..n − 1], S[0..n − 1])
//Input: Array A[0..n − 1] of orderable values
for i ← 0 to n − 1 do
Count[i] ← 0
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
if A[i] < A[j]
Count[j] ← Count[j] + 1
else Count[i] ← Count[i] + 1
for i ← 0 to n − 1 do
S[Count[i]] ← A[i]
return c[n][k]
Greedy Technique
25. a) Write a program to find shortest paths to other vertices using Dijkstra's
algorithm from a given vertex in a weighted connected graph.
b) Algorithm Binomial (n, k)
//Computes C(n, k) by the dynamic programming algorithm
/ /with a one-dimensional table
//Input: A pair of nonnegative integers n ≥ k ≥ 0
for i ← 0 to n do
if i ≤ k //in the triangular part
T[i] ← 1 //the diagonal element
u ← i − 1 //the rightmost element to be computed
else u ← k //in the rectangular part
//overwrite the preceding row moving right to left
for j ← u downto 1 do
T[j] ← T[j − 1] + T[j]
return T[k]
For i 0 to n-1 do
D[a[i]-lb] d[a[i]-lb]+1
For i 1 to ub-lb do
d[i] d[i]+d[i-1]
for i n-1 to 0 do
j a[i]-lb
b[d[j]-1] a[i]
d[j] d[j] -1
return b
27. a) Write a program find Minimum Cost Spanning Tree of a given undirected
graph using Prim’s algorithm.
b) Algorithm GaussJordan(A[1..n, 1..n], b[1..n])
//Applies Gaussian-Jordan elimination to matrix A of a system’s
//coefficients, augmented with vector b of the system’s right-hand sides
//Input: Matrix A[1..n, 1, ..n] and column-vector b[1..n]
for i ← 1 to n do A[i, n + 1] ← b[i] //augment the matrix
for i ← 1 to n do
for j ← 1 to n do
if j ≠ i
temp ← A[j, i] / A[i, i] //assumes A[i, i] ≠ 0
for k ← i to n + 1 do
A[j, k] ← A[j, k] − A[i, k] * temp
28. a) Using Greedy technique, develop an efficient program for finding the
length of a longest path in a directed acyclic graph.
29. a) Suppose you have 6 containers whose weights are 50,10,30,20,60,5 and a
ship whose capacity is 100.Find an optimal solution to this instance of the
container loading problem.
b) Design a one-line algorithm for sorting any array of size n whose values
are n distinct integers from 1 to n.And calculate the efficiency.
Backtracking
32. a) Implement all pair shortest paths problem using Floyd’s algorithm.
b) Algorithm WorldSeries(n, p)
//Computes the odds of winning a series of n games
//Input: A number of wins n needed to win the series
// and probability p of one particular team winning a game
//Output: The probability of this team winning the series
q←1−p
for j ← 1 to n do
P[0, j] ← 1.0
for i ← 1 to n do
P[i, 0] ← 0.0
for j ← 1 to n do
P[i, j] ← p * P[i − 1, j] + q * P[i, j − 1]
return P[n, n]
33. a) Compute the transitive closure of a given directed graph using Warshall’s
algorithm.
b) Algorithm DagLongestPath(G)
//Finds the length of a longest path in a dag
//Input: A weighted dag G = (V,E)
//Output: The length of its longest path dmax
topologically sort the vertices of G
for every vertex v do
dv ← 0 //the length of the longest path to v seen so far
for every vertex v taken in topological order do
for every vertex u adjacent to v do
if dv + w(v, u) > du
du ← dv + w(v, u)
dmax ← 0
for every vertex v do
if dv > dmax
dmax ← dv
return dmax
General problems
34. a) Given integers a1,a2….an,find the value of maximum subsequence sum.( Ex:
For input -2,11,-4,13,-5,-2 the answer is 20 (a2 to a4)).Write more than one
approach for the following problem and compare its time efficiency.
b) Algorithm DagShortestPaths(G, s)
//Solves the single-source shortest paths problem for a dag
//Input: A weighted dag G = (V,E) and its vertex s
//Output: The length dv of a shortest path from s to v and
// its penultimate vertex pv for every vertex v in V
topologically sort the vertices of G
for every vertex v do
dv ←∞; pv ← null
ds ← 0
for every vertex v taken in topological order do
for every vertex u adjacent to v do
if dv + w(v, u) < du
du ← dv + w(v, u); pu ← v