midsol16

# 45 marks compute the time complexity of the above

• 9
• 100% (2) 2 out of 2 people found this document helpful

This preview shows pages 4–6. Sign up to view the full content.

[4.5 marks] Compute the time complexity of the above algorithm in the worst case. You need to explain how you computed the time complexity. [0.5 marks] Compute the order (“bigh Oh”) of the time complexity. The worst case for the algorithm is when the input tree is well balanced as then both recursive calls need to be made per node, and hence, the algorithm does not terminate early. Ignoring recursive calls, the algorithm performs a constant number c 1 of operations when the node being visited is a leaf, and it performs a constant number c 2 of operations when the node is internal. The algorithm performs a preorder traversal of the tree, so the algorithm makes one recursive call per node. Therefore, the total number of operations performed by the algorithm is c 1 × number of leaves + c 2 × number of internal nodes = c 1 n +1 2 + c 2 n 1 2 . Ignoring constant factors in the time complexity, we see that the complexity is O ( n ). 5

This preview has intentionally blurred sections. Sign up to view the full version.

11. [14 marks] Given an array A storing m integer values, and an array B storing n integer values, write in pseudocode an algorithm subarray ( A,B,m,n ) that returns the value true if A is a sub-array of B and it returns false otherwise. A is a sub-array of B iff there is a position 0 j n - m such that A [ i ] = B [ i + j ] for all i = 0 , 1 ,...m - 1. For example, for the arrays A and B shown below the algorithm must return the value true , but for the arrays A and B , the algorithm must return the value false . A 8 4 7 B 1 4 7 5 8 4 7 1 5 5 A 1 5 8 Algorithm subarray( A,B,m,n ) Input: Array A storing n integer values and array B storing m n integer values. Output true if A is a subarray of B ; false otherwise. for i B 0 to n - m do { i A 0 while ( i A <m ) and ( A [ i A ] = B [ i A + i B ]) do i A i A + 1 if i A = m then return true } return false 12. [2 marks] Explain what the worst case for the algorithm is. [2.5 marks] Compute the worst case time complexity of the above algorithm as a function of m and n . You need to explain how you computed the time complexity. [0.5 marks] Compute the order (“bigh Oh”) of the time complexity. The worst case for the algorithm is when A is not a subarray of B and every value in A , except the last one, is in B . For example, the first m - 1 values in A could be all equal to 1 and the last one could be 2, while all values in B are 1. This ensures that the while loop performs the maximum number of iterations and the algorithm does not terminate early. Each iteration of the while loop performs a constant number c 1 of operations and the loop is repeated m - 1 times in the worst case, as described above, so the number of operations performed by this loop is c 1 ( m - 1).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern