PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

Similar documents
DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Analysis of Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Section 7.3 Divide-and-Conquer Algorithms and Recurrence Relations. 0,a m. - Division into thirds gives m = 3

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

DESIGN AND ANALYSIS OF ALGORITHMS

CS Divide and Conquer

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Midterm solutions. n f 3 (n) = 3

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Lecture 3. Brute Force

( D. Θ n. ( ) f n ( ) D. Ο%

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Brute Force: Selection Sort

( ) n 3. n 2 ( ) D. Ο


( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

INSTITUTE OF AERONAUTICAL ENGINEERING

Practical Session 10 - Huffman code, Sort properties, QuickSort algorithm, Selection

DATA STRUCTURES AND ALGORITHMS

csci 210: Data Structures Graph Traversals

Lecture 4 CS781 February 3, 2011

COMP 251 Winter 2017 Online quizzes with answers

Union-Find Disjoint Set

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Outline. Graphs. Divide and Conquer.

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

INSTITUTE OF AERONAUTICAL ENGINEERING

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Test 1 Review Questions with Solutions

Text Compression through Huffman Coding. Terminology

Module 5 Graph Algorithms

Trees. Arash Rafiey. 20 October, 2015

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

DESIGN AND ANALYSIS OF ALGORITHMS

Inf 2B: Graphs, BFS, DFS

LECTURE 17 GRAPH TRAVERSALS

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

L.J. Institute of Engineering & Technology Semester: VIII (2016)

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CS Divide and Conquer

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Master Theorem, Introduction to Graphs

Divide-and-Conquer. Dr. Yingwu Zhu

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Understand graph terminology Implement graphs using

16 Greedy Algorithms

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

MergeSort. Algorithm : Design & Analysis [5]

UNIT-2 DIVIDE & CONQUER

CS 206 Introduction to Computer Science II

Programming II (CS300)

Thomas H. Cormen Charles E. Leiserson Ronald L. Rivest. Introduction to Algorithms

csci 210: Data Structures Graph Traversals

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Graph: representation and traversal

Graphs & Digraphs Tuesday, November 06, 2007

CS302 - Data Structures using C++

CS Data Structures and Algorithm Analysis

CS473 - Algorithms I

15 July, Huffman Trees. Heaps

Recitation 9. Prelim Review

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

END-TERM EXAMINATION

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

COMP251: Background. Jérôme Waldispühl School of Computer Science McGill University

CS171 Final Practice Exam

Algorithm Design and Analysis

CSci 231 Final Review

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Divide and Conquer 4-0

Graph Algorithms. Imran Rashid. Jan 16, University of Washington

Copyright 2000, Kevin Wayne 1

n 2 ( ) ( ) + n is in Θ n logn

CS61BL. Lecture 5: Graphs Sorting

Data Compression Algorithms

Transcription:

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