Sie sind auf Seite 1von 17

Design and Analysis of Algorithms Team Activity

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:

a) What does the algorithm compute?

b) What is its basic operation?

c) Set up a summation/reccurence relation for the algorithms basic operation


count and solve it

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

1. a) Write a program to sort a given set of elements using bubble sort


technique and also propose an alternative solution for bubble sort if the
items in the array are already sorted, and compare with the existing
algorithm.

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]

if modecount >1 return modevalue

return 0

2. a) Write a program to Sort a given set of elements using Selection


sort,Compare the worst case running time of selection sort with bubble
sort technique.

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

3. a) Write a program to find the optimal solution to the Knapsack instance


using brute-force strategy.
b) Algorithm Sort(a,n)
//Input:An array a[0…n-1]
for j1 to n-1 do
flag  0

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

Divide and conquer

5. a) Implement Recursive Binary search and Linear search and determine


the time required to search an element. Which is the most efficient
searching technique as for as worst case running times are concerned,
illustrate the above with appropriate examples.
b) Algorithm Evaluation(P[0..n], x)
//Input: Array P[0..n] of the coefficients of a polynomial of degree n,
// from the lowest to the highest, and a number x
p ← P[0]; power ← 1
for i ← 1 to n do
power ← power * x
p ← p + P[i] * power
return p

6. a) Write a program to sort a given set of elements using Quick sort


technique, Compare the efficiency of quick sort in worst, best and average
case.
b) Algorithm compute(low,high,a)
//Input: The array a[0..n-1],low and high represents the low and high
index in the array
if(low=high) return a[low]
mid  (low+high)/2
return compute(low,mid,a)+compute(mid+1,high,a)

7. a)Write a program to sort a given set of elements using Merge sort


method, Compare the efficiency of merge sort in worst, best and average
case.
b) Algorithm Numbers(P[l..r])
//A divide-and-conquer alg. for the one-dimensional closest-pair
problem
//Input: A subarray P[l..r] (l ≤ r) of a given array P[0..n − 1]
// of real numbers sorted in nondecreasing order
if r = l return ∞
else if r − l = 1 return P[r] − P[l]
else return min{Numbers(P[l..(l + r)/2]),
Numbers(P[((l + r)/2 + 1..r]),
P[(l + r)/2 + 1] − P[(l + r)/2]}

8. a)Devise a ternary search program that first tests the element at


position n/3 for equality with some value x, and then checks the element at
2n/3 and either discovers x or reduces the set size to one-third the size of
the original. Compare this with binary search for small and large values of
n.
b) Algorithm PostOffice1 (P)
//Input: List P of n (n ≥ 2) points x1, x2, ..., xn in increasing order
m ← (x1 + xn )/2
i←1
while xi <m do
i←i+1
if xi − x1 < xn − xi−1
return xi
else return xi−1

9. a) Find a subset of a given set S = {sl,s2,.....,sn} of n positive integers whose


sum is equal to a given positive integer d. For example, if S= {1, 2, 5, 6, 8}
and d = 9 there are two solutions{1,2,6}and{1,8}.A suitable message is to
be displayed if the given problem instance doesn't have a solution.
b) Algorithm TriSort(Low,High)
//Input: Low,High,initially Low=0 and High=n-1
if High<=Low then return endif
Third Low+(High-Low+1)/3
TwoThirds Low +2((High-Low+1)/3)
TriSort(Low,Third)
TriSort(Third+1,TwoThirds)
TriSort(TwoThirds+1,High)
Merge(Low,Third,TwoThirds)
Merge(Low,TwoThirds,High)
Decrease and conquer

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]

15. a) Write a program using decrease-by-one technique for generating the


power set of a set of n elements.
b) Algorithm matrix(A[0..n − 1, 0..n − 1])
//Input: A matrix A[0..n − 1, 0..n − 1] of real numbers
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
if A[i, j] ≠A[j, i]
return false
return true
16. a) Write a program to obtain the topological ordering of vertices in a given
diagraph.

b) Algorithm Distance(A[0..n − 1])


//Input: Array A[0..n − 1] of numbers
dmin ←∞
for i ← 0 to n − 1 do
for j ← 0 to n − 1 do
if i ≠ j and |A[i] − A[j]| < dmin
dmin ← |A[i] − A[j]|
return dmin

17. a) A Celebrity among a group of n people is a person who knows


nobody but is known by everybody else.The task is to identify a
celebrity by only asking questions to people of the form:”Do you know
him/her?”Design an efficient decrease and conquer algorithm to identify a
celebrity or determine that the group has no such person.

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

20. a) Write a program to implement Horspool algorithm for String Matching.

b) Algorithm GaussSub(A[1..n, 1..n + 1])


//Implements the backward substitution stage of Gaussian elimination
//by solving a given system with an upper-triangular coefficient matrix
//Input: Matrix A[1..n, 1, ..n + 1], with the first n columns in the upper-
//triangular form
for i ← n downto 1 do
temp ← 0.0
for j ← n downto i + 1
temp ← temp + A[i, j] * x[j]
x[i] ← (A[i, n + 1] − temp)/A[i, i]
return x

Dynamic Programming

21.a) Write a program to Implement 0/1 Knapsack problem using dynamic


programming. Compare the efiiciency with brute force strategy of
knapsack problem.

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

22. a)Write a program to find the Binomial Co-efficient using Dynamic


Programming.
b) Algorithm mode(a,n)
//Input:An array a[0..n-1]
MergeSort(a,0,n-1)
i 0
frequency  0
while i<n
j 1
while i+j <n and a[i]=a[i+j] do
j j+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]

24. a) Design a dynamic program for the change-making problem: given an


amount n and unlimited quantities of coins of each of the denominations
d1, d2, ..., dm, find the smallest number of coins that add up to n or
indicate that the problem does not have a solution.
b) Algorithm BC(n,k)
//Input:non negative integers n>=k>=0
for i 0 to n do
for j 0 to min(i,k) do
if(j=0 or i=j)
c[i][j] 1
else
c[i][j] c[i-1][j-1]+c[i-1][j]

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]

26. a) Write a program to find Minimum Cost Spanning Tree of a given


undirected graph using Kruskal's algorithm.
b) Algorithm DistributionCounting sort(a,n,b)
//Input: a[[0..n-1],n is the no of items to sort,b-contains the sorted list
For i  0 to ub-lb // ub-upper bound,lb-lower bound in array
D[i]0

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.

b) The median of a list of n numbers is defined as its n/2(ciel of n/2)


smallest element. (The median is larger than one half the elements and
is smaller than the other half.) Design a presorting-based algorithmfor
finding the median and determine its efficiency class.

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.

30. a)Write a program for Hamiltonian circuit problem.Compare the efficiency


with travelling salesman problem of brute force strategy.
b) Algorithm Change(n,D[1..m])
//Implements the greedy algorithm for the change-making problem
//Input: A nonnegative integer amount n and
// a decreasing array of coin denominations D
for i ← 1 to m do
C[i] ← n/D[i] //it is ceil of n/D[i]
n ← nmodD[i]
if n = 0 return C
else return ”no solution”

Backtracking

31. a) Implement N Queen's problem using Back Tracking.


b) Algorithm BinomCoeff (n, k)
if k = 0 or k = n return 1
else return BinomCoeff (n − 1, k − 1)+BinomCoeff (n − 1, k)

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

Das könnte Ihnen auch gefallen