Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Analysis of Algorithms - Discrete Mathematics - Quiz I Solutions | CS 220, Quizzes of Discrete Mathematics

Material Type: Quiz; Professor: Subramani; Class: Discrete Mathematics; Subject: Computer Science; University: West Virginia University; Term: Fall 2007;

Typology: Quizzes

Pre 2010

Uploaded on 07/31/2009

koofers-user-hb9
koofers-user-hb9 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Analysis of Algorithms - Discrete Mathematics - Quiz I Solutions | CS 220 and more Quizzes Discrete Mathematics in PDF only on Docsity! Analysis of Algorithms - Quiz I (Solutions) K. Subramani LCSEE, West Virginia University, Morgantown, WV {ksmani@csee.wvu.edu} 1 Problems 1. Recurrences: (a) Solve the following recurrence. You may assume any convenient form for n. T (1) = 0 T (n) = T ( n 2 ) + 1, n > 1 (b) Consider the following recurrence relation: T (1) = 4 T (n) = T (n− 1) + 4 Argue using mathematical induction that T (n) = 4 ·n. Note that you must use induction to establish the solution. Solution: (a) Assume that n = 2k, so that k = log n. Accordingly, the original recurrence can be rewritten as: T (20) = 0 T (2k) = T (2k−1) + 1, k > 1 Now observe that, T (2k) = T (2k−1) + 1 = T (2k−2) + 1 + 1 = T (2k−3) + 1 + 1 + 1 ... = ... = T (2k−(k−1)) + (k − 1) = T (2k−k)) + k Finally, we note that T (2k−k) = T (20) = 0 and hence, T (2k) = k, from which it follows that T (n) = log n. (b) Base case: When n = 1, as per the definition, T (n) = 4, while as per the conjecture T (n) = 4 · 1 = 4. Since definition and conjecture coincide, the base case is proven. Assume that T (k) = 4 · k, for some k > 1. 1 Let us focus on T (k + 1); as per the definition, T (k + 1) = T (k) + 4 = 4 · k + 4 = 4 · (k + 1) = conjecture Since assuming that conjecture and definition coincide at n = k implies that conjecture and definition coincide at n = k + 1, we apply the first principle of mathematical induction to conclude that T (n) = 4 · n, ∀n ≥ 1. 2 2. Asymptotics: (a) Show that if f(n) ∈ O(g(n)) and g(n) ∈ O(h(n)), then f(n) ∈ O(h(n)). (b) Show that for any function f(n), the set ω(f(n)) ∩ o(f(n)) is empty. Solution: (a) Since, f(n) ∈ O(g(n)), it follows that there exist constants c1 and N1, such that for all n ≥ N1, f(n) ≤ c1 ·g(n). Likewise, there exist constants c2 and N2, such that for all n ≥ N2, g(n) ≤ c2 · h(n). Set N3 = max(N1, N2) and c3 = c1 · c2. It is clearly the case, that for all n ≥ N3, f(n) ≤ c1 · g(n) and g(n) ≤ c2 · h(n). It thus follows that for all n ≥ N3, f(n) ≤ c1 · c2 · h(n) = c3 · h(n), i.e., f(n) ∈ O(h(n)). (b) Let h(n) denote a function that belongs to the set ω(f(n)) ∩ o(f(n)). Since h(n) ∈ ω(f(n)), we know that limn→∞ f(n) h(n) = 0. Likewise, since h(n) ∈ o(f(n)), we have limn→∞ h(n) f(n) = 0. Thus, h(n) simultaneously grows faster and slower than f(n), which is a contradiction, proving that h(n) cannot exist. 2 3. Greedy: In class, we established the correctness of Kruskal’s algorithm by assuming that all edges had distinct weights. Can you modify the proof to prove that if the edge weights are distinct, the Minimum Spanning Tree of the input graph is unique? Solution: Let T denote the Minimum Spanning Tree (MST) produced by Kruskal’s algorithm and let T ′ denote an MST, which is distinct from T , such that w(T ) = w(T ′), where w(T ) and w(T ′) denote the respective weights of T and T ′ respectively. Since the two trees are distinct and both contain exactly (n− 1) edges, where n denotes the number of vertices in the input graph, it must be the case that there exists at least one edge in T , which is not in T ′; pick the lightest such edge, say e. Insert e into T ′; as per the discussion in class, we know that a cycle C is created in T ′. Further all the edges in the cycle C cannot belong to T , since in this case, e cannot be part of T either. Pick any edge e′ ∈ C, such that e′ 6∈ T . Let S denote the set of edges that are lighter than e and are part of T . As per the above discussion, the edges in S are also part of T ′ (since e is the lightest edge in T that is not part of T ′). Since S ∪ e′ ⊆ T ′, S ∪ e′ does not contain a cycle. We now argue that e′ is heavier than e. First note that e′ and e cannot have the same weight, since all edge weights are distinct. Then note that if e′ is indeed lighter than e, it would have been considered before e, by Kruskal’s algorithm and made part of T , since it does not create a cycle with S! Inasmuch as e′ does not create a cycle with S and e′ is not part of T , it follows that e′ is heavier than e. Thus, removing e′ from T ′ maintains the spanning structure of T ′,while lowering its weight. The fact that we could lower the weight of T ′ immediately implies that T ′ is not an MST, i.e., the MST of the input graph must be unique. 2 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved