EC330 Applied Algorithms and Data Structures For Engineers Fall 2018 Homework 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

EC330 Applied Algorithms and Data Structures for Engineers

Fall 2018

Homework 3

Out: September 27, 2018


Due: October 5, 2018

This homework has a written part and a programming part. Both are due at 8:59 am on
October 5. You should submit both parts on Blackboard. For the written part, you should
submit a single PDF file containing either typeset answers or scanned copies of hand-
written answers. Make sure you write your answers clearly. For the programming part,
your code should be easy to read and understand, and demonstrate good code design and
style. Your program must compile and run on the lab computers.

1. Recurrence Relations [40 pt]


a) Suppose you have to choose among the follow three algorithms:
• Algorithm A solves the problem by dividing it into five subproblems of half
the size, recursively solving each subproblem, and then combining the
solutions in linear time.
• Algorithm B solves the problem of size 𝑛 by recursively solving two
subproblems of size 𝑛 − 1 and then combining the solutions in constant time.
• Algorithm C solves problems of size n by dividing them into seven problems
of size 𝑛/3, recursively solving each subproblem, and then combining the
solutions in 𝑂(𝑛! ) time.
What are the running times of each of these algorithms (in big-𝑂 notation). Which
would you choose? [10 pt]

b) Give the tightest asymptotic upper bound (in 𝑂(∙)) you can obtain for the
following recurrences. Justify your answer. [5 pt each]
• 𝑇(𝑛) = 7𝑇(𝑛/3) + 𝑛!
• 𝑇(𝑛) = 2𝑇(𝑛/4) + 5 𝑛
• 𝑇 𝑛 = 20𝑇 𝑛/15 + 𝑛 log 𝑛
• 𝑇 𝑛 = 𝑇 𝑛 − 1 + 5 log 𝑛 , 𝑇(1) = 1
• 𝑇(𝑛) = (log 𝑛)𝑇(𝑛/2) + 1
• 𝑇(𝑛) = 𝑛(𝑇(𝑛/2))!

2. Time Complexity [20 pt]


a) Identify the time complexity of the following piece of code, where 𝑛 is given as
the input. Write your answer using the 𝛩(∙) notation. Justify your answer. Hint:
Assume 𝑛 = 2! . [10 pt]
int i = n;
while (i > 1) {

1
int j = i;
while (j < n) {
int k = 0;
while (k < n) {
k = k + 2;
}
j = j * 2;
}
i = i / 2;
}

b) Write down the recurrence relation of the following sorting algorithm. What is the
time complexity of this method (in 𝑂(∙) notation)? Justify your answer. [10 pt]

void StrangeSort(int a[], int min, int max) {


if (min >= max)
return;
if (a[min] > a[max])
swap(a[min], a[max]); // constant-time operation
int one_third = (max – min + 1) / 3;
if (one_third >= 1) {
StrangeSort(a[], min, max – one_third);
StrangeSort(a[], min + one_third, max);
StrangeSort(a[], min, max – one_third);
}
}

3. Programming [40 pt]


Make sure to write your name and BU ID in a comment at the top of the program,
along with your collaborator’s name and BU ID, if any. To use the compiler on the lab
computers, run “module load gcc” first. Submit only ListNode.h.
a) [20 pt] In this problem, you will implement insertion sort for singly linked list.
You are given two files: ListNode.h and Problem3a.cpp. Your job is to implement
the insertionSortList method in ListNode.h. The Problem3a.cpp file is given as a
sample test program for your implementation. You can compile with the test
program using:
> g++ -std=c++11 (-o myProgram) Problem3a.cpp

b) Recall Floyd’s cycle-detection algorithm that we went over in class.


• [10 pt] Implement this algorithm in the detectCycle method in the same
ListNode.h file. Similar to part a), you are given a sample test program
Problem3b.cpp to test your code. You can compile with the test program
using:
> g++ -std=c++11 (-o myProgram) Problem3b.cpp
• [10 pt] It turns out that you can modify Floyd’s algorithm to find the
node where the cycle starts as well (if one exists). Implement the
findCycleStart method in ListNode.h. You can also test this using
Problem3b.cpp.
Hint: Think about the distance from the cycle start node to the node
where the two points meet (say this is d). Think about the distances
(both non-loopy and loopy parts) the two pointers have traversed
respectively until they meet and their relationship to d.

You might also like