Professional Documents
Culture Documents
Analysis and Design of Algorithm
Analysis and Design of Algorithm
ALGORITHM
Ameya Yellapurkar
Aniruddha Deshpande
December 17,2009
1 KRUSKAL’S ALGORITHM
1.1 INTRODUCTION
Kruskals 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
com- ponent). Kruskals algorithm is an example of a greedy algorithm. The
algorithm begins by sorting the graphs edges in non decresing order of their
weights. Then, starting with empty subgraph it scans this sorted list adding
the next edge on the list to the current sub- graph if such an inclusion does not
create a cycle and simply skipping the edges otherwise.
1
1.3 ALGORITHM
function Kruskal(G)
//Kruskals algorithm for constructing a minimal spanning tree.
//Input: A weighted connected graph G = (V,E).
//Output: Et, the set of edges composing of minimum spanning
tree.
Sort E in nondecreasing order of their weights w1¡w2¡w3...
Et ← 0;
ecounter ← 0;
k ← 0;
while ecounter < | V | - 1
k ← k + 1;
If Et U w(i) is acyclic
Et ← Et U w(i); ecounter ← ecounter + 1
return Et
1.4 COMPLEXITY
The time effieciency of Kruskals Algorithm depends on the data structure
used. For graphs represented by their weight matrix and priority queue imple-
mented as unordered array, it is O(|E| log |V|).
2 N QUEENS ALGORITHM
2.1 INTRODUCTION
The n-queens problem, originally introduced in 1850by Carl Gauss, may be
stated as follows: find a placementof n queens on an nn chessboard, such that no
one queen can be taken by any other. A backtracking search, will systematically
generate all possible solution sets for a given nn board.
2
2.3 ALGORITHM
function n queen(queen:array 1...n of integer)
begin
repeat
generate a random permutation of queen 1 to queen n
for all i,j;where queen i or queen j is attacked do
if swap(queen i,queen j)reduces collisions
then perform swap(queen i,queen j);
until no collisions;
end
2.4 COMPLEXITY
The complexity is of the order of o(n2 )
3 HORSPOOL ALGORITHM
3.1 INTRODUCTION
Horspool algorithm is used in problems pertaining to string matching.
This algorithm is used to search for a substring in a string. It starts by alligning
the start of the substring with the beginnig of the main string, and for each
unmatched character moves to next postion in the main string and alligns it
with the start of the substring. This continues till a matching substring is
found or the end of main string is reached.
3
i ← i + s[ t [ i ] ]
end while
return -1
3.4 COMPLEXITY
The algorithm trades space for time in order to obtain an average-case com-
plexity of O(N) on random text, although it has O(MN) in the worst case. The
length of the pattern is M and the length of the search string is N.
In our application patterns were found easily. The algorithm worked better
than the conventional pattern searching algorithms.
4 PRIM’S ALGORITHM
4.1 INTRODUCTION
In computer science, Prims algorithm is an algorithm that finds a minimum
spanning tree for a connected weighted undirected 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. Prims algorithm is an example
of a greedy algorithm.The Prims algorithm constructs a minimum spanning tree
through a sequence of expanding subtrees. On each iteration, we expand the
current tree in a greedy manner selecting the edge with the least weight among
the nearest vertices not in the subtree.
4
4.3 ALGORITHM
function Prim(G)
Vt ← V(0);
Et ← 0;
for i ← 1 to |V| - 1 do
find a minimum weight edge e* = {v*,u*} among all
edges (v,u) such that v is in Vt and u in V - Vt.
Vt ← Vt U {u*}
Et ← Et U {e*}
return D
4.4 COMPLEXITY
The time efficiency of the algorithm is O(V * V).
5 KNAPSACK ALGORITHM
5.1 INTRODUCTION
The knapsack problem or rucksack problem is a problem in combinatorial
optimization: Given a set of items, each with a weight and a value, determine
the number of each item to include in a collection so that the total weight is less
than a given limit and the total value is as large as possible. It derives its name
from the problem faced by someone who is constrained by a fixed-size knapsack
and must fill it with the most useful items.
Greedy approximation algorithm to solve the unbounded knapsack problem.
His version sorts the items in decreasing order of value per unit of weight, pj /
wj. It then proceeds to insert them into the sack, starting with as many copies
as possible of the first kind of item until there is no longer space in the sack for
more. Provided that there is an unlimited supply of each kind of item, if A is
the maximum value of items that fit into the sack, then the greedy algorithm
is guaranteed to achieve at least a value of A/2. However, for the bounded
problem, where the supply of each kind of item is limited, the algorithm may
be very much further from optimal.
5
• Among the subsets that do not include the ith item, the value of the
optimal subset is, by definition, V[i - 1,j - w]
• Among the subsets that do not include the ith item, an optimal subset is
made up of this edges and all the i - 1 previous edges selected into the
knapsack.
5.3 ALGORITHM
//Implements the memory functions method for the knapsack problem.
//Input: A nonnegative integer i indicating the number of the first.
//Output: The value of an optimal feasible subset of the 1st i items.
if V[i,j] < 0
if V[i,j] < 0
if j < Weights[i]
value ← MFKnapsack(i - 1,j).
else
value ← max(MFKnapsack(i - 1,j)), Values[i]
+ MFKnapsack(i - 1,j - Weights[i]).
V[i,j] ← value.
return V[i,j]
5.4 COMPLEXITY
This is an O(n3) algorithm, where n is the number of vertices in the
digraph. It Uses the principle of Dynamic Programming.
6 WARSHALL’S ALGORITHM
6.1 INTRODUCTION
Warshalls algorithm is used to for computing the transitive closure of aa
directed graph. The algorithm is based on the idea of dynamic programming.
Transistive closure of a graph can be defined as a n * n boolean matrix , in
which the element in the ith row and jth column is 1 if there is a directed vertex
from ith vertex to jth vertex.
• List all the immediate vertices which do not contain the kth vetex.
• If it is present, we assume that it occurs only once in the list.
6
6.3 ALGORITHM
for i = 1 to N
for j = 1 to N
if there is an edge from i to j
dist[0][i][j] = the length of the edge from i to j
else
dist[0][i][j] = ∞
for k = 1 to N
for i = 1 to N
for j = 1 to N
dist[k][i][j] = min(dist[k-1][i][j], dist[k-1][i][k] +])
dist[k-1][k][j
6.4 COMPLEXITY
This algorithm takes (n3 ) time and (n3 ) space, and has the distinct
advantage of hiding a small constant in its behavior, since very little work is
done in the innermost loop. Furthermore, the space-bound can be reduced
further to (n2 ) by noticing that dist(k,i,j) is independent from dist(k - 1,i,j).
7.3 ALGORITHM
Input← a knapsack vector A = (a1, ..., an) and a sum s.
Output←a vector X with AX = s, provided a solution exists.
Divide the knapsack A = (a1, ..., an) into A1 = (a1, ..., at) and A2 = (at+1,
..., an).
For every vector X1 = (x1, ..., xt)
compute s1 = A1X1
7
store s1 togeter with the corresponding x-vector in a table T
sort this table by the sum
Now for all possible vectors X2 = (xt+1, ..., xn)
compute s2 = A2X2
get the difference L = s - s2
search L in T. If found, a solution vector is X = X1||X2.
7.4 COMPLEXITY
The running time of this algorithm is O(n2 n/2). In the first step, all 2n /2
sums are computed.The worst case of step two is, when all 2n /2 sums s2 are
computed. The search for an element in an ordered set of t elements is done in
O(log t) and thus searching after 2n /2elements takes O((n/2)2n /2) steps.