1. Suppose You Are Given a Set P of Integers and Another Integer X. We Wish to Use a Θ(N2)

1. Suppose You Are Given a Set P of Integers and Another Integer X. We Wish to Use a Θ(N2)

Questions:

Search:

1. Suppose you are given a set P of integers and another integer x. We wish to use a Θ(n2) algorithm to decide whether there are 3 integers in P and the sum of these three integers equals to x. Show your algorithm and indicate why its complexity is Θ(n2). (You can use pseudo code or by illustration only)

IF-EQUALS-SET(x, P)

for i ← 1 to P.size - 2

for j ← i + 1 to P.size - 1

if P[i] + P[j] + P[P.size] = x

return true

return false


Recurrence:

2. Use a recursion tree, show the detailed process how to solve the following recurrence in terms of the big O representation. Use the substitution method to verify your result.

T(n) = 4T(n/4)+cn

Draw the recursion tree T(n) = 4T(n/4)+cn. The height of the tree is log4n, the out of the degree will be 4 and the contribution of the ith level will be 4i[cn/4i]. The last level contributes 4log4n Θ(1) = Θ(nlog4n)

Hence we have the bound on the sum given by:

T(n) = 4T(n/4)+cn

T(n) = i=0log4(n-1) 4i * [cn/4i]+ Θ(nlog4n)

<= i=0log4(n-1) 4i * [cn/4i] + Θ (nlog4n)

= cn i=0log4(n-1)1i + Θ (nlog4n)

= cn 1log4n - 1+ Θ (nlog4n)

= Θ (nlog4n)

Using the substitution method we can verify this bound

Let T ([n/4] <= d[n/4]log4 We have:

T(n) = 4T(n/4)+cn

≤ 4d[n/4] log4 + cn

< 4d[n/4] log4 + cn

= 4dn log4 /4log4 + cn

= dnlog4 + cn

= Θ (nlog4n)

Sorting:
3. Can RADIX SORT shown in our textbook work correctly if we use QUICKSORT instead of COUNTING SORT to sort each individual digit? Justify your answer.

No, this cannot work correctly since QUICKSORT is not stable. If RADIX SORT has the first i-1 digits are sorted, using COUNTING SORT to sort the ith digit will not affect the order of two items with the same ith digit. Using QUICKSORT does not guarantee this.

Data structures:

4. If we perform an inorder traversal on a Min Heap (as defined in the textbook), will we get the values in sorted order? Why or why not? Use a simple example to justify your answer.

No, the values will not be in sorted order. One such example that would not yield a sorted order is a min heap with root 7, left child 9 and right child 8.

Greedy algorithms and dynamic programming:

5. You have n programs p1, ..., pn with size m1, ..., mn MB (mega bytes). You have a disk that can store up to W MB. W is less than the total size of the programs. You are asked to use a dynamic programming solution to store the programs onto the disk so as to maximize the disk space utilization. In other words, you want to find a subset of programs such that its total size is maximized but has to be less than C.

Define U[i,w] as the maximum disk utilization of a disk with capacity w MB and programs p1,...,pi to be selected for storing it on the disk. Write a recurrence for U[i,w] and identify the boundary condition.

If we take two programs pi, pk where mi ≤ mj ≤ mk. This cannot hold true as mk + mj mi + mj if and only if mk mi is false. The greedy algorithm maximizes the number of programs on the disk.

6. Consider the problem of making change for n cents using the fewest number of coins. Assume that each coin’s value is an integer.

1) Describe a greedy algorithm to make change consisting of quarters (25 cents each), dimes (10 cents each), nickels (5 cents each), and pennies (1 cent each). Prove that your algorithm yields an optimal solution.

2) Give a set of coin value combination (the value of coins may not be limited to quarter, dime, nickel, or penny) for which the greedy algorithm does not yield an optimal solution. Your set should include a penny value so that there is a solution for every possible value of n. Use an example to justify your answer.

MAKE-CHANGE(n)

nickels ← 0

dimes ← 0

quarters ← 0

while n >= 25

quarters ← quarters + 1

n ← n – 25

while n >= 10

dimes ← dimes + 1

n ← n - 10

while n >= 5

nickels ← nickels + 1

n ← n - 5

pennies ← n

Optimal Substructure: Let {c1, c2,..., cm } be the coins for the optimal solution of the coin-changing problem, where ci is either a penny, nickel, dime or quarter, and the sum, n, is the change to be made. Removing the first coin c1, the subset of coins {c2, ..., cm} represent a solution to the n-d(c1) problem, where d(c) is the denomination of coin c. If the subset was not an optimal solution to the n-d(c1), then there exists another solution {s2,..., sk }, where k m which takes fewer coins. If such a solution exists, then combining it with c1 would hold a better solution to the problem. This would be a contradiction since we started with an optimal solution. {c2,..., cm } must be an optimal solution to the subproblem n-d(c1), and the problem holds optimal substructure.

Greedy choice Property: Assume we have an optimal solution to the change problem {c1, c2,..., cm }. The sum of the coins ci , equal to n regardless of order chosen. Thus, we can swap coin cj swap with c1 and still hold an optimal solution. Assume the greedy choice does not reside in the optimal solution. Then, one of the highest denomination coins less than n is not in the solution. This implies that that lower denomination coins must be used to accommodate for the value of cents in the higher-denomination coin. Since at least two lower-denomination coins are required to equal the value of a higher-denomination coin, we could always replace these coins with the greedy coin and obtain a solution with fewer coins. Therefore, the greedy coin must be in the optimal solution. Thus, a greedy choice for the first coin will lead to an optimal solution.

Tree and graph:

7. Given the following weighted undirected graph, show the order the edges would be added for building a spanning tree using Kruskal’s and Prim’s algorithm. Take the following figure as an example, order edges added using two algorithms. In addition, how many edges will be picked up to build the tree? Why? Show your process.

The minimum spanning tree using Kruskal’s algorithm is defined in the book as follows:

MST-KRUSKAL(G, w)

A ← ∅

for each vertex v ∈ G.V

MAKE-SET (v)

Sort the edges of G.E into nondecreasing order by weight w

for each edge(u,v) ∈ G.E, taken in nondecreasing order by weight

if FIND-SET(u) != FIND-SET(v)

A = A ∪ {(u, v)}

UNION(u,v)

return A

Suppose there is more than one MST for a graph G. Suppose that one MST M’ with the smallest weight edge e1 is as given below:

Suppose that second MST M” without the smallest weight edge e1 is as given below:

If these two MST’s are overlapped, cycles are formed and M” does not have the minimum weight edge and edges should be removed to avoid cycles. In this graph since the minimum weight edge is e1 hence, it should be added to M”.

Thus some other edge should be removed and e1 should be added to M”. The removal of the edge and addition of e1 would impose that the original M” does not exist. Hence, there is only one minimum spanning tree for a graph G with unique edges.

Prim’s algorithm can be used to find a solution for calculating the cost of given graph. The textbook describes the algorithm as follows:

MST-PRIM(G, w, r)

for each u ∈ G.V

u.key ← ∞

u.π ← NIL

r.key ← 0

Q ← G.V

while Q != ∅

u ← EXTRACT-MIN(Q)

for each v ∈ G.Adj[u]

if v ∈ Q AND w(u,v) < v.key

v.π ← u

v.key ← w(u, v)

8. For each of the following, either draw a graph satisfying the given criteria or explain why it cannot be done. Your graphs should be simple, i.e. not having any multiple edges (more than one edge between the same pair of vertices) or self-loops (edges with both ends at the same vertex).

a. A graph with 3 connected components, 11 vertices, and 8 edges.

b. A graph with 3 connected components, 10 vertices, and 30 edges.

c. A graph with 3 connected components and 10 vertices. 9 vertices have degree 3 and 1 vertex has degree 4.

d. A graph with 6 vertices, of which 4 have degree 3, 1 has degree of 4 and 1 has degree 5.

The graph with 3 connected components, 8 edges and 11 vertices is possible. The desired graph is as given below:

b. 

The graph with 3 connected components, 10 vertices, 30 edges and is not possible. If we consider one vertex as one edge component we can get a possible graph otherwise it is not possible. For example, suppose the following where the top has 4 vertices and 2 edges and the bottom has 6 vertices and 15 edges,

The graph with 3 connected components, 8 edges and 10 vertices where 9 vertices have a degree 3 and 1 has degree 4 is not possible. This is because; if 9th vertices have the degree 3, then, it should be connected to other vertex, which will make its degree 4.

The graph with 6 vertices, of which 4 have degree 3, 1 has degree of 4 and 1 has degree 5 is not possible. Suppose that

If we develop the full K graph for 5 vertices, then,

For any 4 vertices have to be degree 4, the order must also be 4 degree.