



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 325 Homework 7 Sample solution
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.
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.
(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)