INTERNAL ASSESSMENT TEST 2 Date : 30/3/15 Max Marks : 50 Name of faculty : Sabeeha Sultana Subject & Code : ADA(13MCA41) Answer any five full question: 1.Illustrate Mergesort for the dataset 8,3,2,9,7,1,5,4. Also analyze the time complexity for the best, average and worst case. General divide & conquer recurrence: An instance of size n can be divided into b instances of size n/b, with a of them needing to be solved. [ a _ 1, b > 1]. Assume size n is a power of b. The recurrence for the running time T(n) is as follows: T(n) = at(n/b) + f(n) where: f(n) a function that accounts for the time spent on dividing the problem into smaller ones and on combining their solutions Therefore, the order of growth of T(n) depends on the values of the constants a & b and the order of growth of the function f(n). Master theorem Theorem: If f(n) (nd) with d _ 0 in recurrence equation T(n) = at(n/b) + f(n), then _ (nd) if a < bd T(n) = _ (ndlog n) if a = bd _ (nlog b a ) if a > bd Example: Let T(n) = 2T(n/2) + 1, solve using master theorem. Solution: Here: a = 2 b = 2 f(n) = _(1) d = 0 Therefore: a > bd i.e., 2 > 20 Case 3 of master theorem holds good. Therefore: T(n) (nlog b a ) (nlogn)
ALGORITHM Mergesort ( A[0 n-1] ) //sorts array A by recursive mergesort //i/p: array A //o/p: sorted array A in ascending order if n > 1 copy A[0 (n/2-1)] to B[0 (n/2-1)] copy A[n/2 n -1)] to C[0 (n/2-1)] Mergesort ( B[0 (n/2-1)] ) Mergesort ( C[0 (n/2-1)] ) Merge ( B, C, A ) ALGORITHM Merge ( B[0 p-1], C[0 q-1], A[0 p+q-1] ) //merges two sorted arrays into one sorted array //i/p: arrays B, C, both sorted //o/p: Sorted array A of elements from B & C i _ 0 j _ 0 k _ 0 while i < p and j < q do if B[i] _ C[j] A[k] _ B[i] i _ i + 1 else A[k] _ C[j] j _ j + 1 k _ k + 1 if i == p copy C [ j q-1 ] to A [ k (p+q-1) ] else copy B [ i p-1 ] to A [ k (p+q-1) ] 2.What is input enhancement technique? Apply the technique to sort the array 25, 45, 10, 20, 50, 15 using comparison counting. Write an algorithm to implement the same. (10 Marks) 3. Write Horspool's algorithm for string matching. Explain it with following inputs Text : THIS IS A DEMO FOR STRING MATCHING Patten : DEMO 4.a.Construct the Huffman code for the following data: (10 Marks) (05 Marks) Character A B C D _ Probability 0.4 0.1 0.2 0.15 0.15 b.encode the text ABACABAD using the code of question a. (05 Marks) Huffman Trees
Some useful definitions: Code word: Encoding a text that comprises n characters from some alphabet by assigning to each of the text s characters some sequence of bits. This bits sequence is called code word Fixed length encoding: Assigns to each character a bit string of the same length. Variable length encoding: Assigns code words of different lengths to different characters. Problem: How can we tell how many bits of an encoded text represent ith character? We can use prefix free codes Prefix free code: In Prefix free code, no codeword is a prefix of a codeword of another character. Binary prefix code : o The characters are associated with the leaves of a binary tree. o All left edges are labeled 0 o All right edges are labeled 1 o Codeword of a character is obtained by recording the labels on the simple path from the root to the character s leaf. o Since, there is no simple path to a leaf that continues to another leaf, no codeword can be a prefix of another codeword Huffman algorithm: Constructs binary prefix code tree By David A Huffman in 1951. Huffman s algorithm achieves data compression by finding the best variable length binary encoding scheme for the symbols that occur in the file to be compressed. Huffman coding uses frequencies of the symbols in the string to build a variable rate prefix code o Each symbol is mapped to a binary string o More frequent symbols have shorter codes o No code is a prefix of another code Huffman Codes for Data Compression achieves 20-90% Compression 13 Construction: Step 1: Initialize n one-node trees and label them with the characters of the alphabet. Record the frequency of each character in its tree s root to indicate the tree s weight. (More generally the weight of a tree will be equal to the sum of the frequencies in the tree s leaves) Step 2: Repeat the following operation until a single tree is obtained. Find two trees with smallest weight. Make them the left and right sub-tree of a new tree and record the sum of their weights in the root of the new tree as its weight Example: Construct a Huffman code for the following data: Character A B C D - probability 0.4 0.1 0.2 0.15 0.15
Encode the text ABACABAD using the code. Decode the text whose encoding is 100010111001010 Algorithm STOPS as SINGLE TREE is obtained Code words are: Character A B C D - probability 0.4 0.1 0.2 0.15 0.15 Code word 0 100 111 101 110 Encoded text for ABACABAD using the code words: 0100011101000110 Decoded text for encoded text 100010111001010 is: BAD-ADA Compute compression ratio: Bits per character = Codeword length * Frequency = ( 1 * 0.4 ) + ( 3 * 0.1) + ( 3 * 0.2 ) + ( 3 * 0.15 ) + ( 3 * 0.15 ) = 2.20 Compression ratio is = ( 3 2.20 )/ 3. 100% = 26.6% 6.Apply the DFS and BFS based algorithms to solve for the following digraph. Depth-first search (DFS) Description: DFS starts visiting vertices of a graph at an arbitrary vertex by marking it as visited. It visits graph s vertices by always moving away from last visited vertex to an unvisited one, backtracks if no adjacent unvisited vertex is available. Is a recursive algorithm, it uses a stack A vertex is pushed onto the stack when it s reached for the first time A vertex is popped off the stack when it becomes a dead end, i.e., when there is no adjacent unvisited vertex Redraws graph in tree-like fashion (with tree edges and back edges for undirected graph) Algorithm: ALGORITHM DFS (G) //implements DFS traversal of a given graph //i/p: Graph G = { V, E} //o/p: DFS tree Mark each vertex in V with 0 as a mark of being unvisited count _ 0 for each vertex v in V do if v is marked with 0 dfs(v) dfs(v) count _ count + 1 mark v with count for each vertex w in V adjacent to v do if w is marked with 0 dfs(w)
Algorithm: ALGORITHM BFS (G) //implements BFS traversal of a given graph //i/p: Graph G = { V, E} //o/p: BFS tree/forest Mark each vertex in V with 0 as a mark of being unvisited count _ 0 for each vertex v in V do if v is marked with 0 bfs(v) bfs(v) count _ count + 1 mark v with count and initialize a queue with v while the queue is NOT empty do for each vertex w in V adjacent to front s vertex v do if w is marked with 0 count _ count + 1 mark w with count add w to the queue remove vertex v from the front of the queue 7.Generate all permutations of{1,2,3,4} by using Johnson-Trotter algorithm. Solution:Refer the assignment book for the solution. 8.Verify the formulas underlying Strassen s algorithm for multiplying 2-by-2 matrices. Find the time complexity of this method. Strassen s matrix multiplication using Divide & Conquer technique: Description : Strassen s algorithm is used for matrix multiplication. It is asymptotically faster than the standard matrix multiplication algorithm ALGORITHM using Divide & Conquer method: Let A & B be two square matrices. C= A * B We have, C00 C01 = A00 A01 * B00 B01 C10 C11 = A10 A11 * B10 B11 = M1 + M4 M5 + M7 M8 + M5 M8 + M5
M2 + M4 M2 + M4 M1 + M3 M2 + M6 Where: M1 = (A00 + A11) * (B00 + B11) M2 = (A10 + A11) * B00 M3 = A00 * (B01 B11) M4 = A11 * (B10 B00) M5 = (A00 + A01) * B11 M6 = (A10 A00) * (B00 + B01) M7 = (A01 A11) * (B10 + B11) Analysis: Input size: n order of square matrix. Basic operation: o Multiplication (7) o Addition (18) o Subtraction (4) No best, worst, average case Let M(n) be the number of multiplication s made by the algorithm, Therefore we have: M (n) = 7 M(n/2) for n > 1 M (1) = 1 Assume n = 2k M (2k) = 7 M(2k-1) = 7 [7 M(2k-2)] = 72 M(2k-2) = 7i M(2k-i) When i=k = 7k M(2k-k) = 7k K = log2n, we have M (n) = 7 log 2 n = n log 2 7 _ n2.807