Capital University of Science and Technology Department of Computer Science CS 3163: Design and Analysis of Algorithms (3) : Fall 2020
Capital University of Science and Technology Department of Computer Science CS 3163: Design and Analysis of Algorithms (3) : Fall 2020
Assignment 4: Fundamentals of Algorithmic Problem Solving, Algorithm Iterative and Recursive Analysis, Efficiency Class
CLO 1 Reproduce algorithms using different algorithms design techniques i.e. Brute Force, Divide & Conquer, Dynamic
Programming, Greedy Algorithms & Backtracking, Branch & Bound. [C2-Comprehension]
CLO 2 Analyze the time and space complexity of different algorithms by using standard analysis techniques. [C4-Analysis]
CLO 3 Implement the algorithms, compare the implementations empirically. [C3-Application]
Maximum Marks: 10 Instructor: Ms. Tayyaba Zaheer
Announcement Date: 12th October 2020 Due Date: 19th October 2020 till 02:00 PM via Microsoft
Teams
Guidelines:
You are required to submit the hand written assignment file (word or pdf – pictures attached
must be readable and in portrait mode) as courseCode_studentReg#_studenName via Microsoft Teams
(\Assignments\ DAA-CS3163(2)\Assignment 4).
Objectives:
After completion of this Assignment, you will have gained basic knowledge of Iterative and
Recursive Analysis of Algorithms.
Related Reading: Class lectures shared on oneDrive and Portal and Chapter 2 (Section 2.2,
Section 2.3 and Section 2.4) of Text Book.
Tools/Software Requirement (Optional):
1. Microsoft Word.
Description:
Fundamentals of Algorithmic Problem Solving and algorithm analysis.
Tasks:
Task#1: Below is pseudocode for the iterative algorithm SORTEDINSERT, which inserts the key k into
an already sorted array A and maintains the sorted order of the array.
Solution:
Page 1 of 4
b) Describe the best-case scenario for SORTEDINSERT and give both a precise and asymptotically-
tight bound on the best-case running time.
Solution:
c) Describe the worst-case scenario for SORTEDINSERT and give both a precise and
asymptotically-tight bound on the worst-case running time.
Solution:
Task#2: Consider the following alternative approach to SORTEDINSERT that first uses binary
search to find the position for key k and then shifts over the elements to the right and inserts k.
a) Give a recurrence describing the worst-case running time T(n) of Search, where n = r -
p + 1.
Solution:
b) Give an asymptotically-tight bound for the worst-case running time of this version of
SORTEDINSERT. You do not have to solve the recurrence.
Page 2 of 4
Solution:
Task#3: Solve the following recurrence using the master method. Show your work.
Solution:
Task#4: Use the substitution method to show that T(n) = O(n) for the recurrence in Task#3.
Solution:
Question 1:
a) Implement the 3-sum algorithm and print out running times for 1k, 4k and 8k integers. You
can generate these integers using a simple function that runs a for loop with the random
function to generate these integers.
Question 2:
a) Implement the fast 3-sum algorithm and print out running times for 1K, 4K, 8K and 12K
integers. You will need to use a sorting algorithm to sort your data and the binary search
algorithm to search data.
Page 3 of 4
b) Fill in the above table and plot a graph to show the comparisons of running times of the two
algorithms.
Note
You can measure the efficiency of the algorithms by calculating the time it takes to execute the
algorithm. You can use a built-in C++ function called clock() to compute the time.
Page 4 of 4