0% found this document useful (0 votes)
84 views

Capital University of Science and Technology Department of Computer Science CS 3163: Design and Analysis of Algorithms (3) : Fall 2020

The document is an assignment for a computer science algorithms course. It includes 5 tasks: [1] Analyzing the time complexity of an iterative sorting algorithm, [2] Analyzing an alternative sorting algorithm using binary search, [3] Solving a recurrence relation using the master method, [4] Using substitution to solve a recurrence, and [5] Empirically comparing the 3-sum and fast 3-sum algorithms. Students are asked to implement algorithms, derive time complexities, and compare running times.

Uploaded by

Malik Naveed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Capital University of Science and Technology Department of Computer Science CS 3163: Design and Analysis of Algorithms (3) : Fall 2020

The document is an assignment for a computer science algorithms course. It includes 5 tasks: [1] Analyzing the time complexity of an iterative sorting algorithm, [2] Analyzing an alternative sorting algorithm using binary search, [3] Solving a recurrence relation using the master method, [4] Using substitution to solve a recurrence, and [5] Empirically comparing the 3-sum and fast 3-sum algorithms. Students are asked to implement algorithms, derive time complexities, and compare running times.

Uploaded by

Malik Naveed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

a) Perform a line-by-line analysis of SORTEDINSERT and derive a precise (non-asymptotic)


expression of the running time T(n), where n = length(A). You may assume a cost ci = 1 for
each line of pseudocode.

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:

Task#5: Empirical Analysis - Analyzing 3-sum and Fast 3-sum algorithms:


Objectives
The objective of this part is to compare the running time complexity of 3-sum and fast 3-sum algorithms
in C/C++.
Description
The 3-sum is a simple algorithm that finds triplets in an array that sum to 0. The brute force method to
solve this problem uses triple for loops to find all combinations. The complexity for this method is high
i.e. the order of N 3. The fast 3-sum algorithm is an improvement that uses sorting and binary search to
find triplets that sum to 0. The fast 3-sum improves on the 3-sum algorithm in terms of complexity.

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.

Question 3: Mention processor, RAM and OS of your computer.


Processor: ____________________________________________________________________
RAM: ________________________________________________________________________
OS: __________________________________________________________________________

Question 4: What makes Fast-3-sum faster than 3 sum implementation?


______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Page 4 of 4

You might also like