Sie sind auf Seite 1von 38

# 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); } }

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) A->C

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

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

## TOH(B,C,A,1) TOH(B,A,C,1) TOH(A,B,C,1) 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; a:=t; Adjust(a,1,i-1); } }

Algorithm Heapify(a,n) // Readjust the elements in a[1:n] to form a heap. { for i:= n / 2 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(jn) 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[ j / 2 ] := a[j]; j:=2j; } a[ j / 2 ] := item; }

4. BINARY SEARCH:
Let ai , 1in, 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 a j = x. if x is not in the list, then j is to be set to zero.

Algorithm BinSrch(a,i,l,x) //Given an array a[i:l] of elements in nondecreasing order, 1il, 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 := (i + l ) / 2 ; 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 := (low + high) / 2 ; 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:

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:=lowi:=lowj:=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)

// 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 method different from merge sort. In merge sort, midpoint into subarrays which were indipendently sort, the division into two subarrars is made so 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. } } to arrive at an efficient sorting the file a[1:n] was divideed at its sorted and later merged. In Quick that the sorted subarrays donot

Algorithm Partition(a,m,p)

// 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; }

## 7. STRASSENS MATRIX MULTIPLICATION:

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) 1kn For 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=2 k . 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 A21 Then A22 B11 B 12 B21 B22

C11 C 12 C 21 C 22
.(2)

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 cn for some constant c, the overall computing time T(n) of the resulting divide and conquer algorithm is given by the recurrence b T(n) if n 2
2 2

8 T(n) + cn

if n > 2

(4)

Where b and c ae constants. We can attempt to reformulate the equations for C ij 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 Cijs 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) C11 = P + S T + V C12 = R + T C21 = Q + S C22 = P + R Q + U .(6)
..(5)

## The resulting recurrence relation for T(n) is b T(n) if n 2

2

7 T(n) + cn

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

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):

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

capacity m. if a fraction xi , 0 xi1, 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 i n subject to wi xi m 1 i n ..(1) ..(2)

and 0 xi n , 1 i n .(3) the profits and weights are positive numbers. A feasible solution is any set ( x 1 , . . . , 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. PRIMS 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.

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. 1 10 1 2 14 16 6 25 5 22 4 24 7 18 3 12 6 25 5 22 4 7 10 2 14 16 3 12

28

## 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;

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. KRUSKALS ALGORITHM: Kruskals 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. Kruskals algorithm is a greedy algorithm, because at each step it adds to the forest an edge of least possible weight. Our implementation of Kruskals 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.

1 10 10 6 25 24 5 14 14 7 18

28 2 16 16 3 25 6

1 2

12

5 4

12 22

22

## Figure 2: A graph and its minimum cost spanning tree

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. DIJKSTRAS ALGORITHM: Dijkstras 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 graphs edge length. The length of the edge from v 1 to v2 is denoted by l(v1,v2), starting with an initial vertex called v 0. 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 iti). 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 hasnt. 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 v 0 along the shortest path found so far from v to v 0. Dijkstras Algorithm() { status[v0]:=1; dist[v0]:=0; next[v0]:=*; for(all vv0){ status[v]:=?; dist[v]:=l(v,v0); next[v]:=v0; } while(there are remaining ? vertices){

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 , 1in are the deadline n 1 , The jobs are ordered such that p p // . 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 :=J := 0; // initialize J :=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; }

DYNAMIC PROGRAMMING Dynamic 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) .

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 ] + pi1 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 Ai; else { print ( ; PRINT-OPTIMAL-PARENS(s, i, s[i, j ]) ; PRINT-OPTIMAL-PARENS(s, s[i, j ] + 1, j ) ; print ) ; } }

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 negativeweight 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 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 definition following the above discussion is given by
(0)

(k)

= wij . A recursive

dij

(k) =

## wij min{ dij

if k = 0; (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

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 . . . , where = and ij 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 has no intermediate vertices at all. Thus, ij
(0) (k) (0) (1) (n) (n) (k)

NIL i

## if i = j or wij = 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 ij
(k)

(k-1) (k-1)

if if

dij dij

(k-1)

## dik(k-1)+ dkj(k-1) > dik(k-1)+ dkj(k-1)

kj

(k-1)

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.

1 Row 1 2 3

Column 2 3

4 Q

Q 4 5 6 Q 7 8 Q Q Q 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.

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.

a 1

## 3 1 An 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) // x,,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 v 1 G and the vertices of G are visited in the order v 1, v2, . . . , vn+1 , then the edges (vi , vi+1) are in E, 1in, and the vi are distinct except for vi vn+1 , which are equal.

Algorithm Hamiltonian(k) // 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.

{ 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]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.

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 2 7 8 6 9 3 4 5 11 10 15 12 14 13

1 5 9 13 (a) An arrangement

2 6 10 14

3 7 11 15

4 8 12

## (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; 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 outputs 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 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 designsof 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 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 Strassens 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 methods Prims Algorithm : O(n*n) Kruskals Algorithm : O(e loge) 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 correctly awarded. 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.

## 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 Strassens 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 Prims Algorithm using Greedy Approach. 12.Program to implement Krushkals Algorithm using Greedy Approach.
ASSIGNMENT - 5

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

ASSIGNMENT - 6

## 15.Program to implement Programming Approach.

Matrix

Chain

Multiplication

using

Dynamic

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 Approach.

problem

using Backtracking

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 ASYMPTOTIC NOTATION: (5) Big-O, omega, theta etc.; finding time complexity of well known algorithms likeInsertion sort, Heap sort etc. ALGORITHM DESIGN TECHNIQUES: (8) Recursion Definition, Recurrence relation, Substitution Method, Recursion tree, Masters 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, Strassens 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) 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: Processor RAM Hard Disk Mouse Network Interface card : PENTIUM 4TM 2.4 GHz : 1 GB : 40 GB : Optical Mouse : 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 todays experiment by the concerned faculty using PencilBoard/ PPT covering the following aspects: 1. Name of the experiment/ Aim: 2. Algorithm: 3. Result / Output: b) Writing the source program by the students c) Compiling and execution of the program d) 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 todays experiment in the Observation book as per the following format: a) Name of the experiment/ Aim b) Algorithm c) Source Program d) Input/ Output e) Result for different data sets f) Signature of the faculty

GUIDELINES TO STUDENTS

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.