Docsity

Analysis of Sorting Algorithms and Complexity: Binary Trees and Lower Bounds, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS 325 Homework 7
Sample solution
1. A sorting algorithm that uses comparisons can be seen as a binary tree whose nodes
correspond to comparisons, branching corresponds to the decision made based on the
result of the comparison and leaves correspond to the right order for the input elements.
The computation starts from the root node and, depending on the order in which the
elements were fed, it will take a certain path within the tree and continues going down
until it hits a leaf. At that point, the algorithm outputs the elements in the order given by
that leaf node and then halts. For example, the following tree corresponds to an algorithm
that sorts three elements a1, a2 and a3:
Yes
Yes
Yes
Yes
No
No
No
No
Yes
No
a1< a3?
a1, a2, a3
a3, a2, a1a2, a3, a1
a2< a3?
a2, a1, a3
a3, a1, a2a1, a3, a2
a1< a3?
a2< a3?
a1< a2?
Applying the above algorithm on the input: 12, 8, 7, the algorithm will take the
highlighted path. The algorithm in this case makes 3 comparisons, the number of
comparisons can also be seen as the length of the path from root to leaf. Note that not all
paths have the same length, but here we’re interested with the worst case analysis.
In general, any such tree that sorts n elements must have a leaf node for each possible
permutation of the elements (all possible orderings) since, if one of the possible
permutations is missing and we feed the algorithm with elements ordered in that
particular ordering there will be no way the algorithm outputs the right output. We thus
pf3
pf4
pf5

Partial preview of the text

Download Analysis of Sorting Algorithms and Complexity: Binary Trees and Lower Bounds and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 325 Homework 7 Sample solution

  1. A sorting algorithm that uses comparisons can be seen as a binary tree whose nodes correspond to comparisons, branching corresponds to the decision made based on the result of the comparison and leaves correspond to the right order for the input elements. The computation starts from the root node and, depending on the order in which the elements were fed, it will take a certain path within the tree and continues going down until it hits a leaf. At that point, the algorithm outputs the elements in the order given by that leaf node and then halts. For example, the following tree corresponds to an algorithm that sorts three elements a 1 , a 2 and a 3 :

Yes

Yes

Yes

Yes

No

No

No

No

No Yes

a 1 < a 3?

a1, a2, a 3

a3, a2, a 1 a2, a3, a 1

a 2 < a 3?

a2, a1, a 3

a3, a1, a 2 a1, a3, a 2

a 1 < a 3?

a 2 < a 3?

a 1 < a 2?

Applying the above algorithm on the input: 12, 8, 7, the algorithm will take the highlighted path. The algorithm in this case makes 3 comparisons, the number of comparisons can also be seen as the length of the path from root to leaf. Note that not all paths have the same length, but here we’re interested with the worst case analysis. In general, any such tree that sorts n elements must have a leaf node for each possible permutation of the elements (all possible orderings) since, if one of the possible permutations is missing and we feed the algorithm with elements ordered in that particular ordering there will be no way the algorithm outputs the right output. We thus

conclude that any such tree must have n! leaves. We also know that a binary tree of depth d has at most 2d^ leaves*. So the depth of the tree (and thus the number of comparisons in worst case) must be at least log(n!). Using Stirling’s approximation we get: n! ≥ (n/e)n log(n!) ≥ nlog(n/e) i.e # of comparisons = Ω(nlogn). We thus conclude that nlogn is a lower bound on the runtime of any comparison-based algorithm.

  • The fact that a binary tree of depth d has at most 2d^ leaves can be easily proved by induction as follows: Base step: d = 0 (A single node)

of leaves = 1 = 2^0

Inductive step: We assume that a binary tree of depth d has at most 2d^ leaves (Inductive assumption). From that we can see that a binary tree of depth d + 1 has # of leaves = 2 *(# of leaves of a binary tree of depth d) (since we can add at most two node from each of the leaves of the d binary tree to get the d+1 tree) Then # of leaves of a depth d + 1 binary tree = 2d+1, which completes the proof.

  1. We have to prove that there exists a checking algorithm that takes as input an instance I and a proposed solution S, and outputs true if and only if S really is a solution to instance I. We also have to show this algorithm is a polynomial time algorithm. In the case of Graph Isomorphism, the instance is of the form: “Given two graphs, G 1 =

(V 1 , E 1 ), and G 2 = (V 2 , E 2 ), is there a one-to-one function f : V 1 –> V 2 such that for any

edge ( x , y ) ∈ E 1 if only if ( f ( x ), f ( y ) ) ∈ E 2 ?”

The proposed solution is the mapping function f. Then our checking algorithm simply checks that if, applying the mapping, the two graphs become identical, i.e. any edge ( x , y ) ∈ E 1 if only if ( f ( x ), f ( y ) ) ∈ E 2. Here is the

algorithm:

We now have to show that G’ has a H.P between s and t iff G has a H.C: (note that a cycle can start and end at any node, we here choose to start at v)

  1. If G has a H.C we can construct an s-t H.P in G’ as follows: start at s, follow the same edges of the cycle starting at v. To go back to v, the cycle will ultimately end at one of the nodes connected to v (say u) and then ends at v, so from u, instead of going back to v, the path will go to t. Since all nodes connected to v in G are connected to t in G’ such a path is possible. Since the constructed path hits every node in G’ exactly once starting from s and ending at t then it is an s-t H.P.
  2. If there is an s-t H.P in G’ then it has to start at s, goes to v then hits all the nodes of the original graph G, reaches one of the nodes connected to v, and then ends at t. A H.C on the nodes of G can be constructed by starting at v, following the same edges of the s-t path, then at the node just before t goes back to v. The constructed cycle hits all nodes in G exactly once, then it’s a H.C. Note that proving both statements is necessary to prove the reduction is correct. Finally, we have to show that the transformation from an instance of H.C to an instance of H.P can be done in polynomial time. Since we basically add two nodes to the graph and at most m+1 edges (such that m is the number of edges of G), then the construction is O(m). This completes the proof.
  3. To prove the Hitting Set problem is NP-Complete we have to prove that:
  • Hitting Set is in NP: Given an instance of the Hitting Set problem and a proposed solution (a set H) then the checker algorithm will simply verify whether it is actually a Hitting set by checking the intersection of H and B 1 , B 2 … Bm (that will take time polynomial in m). This proves the problem is in NP.
  • For some NP-Complete problem pi, pi ≤p Hitting Set Problem: We choose pi to be the Vertex Cover problem, i.e. Vertex Cover ≤p Hitting Set Problem. We transform an instance of Vertex Cover “Given a graph G = (V, E), is there a vertex cover of size at most k” into an instance of Hitting set “Given a set A, subsets B 1 , B 2 … BB m and an integer k’ is there a Hitting set of size at most k’?” as follows:
  1. A = V
  2. For each edge (u, v) in E add a subset Bi = {u, v} (i.e. a subset for each edge in G)
  1. k’= k We next have to show that our construction is right, i.e. there is a vertex cover in G of size at most k if and only if there is a hitting set of size at most k’:
    • If there is a vertex cover C of size at most k, then, by definition, for all (u, v) in E either u or v (or both) is in C, then for all Bi, C ∩ Bi ≠ Φ, then C is also a hitting set of size at most k’.
    • If there is a hitting set H of size at most k’, then for all Bi, C ∩ Bi ≠ Φ then for all (u, v) in E either u or v (or both) is in H, then H is also a vertex cover of size at most k. Finally, we show that the transformation is polynomial. Analyzing the construction:
  2. A = V : takes O(n) time.
  3. For each edge (u, v) in E add a subset Bi = {u, v}: takes O(m) time.
  4. k’= k: constant time. Thus, the construction as a whole can be done in polynomial time (we don’t really care about the exact bound on time, what matters is that the transformation takes polynomial time in the size of the input).