Docsity

3 Solved Problems on Algorithmic Functions- Assignment 1 | CS 401, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Assignment #1
(Course: CS 401)
These are the three problems for Assignment 1. The deadline is March 1, 2011, in class.No
late assignments will be accepted.
Any answer that is not sufficiently clear even after a reasonably careful reading
will not be considered a correct answer, and only what is written in the answer
will be used to verify accuracy. No hand waiving, vague descriptions or sufficiently
ambiguous statements that can be interpreted in multiple ways will be considered as
a correct answer, nor will the student be allowed to add any explanations to his/her
answer after it has been submitted.
Problem 1 (40 points): Consider the following basic problem. You are given an array A
consisting of nintegers A[1], A[2],...,A[n]. You would like to output a two-dimensional n-by-n
array Bin which B[i, j] (for i < j) contains the sum of array entries A[i] through A[j]—that
is, the sum A[i] + A[i+ 1] + ··· +A[j]. The value of the array entry B[i, j] is left unspecified
whenever i≥j, so it does not matter what is output for these values.
Here is a sample algorithm to solve the problem.
For i= 1,2,...,n
For j=i+ 1, i + 2,...,n
Add up array entries A[i]through A[j]
Store the result in B[i, j]
Endfor
Endfor
(a) [10 points] For some function fthat you should choose, give a bound of the form O(f(n))
on the running time of this algorithm on an input of size n(i.e., a bound on the number of
operations performed by the algorithm).
(b) [10 points] For the same function f, show that the running time of the algorithm on an
input of size nis also Ω(f(n)). (This shows an asymptotically tight bound of Θ(f(n)) on the
running time.)
(c) [20 points] Although the algorithm you analyzed in parts (a) and (b) is the most natural
way to solve the problem—after all, it just iterates through the relevant entries of the array B,
filling in a value for each—it contains some highly unnecessary sources of inefficiency. Give a
different algorithm to solve the problem, with an asymptotically better running time. In other
words, you should design an algorithm with running time O(g(n)), where limn→∞ g(n)/f (n) = 0.
1
pf2

Partial preview of the text

Download 3 Solved Problems on Algorithmic Functions- Assignment 1 | CS 401 and more Study notes Algorithms and Programming in PDF only on Docsity!

Assignment # (Course: CS 401)

These are the three problems for Assignment 1. The deadline is March 1, 2011, in class. No late assignments will be accepted.

Any answer that is not sufficiently clear even after a reasonably careful reading will not be considered a correct answer, and only what is written in the answer will be used to verify accuracy. No hand waiving, vague descriptions or sufficiently ambiguous statements that can be interpreted in multiple ways will be considered as a correct answer, nor will the student be allowed to add any explanations to his/her answer after it has been submitted.

Problem 1 (40 points): Consider the following basic problem. You are given an array A consisting of n integers A[1], A[2],... , A[n]. You would like to output a two-dimensional n-by-n array B in which B[i, j] (for i < j) contains the sum of array entries A[i] through A[j]—that is, the sum A[i] + A[i + 1] + · · · + A[j]. The value of the array entry B[i, j] is left unspecified whenever i ≥ j, so it does not matter what is output for these values. Here is a sample algorithm to solve the problem.

For i = 1, 2 ,... , n For j = i + 1, i + 2,... , n Add up array entries A[i] through A[j] Store the result in B[i, j] Endfor Endfor

(a) [10 points] For some function f that you should choose, give a bound of the form O(f (n)) on the running time of this algorithm on an input of size n (i.e., a bound on the number of operations performed by the algorithm).

(b) [10 points] For the same function f , show that the running time of the algorithm on an input of size n is also Ω(f (n)). (This shows an asymptotically tight bound of Θ(f (n)) on the running time.)

(c) [20 points] Although the algorithm you analyzed in parts (a) and (b) is the most natural way to solve the problem—after all, it just iterates through the relevant entries of the array B, filling in a value for each—it contains some highly unnecessary sources of inefficiency. Give a different algorithm to solve the problem, with an asymptotically better running time. In other words, you should design an algorithm with running time O(g(n)), where limn→∞ g(n)/f (n) = 0.

1

Problem 2 (20 points): There’s a natural intuition that two nodes that are far apart in a communication network - separated by many hops - have a more tenuous connection than two nodes that are close together. There are a number of algorithmic results that are based to some extent on different ways of making this notion precies. Here’s one that involves the susceptibility of paths to the deletion of nodes. Suppose that an n-node undirected graph G = (V, E) contains two nodes s and t such that the distance beteween s and t is strictly greater than n/2. Show that there must exist some node v, not equal to either s or t, such that deleting v from G destroys all s − t paths. (In other words, the graph obtained from G by deleting v contains no path from s to t.) Give an algorithm with running time O(m + n) to find such a node v.

Hint: Does BFS help?

Problem 3 (40 points): The manager of a large student union on campus comes to you with the following problem. She is in charge of a group of n students, each of whom is scheduled to work one shift during the week. There are different jobs associated with these shifts (tending the main desk, helping with package delivery, rebooting cranky information kiosks, etc.), but we can view each shift as a single contiguous interval of time. There can be multiple shifts going at once. She is trying to choose a subset of these n students to form a supervising committee that she can meet with once a week. She considers such a committee to be complete if, for every student not on the committee, that student’s shift overlaps (at least partially) the shift of some student in the committee. In this way, each student’s performance can be observed by at least one person who is serving on the committee. Give an efficient algorithm that takes the schedule of n shifts and produces a complete supervising committee containing as few students as possible.

Example: Suppose n = 3, and the shifts are

Monday 4 PM–Monday 8 PM, Monday 6 PM–Monday 10 PM, Monday 9 PM–Monday 11 PM.

Then the smallest complete supervising committee would consist of just the second student, since the second shift overlaps both the first and the third.

Hint: A greedy strategy works.