final algorithm lab manual.doc

47
1. Algorithm InsertionSort(a,n) // a is a global array that containing n elements to be sorted. { for j:=2 to n do { key := a[j]; i:= j-1; while( i> 0 and a[i] > key) do { a[i+1]:= a[i]; i:= i-1; } a[i+1]:=key; } } 2. Algorithm Tower_of_Hanoi(A, B, C, N) //This algorithm finds the moves to solve the Tower of Hanoi problem for N discs. N is // total number of discs. { if(N=1) then { Move top disc from A to C; return; } else {

Upload: mainak-de

Post on 02-Jan-2016

66 views

Category:

Documents


4 download

DESCRIPTION

vv

TRANSCRIPT

Page 1: Final Algorithm lab manual.doc

1. Algorithm InsertionSort(a,n)// a is a global array that containing n elements to be sorted. for j:=2 to n do key := a[j]; i:= j-1; while( i> 0 and a[i] > key) do a[i+1]:= a[i]; i:= i-1; a[i+1]:=key;

2. Algorithm Tower_of_Hanoi(A, B, C, N)//This algorithm finds the moves to solve the Tower of Hanoi problem for N discs. N is // total number of discs.

if(N=1) then Move top disc from A to C; return; else Tower_of_Hanoi(A, C, B, N-1); Tower_of_Hanoi(A, B, C, 1); Tower_of_Hanoi(B, A, C, N-1);

Page 2: Final Algorithm lab manual.doc

Number of discs is 3.

TOH(A,B,C,3)

TOH(A,C,B,2) TOH(A,B,C,1) TOH(B,A,C,2)A->C

TOH(A,B,C,1) TOH(A,C,B,1) TOH(C,A,B,1) TOH(B,C,A,1) TOH(B,A,C,1) TOH(A,B,C,1) A->C A->B C->B B->A B->C A->C

3. HEAP SORT:Heap: A max(min) heap is a complete binary tree with the property that the value at each node is at least as large as(as small as) the values at its children(if they exist).

Algorithm HeapSort(a,n)// a[1:n] contains n elements to be sorted. HeapSort rearranges them inplace into // nondecreasing order.

Heapify(a,n); // Transform the array into a heap. Interchange the new maximum // with the element at the end of the array.

for i:=n to 2 step -1 do t:=a[i]; a[i]:=a[1]; a[1]:=t; Adjust(a,1,i-1);

Page 3: Final Algorithm lab manual.doc

Algorithm Heapify(a,n)// Readjust the elements in a[1:n] to form a heap.

for i:= to 1 step -1 do Adjust(a,i,n);

Algorithm Adjust(a,i,n)// the complete binary trees with roots 2i and 2i+1 are combinded with node i to form // heap rooted at i. No node has an address greater than n or less than 1.

j:= 2i; item :=a[i]; while(j≤n) do if (( j<n ) and ( a[j] < a[j+1])) then j:=j+1; // compare left and right child and let j be the larger child. if(item ≥ a[j]) then break; // A position for item is found. a[ ] := a[j]; j:=2j;

a[ ] := item;

4. BINARY SEARCH:

Let ai , 1≤i≤n, be a list of elements that are sorted in nondecreasing order. Consider the problem of determining whether a given element x is present in the list. If x is present , we are to determine a value j such that aj = x. if x is not in the list, then j is to be set to zero.

Page 4: Final Algorithm lab manual.doc

Algorithm BinSrch(a,i,l,x)

//Given an array a[i:l] of elements in nondecreasing order, 1≤i≤l, determine whether // whether x is present, and if so, return j such that x=a[j]; else return 0.

if(l=i) then // if Small(P) if(x=a[i]) then return i; else return 0;else // reduce P into a smaller sub problem.

mid := ;

if( x=a[mid] then return mid; else if (x< a[mid]) then return BinSrch(a,i, mid-1,x); else return BinSrch (a,mid+1,l,x);

Algorith BinSearch(a,n,x)// Given an array a[1:n] of elements in nondecreasing order, n>0, determine whether // x is present, and if so return j. such that x=a[j]; else return 0.

low:=1; high:=n; while ( low ≤ high ) do

mid := ;

if ( x< a[mid]) then high := mid-1; else if (x > a[mid]) then low := mid +1; else return mid; return 0;

Divide-and-Conquer:

Page 5: Final Algorithm lab manual.doc

These algorithms have the following outline: To solve a problem, divide it into sub problems. Recursively solve the sub problems. Finally glue the resulting solutions together to obtain the solution to the original problem.

5. MERGE SORT:Merging two sorted subarrays using Merge algorithm.

Algorithm Merge (low, high, mid)// a [low: high] is a global array containing two sorted subsets in a [low: mid] and in// a[mid+1: high]. The goal is to merge these two sets into a single set residing in // a [low:high]. b [] is an auxiliary global array.

h:=low;i:=low;j:=mid+1; while((h<=mid) and ( j<=high)) do if(a[h]<=a[j]) then b[i]:=a[h]; h:=h+1; else b[i]:=a[j]; j:=j+1; i:=i+1; if(h>mid) then for k:=j to high do b[i]:=a[k]; i:=i+1; else for k:=h to mid do b[i]:=a[k]; i:=i+1; for k:=low to high do a[k]:=b[k];

Algorithm MergeSort(low,high)

Page 6: Final Algorithm lab manual.doc

// a [low: high] is a global array to be sorted. if(low<high) then // divide array into subarrays ; mid=[(low+high)/2]; MergeSort(low,mid); MergeSort(mid+1,high); // combine the sorted subarrays Merge(low,mid,high);

6. QUICK SORT:

The divide-and-conquer approach cab be used to arrive at an efficient sorting method different from merge sort. In merge sort, the file a[1:n] was divideed at its midpoint into subarrays which were indipendently sorted and later merged. In Quick sort, the division into two subarrars is made so that the sorted subarrays donot need to be merged later.

Algorithm QuickSort(p,q)

//Sorts the elements a[p],…,a[q] which reside in the global array a[1:n] into ascending//order; a[n+1] is considered to be defined and must be ≥ all the elements in a[1:n].

if(p<q) then // if there are more than one element // divide P into two subproblems. j:=Partition(a,p,q+1); // j is the position of the partitioning element. // Solve the subproblems. QuickSort(p,j-1); QuickSort(j+1,q); // There is no need for combinding solutions.

Algorithm Partition(a,m,p)

Page 7: Final Algorithm lab manual.doc

// Within a[m],a[m+1],…,a[p-1] the elements are rearranged in such a manner that if // initially t=a[m], then after completion a[q]=t for some q between m and p-1, a[k]≥ t // for q<k<p. q is returned. Set a[p]=∞.

v:=a[m]; i:=m; j:=p; repeat repeat i:=i+1; until(a[i]≥v); repeat j:=j-1; until(a[j]≤v);

if (i<j) then Interchange(a,i,j); a[m]:=a[j]; a[j]:=v; return j;

Algorithm Interchange(a,i,j) // Exchange a[i] with a[j]. p:=a[i]; a[i]:=a[j]; a[j]:=p;

7. STRASSEN’S MATRIX MULTIPLICATION:

Page 8: Final Algorithm lab manual.doc

Let A and B be two n*n matrices. The product matrix C=AB is also an n*n matrix whose i,jth element is formed by taking the elements in the ith row of A and the jth column of B and multiplying then to get

C(i,j) = ∑ A(i,k) B(k,j) ----- (1) 1≤k≤nFor al i and j between 1 and n. to compute C(i,j) using above formula, we need n multiplications. As the matrix C has n2 elements, the timr for the resulting matrix multiplication algorithm, which we refer to as the conventional method is (n3).

The divide-and-conquer strategy suggests another way to compute the product of two n*n matrices. For simplicity we assume that n is a power of 2, that is, that there exists a nonnegative integer k such that n=2k . imagine that A and B are each partitioned into four square submatrices, each submatrix matrix having dimensions n/2 *n/2. Then the product AB can be computed by using the above formula for the product of 2*2 matrices: if AB is

A11 A12 B11 B 12 = C11 C 12

A21 A22 B21 B22 C 21 C 22 …….(2)

Then C11 = A11 B11 + A12 B21

C12 = A11 B12 + A12 B22

C21 = A21 B11 + A22 B21

C22 = A21 B12 + A22 B22 ……(3)

If n=2, then formulas (2) and (3) are computed using a multiplication operation for the elements of A and B. these elements are typically floating point numbers. For n>2, the elements of C can be computed using matrix multiplication and addition operations applied to matrices n/2*n/2. Since n is a power of 2, these matrix products cab be recursively computed by the same algorithm we are using for the n*n case. To compute AB using (3), we need to perform eight multiplications of n/2*n/2 matrices and four additions n/2*n/2 matrices. Since two n/2*n/2 matrices can be

added in time cn2 for some constant c, the overall computing time T(n) of the resulting divide and conquer algorithm is given by the recurrence

b if n ≤ 2

T(n) = 8 T(n) + cn2 if n > 2 ………………(4)

Page 9: Final Algorithm lab manual.doc

Where b and c ae constants.

We can attempt to reformulate the equations for Cij so as to have fewer multiplications and possible more additions. Volker stressen has discovered a way

to compute the Cij of (3) using only 7 multiplications and 18 additions or substractions.His method involves first computing the seven n/2*n/2 matrices P, Q, R, S, T, U, and

V as in (5) . Then the Cij are computed using the formulas in 6. As can ve seen, P, Q, R, S, T, U, and V can be computed using 7 matrix multiplications and 10 matrix

additions or subtractions. The Cij’s require an additional 8 additions or subtractions.

P= (A11 + A22)(B11 +B 22)

Q= (A21 + A22)B11

R= A11(B12 - B 22)

S= A22(B21 - B 11)

T= (A11 + A12)B 22

U= (A21 - A11)(B11 +B 12)

V= (A12 - A22)(B21 +B 22) …………………………..(5)

C11 = P + S – T + V

C12 = R + T

C21 = Q + S

C22 = P + R – Q + U ……………………….(6)

The resulting recurrence relation for T(n) is

b if n ≤ 2

T(n) = 7 T(n) + cn2 if n > 2 ………………(7)

Traversal of Graph:

There are two standard ways to traverse a graph. First one is breadth first search(BFS) and the second one is Depth first search(DFS). The procedure BFS uses

Page 10: Final Algorithm lab manual.doc

a queue to store the unvisited vertices and DFS uses a stack for this purpose. To distinguish between the visited vertices and unvisited vertices, we give the color to each and every node of the graph.

8. BREADTH FIRST SEARCH(BFS):

In this traversal technique, first select a vertex S to start the traversal. Initially set the color of all vertices to WHITE. Insert S into the queue and change its color to BLACK as now S is a visited vertex.Now delete the front node N from the queue. Visit all the adjacent vertices of N and insert to queue those neighbors of N that have the color WHITE. Change their colors to BLACK.

Algorithm BFS(S) for ( all the vertices j of the graph ) color[j]:=WHITE; // Initialize the color of all nodes to WHITE. fnInsert(S); // put the starting node S into queue. color[S] := BLACK; while(queue is not empty) N:=fnDelete(); // Remove the front node N of queue. for(each adjacent vertex j to N) if color[j] = WHITE then fnInsert(j); color[j] = BLACK;

9. DEPTH FIRST SEARCH(DFS):

Page 11: Final Algorithm lab manual.doc

In this traversal technique, we use a stack instead of queue. First select a vertex S to start the traversal. Initially set the color of all vertices to WHITE. Insert S into the stack and change its color to BLACK as now S is a visited vertex.Now delete the top node N from the stack. Visit all the adjacent vertices of N and insert to stack those neighbors of N that have the color WHITE. Change their colors to BLACK.

Algorithm DFS(S) for ( all the vertices j of the graph ) color[j]:=WHITE; // Initialize the color of all nodes to WHITE. fnPush(S); // put the starting node S into stack. color[S] := BLACK; while(stack is not empty) N:=fnPop(); // Remove the front node N of stack. for(each adjacent vertex j to N) if color[j] = WHITE then fnPush(j); color[j] = BLACK;

GREEDY METHOD:The greedy method is perhaps the most straighforword design technique. Most, though not all, of these problems have n inputs and require us to obtain a setset that satisifies some constraints. Any subset thatsatisfies these constraints is called a feasible solution. We needd to find to find a feasible solution that either maximizes or minimizes a given objective function. A feasible solution that does this is called an optimal solution.

10. KNAPSACK PROBLEM:

Let us try to apply the greedy method to solve the knapsack problem. We are given n objects and a knapsack or bag. Object i has a weight w i and the knapsack has a

Page 12: Final Algorithm lab manual.doc

capacity m. if a fraction xi , 0≤ xi≤1, of object i is placed into the knapsack, then a profit of pi xi is earned. The objective is to obtain a filling of the knapsack that maximizes the total profit earned. Since the knapsack capacity is m, we require the total weight of all chosen objects to be at most m. Formally, the problem can be stated as

maximizes ∑ pi xi ………………..(1) 1≤ i ≤n subject to ∑ wi xi ≤ m ………………..(2)

1≤ i ≤n

and 0 ≤ xi ≤ n , 1≤ i ≤n ……………….(3)the profits and weights are positive numbers.A feasible solution is any set ( x1 , . . . , xn ) satisfying (2) and (3) above. An optimal solution is a feasible solution for which (1) is maximized.

Algorithm GreedyKnapsack(m,n)// p[1:n] and w[1:n] contain the profits and weights respectively of the n objects // ordered such that p[i]/w[i]≥ p[i+1]/w[i+1]. M is the knapsack size and x[1:n] is the // solution vector.

for i:= 1 to n do x[i] := 0.0; // Initialize x U:=m; for i:= 1 to n do if ( w[i] > U) then break; x[i] := 1.0; U := U – w[i]; if(i ≤ n) then x[i] := U/w[i];

11. PRIM’S ALGORITHM:A greedy method to obtain a minimum-cost spanning tree builds this tree edge by edge. The next edge to include is choosen according to some optimization criterion.

Page 13: Final Algorithm lab manual.doc

The simplest such criterion is to choose an edge that results in a minimum increase in the sum of the costs of the edges so far included.

28

10 10

14 16 14 16

25 24 18 12 25 12

22 22

Figure 1: A graph and its minimum cost spanning tree

Algorithm Prim(E, cost, n, t)

// E is the set of edges in G. cost[1:n,1:n] is the cost adjacency matrix of an n vertex // graph such that cost [i,j] is either a positive real number or ∞ if no edge (i,j) exists. // A minimum spanning tree is computed and stored as a set of edges in the array // t[1:n,1:2]. (t[I,1],t[I,2]) is an edge in the minimum-cost spanning tree. The final cost // is returned.

Let (k,l) be an edge of minimum cost in E; mincost:=cost[k,l]; t[1,1]:=k; t[1,2]:=l; for i:=1 to n do //initialize near. if ( cost[i,l] < cost[i,k] ) then near[i]:=l; else near[i]:=k; near[l]:=near[k]:=0; for i:= 2 to n-1 do // find n-2 additional edges for t. Let j be an index such that rear[j]≠0 and cost[j,near[j]] is minimum;

1

6 7

4

3

2

5

1

6 7

4

3

2

5

Page 14: Final Algorithm lab manual.doc

t[i,1]:=j; t[i,2]:=near[j]; mincost := mincost+cost[j,near[j]]; near[j]:=0; for k:= 1 to n do // Update near[]. if((near[k] ≠ 0) and (cost[k,near[k]] > cost [k,j])) then near[k] :=j; return mincost;

12. KRUSKAL’S ALGORITHM: Kruskal’s algorithm is based directly on the generic minimum-spanning-tree algorithm given below. It finds a safe edge to add to the growing forest by finding, of all the edges that connect any two trees in the forest, an edge (u, v) of least weight. Let C1 and C2 denote the two trees that are connected by (u, v). Since (u, v) must be a light edge connecting C1 to some other tree, implies that (u, v) is a safe edge for C1. Kruskal’s algorithm is a greedy algorithm, because at each step it adds to the forest an edge of least possible weight. Our implementation of Kruskal’s algorithm is like the algorithm to compute connected components from Section 21.1. It uses a disjoint-set data structure to maintain several disjoint sets of elements. Each set contains the vertices in a tree of the current forest. The operation FIND-SET(u) returns a representative element from the set that contains u. Thus, we can determine whether two vertices u and v belong to the same tree by testing whether FIND-SET(u) equals FIND-SET(v). The combining of trees is accomplished by the UNION procedure.

28

10 10 14 16 14 16

25 24 18 12 2512

22 22

Figure 2: A graph and its minimum cost spanning tree

1

6 7

4

3

2

5

1

6 7

4

3

2

5

Page 15: Final Algorithm lab manual.doc

Algorithm Kruskal(G,w) A := ∅ for each vertex v ∈ V[G] do MAKE-SET(v) Sort the edges of E into nondecreasing order by weight w for each edge (u, v) ∈ E, taken in nondecreasing order by weight do if FIND-SET(u) = FIND-SET(v) then A:= A ∪ (u, v) UNION(u, v) return A

13. DIJKSTRA’S ALGORITHM:Dijkstra’s Algorithm takes a labelled graph and a pair of vertices P and Q, and finds the shortest path between them(or one of the shortest paths, if there is more than one). The algorithm is given the length matrix of the graph, which contains information about all the graph’s edge length. The length of the edge from v1 to v2 is denoted by l(v1,v2), starting with an initial vertex called v0. The vertex whose shortest path from v0 has most recently been found will be denoted by v*.

At all times, each vertex v will have three things assigned to it- i). a status, denoted Status[v] will be either “!” meaning that the shortest path from v to v0 has definitely been found or “?” meaning that it hasn’t. ii). a distance, denoted by dist[v] with a number representing the length of the shortest path from v to v0 found so far. iii). a next vertex denoted next[v] will be the first vertex on the way to v0

along the shortest path found so far from v to v0.

Dijkstra’s Algorithm() status[v0]:=1; dist[v0]:=0; next[v0]:=*; for(all v≠v0) status[v]:=?; dist[v]:=l(v,v0); next[v]:=v0;

while(there are remaining ? vertices)

Page 16: Final Algorithm lab manual.doc

v*:=? Vertex for which dist is smallest; status[v*]:=!; for(each remaining ? vertex v) if(dist[v]>dist[v*]+l(v*,v)) dist[v]:= dist[v*]+l(v*,v)); next[v]:= v*;

14. JOB SEQUENCING WITH DEADLINES:

We are given a set of n jobs. Associated with job i is an integer deadline d i ≤ 0 and a profit pi > 0. For any job i the profit pi is earned iff the job is completed by its deadline. To complete a job, one has to process the job on a machine for one unit of time. Only one machine is available for processing jobs. A feasible solution for this problem is a subset J of jobs such that each job in this subset can be completed by its deadline. The value of a feasible solution J is the sum of the profits of the jobs in J, or ∑i €J pi.

Algorithm JS(d,j,n)

// d[i] ≥ 1 , 1≤i≤n are the deadline n ≥ 1 , The jobs are ordered such that p[1] ≥ p[2] ≥ // …….≥ p[n]. J[i] is the ith job in the optimal solution , 1≤ i ≤ k. Also, at termination // d[J[i]] ≤ d[J[i+1]], 1≤ i ≤ k.

d[0] :=J[0] := 0; // initialize J[1] :=1 ; // Include job 1 for i:= 2 to n do // Consider jobs in the nonincreasing order of p[i].Find position for i and // check feasibility of insertion . r := k ; while((d[J[r]] > d[i]) and (d[J[r]] ≠r) ) do r := r-1; if((d[J[r]] ≤ d[i]) and (d[i] > r)) then

// Insert i into J[]for q := k to (r+1) step -1 do J[q+1] := J[q];J[r+1] := i; k=k+1;

return k;

Page 17: Final Algorithm lab manual.doc

DYNAMIC PROGRAMMINGDynamic programming, like the divide-and-conquer method, solves problems by combining the solutions to subproblems. (“Programming” in this context refers to a tabular method, not to writing computer code.) In divide-and- conquer algorithms partition the problem into independent subproblems, solve the subproblems recursively, and then combine their solutions to solve the original problem. In contrast, dynamic programming is applicable when the subproblems are not independent, that is, when subproblems share subsubproblems. In this context,a divide-and-conquer algorithm does more work than necessary, repeatedly solving the common subsubproblems. A dynamic-programming algorithm solves every subsubproblem just once and then saves its answer in a table, thereby avoiding the work of recomputing the answer every time the subsubproblem is encountered. Dynamic programming is typically applied to optimization problems. In such problems there can be many possible solutions. Each solution has a value, and we wish to find a solution with the optimal (minimum or maximum) value. We call such a solution an optimal solution to the problem, as opposed to the optimal solution, since there may be several solutions that achieve the optimal value.

15. MATRIX-CHAIN MULTIPLICATION:dynamic programming is an algorithm that solves the problem of matrix-chain multiplication. We are given a sequence (chain) A1, A2, . . . , An of n matrices to be multiplied, and we wish to compute the product A1 A2 · · · An . We can evaluate the expression using the standard algorithm for multiplying pairs of matrices as a subroutine once we have parenthesized it to resolve all ambiguities in how the matrices are multiplied together. A product of matrices is fully parenthesized if it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses. Matrix multiplication is associative, and so all parenthesizations yield the same product. For example, if the chain of matrices is A1, A2, A3, A4 the product A1 A2 A3 A4 can be fully parenthesized in five distinct ways:

(A1(A2(A3 A4))) ,(A1((A2 A3)A4)) ,((A1 A2)(A3 A4)) ,((A1(A2 A3))A4) ,(((A1A2)A3)A4) .

Page 18: Final Algorithm lab manual.doc

Algorithm Matrix-Chain-Order(p) n:= length[p] – 1; for i := 1 to n do m[i, i ] := 0; for l := 2 to n do // l is the chain length. for i := 1 to n − l + 1 do j ←i + l – 1 ; m[i, j ]:=∞ ; for k :=i to j – 1 do q := m[i, k] + m[k + 1, j ] + pi−1 pk pj ; if q < m[i, j ] then m[i, j ]:= q ; s[i, j ] := k ; return m and s ;

Algorithm PRINT-OPTIMAL-PARENS(s, i, j ) if i = j then print “A”i; else print “(” ; PRINT-OPTIMAL-PARENS(s, i, s[i, j ]) ; PRINT-OPTIMAL-PARENS(s, s[i, j ] + 1, j ) ; print “)” ;

Page 19: Final Algorithm lab manual.doc

16. THE FLOYD-WARSHALL ALGORITHM: We shall use a different dynamic-programming formulation to solve the all-pairs shortest-paths problem on a directed graph G = (V, E). The resulting algorithm, known as the Floyd-Warshall algorithm, runs in Θ(V3) time. As before, negative-weight edges may be present, but we assume that there are no negative-weight cycles. We shall follow the dynamic programming process to develop the algorithm. After studying the resulting algorithm, we shall present a similar method for finding the transitive closure of a directed graph.

Let dij(k) be the weight of a shortest path from vertex i to vertex j for which all

intermediate vertices are in the set 1, 2, . . . , k. When k = 0, a path from vertex i to vertex j with no intermediate vertex numbered higher than 0 has no intermediate

vertices at all. Such a path has at most one edge, and hence dij(0) = wij . A recursive

definition following the above discussion is given by

dij(k) = wij if k = 0;

min dij(k-1), dik

(k-1)+ dkj(k-1) if k ≥ 1;

The procedure returns the matrix D(n) of shortest-path weights.

FLOYD-WARSHALL(W) N:= rows[W]

D(0):= W

for k := 1 to n do for i := 1 to n do for j := 1 to n do

dij(k) := min dij

(k-1), dik(k-1)+ dkj

(k-1) return D(n)

Constructing a shortest path

Page 20: Final Algorithm lab manual.doc

We can compute the predecessor matrix π “on-line” just as the Floyd-Warshall algorithm computes the matrices D(k). Specifically, we compute a sequence of matrices

Π(0) π(1) . . . π(n), where π = π(n) and πij(k) is defined to be the predecessor of

vertex j on a shortest path from vertex i with all intermediate vertices in the set 1, 2, . . . , k.

We can give a recursive formulation of πij(k). When k = 0, a shortest path from i to j

has no intermediate vertices at all. Thus,

πij(0) = NIL if i = j or wij = ∞

i if i ≠ j and wij < ∞

For k ≥ 1, if we take the path i k j, where k ≠ j , then the predecessor of j we choose is the same as the predecessor of j we chose on a shortest path from k with all intermediate vertices in the set 1, 2, . . . , k − 1. Otherwise, we choose the same predecessor of j that we chose on a shortest path from i with all intermediate vertices in the set 1, 2, . . . , k − 1. Formally, for k ≥ 1,

πij(k-1) if dij

(k-1) ≤ dik(k-1)+ dkj

(k-1)

πij(k) = πkj

(k-1) if dij(k-1) > dik

(k-1)+ dkj(k-1)

Page 21: Final Algorithm lab manual.doc

BACKTRACKING:In the search for fundamental principles of algorithm design, backtracking represents one of the most general techniques. Many problems which deal with searching for a set of solutions or which ask for an optimal solution satisfying some constraints can be solved using the backtracking formation.In many applications of the backtrack method, the desire solution is expressible as an n-tuple(x1, . . . , xn ), where the xi are choosen from some finite set Si .

17. EIGHT QUEENS PROBLEM:A clasic combinatorial problem is to place eight queens on an 8x8 chessboard so that no two “attack” that is, so that no two of them are on the same row, column, or diagonal. Let us number the rows and columns of the chessboard 1 through 8.

Column 1 2 3 4 5 6 7 81 Q

2 Q

3 Q

4 Q

5 Q

6 Q

7 Q

8 Q

Since each queen must be on a different row, we can without loss of generality assume queen I is to be placed on row i. All solutions to the 8-queens problem can therefore be represented as 8-tuples((x1, . . . , x8 ), where xi is the column on which queen i is placed.

Row

Page 22: Final Algorithm lab manual.doc

Algorithm NQueens(k,n)// Using backtracking, this procedure prints all possible placements of n queens on // an n*n chessboard so that they are nonattacking. for i:=1 to n do if(Place(k,i) then x[k]:=i; if(k=n) then write(x[1:n]);

Algorithm Place(k,i)

// Returns true if a queen can be placed in kth row and ith column. Otherwise it // returns false. X[] is a gloabal array whose first (k-1) values have been set. Abs( r ) // returns the absolute value of r. for j:= 1 to k-1 do if((x[j]=i) //Two in the same column or(Abs(x[j]-i) = Abs(j-k))) // or in the same diagonal then return false; return true;

18. GRAPH COLORING: Let G be a graph and m be a given positive integer. We want to discover whether the nodes of G can be colored in such a way that no two adjacent nodes have the same color yet only m colors are used.Note that if d is the degree of the given graph, then it can be colored with d+1 colors. The m-colorability optimization problem asks for thr smallest integer m for which the graph G can be colored. This integer is referred to as the chromatic number of the graph.

b

Page 23: Final Algorithm lab manual.doc

2

3 1

3 1An example graph and its coloring

Algorithm mColoring(k) // This algorithm was formed using the recurrsive backtracking schema. The graph is //represented by its booleen adjacency matrix G[1:n,1:n]. All assignments of 1,2,…,m //to the vertices of the graph such that adjacency vertices are assigned distict integers //are printed. k is the index of the next vertex to color.

repeat // Generate all legal assignments for x[k]. NextValue(k);//Assign to x[k] a legal color. if(x[k]=0) then return; // No new color possible. if(k==n) then // At most m colors have been used to color the n vertices. Write (x[1:n]); else mColoring(k+1); until(false);

Algorithm NextValue(k)

a

dc

e

Page 24: Final Algorithm lab manual.doc

// x[1],…,x[k-1] have been assigned integer values in the range [1,m] such that // adjacency vertices have distinct integers. A value for x[k] is determined in the // range [0,m]. x[k] is assigned the next highest numbered color while maintaining // distinctness from the adjacent vertices of vertex k. if no such color exixts, then // x[k] is 0.

repeat x[k]:= (x[k] +1 ) mod (m+1); //Next highest color. if(x[k] = 0) then return; // All colors have been used. for j:= 1 to n do // check if this color is distinct from adjacency colors. if( ( G[k,j] ≠ 0) and (x[k] = x[j])) // if (k,j) is and edge and if adjacent vertices have the same color. then break; if( j=n+1) then return; // New color found until(false); // otherwise try to find another color.

19. HAMILTONIAN CYCLES:Let G=(V,E) be a connected graph with n vertices. A Hamiltonian cycles is a round trip path along n edges of G that visits every vertex once and returns to its starting position. In other words if a Hamiltonian begins at some vertex v1 € G and the vertices of G are visited in the order v1, v2, . . . , vn+1 , then the edges (vi , vi+1) are in E, 1≤i≤n, and the vi are distinct except for vi vn+1 , which are equal.

Algorithm Hamiltonian(k)

1

8 7 6 5

432

Page 25: Final Algorithm lab manual.doc

// This algorithm uses the reurssive formulation of backtracking to find all the // Hamiltonian cycles of a graph. The grapy is stored as an adjacency matrix// G[1:n, 1:n. All cycles begin at node1.

repeate // Generate values for x[k] NextValue(k); // Assign a legal next value to x[k]. if (x[k]=0) then return; if(k=n) then write(x[1:n]); else Hamiltonian(k+1); until(false);

Algorithm NextValue(k)

// x[1:k-1] is a path of k-1 distinct vertices. If x[k]=0, then no vertex has as yet been // assigned to x[k]. After execution, x[k] is assigned to the next highest numbered // vertex which does not already appear in x[1: k-1] and is connected by an edge to // x[k-1]. Otherwise x[k]=0. If k=n, then in addition x[k] is connected to x[1].

repeat x[k]:= (x[k] +1 ) mod (n+1); //Next vertex. if(x[k] = 0) then return; if( ( G[x[k-1], x[k]]≠0)then // is there an edge? for j:= 1 to k-1 do if(x[j]=x[k]) then break; // check for distinctness. if(j = k) then // if true, then the vertex is distinct. if((k<n) or ((k=n) and g[x[n],x[1]]≠0)) then break; until(false);

BRANCH AND BOUND:

Branch and bound refers to all state space search methods in which all children of the E-node are generated before any other live node can become the E-node.

Page 26: Final Algorithm lab manual.doc

20. 15-PUZZLE:The 15-puzzle consists of 15 numbered tiles on a square frame with a capacity of 16 tiles. We are given an initial arrangement of the tiles and the objective is to transform this arrangement into the goal arrangementthrough a series of legal moves. The only legal moves are ones in which a tile adjacent to the empty spot(ES) is moved ES.

1 3 4 152 5 127 6 11 148 9 10 13

(a) An arrangement (b) Goal arrangement

We begin with the root as the E-node and generate all children of E-node and compute c()-value all nodes , choose one with minimum c() value.

We can write c(x) = f(x) + g(x), where f(x) is the length of the path from the root to node x and g(x) is an estimate of the length of a shortest path from x to a goal node in the subtree with root x. One possible choice for g(x) is g(x) = number of nonblank tiles not in their goal position

listnode=recode listnode *next, *parent; float cost;

Algorithm LCSearch(t)// Search t for an answer node. if *t is an answer node then output *t and return; E:=t; //E-node. Initialize the list of live nodes to be empty; repeat for each child x of E do if x is an answer node then output the path from x to t and return;

1 2 3 45 6 7 89 10 11 12

13 14 15

Page 27: Final Algorithm lab manual.doc

Add(x); //x is a new live node. (x->parent):=E; // Pointer for path to root.

if there are no more live nodes then write (“ No answer node “); return; E:=Least(); until(false);

1. What is an ‘algorithm’? An algorithm consists of a finite set of steps that may require one or more operations. These operations should be definite and effective. An algorithm should produce one or more output’s and may have zero or more inputs.This consists of five distinct areas: 1. to device algorithms 2. to validate the algorithms 3. to express the algorithms 4. to analyze the algorithms 5. to test the programs for the algorithms

2. What is a ‘computational procedure’? An algorithm that does not terminate is called ‘computational procedure’.Example for such ‘computational procedure’ is an ‘operating system’.

3. Define ‘recursive algorithm’. An algorithm is said to be recursive if the same algorithm is invoked in the body of the algorithm. Recursive algorithms can be divided into direct recursive and indirect recursive algorithms.Direct recursive: An algorithm that calls itself.Indirect Recursive: An algorithm ‘A’ is said to be indirect recursive if it calls another algorithm which Inturn calls algorithm ‘A’.

4. How can you classify performance analysis? ‘Performance analysis’ can be classified as: i. priori analysis ii. posteriori analysis

Page 28: Final Algorithm lab manual.doc

STUDENT_REC NAME YEAR_OF_STUDY FIRST_NAME LAST_NAME FIRST_SEM SECOND_SEM 01 02 02 03 03 03 03

Priori Analysis: The bounds of algorithms’ computing time are obtained by formulating a function. Posteriori Analysis: Testing the actual computation of space and time are recorded while the algorithm is executing.

5. Define ‘BigO’. For the function f(n), f(n)=O(g(n)) iff there exist positive constants c and d such that: f(n) <=c*g(n) for all n,n>=d. This is defined to be the worsttime complexity of the function f(n).

For example: O(n)=3n+2 because, 3n+2 <=4n for all n>=2.

6. Give various computing times and their meaning. Few of the important computing times are: Computing Time Meaning O(1) : constant computing time O(n) : linear computing time O(n*n) : quadratic computing time O(n*n*n) : cubic computing time O(2*2*2*2*..............*n) : exponential computing time

7. Give the most important ‘basic designs‘of algorithms. There are five important basic designs for algorithms. They are: i. Divide and conquer, ii. The greedy method, iii. Dynamic programming, iv. Backtracking, v. Branch and bound.

8. How do ‘divide and conquer’ algorithms work? For a function to compute on n inputs the divide and conquer strategy suggests the inputs into a k distinct subsets, 1<k<=n, yielding k subproblems. These subproblems must be solved and then a method must be found to combine the subsolutions into a solution of the whole. An example for this approach is

Page 29: Final Algorithm lab manual.doc

‘binary search’ algorithm. The time complexity of binary search algorithm is O(log n).

9. What is Greedy Method? The greedy method suggests that one can devise an algorithm that works in stages, considering one input at a time. At each stage, a decision is made regarding whether a particular input is an optimal solution. An example for solution using greedy method is ‘knapsack problem’.

10. What is Dynamic Programming? Dynamic Programming is an algorithm design method that can be used when the solution to a problem can be viewed as the result of a sequence of decisions. An example for algorithm using dynamic programming is ‘multistage graphs’.

11. What are the time complexities for the following algorithms? Binary search : O(logn) Finding maximum and minimum for a given set of numbers : O(3n/22)Merge Sort : O(nlogn)Insertion Sort : O(n*n);Quick Sort : O(nlogn)Selection Sort : O(n)

12. What is the difference between Merge Sort and Quick sort? Both Mergesort and Quicksort have same time complexity i.e. O(nlogn). In merge sort the file a[1:n] was divided at its midpoint into subarrays which are independently sorted and later merged. Whereas, in quick sort the division into two subarrays is made so that the sorted subarrays do not need to be merged latter.

13. Is there any optimum solution for Matrix multiplication? Yes. Divide and conquer method suggests Strassen’s matrix multiplication method to be used. If we follow this method, the time complexity is O(n*n*n……..*2.81) times rather O(n*n*n*………*3) times.

14. Define ‘minimum cost spanning method’.Let G=(V,E) be an undirected connected graph. A subgraph t =(V, E’) of G is a spanning tree of G if and only if t is a tree. To find out minimum cost spanning method we have following method’s; Prim’s Algorithm : O(n*n) Kruskal’s Algorithm : O(e loge)

Page 30: Final Algorithm lab manual.doc

15. Define ’articulation points’. A Vertex V in a connected graph G is an articulation point if and only if the deletion of vertex V together will all edges incident for disconnects the graph into two or more nonempty Components.

16. Define ‘biconnected graph’.A graph G is biconnected if and only if it contains no articulation points.

17. What are ‘explicit’ and ‘implicit’ constraints?‘Explicit constraints’ are rules that restrict each xi to take on values only from a given set. ‘Implicit constraints’ are rules that determine which of the tuples in the solution space of i satisfy the criterion function.

Evaluation and marking system:Basic honesty in the evaluation and marking system is absolutely essential and in the process impartial nature of the evaluator is required in the examination system to become popular amongst the students. It is a wrong approach or concept to award the students by way of easy marking to get cheap popularity among the students to which they do not deserve. It is a primary responsibility of the teacher that right students who are really putting up lot of hard work with right kind of intelligence are correctlyawarded. The marking patterns should be justifiable to the students without any ambiguity and teacher should see that students are faced with unjust circumstances.

LABORATORY ASSIGNMRNTS FOR CSE/IT

ASSIGNMENT - 1

1. Program to implement Insertion Sort Algorithm to arrange a set of unsorted numbers into ascending order.

Page 31: Final Algorithm lab manual.doc

2. Program to implement Tower of Hanoi problem of atleast three disk.

3. Program to implement Heap Sort Algorithm to arrange a set of unsorted number into ascending order.

ASSIGNMENT - 2

4. Program to implement Binary Search Algorithm using Divide & Conquer Approach to search a number from a set of sorted numbers.

5. Program to implement Merge Sort Algorithm using Divide & Conquer Approach to arrange a set of unsorted number into ascending order.

6. Program to implement Quick Sort Algorithm using Divide & Conquer Approach to arrange a set of unsorted number into ascending order.

ASSIGNMENT - 3

7. Program to implement Strassen’s Matrix Multiplication using Divide & Conquer Approach.

8. Program to implement Breadth First Search Algorithm to traverse a graph.

9. Program to implement Depth First Search Algorithm to traverse a graph.

ASSIGNMENT - 4

10.Program to implement Knapsack problem(Fractional) using Greedy Approach.

11.Program to implement Prim’s Algorithm using Greedy Approach.

12.Program to implement Krushkal’s Algorithm using Greedy Approach.

ASSIGNMENT - 5

13.Program to implement Dijkstra’s Algorithm using Greedy Approach. L5

14.Program to implement Job sequencing with deadlines problem using Greedy Approach. L5

Page 32: Final Algorithm lab manual.doc

ASSIGNMENT - 6

15.Program to implement Matrix Chain Multiplication using Dynamic Programming Approach.

16.Program to implement All pairs shortest path(Floyd-Warshall Algorithm) using Dynamic Programming Approach.

ASSIGNMENT - 7

17.Program to implement Eight Queens problem using Backtracking Approach.

18.Program to implement Graph Coloring problem( Chromatic number cannot be less than 4) using Backtracking Approach.

ASSIGNMENT - 8

19.Program to implement Hamiltonian Cycle problem using Backtracking Approach.

20.Program to implement 15-Puzzle problem using Branch and Bound Approach ( LC Method ).

DESIGN & ANALYSIS OF ALGORITHM

CS-501/IT501

MODELS OF COMPUTATION: (2)RAM, TM etc. time and space complexity

Page 33: Final Algorithm lab manual.doc

ASYMPTOTIC NOTATION: (5)Big-O, omega, theta etc.; finding time complexity of well known algorithms like- Insertion sort, Heap sort etc.

ALGORITHM DESIGN TECHNIQUES: (8)Recursion Definition, Recurrence relation, Substitution Method, Recursion tree, Master’s Theorem, Generating function, Use, Limitations, Examples: Tower of Hanoi problem. Tail Recursion.

DIVIDE AND CONQUER: (6)Basic method, use, Examples: Binary Search, Merge sort, Quick Sort, Selection algorithm, Strassen’s Matrix multiplication.

GREEDY METHOD: (7) Basic method, use, Examples: Knapsack problem, Job sequencing with deadlines, minimum spanning tree(Prim's and Kruskal's algorithms), Dijkstra algorithm.

DYNAMIC PROGRAMMING: (4) Basic method, use, Examples: matrix-chain multiplication, All pair shortest paths, single-source shortest path, Traveling Salesman problem.

BACKTRACKING: (5) (1ST INTERNAL TEST)Basic method, use, Examples: Eight queens problem, Graph coloring problem, Hamiltonian problem.

BRANCH AND BOUND: (2)Basic method, use, Examples: The 15-puzzle problem, LC Branch & Bound.

PROPERTIES OF GRAPHS AND GRAPH TRAVERSAL ALGORITHMS: (1)BFS and DFS

DISJOINT SET MANIPULATION : (2)Set manipulation algorithm like UNION-FIND, union by rank, Path compression.

LOWER BOUND THEORY: (2)Bounds on sorting techniques using partial and total orders.

NOTION OF NP-COMPLETENESS: (2)

Page 34: Final Algorithm lab manual.doc

P class, NP-hard class, NP-complete class, Circuit Satisfiability problem, Clique Decision Problem.

MATRIX MANIPULATION ALGORITHMS: (2)Different types of algorithms and solution of simultaneous equations, DFT & FFT algorithm; integer Multiplication Schemes.

APPROXIMATION ALGORITHMS: (2) (2ND INTERNAL TEST)

Necessity of approximation scheme, performance guarantee, Polynomial time approximation schemes: 0/1 Knapsack problem.

----------------------------------------------------------------------------------------------------------INTRODUCTION ABOUT LAB

----------------------------------------------------------------------------------------------------------

There are 39 systems (Compaq Presario) installed in this Lab.

Their configurations are as follows:

Page 35: Final Algorithm lab manual.doc

Processor : PENTIUM 4TM 2.4 GHz

RAM : 1 GB

Hard Disk : 40 GB

Mouse : Optical Mouse

Network Interface card : Present

Software

Operating System : Windows XP Software installed : Turbo C++ LAN connection in client machines. Systems are provided for student in the 1:1 ratio. Systems are assigned numbers and same system is allotted for students

when they do the lab.

STANDARD OPERATING PROCEDURE-SOP------------------------------------------------------------------------------------------------------

----------------

a) Explanation on today’s experiment by the concerned faculty using Pencil-Board/ PPT covering the following aspects:

Page 36: Final Algorithm lab manual.doc

1. Name of the experiment/ Aim:2. Algorithm:3. Result / Output:

b) Writing the source program by the studentsc) Compiling and execution of the programd) Explaining the reason of error (if any) in the student programs by the

concerned faculty member and technical assistant.e) Correcting errors in the program by the faculty member and technical

assistant.f) Viva Voce on the concerned program by the faculty.

Writing of the experiment in the Laboratory Note Book:Students have to write today’s experiment in the Observation book as per the following format:

a) Name of the experiment/ Aimb) Algorithmc) Source Programd) Input/ Outpute) Result for different data setsf) Signature of the faculty

GUIDELINES TO STUDENTS

Page 37: Final Algorithm lab manual.doc

Equipment in the lab for the use of student community. Students need to maintain a proper decorum in the computer lab. Students must use the equipment with care. Any damage is caused is punishable.

Students are required to carry their observation/ programs book with complete exercises while entering the lab

Students are supposed to occupy the machines allotted to them and are not supposed to talk or make noise in the lab. The allocation is put in the lab register.

Lab can be used in free time / lunch hours by the students who need to use the systems should take prior permission from the lab in-charge.

Lab records need to be submitted on or before date of submission.

Students are not supposed to use floppy disks/CD.

Students can take their work with pen drive or any other removable disk at the end of the lab.