Aoa Experiments
Aoa Experiments
Lab Manual
Second Year Semester-IV
Sub: Analysis of Algorithm
Even Semester
1
List of Experiments
Sr. No. Experiments Name
1 Implementation of Selection Sort.
2
Course Objective, Course Outcome &
Experiment Plan
Course Objective:
Course Outcomes:
CO1 Ability to select appropriate problem solving strategies and calculate time
complexity and space complexity of an algorithm.
CO5 Ability to analyze different backtracking problems and branch & bound problems.
3
Study and Evaluation Scheme
Course
Course Name Teaching Scheme Credits Assigned
Code
Term Work:
1. Term work assessment must be based on the overall performance of the student with
every experiment graded from time to time. The grades should be converted into
marks as per the Credit and Grading System manual and should be added and
averaged.
2. The final certification and acceptance of term work ensures satisfactory performance
of laboratory work and minimum passing marks in term work.
Practical/Experiments:
1.
2.
4
The final certification and acceptance of term work ensures that satisfactory performance of l
aboratory work and minimum passing marks in term work.
5
Analysis of Algorithm
Experiment No. : 1
Selection Sort
6
Experiment No. 1
1. Aim: To implement Selection Sort.
2. Objectives:
Able to calculate time complexity and space complexity.
To teach problem solving strategies.
3. Outcomes:
Student will be able to identify appropriate method for specified problem
definition
To engage in continuing professional development.
An ability to match the industry requirements in the domain of Programming with
the required skills.
5. Theory:
This sorting is called "Selection Sort" because it works by repeatedly element. It
works as follows: first find the smallest in the array and exchange it with the element
in the first position, then find the second smallest element and exchange it with the
element in the second position, and continue in this way until the entire array is
sorted.
Selection sort is among the simplest of sorting techniques and it work very
well for small files. Furthermore, despite its evident "naïve approach "Selection sort
has a quite important application because each item is actually moved at most once,
Section sort is a method of choice for sorting files with very large objects (records)
and small keys
The worst case occurs if the array is already sorted in descending order.
Nonetheless, the time require by selection sort algorithm is not very sensitive to the
original order of the array to be sorted: the test "if A[j] < min x" is executed exactly
the same number of times in every case. The variation in time is only due to the
number of times the "then" part (i.e., min j ← j; min x ← A[j] of this test are
executed.
The Selection sort spends most of its time trying to find the minimum element
in the "unsorted" part of the array. It clearly shows the similarity between Selection
sort and Bubble sort. Bubble sort "selects" the maximum remaining elements at each
stage, but wastes some effort imparting some order to "unsorted" part of the
array. Selection sort is quadratic in both the worst and the average case, and
requires no extra memory.
7
6. Algorithm
1. Start.
2. Define an array ‘A’ of size ‘n’.
3. Read array elements from the user.
4. Define first element of the array as ‘min’ i.e. min=A[0].
5. For i0 to n-1
5.1 minA[i]
5.2 loci //loc variable keeps the track of minimum element
5.3 for ji+1 to n-1
5.3.1 if (A[j]<min) then
5.3.1.1 minA[j]
5.3.1.2 locj
5.4 if (loc!=i) then
5.5 swap( A[i] , A[loc])
6. Display sorted array.
7. Stop.
7. Conclusion and Discussion: In selection sort the first pass makes (n-1) comparisons,
the second pass makes (n-2) and so on. Therefore, time complexity becomes O (n2).
8
Analysis of Algorithm
Experiment No. : 2
9
Experiment No. 2
1. Aim: Write a program to implement binary search technique.
2. Objectives:
To teach mathematical background for algorithm analysis and implementation of
divide and conquer strategies.
Able to calculate time complexity and space complexity.
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
To engage in life long learning
5. Theory:
A binary search algorithm is a technique for finding a particular value in a linear
array, by ruling out half of the data at each step, widely but not exclusively used in
computer science.
A binary search finds the median, makes a comparison to determine whether
the desired value comes before or after it, and then searches the remaining half in the
same manner. Another explanation would be: Search a sorted array by repeatedly
dividing the search interval in half Begin with an interval covering the whole array. If
the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half Otherwise, narrow it to the upper half.
6. Algorithm
Sort an array a[j] in increasing order.
1. Read ‘n’ elements in the array , say ‘a ’.
2. Read the element to be searched, say ‘KEY ‘.
3. low = 0 , high = n-1
4. while(high>=low) perform following steps.
Find the middle element.
mid = (low + high )/ 2
5. if ( KEY= a[mid] )
print (‘ element found at mid position)
6. if KEY < a[mid] then search for target in a[low] to a[mid-l]
else search for target in a[mid+l] to a[high].
7. if (KEY < a[Mid_position])
high = mid_position - 1;
else
low = mid_position + 1;
stop.
10
7. Conclusion and Discussion:
The method is called is binary search because at each step, we reduce the length of
the table to be searched by half and the table is divided into two equal parts. Binary
Search can be accomplished in logarithmic time in the worst case, i.e., T(n) = θ(log
n). This version of the binary search takes logarithmic time in the best case.
11
Analysis of Algorithm
Experiment No. : 3
Merge Sort
12
Experiment No. 3
1. Aim: Write a program to implement Merge Sort.
2. Objectives:
To teach mathematical background for algorithm analysis and implementation of
divide and conquer strategies.
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
To engage in life long learning
5. Theory:
Mergesort algorithms are based on a divide and conquer strategy. First, the
sequence to be sorted is decomposed into two halves (Divide). Each half is
sorted independently (Conquer). Then the two sorted halves are merged to a
sorted sequence (Combine).
Its worst-case running time has a lower order of growth than insertion sort.
Since we are dealing with subproblems, we state each subproblem as sorting a
subarray A[p .. r]. Initially, p = 1 and r = n, but these values change as we
recurse through subproblems.
To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted.
Otherwise, split A[p .. r] into two subarrays A[p.. q] and A[q + 1 .. r], each
containing about half of the elements of A[p .. r]. That is, q is the halfway
point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted
subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this
step, we will define a procedure MERGE (A, p, q, r).
13
Example
6. Algorithm
Algorithm MergeSort ( int A[0....n-1,low,high) :
if (low<high) then
{
mid ← (low+high)/2
MergeSort(A,low,mid)
MergeSort(A,low,mid)
Combine(A,low,mid,high)
}
14
4. while(i<= mid && j<=high) do
{
if(A[i]<=A[j] then
{
temp[k] ← A[i]
i←i+1
k←k+1
}
else
{
temp[k] ← A[j]
j←j+1
k←k+1
}
}
5. while(i<=mid) do
{
temp[k] ← A[i]
i←i+1
k←k+1
}
6. while(j<=high) do
{
temp[k] ← A[j]
j←j+1
k←k+1
}
15
Analysis of Algorithm
Experiment No. : 4
Quick Sort
16
Experiment No. 4
1. Aim: Write a program to implement Quick Sort.
2. Objectives:
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
To engage in life long learning
5. Theory:
Quicksort is a very efficient sorting algorithm invented by C.A.R. Hoare.
It has two phases:
• the partition phase and
• the sort phase.
As we will see, most of the work is done in the partition phase - it works out where to
divide the work. The sort phase simply sorts the two smaller problems that are generated
in the partition phase.
In quicksort, we divide the array of items to be sorted into two partitions and then call
the quicksort procedure recursively to sort the two partitions, ie we divide the problem
into two smaller ones and conquer by solving the smaller ones.
For the strategy to be effective, the partition phase must ensure that all the items in one
part (the lower part) and less than all those in the other (upper) part.
To do this, we choose a pivot element and arrange that all the items in the lower part are
less than the pivot and all those in the upper part greater than it. In the most general case,
we don’t know anything about the items to be sorted, so that any choice of the pivot
element will do - the first element is a convenient one.
17
6. Algorithm
Algorithm Quicksort( A[0……n-1, low, high) :
if( low<high) then
m ← partition(A[low………high])
Quicksort( A[low……m-1])
Quicksort( A[mid+1……high])
AlgorithmPartition(A[low……high]):
pivot ← A[low]
i ← low
j ← high +1
while ( i<=j) do
{
while(A[i]<=pivot)do
i ← i+1
while(A[j]<=pivot)do
j ← j+1
if(i<=j)then
swap(A[i],A[j])
}
swap(A[low],A[j])
return j
7. Conclusion and Discussion: Thus, in quick sort choice of pivot affects the nature of
partitioning. Best case when the pivot divides array into equal halves: both partitions
have same nearly number of elements. Number of levels of partitioning - logN
Complexity: (N log N). In Worst case, when the pivot is the largest/smallest element
of the array, one partition is empty and the other has all the elements. Number of
levels of partitioning N. Complexity degrades to selection sort - 0(N2). Suitable for
large and randomly sorted data.
8. Viva Questions:
9. References:
http://interactivepython.org/runestone/static/pythonds/SortSearch/TheQuickSort.ht
ml
http://www.algolist.net/Algorithms/Sorting/Quicksort
18
https://www.khanacademy.org/computing/computer-science/algorithms/quick-
sort/a/overview-of-quicksort
Analysis of Algorithm
Experiment No. : 5
Tennis Tournament
19
Experiment No. 5
1. Aim: Write a program for constructing tennis tournament using divide and conquer
strategy.
2. Objectives:
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
To engage in life long learning
5. Theory:
The divide and conquer strategy can be applied to schedule the tennis tournaments.
The round robin tennis tournament problem can be stated as follows –
The tournament schedule is represented by n row by n-1 column table. The row i and
column j can be interpreted as “ player I must play with which player on jth day “.
Here, we have to find schedule for half of the players and this can be continued till
we get the two players.
6. Algorithm
I. Read ‘n’ number of players from the user.
II. Create a matrix of ‘n’ row by ‘n-1’ column.
III. Divide matrix into four parts.
IV. The upper left corner is obtained from the smallest schedule recursively.
V. The bottom left corner is obtained by adding n/2 to each player code of top
left corner.
VI. The top right corner is by pitting the high order players against low order
players, by cyclically permuting the orders.
VII. The bottom right corner is similarly obtained by pitting the low order
players against high order players, by cyclically permuting the orders.
VIII. Stop.
20
7. Conclusion and Discussion: Thus program for constructing tennis tournament using
divide and conquer strategy is implemented successfully.
8. Viva Questions:
Which strategy tennis tournament uses to generate a schedule?
Create a schedule for 8 players?
9. References:
http://orion.lcg.ufrj.br/Dr.Dobbs/books/book9/mf1210.htm
21
Analysis of Algorithm
Experiment No. : 6
Knapsack Problem
22
Experiment No. 6
1. Objectives:
To teach mathematical background for algorithm analysis and implementation
greedy strategies.
Able to calculate time complexity and space complexity.
To improve the logical ability
2. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
An ability to use techniques, skills for computing practice.
4. Theory:
Problem Definition :
We are given a empty knapsack of capacity ‘W’ and we are given ‘n’
different objects from i=1,2………n. Each object ‘i’ has some positive weight ‘wi’
and some profit value is associated with each object which is denoted as ‘pi’.
We want to fill the knapsack with total capacity ‘W’ such that profit earned
is maximum. When we solve this problem main goal is :
1. Choose only those objects that give maximum profit.
2. The total weight of selected objects should be <= W
Most problem have n input and require us to obtain a subset that satisfies some
constraints is called as a feasible solution. We are required to find a feasible solution
that optimize (minimize or maximizes ) a given objective function. The feasible
solution that does this is called an optimal solution.
Example :
n=3
profit ->Pi = (25,24,15)
Weight (in kg) -> Wi = (18,15,10) where i = 1,2,3
and knapsack capacity = 20
Feasible solutions :
1. As kanpsack capacity is 20 , we first put W1( i.e 18 kg) into kanpsack , hence
gained profit is 25 ; but now we can put only 2 kg , in knapsack ,so we will put 2 kgs
from W2 , hence profit from this 2kg of w2 is 3.2.
23
Thus , total profit is 25+ 3.2 = 28.2
2. Now , we will put W3 (i.e 10 KG) into knapsack , hence gained profit is 15 , but
we can put only 10 kg in knapsack , so we will put 10 kg form w2 , hence profit from
this 10 kg of w2 is 16.
Thus, total profit is 15+ 16 = 31
3. Now , we will put W2 (i.e 15 KG) into knapsack , hence gained profit is 24 , but
we can put only 5 kg in knapsack , so we will put 5 kg form w3 , hence profit from
this 5 kg of w3 is 7.5.
Thus , total profit is 24+ 7.5 = 31.5
Hence , optimal solution for given problem is 3rd solution, which is giving
total profit as 31.5.
The knapsack problems are often solved using dynamic programming, though no
polynomial-time algorithm is known. Both the knapsack problem(s) and the subset
sum problem are NP-hard.
5. Algorithm
1. Let ‘W’ be the maximum weight of the knapsack
2. Let ‘wi’ and ‘pi’ be the weight and profit of individual items i.e. for
i=1……n
3. Calculate pi / wi ratio and arrange that in decreasing order.
4. initially weight=0 and profit = 0
5. for i=1 to n
{
add item in knapsack till weight <= W
i. profit = profit + pi
}
6. Stop
6. Conclusion and Discussion: Most problems have n inputs and require us to obtain
a subset that satisfies some constraints is called as feasible solution. In knapsack
problem, greedy strategy, we find out feasible solution that optimize (minimizes or
maximizes) a given objective function. A, feasible solution that does this is called
an optimal solution. Hence, in knapsack problem we found out an optimal solution.
The knapsack algorithm takes θ(nw) times, broken up as follows: θ(nw) times to fill
the c-table, which has (n +1).(w+1) entries, each requiring θ(1) time to
compute. O(n) time to trace the solution, because the tracing process starts in
row n of the table and moves up 1 row at each step.
7. Viva Questions:
Type of knapsack problem?
Which strategy provides optimal solution Greedy or Dynamic programming
8. References:
24
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Dynamic/k
napsackdyn.htm
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Greedy/kn
apscakFrac.htm
http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-
problem/
https://home.cse.ust.hk/~dekai/271/notes/L14/L14.pdf
25
Analysis of Algorithm
Experiment No. : 7A
Kruskal’s Algorithm
26
Experiment No. 7A
1. Aim: Write a program to implement Kruskal’s Algorithm
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
greedy strategies.
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
An ability to use techniques, skills for computing practice.
5. Theory:
Graphs : A graph is a set of vertices and edges which connect them. We write:
G = (V,E)
where V is the set of vertices and the set of edges,
E = { (vi,vj) }
where vi and vj are in V.
Paths : A path, p, of length, k, through a graph is a sequence of connected vertices:
p = <v0,v1,...,vk>
where, for all i in (0,k-1):
(vi,vi+1) is in E.
Cycles : A graph contains no cycles if there is no path of non-zero length through the
graph, p = <v0,v1,...,vk> such that v0 = vk.
Spanning Trees : A spanning tree of a graph, G, is a set of |V|-1 edges that connect all
vertices of the graph.
Minimum Spanning Tree :
Given a connected, undirected graph, a spanning tree of that graph is a subgraph
which is a tree and connects all the vertices together. A single graph can have many
different spanning trees. We can also assign a weight to each edge, which is a
number representing how unfavourable it is, and use this to assign a weight to a
spanning tree by computing the sum of the weights of the edges in that spanning tree.
A minimum spanning tree (MST) or minimum weight spanning tree is then a
spanning tree with weight less than or equal to the weight of every other spanning
tree. More generally, any undirected graph (not necessarily connected) has a
minimum spanning forest, which is a union of minimum spanning trees for its
connected components.
27
In general, it is possible to construct multiple spanning trees for a graph, G. If a cost,
cij, is associated with each edge, eij = (vi,vj), then the minimum spanning tree is the
set of edges, Espan, forming a spanning tree, such that: C = sum( cij | all eij in Espan
) is a minimum.
.
Kruskal's Algorithm :
Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning
tree for a connected weighted graph. This means it finds a subset of the edges that
forms a tree that includes every vertex, where the total weight of all the edges in the
tree is minimized. If the graph is not connected, then it finds a minimum spanning
forest (a minimum spanning tree for each connected component). Kruskal's algorithm
is an example of a greedy algorithm.
This algorithm is continually to select the edges in the order of smallest weights and
accepts an edge if it does not form a cycle. Initially the forest consists of n single
node trees (and no edges). At each step, we add one (the cheapest one) edge so that it
joins two trees together. If it were to form a cycle, it would simply link two nodes
that were already part of a single connected tree, so that this edge would not be
needed.
6. Algorithm
I. Let ‘G’ be a connected weighted graph having ‘v’ vertices and ‘e’ edges.
II. Let ‘x’ be a set of all edges of ‘G’ arranged in increasing order of weights.
III. Let ‘T’ be the minimum spanning tree which is initially empty.
IV. while( T contains lesser than v-1 edges AND x is not empty)
{
Let ‘w’ be the next edge of set ‘x’. i.e. an edge of lowest cost in ‘x’.
Remove ‘w’ from ‘x’.
if ( ‘w’ does not create a cycle in ‘T’)
Add ‘w’ to ‘T’
else
Discard ‘w’
}
V. Stop
8. Viva Questions:
State the number of edges in a minimum spanning tree of network with 10
vertices.
State the number of edges in a minimum spanning tree of network with n
vertices.
State the complexity of kruskal’s algorithm?
9. References:
28
http://interactivepython.org/runestone/static/pythonds/Graphs/PrimsSpanningTre
eAlgorithm.html
https://www.cse.ust.hk/~dekai/271/notes/L07/L07.pdf
http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-
spanning-tree-mst-2/
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAl
gor/kruskalAlgor.htm
http://scanftree.com/Data_Structure/kruskal's-algorithm
29
Analysis of Algorithm
Experiment No. : 7B
Prim’s Algorithm
30
Experiment No. 7B
1. Aim: Write a program to implement Prim’s Algorithm.
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
greedy strategies.
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
An ability to use techniques, skills for computing practice.
5. Theory:
Prim's algorithm is an algorithm in graph theory that finds a minimum spanning
tree for a connected weighted graph. This means it finds a subset of the edges that
forms a tree that includes every vertex, where the total weight of all the edges in the
tree is minimized.
The algorithm continuously increases the size of a tree starting with a single
vertex until it spans all the vertices.
• Input: A connected weighted graph with vertices V and edges E.
• Initialize: Vnew = {x}, where x is an arbitrary node (starting point) from V,
Enew = {}
• Repeat until Vnew = V:
Choose edge (u,v) from E with minimal weight such that u is in Vnew and v is
not (if there are multiple edges with the same weight, choose arbitrarily)
oAdd v to Vnew, add (u, v) to Enew
• Output: Vnew and Enew describe a minimal spanning tree
.
6. Algorithm
1. Let ‘adj’ be the adjacency matrix of graph ‘G’ having ‘v’ vertices numbered from
1 to v and having ‘e’ edges.
2. Let distance, path and visited be arrays of ‘v’ elements each.
3. Array ‘distance’ is initialized to ∞, while ‘path’ array and ‘visited’ array is
initialized to 0.
4. Let current = 1.
5. Let number of vertices already added to the trace be given as nv and let nv=1.
31
6. Repeat steps 7,8 and 9 while nv ≠ v.
7. for i= 1 to v
if ( adj[current][i] ≠ 0)
if ( visited[i] ≠ 1)
if ( distance[i] > adj[current][i])
{
distance[i] = adj[current][i]
path[i]=current
}
8. min = ∞ (in program min=32767)
for i= 1 to v
if ( visited[i] ≠ 1)
if ( distance[i] < min)
{
min = distance[i]
current = i
}
9. visited[current]=1
10. nv = nv+1
11. Let c be the minimum cost, initially c=0
12. for i=2 to v
c = c+distance[i]
13. for i = 2 to v
Display vertex i is connected to vertex path[i].
14. Stop
8. Viva Questions:
State the complexity of prim’s algorithm?
Does more than one solution exists for the minimum spanning tree?
9. References:
http://interactivepython.org/runestone/static/pythonds/Graphs/PrimsSpanningTre
eAlgorithm.html
https://www.cse.ust.hk/~dekai/271/notes/L07/L07.pdf
http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-
spanning-tree-mst-2/
32
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAl
gor/kruskalAlgor.htm
http://scanftree.com/Data_Structure/kruskal's-algorithm
Analysis of Algorithm
Experiment No. : 8
Dijikstra’s Algorithm
33
Experiment No. 8
1. Aim: Write a program to find single source shortest path using Dijkstra’s Algorithm
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
greedy programming.
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
An ability to use techniques, skills for computing practice.
5. Theory:
Dijkstra's shortest path algorithm, a greedy algorithm that efficiently finds shortest
paths in a graph. Dijkstra's algorithm solves the single-source shortest-path problem
when all edges have non-negative weights. It is a greedy algorithm and similar to
Prim's algorithm. Algorithm starts at the source vertex, s, it grows a tree, T, that
ultimately spans all vertices reachable from S. Vertices are added to T in order of
distance i.e., first S, then the vertex closest to S, then the next closest, and so on.
Following implementation assumes that graph G is represented by adjacency lists.
6. Algorithm
1. Let ‘adj’ be the adjacency matrix of graph ‘G’ having ‘v’ vertices numbered
from 1 to v and having ‘e’ edges.
2. Let distance, path and visited be arrays of ‘v’ elements each.
3. Array ‘distance’ is initialized to ∞, while ‘path’ array and ‘visited’ array is
initialized to 0.
4. Let current = source vertex.
5. visited[current]=1
6. T = 0
7. Let number of vertices already added to the trace be given as nv and let nv=1.
8. Repeat steps 9 to 13 while nv ≠ v.
9. for i= 1 to v
if ( adj[current][i] ≠ 0)
if ( visited[i] ≠ 1)
if ( distance[i] > adj[current][i])
{
distance[i] = adj[current][i] + T
34
path[i]=current
}
10. min = ∞ (in program min=32767)
for i= 1 to v
if ( visited[i] ≠ 1)
if ( distance[i] < min)
{
min = distance[i]
current = i
}
11. visited[current]=1
12. nv = nv+1
13. T = distance[current]
14. Let dest be the destination vertex
Shortest distance from the sorce vertex to the destination vertex =
distance[dest]
y = dest
do
{
x = path[y]
Display vertex i is connected to vertex x.
}
15. while y ≠ sorce vertex repeat steps 2 to 15 for each vertex as the sorce vertex.
16. Stop.
8. Viva Questions:
Does dijikstra’s is capable of handling negative weight edges?
3. which strategy dijikstra’s make use of, to find the shortest path?
4. Given a directed weighted graph. You are also given the shortest path from a
source vertex ‘s’ to a destination vertex ‘t’. If weight of every edge is increased
by 10 units, does the shortest path remain same in the modified graph?
9. References:
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgo
r/dijkstraAlgor.htm
35
http://math.mit.edu/~rothvoss/18.304.3PM/Presentations/1-Melissa.pdf
http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-
algorithm/
36
Analysis of Algorithm
Experiment No. : 9
37
Experiment No. 9
1. Aim: Write a program to implement Optimal Binary Search Tree Algorithm
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
dynamic programming problem
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and
validate the solution for software.
An ability to analyze the local and global impact of dynamic programming in
real world.
5. Theory:
A binary search tree is a tree where the key values are stored in the internal nodes,
the external nodes (leaves) are null nodes, and the keys are ordered lexicographically.
I.e. for each internal node all the keys in the left subtree are less than the keys in the
node, and all the keys in the right subtree are greater.
When we know the probabilities of searching each one of the keys, it is quite easy to
compute the expected cost of accessing the tree. An OBST is a BST which has
minimal expected cost.
Problem Description :
Let {a1, a2,…an} be a set of identifiers such that a1<a2<a3. Let p(i) be the
probability with which we can search for ai and q(i) be the probability of searching
element x such that ai<x<ai+1 and
0 < i < n. In other words p(i) is the probability of successful search and q(i) be the
probability of unsuccessful search. Also ∑1< i < n p(i) + ∑1< i < n q(i) then obtain
a tree with minimum cost. Such a tree with optimum cost is called as optimal binary
search tree.
Let, Tij = OBST(ai+1,….,aj), Cij denotes the cost(Tij), Wij is the weight of each Tij,
T0n is the final tree obtained, T00 is empty, Ti,i+1 is a single-node tree that has
element ai+1.
6. Algorithm
1. Algorithm Wt(p,q,W,n)
38
//p is an input array [1…n]
//q is an input array [0…n]
//W[i,j] will be output for this function such that W[0..n,0..n]
{
for i1 to n do
W[i,j] q[i]; //initialize
for m1 to n do
{
for i0 to n-m do
{
k i+m;
W[i,k] W[i,k-1] +p[k] +q[k];
}
}
write(W[0:n]); //print the values of W
} // For computing Cij ,rij following algorithm is used
2. Algorithm OBST(p,q,W,C,r)
{
// computation of first two rows
for i =0 to n do
{
C[i,i]0;
r[i,i]0;
C[i,i+1] q[i]+q[i+1]+p[i+1];
r[i,i+1] i+1;
}
for m2 to n do
{
for 0 to n-m do
{
j i+m;
k Min_Value(C,r,i,j); // call for algorithm Min_Value
//minimum value of Cij is obtained for deciding value of k
C[i,j]W[i,j] + C[i,k-1] + C[k,j];
r[i,j]k;
}
}
write(C[0:n],r[0:n]); //print values of Cij and rij
}
3. Algorithm Min_Value(C,r,i,j)
{
minimum ∞; // finding the range of k
for (mr[i,j-1] to r[i+1,j]) do
{
39
if(C[i,m-1] + C[m,j]) < minimum then
{
minimum C[i, m-1] + C[m,j];
km;
}
return k; // that k gives minimum value of C
}
}// Following algorithm is used is used for creating the tree Tij
4. Algorithm build_tree(r,a,i,j)
{
T new(node); // allocate memory for new node
k r[i,j];
T dataa[k];
if(j== i+1)
return;
Tleft =build_tree(r,a,i,k-1);
Tright =build_tree(r,a,k,j);
}
7. Conclusion and Discussion: The computation of each C and r can be done using
three nested for loops. Hence the time complexity turns out to be O(n3).
8. Viva Questions:
What is the complexity of OBST using Dynamic programming?
What is the total number of binary search tree possible with n nodes
9. References:
http://www.radford.edu/~nokie/classes/360/dp-opt-bst.html
http://software.ucv.ro/~cmihaescu/ro/laboratoare/SDA/docs/arboriOptimali_e
n.pdf
http://www.geeksforgeeks.org/dynamic-programming-set-24-optimal-binary-
search-tree/
40
Analysis of Algorithm
Experiment No. : 10
41
Experiment No. 10
1. Aim: Write a program to implement the Travelling Salesman Problem (TSP).
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
dynamic programming problem
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and
validate the solution for software.
An ability to analyze the local and global impact of dynamic programming in
real world.
5. Theory:
Definition
Given a set of cities and the distance between each possible pair, the Travelling
Salesman Problem is to find the best possible way of ‘visiting all the cities exactly
once and returning to the starting point’.
The (original) problem can be formulated as:"If there are n cities a salesman must
visit, and the distance between each pair of these cities is given, find the shortest tour
where each city is visited exactly once and returning to your starting point."
Dynamic Programming
Dynamic programming is typically applied to optimization problems. For each
given problem, we may get any number of solutions we seek for optimum solution
(i.e. minimum value or maximum value solution). And such an optimal solution
becomes the solution to the given problem. It is guaranteed that the dynamic
programming will generate optimal solution using principle of optimality.
Problem Description: Let G be directed graph denoted by (V, E) where V denotes set
of vertices and E denotes set of edges. The edges are given along with their cost Cij.
The cost Cij>0 for all i and j. If there is no edge between i and j then Cij=∞.
A tour for a graph should be such that all the vertices should be visited only once and
cost of the tour is sum of the cost of edges on the tour. The travelling salesperson
problem is to find the tour of minimum cost.
42
Step 1: Let the function C (1, V-{1}) is the total length of the tour terminating
at 1. The objective of TSP problem is that the cost of this tour should be minimum.
Let d [i, j] be the shortest path between two vertices i and j.
Step 2: Let V1, V2… Vn be the sequence of vertices followed in optimal tour.
Then (V1, V2,… Vn) must be a shortest path from V1 to Vn which passes through
each vertex exactly once.
Here the principle of optimality is used. The path Vi, Vi+1,…Vj must be
optimal for all paths beginning at v(i), ending at v(j),and passing through all the
intermediate vertices{V(i+1)..V(j-1)} once.
Step 3: Following formula can be used to obtain the optimum cost tour.
Cost(i,S) = min{d[i,j] + Cost(j,S-{j})} where jЄS and iЄS
6. Algorithm
7. Conclusion and Discussion: Given n is the number of cities to be visited, the total
number of possible routes covering all cities can be given as a set of feasible
solutions of the TSP and is given as (n-1)!/2.
8. Viva Questions:
What are the real world application of TSP
Is the traveling salesman problem NP complete?
9. References:
http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/CG-
Applets/TSP/notspcli.htm
http://www.csd.uoc.gr/~hy583/papers/ch11.pdf
http://www.geeksforgeeks.org/travelling-salesman-problem-set-1/
43
Analysis of Algorithm
Experiment No. : 11
8 Queen’s Problem
44
Experiment No. 11
1. Aim: Write a program to implement 8 Queen’s Problem.
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
backtracking problem.
Able to calculate time complexity and space complexity.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and
validate the solution for software.
An ability to analyze the local and global impact of dynamic programming in
real world.
5. Theory:
The eight queens puzzle is the problem of putting eight chess queens on an 8×8
chessboard such that none of them is able to capture any other using the standard
chess queen's moves. The queens must be placed in such a way that no two queens
would be able to attack each other. Thus, a solution requires that no two queens share
the same row, column, or diagonal. The eight queens puzzle is an example of the
more general n queens puzzle of placing n queens on an n×n chessboard, where
solutions exist only for n = 1 or n ≥ 4.
The N queen's problem solved using the recursive approach, where in the placement
of the queen is decided by every recursive call to the function that decides queen's
placement at every level . The main care has to be taken to place the queen is that:
1. No queen can be placed in the immediate vicinity of the queen at preceding level.
2. No queens should be on same diagonal.
The problem can be quite computationally expensive as there are 4,426,165,368 or
64! / (56!8!)) possible arrangements of eight queens on the board, but only 92
solutions. For example, just by applying a simple rule that constrains each queen to a
single column (or row), it is possible to reduce the number of possibilities to just
16,777,216 (8^8) possible combinations, which is computationally manageable for
n=8, but would be intractable for problems of n=1,000,000. This heuristic solves n
queens for any n, n ≥ 4 or n = 1:
Steps :
1. Divide n by 12. Remember the remainder (n is 8 for the eight queens puzzle).
2. Write a list of the even numbers from 2 to n in order.
45
3. If the remainder is 3 or 9, move 2 to the end of the list.
4. Append the odd numbers from 1 to n in order, but, if the remainder is 8,
switch pairs (i.e. 3, 1, 7, 5, 11, 9, …).
5. If the remainder is 2, switch the places of 1 and 3, then move 5 to the end of
the list.
6. If the remainder is 3 or 9, move 1 and 3 to the end of the list.
7. Place the first-column queen in the row with the first number in the list, place
the second-column queen in the row with the second number in the list, etc.
For n = 8 this results in the solution shown above. A few more examples follow.
• 14 queens (remainder 2): 2, 4, 6, 8, 10, 12, 14, 3, 1, 7, 9, 11, 13, 5.
• 15 queens (remainder 3): 4, 6, 8, 10, 12, 14, 2, 5, 7, 9, 11, 13, 15, 1, 3.
• 20 queens (remainder 8): 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3, 1, 7, 5, 11, 9, 15,
13, 19, 17.
6. Algorithm
Algorithm Queen (n)
// Input : Total number of Queen’s n
for column 1 to n do
{
if(place(row,column)) then
{
board[row]=column // no conflict so place queen
if(row==n) then //dead end
print_board(n) // printing the board configuration
else // try next queen with next position
Queen(row+1,n);
}
}
Algorithm place(row,column)
// This algorithm is for placing the queen at appropriate position
// Input : row and column of the chessboard
for i 1 to row-1 do
{
// checking for column and diadonal conflicts
if ( board[i] = column ) then
return 0;
else if ( abs(board[i] – column) = abs(i-row)) then
return 0;
}
// no conflicts hence Queen can be placed
return 1;
46
8. Viva Questions:
Which strategy N-queen problem uses to find solution?
How many solutions exist for 8-queen problem?
Draw the 4X4 board and place a 4-queens using backtracking step by step.
9. References:
http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/
http://algorithms.tutorialhorizon.com/backtracking-n-queens-problem/
https://developers.google.com/optimization/puzzles/queens
47
Analysis of Algorithm
Experiment No. : 12
48
Experiment No. 12
1. Aim: Write a program to find Longest Common Subsequence from the given two
sequences.
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
string matching algorithm problem.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming
with the required skills.
To understand, identify, analyze and design the problem, implement and
validate the solution for software.
5. Theory:
A Longest Common Subsequence is a common subsequence of minimal length. In
the longest common subsequence problem we are given two sequences X=<x1,
x2…xm> and Y=<y1,y2…yn> and wish to find a maximum-length common
subsequence of x and y.
The optimal substructure of the LCS problem gives the recursive formula,
0 if i=0 or j=0
c[i,j]= c[i-1,j-1]+1 if i,j>0 & xi=yj
max(c [i, j-1], c[i-1,j]) if i,j>0 & xi≠yj
Procedure LCS-LENGTH takes two sequences X=<x1, x2… xm> and Y=<y1, y2…
yn> as inputs.
It stores the c[i,j] values in a table c[0…m,0…n] whose entries are computed in row-
major order i.e. the first row of c is filled in form left to right, then second row and so
on. It also maintains the table b [1…m, 1…n] to simplify construction of an optimal
solution. Initially, b[i,j] points to the table entry corresponding to the optimal sub
problem solution. The procedure returns the b and c tables; c[m,n] contains the length
of an LCS of X and Y.
LCS - Length ( X , Y )
m length ( X )
n length ( Y )
49
for ( i 1 to m ) do
c[i,0] 0
for ( j 1 to n ) do
c[0,j] 0
for ( i 1 to m ) do
{
for ( j 1 to n ) do
{
if ( xi = yi ) then
{
c[i,j] c[i–1,j–1]+1
b[i,j] “ “
}
else if ( c [ i – 1 , j ] >= c [ i , j – 1 ] then
{
c[i,j] c[i–1,j]
b[i,j] “ “
}
else
{
c[i,j] c[i,j-1]
b[i,j] “ “
}
} // end of inner for loop
Constructing an LCS :
PRINT-LCS ( b, x, i, j)
if i = 0 or j = 0
then return
if b [ i , j ] = ” ” then
{
50
PRINT – LCS ( b , x , i – 1 , j - 1)
print xi
}
else if b [ i , j ] = ” ” then
PRINT – LCS ( b , x , i – 1 , j )
else
PRINT – LCS ( b , x , i , j – 1 )
7. Viva Questions:
.
Find the LCS for LCS for input Sequences “ABCDGH” and “AEDFHR”?
Find the LCS for LCS for input Sequences “AGGTAB” and “GXTXAYB”?
Complexity of LCS using Dynamic programming?
Complexity of LCS using naïve recursive approach?
8. References:
https://www.hackerrank.com/challenges/dynamic-programming-classics-the-longest-
common-subsequence
http://wordaligned.org/articles/longest-common-subsequence
http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-
subsequence/
51
Analysis of Algorithm
Experiment No. : 13
15-Puzzle Problem
52
Experiment No. 13
1. Aim: Write a program to implement 15-Puzzle Problem
2. Objectives:
To teach mathematical background for algorithm analysis and implementation
branch and bound problem.
To improve the logical ability
3. Outcomes:
An ability to match the industry requirements in the domain of Programming with
the required skills.
To understand, identify, analyze and design the problem, implement and validate
the solution for software.
To engage in life long learning
5. Theory:
Branch and Bound is a general algorithmic method for finding optimal solutions of
various optimization problems where greedy method & dynamic programming fails. In
branch and bound method a state space tree is built and all the children of E nodes (a live
node whose children are currently being generated) are generated before any other node
can become a live node. For exploring new nodes either BFS or D-search technique can
be used. In Branch and bound technique, BFS-like state space search will be called FIFO
(First In First Out) search. This is because the list of live node is First In First Out list
(queue). On the other hand the D-search like state space search will be called LIFO
search because the list of live node is Last In First Out list (stack).
In this method a space tree of possible solutions is generated. Then partitioning (called as
branching) is done at each node of the tree. We compute lower bound and upper bound at
each node. This computation leads to selection of answer node. Bounding functions are
used to avoid the generation of subtrees that do not contain an answer node.
The 15-puzzle problem is invented by Sam Lyod in 1878. In this puzzle there are 15 tiles
which are numbered from 1 to 15. The objective of this problem is this problem is to
transform the management of tiles from initial arrangement to a goal arrangement. The
initial and goal arrangement is shown by the following figure.
Fig. 11.a
There is always one empty slot in the initial arrangement. The empty slot is denoted by
ES. The legal moves are the moves in which the tiles adjacent to ES are moved to either
53
left, right, up or down. For eg. in Fig.11.a tile 7 can be moved to right , tile 4 can be
moved down or tile 8 can be moved up. A left move is not possible in this case. Each
move creates a new arrangement of tiles. These arrangements are called states of the
puzzle. The initial arrangement is called initial state and the goal arrangement is called
goal state. A state space tree can be drawn for representing various states. In the state
space tree the path from initial state to goal state represents the answer. But the state
space tree 15-puzzle problem is very large because there can be 16 ! Different
arrangements. It is not possible to represent complete state space tree, a partial state
space tree can be shown in Fig.11.b.
Fig. 11.b
In this state space tree, the nodes are numbered as per their levels. Each next move is
generated based on the empty slot positions . Edges are labeled according to the direction
in which the empty spaces moves. In the given state space tree , we start searching for
goal state from scanning the root node . The root node 1 becomes an E-node. The child
nodes (live nodes) 2, 3, 4 and 5 for this E-node gets generated. Out of which node 3
becomes an E-node . For this node the live nodes 8,9 and 10 gets generated. Then the
node 9 becomes an E-node for which child nodes 19 and 20 gets generated.
Finally we get a goal state at node 20. We can decide node to become an E-node, based
on estimation of c^(x). The formula is
c^(x) = f(x)+g^(x)
6. Algorithm
1. Define initial arrangement of 15 tiles.
2. Define proper arrangement of 15 tiles which is called as goal state.
3. Define functions for legal moves of tiles i.e. function for left, right up or down
movement.
4. Define function for calculating lower bound cost of particular node.
C(x) =f(x)+g(x)
5. Call movement function to perform different operations to place the tile as per the
goal state requirement.
6. Display the intermediate state obtained and minimum cost required for that.
7. Stop.
54
7. Conclusion and Discussion: Thus 15 Puzzle problem is solved using branch and
bound method.
8. Viva Questions:
How to check if an instance of 15 puzzle is solvable?
Is the following instance of 15 – puzzle solvable?
9. References:
http://www.geeksforgeeks.org/check-instance-15-puzzle-solvable/
https://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.ht
ml
55