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 a1, a2 and a3: Yes Yes Yes Yes No No No No YesNo a1< a3? a1, a2, a3 a3, a2, a1 a2, a3, a1 a2< a3? a2, a1, a3 a3, a1, a2 a1, 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 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 = 20 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. 2. 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: