Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
DESIGN AND ANALYSIS OF ALGORITHM CODE: PCCS 7203 4TH SEMESTER CSE DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING GANDHI ENGINEERING COLLEGE BPUT SYLLABUS Design and Analysis of Algorithms Lab 1. Using a stack of characters, convert an infix string to postfix string.(1 class) 2. Implement insertion, deletion, searching of a BST. (1 class) 3. (a) Implement binary search and linear search in a program (b) Implement a heap sort using a max heap. 4. (a) Implement DFS/ BFS for a connected graph. (b) Implement Dijkstra’s shortest path algorithm using BFS. 5. (a) Write a program to implement Huffman’s algorithm. (b) Implement MST using Kruskal/Prim algorithm. 6. (a) Write a program on Quick sort algorithm. (b) Write a program on merge sort algorithm. Take different input instances for both the algorithm and show the running time. 7. Implement Strassen’s matrix multiplication algorithm. 8. Write down a program to find out a solution for 0 / 1 Knapsack problem. 9. Using dynamic programming implement LCS. 10. (a) Find out the solution to the N-Queen problem. (b) Implement back tracking using game trees. Lab Objective 1. To study paradigms and approaches used to analyze and design algorithms and to appreciate the impact of algorithm design in practice. 2. It also ensures that students understand how the worst-case time complexity of an algorithm is defined, how asymptotic notation is used to provide a rough classification of algorithms, 3. How a number of algorithms exists for fundamental problems in computer science and engineering work and compare with one another, and how there are still some problems for which it is unknown whether there exist efficient algorithms, and how to design efficient algorithms. 4. Use different computational models (e.g., divide-and-conquer), order notation and various complexity measures (e.g., running time, disk space) to analyze the complexity/performance of different algorithms. 5. Understand the difference between the lower and upper bounds of various problems and their importance in deciding the optimality of an algorithm. 6. Use various techniques for efficient algorithm design (divide-and-conquer, greedy, and dynamic algorithms) and be able to apply them while designing algorithms. 7. Differentiate between various algorithms for sorting (e.g., insertion, merge, quick-sort, and heap sort), searching (e.g., linear and binary search), and selection (e.g., min, max) and when to use them. Lab Outcome 1. The students will learn to write, compiling, execute and analyze program. 2. The student will be able to create algorithm based on dynamic programming and greedy approach etc. 3. The student will learn to analyze program and able to compute the upper and lower bound. 4. The student will be able to solve various problem of searching, sorting algorithm. 5. The student will learn the concept of matrix chain multiplication, LCS, Graph coloring etc. Lab Mapping Contribution of Courses to Program Outcomes: (mapping) Program Outcomes TYPE Modules COURSE NUMBER & TITLE a b c d e f g h i j k LAB Number of courses contributing strongly to each program outcome Type: LAB - - Strong contribution - Average contribution - Some contribution - No contribution List of Experiment 1. Experiment 1 1.1 Write a program factorial of a given number using recursion. 1.2 Write a program to add two numbers multiply two numbers using recursion. 1.3 Write a program to print Fibonacci series up to a given number of terms using recursion. 2. Experiment 2 2.1 Write a program converts an infix string to postfix string using a stack of character. 3. Experiment 3 3.1 Write a program to implement insertion, deletion, searching operations on a BST. 4. Experiment 4 4.1 Write a program to search a given number from an array using Binary search and Linear search. 5. Experiment 5 5.1 Write a program to sort an array of integers using Quick sort algorithm. 6. Experiment 6 6.1 Write a program to sort an array of integers using Merge sort algorithm. 7. Experiment 7 7.1 Write a program to sort an array of integers in ascending order using Heap sort algorithm . 8. Experiment 8 8.1 Write a program to find the LCS between two given strings. 8.2 Write a program to print the optimal parenthesisation of given five matrices using CMM. 9. Experiment 9 9.1 Write a program to find the coded version of the string “I LIKE ROSE” using Huffman’s algorithm. 9.2 Write down a program to find out an optimal solution for 0 / 1 Knapsack problem for n objects with their given weight and profit. 10. Experiment 10 10.1 Write a program to implement minimum spanning tree using Kruskal/Prim algorithm. 11. Experiment 11 11.1 Write a program to implement DFS/ BFS for a given connected graph. 11.2 Write a program to implement Dijkstra shortest path algorithm using BFS. List of Experiment Beyond Syllabus 1. Experiment 1 1.1 Write a program to find shortest path using FLOYD Warshall algorithm. 2. Experiment 2 2.1 Write a program to find the optimal sequence of given n activities with start time and finish time using Activity selection problem. 3. Experiment 3 3.1 Write a program to implement Strassen’s matrix multiplication algorithm. INTRODUCTION TO DESIGN AND ANALYSIS OF ALGORITHM An algorithm takes some inputs, carries out a number of effective steps in a finite amount of time and produces some output. An algorithm must have the following properties: Input(s) Output(s) : An algorithm must have one (1) or more pre-specified input(s). : An algorithm must have one (1) or more output(s). Definiteness : Each steps of an algorithm must define clearly (i.e. without any confusion or Contradiction or Ambiguity). Finiteness time. : Each steps of an algorithm must be terminated in finite (tolerable) amount of Effectiveness : Any calculations performed / Decision taken by a computer or any electronic machine with respect to an algorithm should be computable by the human beings with pencil and paper although time may be longer. 1. Algorithm is a branch of computer science that consists of designing and analysing computer algorithms, algorithm describes by means of pseudo languages. 2. The analysis deals with performance evaluation i.e complexity analysis. The complexity of an algorithm is a function g(n) that gives the upper bound of the number of operation i.e running time performed by an algorithm when the input size is n. The complexity is of three type. i) Worst case time complexity:-The running time for given size input will be the maximum number of operations over all problem instances for a given size. ii) Average case time complexity:-The running time for given size input will be the average number of operations over all problem instances for a given size. iii) Best case time complexity:-The running time for given size input will be the minimum number of operations over all problem instances for a given size. LAB 1 Aim:- a)Write a program factorial of a given number using recursion. b) Write a program to add two numbers , multiply two numbers using recursion. c)Write a program to print fibonacci series upto a given number of terms using recursion. Theory:A function that calls itself is known as recursive function and this technique is known as recursion. In lab 1 we are discussing recursion, as many algorithms require the concept of recursion technique. Pseudo Code a) Factorial of a given number. A factorial is product of all the number from 1 to the user specified number. We have to express factorial in a recursive form using the steps i) ii) factorial(num) = num*factorial(num-1) if num=0 or num=1 , return 1 (recursive definition of factorial) (termination step) int factorial (int num) { if (num==0!! num==1) return 1; else return (num *factorial(num-1)); } b) Addition of two numbers We have to express addition in a recursive form using the steps i) ii) add(m,n) = add(m,n-1)+1 (recursive definition of addition) if n=0 , return m (termination step) int add (int m , int n) { if (n==0) return m; else return (add(m,n-1)+1); } Multiplication of two numbers. We have to express Multiplication in a recursive form using the steps i) ii) mult(m,n) = m+mult(m,n-1) (recursive definition of Multiplication) if n=1 , return m (termination step) int mult (int m , int n) { if (n==1) return m; else return (m+mult(m,n-1)); } c) Fibonacci series. We have to express Fibonacci series in a recursive form using the steps i) ii) fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) fibonacci) if n=1 , return 1 and if n=0 return 0 (recursive definition of (termination step) int fibonacci (int n) { if (n==0) return 0; else if (n==1) return 1; else return (fibonacci ( n-1)+ fibonacci ( n-2)); } Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input: a) Find the factorial of a number 10. b) Print Fibonacci series up to 10 numbers. Output:Invalid Input: a) Find the factorial of a number 100. Output:Sample Viva Questions:i) What is algorithm? ii) What are the properties of algorithm? iii) What is recursion? iv) Why is the requirement of recursion? v) What kind of problem solve by algorithm? LAB 2 Aim:- Write a program to convert an infix string to postfix string using a stack of character. Theory:It uses a stack. When an operand is read, Push into stack. When an operator is read. o Pop until the top of the stack has an element of lower precedence. o Then push it. When ) is found, pop until we find the matching ( ( has the lowest precedence when in the stack. but has the highest precedence when in the input. When we reach the end of input, pop until the stack is empty. Infix notation Postfix notation a+ b a b+ (a +b)*c a b+ c* Analysis:- The complexity of Postfix is Θ(n), where n is the number of tokens in the expression is the number of tokens in the expression. PSEUDO CODE infixtopostfix(istr) //Here istr in infix string,s is stack variable,ostr is postfixstring begin i=0 j=0 stack s s.top=-1 while(istr[i]!='\0') do x=istr[i] if(x='\t'||x=' ') continue else if(x=='(') push(&s,x) else if(isalpha(x)||isdigit(x)) ostr[j]=x j=j+1 else if(x==')') while(!isempty(&s)&&peek(&s)!='(') do ostr[j]=pop(&s) j=j+1 enddo opr=pop(&s) else if(x=='^'||x=='*'||x=='/'||x=='+'||x=='-'||x=='%') while(!isempty(&s)&&prec(peek(&s))>=prec(x)) do ostr[j]=pop(&s) j=j+1 enddo push(&s,x) endif i=i+1 end do while(s.top!=-1) do ostr[j]=pop(&s) j=j+1 end do ostr[j]='\0' print "postfix:",ostr end prec(ch) begin if(ch=='^') return 3; else if(ch=='*'||ch=='/'||ch=='%') return 2; else if(ch=='+'||ch=='-') return 1; else return 0 endif end Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Convert the infix expression to postfix expression (a+b)*c-d/a . Output: Invalid Input:- Convert the infix expression to postfix expression (a+b)*/c-d/a . Output: Sample Viva Questions:i) What is stack? ii) Which principle is used in stack? iii)Write the postfix expression of (a+b)*b iv) Give one example of infix and postfix notation. v) Write PUSH and POP functions. LAB 3 Aim:- Write a program to implement insertion, deletion, searching operations on a BST. Theory:A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key and 1. All keys in the left sub-tree are less than the key in the root node. 2. All keys in the right sub-tree are greater than or equal to the key in the root node. 3. The left and right sub-trees of the root are binary search trees. Searching: Start from the root node. i) If the tree is null- Key is not present. ii) If the key equal to the root-return the node. iii)If the key is less than that of the root-search from left sub-tree. iv) If the key is greater than that of the root-search from right sub-tree. This process is repeated until the key is found or the remaining sub-tree is null. If the searched key is not found before a null sub-tree is reached, then the item must not be present in the tree. Analysis:- The height h is O(n) in the worst case and O(1) in best case and O(log n) in the Average case. Pseudo Code bstnode search(bstnode t,int key) { if ((t==NULL)||(key== t->info)) return (t); if (key < t->info) return search(t->left, key) ; else return search(t->right, key) ; } Insertion:i)if the key is not equal to that of the root-search the left or right sub-tree. ii) Add the new key-value pair as its right or left child, depending on the node's key. Analysis:- The height h is O(n) in the worst case and O(log n) in the best case. PSEUDO CODE bstnode insert(bstnode t,int x) { if(t==NULL) { t=(bstnode)malloc(sizeof(struct node)); if(t==NULL) printf("\n out of memory space"); else { t->info=x; t->left=t->right=NULL; } } else if(x<t->info) t->left=insert(t->left,x); else if(x>t->info) t->right=insert(t->right,x); return t; } Deletion:i) Deleting a node with no children: simply remove the node from the tree. ii) Deleting a node with one child: remove the node and replace it with its child. iii) Deleting a node with two children: call the node to be deleted N. Do not delete N. Analysis:- The height h is O(n) in the worst case and O(log n) in the best case. PSEUDO CODE bstnode delet(bstnode t,int x) { bstnode temp; if(t==NULL) printf("\n the element is not found"); else if(x<t->info) t->left=delet(t->left,x); else if(x>t->info) t->right=delet(t->right,x) ; else if(t->left&&t->right) { temp=findmin(t->right); t->info=temp->info; t->right=delet(t->right,t->info); } else { temp=t; if(t->left==NULL) t=t->right; else if(t->right==NULL) t=t->left; free(temp); } return t; } Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input: Construct the BST of the input 3, 30, 10, 20, 40, 15 Sample Viva Questions:i) What is binary tree? ii) What is Binary search tree? iii) What is height balanced tree? iv) Write the logic of traversing of BST. v) Write the logic of inserting the node in BST. LAB 4 Aim:- Write a program to search a given number from an array using linear search and Binary search. Theory:Linear search is a search algorithm, also known as sequential search that is suitable for searching a list of data for a particular value. It operates by checking every element of a list one at a time in sequence until a match is found. Analysis:- Linear search runs in O(n). If the data are distributed randomly, the expected number of comparisons that will be necessary is: 𝑛 (𝑛 + 1)/(𝑘 + 1) 𝑘=0 1≤𝑘≤𝑛 where n is the number of elements in the list and k is the number of times that the value being searched for appears in the list. The best case is that the value is equal to the first element tested, in which case only 1 comparison is needed. The worst case is that the value is not in the list (or it appears only once at the end of the list), in which case n comparisons are needed. Input :- A sequence of n numbers A = [a1, . . . , an] and a value searching element item. Output:- An index i such that v = A[i] or the special value NIL if v does not appear in A LINEAR-SEARCH (A, item) for i ← 1 to length[A] do if A[i] = item then return i return NIL Binary search: A binary search algorithm (or binary chop) is a technique for locating a particular value in a sorted list. The method makes progressively better guesses, and closes in on the location of the sought value by selecting the middle element in the span , comparing its value to the target value, and determining if it is greater than, less than, or equal to the target value. A guessed index whose value turns out to be too high becomes the new upper bound of the span, and if its value is too low that index becomes the new lower bound. Pursuing this strategy iteratively, the method reduces the search span by a factor of two each time, and soon finds the target value or else determines that it is not in the list at all. A binary search is used the design technique divide and conquer search algorithm. Analysis:- Generally Recursive algorithm the algorithm the time complexity is T(n)=aT(n/b) + f(n) Here a=1 and b=2 f(n)=1 using master method case I T(n) = θ(log n). Binary Search can be accomplished in logarithmic time in the worst case i.e. T(n) = θ(log n). This version of the binary search takes logarithmic time in the best case. Input: Array A indexed from l to u with items sorted from smallest to largest and item x. Output: Bsearch returns a location of item x in array A; if x is not found, -1 is returned. PSEUDO CODE bsearch(a, l,u, x) { if (l > u) then return (-1); else { m = [(l + u)/2]; if (a[m] == x) then return(m); else if (a[m] < x) then return(bsearch(a, m+1, u, x)); else return(bsearch(a, l, m-1, x)); } } Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Input an array of 10 sorted elements and find out the time complexity for searching the biggest element in the array. Output:- Valid Input:- Input an array of 10 sorted elements and make sure to add few repeating value. What changes you will make in program such as to find all the occurance of given number. Output:- Sample Viva Questions:i) What is time complexity of linear search? ii) What is the recurrence relation of Binary search? iii) What is the requirement of Binary search? iv) What is divide and conquer design technique? v) Write the worst case time complexity of linear search and binary search? LAB 5 Aim:- Write a program to sort an array of integers using Quick sort algorithm. Theory:Quick sort is a divide and conquer algorithm. Quick sort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quick sort can then recursively sort the sub-arrays. The steps are: 1. Pick an element, called a pivot, from the array. 2. Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 3. Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values. Analysis: Best Case The best thing that could happen in Quick sort would be that each partitioning stage divides the array exactly in half. If the procedure 'Partition' produces two regions of size n/2. the recurrence relation is then T(n) = T(n/2) + T(n/2) + ϴ (n) = 2T(n/2) + ϴ (n) And from case 2 of Master theorem T(n) = ϴ (n lg n) Worst case Partitioning The worst-case occurs if given array A[1 . . n] is already sorted. The PARTITION (A, p, r) call always return p so successive calls to partition will split arrays of length n, n-1, n-2, . . . , 2 and running time proportional to n + (n-1) + (n-2) + . . . + 2 = [(n+2)(n-1)]/2 = ϴ (n2). Average case Partitioning The running time of quick sort depends on whether partition is balanced or unbalanced, which in turn depends on which elements of an array to be sorted are used for partitioning. A very good partition splits an array up into two equal sized arrays. A bad partition, on other hand, splits an array up into two arrays of very different sizes. T(n) = ϴ (n lg n) PSEUDO CODE quick_sort(a, p, r) { if(p<r) { q=partition(a,p,r); quick_sort(a,p,r-1); quick_sort(a,q+1,r); } } partition(a,p, r) { pivot=a[r]; i=p-1; for(j=p;j<=r-1;j++) { if(a[j]<=pivot) { i++; exchange( a[i]> a[j] ) } } exchange( a[i+1]>a[r]) return (i+1); } Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Arrange the given element 1,2,3,4,5,6 and write down the time complexity. Output:Sample Viva Questions:i) What is divide and conquer design technique? ii) Which element is selected for pivot element? iii) Write the recursive relation of quick sort. iv) Write the worst case time complexity of quick sort. v) Write the time complexity if the elements are sorted in ascending and descending order. LAB 6 Aim:- Write a program to sort an array of integers using Merge sort algorithm. Theory:Merge-Sort is a Divide and Conquer algorithm. Divide Step: If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r] into two sub-arrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r]. That is, q is the halfway point of A[p .. r]. Conquer Step: Conquer by recursively sorting the two sub-arrays A[p .. q] and A[q + 1 .. r]. Combine Step: Combine the elements back in A[p .. r] by merging the two sorted sub-arrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r). Analysis:Running time T(n) of Merge Sort: Divide: computing the middle takes ϴ (1) Conquer: solving 2 sub problems takes 2T(n/2) Combine: merging n elements takes ϴ (n) Total: T(n) = ϴ (1) if n = 1 T(n) = 2T(n/2) + ϴ (n) if n > 1 T(n) = ϴ (n lg n) PSEUDO CODE MergeSort (a, p, r) // sort a[p..r] by divide & conquer { If( p < r) { then q (p+r)/2 MergeSort (a, p, q) MergeSort (a, q+1, r) Merge (a, p, q, r) // merges a[p..q] with a [q+1..r] } } void merge(a, low, mid, high) { int temp[50]; //array used for merging int i,j,k; i=low; h=low; //beginning of the first list j=mid+1; k=0; //beginning of the second list while(h<=mid && j <=high) //while elements in both lists { if(a[h]<a[j]) temp[k++]=a[h++]; else temp[k++]=a[j++]; } while(h<=mid) //copy remaining elements of the first list temp[k++]=a[h++]; while(j <=high) //copy remaining elements of the second list temp[k++]=a[j++]; //Transfer elements from temp[] back to a[] for(i=low;i<=high;i++) a[i]=temp[i]; } Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Arrange the given element 1,2,3,4,5,6 and write down the time complexity. Output:Sample Viva Questions:i) Which design technique is used in merge sort? ii) Write the recursive relation of merge sort. iii) Write the time complexity of merge sort. iv) What is divide and conquer technique? v) Explain merge sort in the input numbers 33 , 2 , 11 , 22 ,9 ? LAB 7 Aim:- Write a program to sort an array of integers in ascending order using Heap sort algorithm. Theory:The heap data structure is an array object that can be viewed as a complete binary tree . An array A that represents a heap is an object with two attributes: length[A], which is the number of elements in the array, and heap-size[A], the number of elements in the heap stored within array A. The root node is A[1] Node i is A[i] The parent of node i is A[i/2] The left child (l)of node i is A[2i] The right child( r) of node i is A[2i + 1] Heaps satisfy the heap property: for every node i other than the root A[PARENT(i)] A[i] max-heapify The largest element in a heap is stored at the root, and the sub trees rooted at a node contain smaller values than does the node itself. PSEUDO CODE buildmaxheap( a, n) { for(i=n/2;i>=1;i--) { max-heapif(a,i,n); } } max-heapify( a, i, n) { R,L,largest,t; L=2*i; R=2*i+1; if((L<=n) && (a[L]>a[i])) largest=L; else largest=i; if((R<=n) && (a[R]>a[largest])) largest=R; if(largest!=i) { exchange a[i] <=> a[largest] max-heapify(a,largest,n); } } heapsort( a, n) { buildmaxheap(a,n); for(i=n;i>=2;i--) { exchange a[1] <=> a[i] max-heapify(a,1,i-1); } } Analysis:Fixing up relationships between i, l, and r takes ϴ (1) time. The worst case: bottom row 1/2 full contains 2n/3 number of element. Time taken by Heapify() is T(n) T(2n/3) + ϴ (1) using case 2 of the Master Theorem, T(n) = O(lg n) . The function max-heapify calls O(n) times. The total running time is O(n lg n). The call to BuildHeap() takes O(n) time Each of the n - 1 calls to Heapify() takes O(lg n) time Thus the total time taken by HeapSort() = O(n) + (n - 1) O(lg n) = O(n) + O(n lg n) = O(n lg n) Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Construct the Max-heap using the elements 10,50,30,20,25,45 and arrange using heap sort. Output:Sample Viva Questions:i) What is heap? ii) What is max-heap? iii) Construct the heap using the input sequence 10,2,11,30,8. iv) Write time complexity of max-heapify. v) Arrange the input sequence 2, 11,3,8,1 using heapsort. LAB 8 Aim:- a) Write a program to find the LCS between two given strings. b)Write a program to print the optimal parenthesisation of given five matrices using CMM. Theory:Longest Common Subsequence Sequences x1,…,xn, and y1,…,ym LCS(i,j) = length of a longest common subsequence of x1,…,xi and y1,…,yj . if xi yj then LCS(i,j) = max (LCS(i-1,j),LCS(i,j-1)) xi and yj cannot both be in LCS if xi = yj then LCS(i,j) = 1 + LCS(i-1,j-1) . PSEUDOCODE void lcs_length(void) { m=strlen(x); n=strlen(y); for(i=0;i<=m;i++) c[i][0]=0; for(i=0;i<=n;i++) c[0][i]=0; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { if(x[i-1]==y[j-1]) { c[i][j]=c[i-1][j-1]+1; b[i][j]='c'; } else if(c[i-1][j]>=c[i][j-1]) { c[i][j]=c[i-1][j]; b[i][j]='u'; } else { c[i][j]=c[i][j-1]; b[i][j]='l'; } } print_lcs(m,n); } void print_lcs( i, j) { if(i==0 || j==0) return; if(b[i][j]=='c') { print_lcs(i-1,j-1); printf(" %c",x[i-1]); } else if(b[i][j]=='u') print_lcs(i-1,j); else print_lcs(i,j-1); } Analysis:- Time complexity of length LCS is O(nm) and print LCS is O(n+m). Matrix chain Multiplication: Given a sequence of matrices A1 A2…An , and dimensions p0 , p1, …pn where Ai is of dimension pi-1 x pi , determine multiplication sequence that minimizes the number of operations. Consider 3 matrices: A1 be 2 x 4, A2 be 4 x 3, and A3 be 3 x 2. Mult [((A1 A2)A3)] = (2x4x3) + (2x3x2) = 36 ---------(1) Mult [(A1 (A2A3 ))] = (4x3x2) + (2x4x2) = 40 ---------(2) Scalar multiplication is directly proportional to time complexity. So case 1 will select. Let Ai be pi−1 by pi . Let m[i, j] be the cost of computing Aij If the final multiplication for Aij is Aij = Aik Ak+1,j then m[i, j] = m[i, k] + m[k + 1, j] + pi−1 pk pj . PSEUDOCODE void matmultiply(void) { long int q; int k; for(i=n;i>0;i--) { for(j=i;j<=n;j++) { if(i==j) m[i][j]=0; else { for(k=i;k<j;k++) { q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]; if(q<m[i][j]) { m[i][j]=q; s[i][j]=k; } } } } } } Analysis:-Time complexity of matrix chain multiplication is T(n)=O(n3). Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Find an optimal parenthesization of Matrix-chain product whose sequence of dimension is <2,34,5,2,6,7,1>. Output:Sample Viva Questions:i) What is dynamic programming? ii) What is principle of optimality? iii) What is the application of LCS? iv) Write the recursive relation of LCS and MCM. v) Write the time complexity of LCS and MCM. LAB 9 Aim:- a)Write a program to find the coded version of the string “I LIKE ROSE” using Huffman’s algorithm. b)Write down a program to find out an optimal solution for 0 / 1 Knapsack problem for n objects with their given weight and profit. Theory:Huffman Coding Character Frequency Fixed-length coding Prefix coding a 60 00 0 b 5 01 110 c 30 10 10 d 5 11 111 To store 100 of these characters, (1) The fixed-length code requires (2) The prefix code uses only 100X2=200, 60X1+5X3+30X2+5X3=150 25% Saving. Prefix Code: A code is called a prefix (free) code if no codeword is a prefix of another one. Steps for Huffman code 1. Create a leaf node for each symbol and add it to the priority queue. 2. While there is more than one node in the queue: 1. Remove the node of highest priority (lowest probability) twice to get two nodes. 2. Create a new internal node with these two nodes as children and with probability equal to the sum of the two nodes' probabilities. 3. Add the new node to the queue. 3. The remaining node is the root node and the tree is complete. PSEUDOCODE Huffman-code(A) { n= | A|; Q= A; for i=1 to n-1 { z= new –node; left-child(z)=extract-min(Q); right-child(z)=extract-min(Q); f[z]=f[left-child(z)+ f[right-child(z)]; Insert( Q,z); } return extract-min(Q); } 0 / 1 Knapsack problem Theory There are n number of items are present , we need to fill the knapsack or bag choosing the items from the group of items in such a manner that the total weight of the chosen item does not exceed the total weight of the knapsack or bag. There are three number of categories to chose items from group of items i) The items which give maximum profit is taken first to the knapsack or bag then the next. ii) The items which has least weight is taken first to knapsack or bag then the next. iii) We have to find out the ratio between value and weight which gives the unit price. The item which gives maximum unit price is taken first knapsack or bag then the next. PSEUDOCODE Knapsack(m,n) // m Capacity of Knapsack // n number of items. { for i= 1 to n { X[i]=0; U=m; } for i= 1to n { If(w[i]>U) Break; X[i]=1; U=U-w[i]; } If (i<=n) X[i]= U/w[i]; } Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Encode the following string “ENGINEERING” . Output:- Sample Viva Questions:i) What is Greedy Approach? ii) What is the application of Huffman coding? iii) What is prefix encoding? iv) What is the difference between Greedy approach and Dynamic Programming? v) Write the time complexity of Huffman coding and Knapsack problem. LAB 10 Aim:- Write a program to implement minimum spanning tree using Kruskal/Prim algorithm. Theory:A spanning tree T of an undirected graph G is a sub-graph that includes all of the vertices of G that is a tree. A single graph can have many different spanning trees out of which one is minimum weight which is called minimum spanning tree. Kruskal’s algorithm: Start with no nodes or edges in the spanning tree, and repeatedly add the cheapest edge that does not create a cycle. PSEUDOCODE MST-KRUSKAL (G, w) { A=ɸ; for each vertex v ϵ G[V] Make-set (v) sort the edges of G: E into non-decreasing order by weight w for each edge (u , v) ϵ G[ E], taken in non-decreasing order by weight if Find-set (u)≠ Find-set(v) A = A U { (u, v )} Union(u, v) return A } Analysis: The running time is O (E log V). Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Compute the minimum cost spanning tree for the graph given below 10 1 8 1 2 11 18 Output:Sample Viva Questions:i) What is spanning tree? ii) What is minimum spanning tree? iii) What is the time complexity of Kruskal algorithm and Prim’s algorithm? iv) v) What is safe edge? What is the condition for checking cycle in spanning tree? LAB 11 Aim:- a) Write a program to implement DFS/ BFS for a given connected graph. b) Write a program to implement Dijkstra shortest path algorithm using BFS. Theory: Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root(selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking. Stack data structure is used in DFS. PSEUDOCODE DFS(G) { for each vertex u ϵ G[V] u[ color]= White u [Π]= NIL time = 0 for each vertex u ϵ G[V] if u[ color]== White DFS-VISIT (G, u) } DFS-VISIT (G, u) { time= time+1 / /white vertex u has just been discovered// u[ d] = time u[ color]=Gray for each v ϵ G[ Adj[u]] ///explore edge .u; // if ( v[color] == White) v[Π]= u DFS-VISIT( G,v) u[ color ]= Black //blacken u; it is finished// time= time+1 u[f]= time } Analysis:- Time complexity is O(V+E) Dijkstra shortest path algorithm Theory: Dijkstra’s algorithm solves the single-source shortest-paths problem on a weighted, directed graph G =(V, E) for the case in which all edge weights are nonnegative. It maintains a set S of vertices whose final shortest-path weights from the source s have already been determined. The algorithm repeatedly selects the vertex u ϵ V -- S with the minimum shortest-path estimate, adds u to S, and relaxes all edges leaving u. Here we implement min-priority queue Q of vertices with their d values. PSEUDOCODE DIJK STR A (G, w, s) { Initialize-single-source(G , S) S =ɸ Q = G[ V] while (Q≠ɸ); u = Extract-min (Q) S =S U {u} for each vertex v ϵ G[Adj[u]] RELA X (u, v, w) } RELAX(u,v,w ) { If( d[v]> d[u]+w(u,v)) d[v]= d[u]+w(u,v) Π[v]= u } Analysis:- Time complexity is O(V+E) Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input: Illustrate the DFS of the following graph 10 1 1 2 18 Output:Sample Viva Questions:i) What is Relax technique? ii) Is it applicable for negative weight? iii) Which design technique is used in Dijsktra Algorithm? iv) What is the time complexity of Dijsktra Algorithm? v) Which data structure is used in DFS and BFS? EXPERIMENT BEYOND SYLLABUS EXP. NO-1 Aim:- Write a program to find shortest path using Warshall algorithm. Theory: This problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph G = (V, E, w). There are no cycle with zero or negative cost. Dynamic programming solution to compute all sources shortest paths . A path exists between two vertices i, j, iff there is an edge from i to j; or there is a path from i to j going through intermediate vertices which are drawn from set { 1,2,3, ……} Construct matrices D(0), D(1), … D(k-1), D(k) … D(n) dij(k): weight of the shortest path from ui to uj with all intermediate vertices in Vk dij(0)=wij dij(k)=min (dij(k-1), dik(k-1)+ dkj(k-1)) for k≥1 PSEUDOCODE FLOYD-WARSHALL(w) { n= row[w] D(0)=w For k= 1 to n D(k)= dij(0) For i= 1 to n For j= 1 to n dij(k)= min (dij(k-1), dik(k-1)+ dkj(k-1)) return D(n) } Analysis:- Time complexity is O(n) . Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:Output:SampleViva Questions:i) ii) iii) Which design technique is used in FLOYD WARSHALL algorithm? What is the time complexity of FLOYD WARSHALL algorithm? How to find the weight matrix? EXP. NO-2 Aim:- Write a program to find the optimal sequence of given n activities with start time and finish time using Activity selection problem. Theory: In activity scheduling problem, we have to find out the optimum number of activities are going to be executed. Let there is a set S which indicates state of activities and Si and Fi indicates the start time and finish time of the activity. Tow activities are not overlap if start time of the next activity is greater than or equal to the finishing time of the previous activity. PSEUDOCODE Activity(s, f) { n= length(s); A= { }; I=1; for m = 2 to n { If(sm≥fi) { A= A U {am}; i= m; } return A; } Analysis: Time complexity is O(n logn). Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:- Schedule the activity A1 , A2 , A3 , A4, A5 with given starting time 1 , 2, 5, 6, 7 and finishing time 4,5,2,3,6 respectively. Output:Sample Viva Questions:i) Which design technique is used in activity scheduling problem? ii) What is the time complexity of activity scheduling problem? iii) What is the difference between Gready approach and dynamic programming? EXP. NO-3 Aim:- Write a program to implement Strassen’s matrix multiplication algorithm. Theory: Generally Strassen’s Method is not preferred for practical applications for following reasons. 1) The constants used in Strassen’s method are high and for a typical application 2) Because of the limited precision of computer arithmetic on non-integer values, larger errors accumulate in Strassen’s algorithm than in Naive Method 3)The sub matrices in recursion take extra space. Divide and Conquer Approach is used in this algorithm. PSEUDOCODE The following algorithm multiplies nxn matrices A and B: // Initialize C. for i = 1 to n for j = 1 to n for k = 1 to n C [i, j] += A[i, k] * B[k, j]; Analysis : Time complexity is O(n3). Program:- Implementation of algorithm using Programming language in C/ C++. Valid Input:Output:Sample Viva Questions:i) Which technique is used in the algorithm? ii) What is the time complexity the algorithm? iii) Write the recursive relation of the algorithm.