 # C analysis of s trassens algorit hm 4 marks tn 7 tn2

• 233

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 37 - 41 out of 233 pages.

c.Analysis of Strassen’s Algorithm4 marksT(n) =7 * T(n/2) +Θ(n2)a=7,b=2=nlg7
37case 1 of Master theoremT(n) =Θ(nlg7) =O(n2.81)d.6 marksThe recursive relation isT(n) = 2T(n/2) +M(n), whereM(n) is linear inn.Using Master's TheoremSolution: a = 2, b = 2, c = 1a = bcCase 2HenceT(n) ε O(n log n)3 a. Recursion is the process of defining a problem (or the solution to a problem) in terms of (asimpler version of) itself. An algorithm is said to be recursive if the same algorithm is invoked inthe body.Recursion occurs where the definition of an entity refers to the entity itself. Recursion can be directwhen an entity refers to itself directly or indirect when it refers to other entities which refers to it.A (Directly) recursive routine calls itself. Mutually recursive routines are an example of indirectrecursion. A (Directly) recursive data type contains pointers to instances of the data type.(3marks)b. The Fibonacci algorithm is given below;(21/2marks)long fibonacci (int n) {if( n == 1 || n == 2)return 1;elsereturn fibonacci(n1) + fibonacci(n2);}c.(6 marks)Tower of Hanoi is a puzzle that consists of three pegs and five disks. Figure 1 shows the startingposition of the puzzle. The goal is to reposition the stack of disks from peg A to peg C bymoving one disk at a time, and, never placing a larger disk on top of a smaller disk.Figure 1
38The algorithm for solving the puzzle is given by:Tower_Of_Hanoi(N, source, destination, helper):If N==1:Move disk from source to destinationElse:Tower_Of_Hanoi (N-1, source, destination, helper)Move disk from source to destinationTower_Of_Hanoi (N-1, helper, destination, source)The recurrence relation isT(n)= aif n = 1T(n)=2T(n - 1) +b if n > 1T(n)εO(2n)d.(6 marks)The time complexity of the Parallel-Product algorithm can be expressed asT(n) =T(n/2) + O(n/2)T(1) = 1We can solve it as:T(n) =T(n/2) + O(n/2)=(T(n/2^2) + O(n/2^2)) + O(n/2)=T(n/2^2) + O(n/2^2) + O(n/2)=T(n/2^3) + O(n/2^3) + O(n/2^2) + O(n/2)...=T(n/2^i) + O(n/2^i) +...+O(n/2^2) + O(n/2)=T(n/2^log n) + O(n/2^log n) +...+O(n/2^2) + O(n/2)// We stop the expansion at i = log n because2^log n =n //=T(1) + O(n/2^log n) +...+O(n/2^2) + O(n/2)=1 + O(n/2^log n +...+n/2^2 + n/2)=1 + O(n*(1/2^log n +...+1/2^2 + 1/2)=O(n)4a. Analysis of the linear search algorithmThis is O(n) in the worst case and Ω (1) in the best case.If the elements of an array A are distinct and query point q is indeed in the array then loopexecuted (n + 1) / 2 average number of times. On average (as well as the worst case), sequentialsearch takes θ(n) time.b.i. Yes, the binary search algorithm is an improvement over the sequential search algorithm. Thisis because it employs the Divide and Conquer approach which explains why it has increasedefficiency.ii. Condition for best casethe element is the first element in the array
39average casethe element is the middle element in the arrayworst casethe element is the last element in the arrayc.

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 233 pages?

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags
• • • 