online advertising

Wednesday, November 25, 2015

Algorithms: Design and Analysis, Part 1 - Exam Question 8

Problem:

What is the asymptotic running time of Randomized QuickSort on arrays of length $ n $, in expectation (over the choice of random pivots) and in the worst case, respectively?

Solution:

It is again just memorizing standard results:

The expected result is $ \theta(n \log n) $, and the worst case result is $ \theta(n^2) $.

Algorithms: Design and Analysis, Part 1 - Exam Question 7

Problem:

On adding one extra edge to a directed graph G, the number of strongly connected components...?

Solution:

Anything could happen!

Suppose the graph is a direct chain, adding an edge to make it a cycle can make it reduce a lot of strongly connected components.

1 -> 2 -> 3 -> 4 (4 strongly connected components)

Adding 4 -> 1 will result in just 1 strongly connected components.

On the other hand adding one more edge, say 1 -> 3, does not decrease it any more.

So the answer is this choice:

...might or might not remain the same (depending on the graph).

Algorithms: Design and Analysis, Part 1 - Exam Question 6

Problem:

What is the asymptotic running time of the Insert and Extract-Min operations, respectively, for a heap with $ n $ objects?

Solution:

This is just memorizing the standard results.

Insert takes $ O(\log n) $ time.
Extract-Min takes $ O(\log n) $ time.

Note that the problem is a little sloppy here, it requires an answer in $ \Theta $, however, in the best case both operation takes just $ O(1) $ time, and the problem do not explicitly ask for the worst case time.

Algorithms: Design and Analysis, Part 1 - Exam Question 5

Problem:

What is the running time of depth - first search, as a function of n and m, if the input graph $ G = (V,E) $ is represented by an adjacency matrix (i.e., NOT an adjacency list), where as usual $ n=|V| $ and $ m=|E| $ ?

Solution:

Each node need to be gone through. And for each of them it need to look at all other nodes for a possible edge, so the time is $ \theta(n^2) $.

Algorithms: Design and Analysis, Part 1 - Exam Question 4

Problem:

Consider a directed graph $ G = (V,E) $ with non-negative edge lengths and two distinct vertices $ s $ and $ t $ of $ V $. Let $ P $ denote a shortest path from $ s $ to $ t $ in $ G $. If we add 10 to the length of every edge in the graph, then:

Solution:

Let consider this graph:

s -1-> a
a -1-> t
s -3-> t

The current shortest path is s -1-> a -1-> t has cost 2.

Suppose we add 10 to each edges, the path is no longer optimal, the optimal path will become

s -13-> t

The general transformation of path cost is path cost + number of edges times 10, therefore, path with less number of edge will be proportionally shorten than the one with larger number of edges, the construction of the example above is based on this.

Therefore we can consider the choices now:

(Option 1) $ P $ definitely remains a shortest $ s - t $ path.
This is obviously wrong given the example above.

(Option 2) $ P $ might or might not remain a shortest $ s − t $path (depending on the graph).
This is true, the example above shown this could be not the shortest path. It could be the shortest path, for example, in case there is only one path.

(Option 3) $ P $ definitely does not remain a shortest $ s - t $ path.
This is obviously wrong given the above.

(Option 4) If $ P $ has only one edge, then $ P $ definitely remains a shortest $ s − t $ path.
This is true, but it is non-obvious. This can be proved by looking at the cost transformation.

Suppose the contrary that there exists another path $ Q $ after the transformation is shorter than $ P $, then

New Cost of Q < New Cost of P
Old Cost of Q + num edges in Q times 10 < Old Cost of P + 10

Now num edges in Q must be at least 1, so we have

Old Cost of Q + 10 < Old Cost of P + 10
Old Cost of Q < Old Cost of P

But that is impossible! $ P $ was the shortest path!

Therefore we proved option 4 is true.

Algorithms: Design and Analysis, Part 1 - Exam Question 3

Problem:

What is the asymptotic worst-case running time of MergeSort, as a function of the input array length $ n $?

Solution:

Actually all recursion levels will take $ \theta(n) $ time to create the merged arrays, so the time is obviously $ \theta(n \log n) $ time.

Algorithms: Design and Analysis, Part 1 - Exam Question 2

Problem:

Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4

Suppose we run MergeSort on this array. What is the number in the 7th position of the partially sorted array after the outermost two recursive calls have completed (i.e., just before the very last Merge step)? (When we say "7th" position, we're counting positions starting at 1; for example, the input array has a "0" in its 7th position.)

Solution:

Just before the last merge, the left half and the right half must be sorted, so it should be

1 3 5 8 9 0 2 4 6 7

Therefore the solution is 2.