

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This assignment was submitted to Prof. Madhulata Nirmal at Chhattisgarh Swami Vivekanand Technical University for Theory of Complexity and Algorithms course. It includes: Depth, External, Nodes, Maximum, Big, Swap, Integer, Variables, Addition, Subtraction, Programming, Language
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!


A Question of Some Depth
Let T be a k-ary tree, i.e. each internal node of T has at most k children. Suppose that the depth of T is d (the maximum depth of any node of T is d). What is the maximum number of external nodes (leaves) that T can have? What is the maximum total number of nodes that T can have?
Solution: The number of external nodes and the total number of nodes will clearly be maximized when every internal node has the maximum number of children. For a k-ary tree, the root node then has k children. Each of these children will have k children of their own, so there will be k^2 children at depth 2 in T. Similarly, each of these k^2 nodes will have k children, so there are k^2 · k = k^3 vertices at depth 3, and so forth. The leaves of the tree will have depth d. Therefore, there will be kd^ nodes (leaves) at depth d. So the maximum number of leaves for a k-ary tree with depth d is kd. Then the maximum total number of nodes is then
1 + k + k^2 + k^3 + · · · + kd^ = kd+1^ − 1 k − 1
Note: For your information, here we show that (assuming k 6 = 1)
1 + k + k^2 + k^3 + · · · + kd^ =
kd+1^ − 1 k − 1
(I don’t necessarily expect you to remember this formula, but more importantly, if you wanted to reproduce it, you should remember the idea behind you could get it.)
Let S = kd^ + kd−^1 + · · · + k^2 + k + 1.
Then kS = kd+1^ + kd^ + · · · + k^3 + k^2 + k.
Therefore
kS − S = (kd+1^ + kd^ + · · · + k^2 + k) − (kd^ + kd−^1 + · · · + k + 1)
and hence (k − 1)S = kd+1^ − 1.
So (dividing by (k − 1) which is allowed since k 6 = 1) we get that
kd+1^ − 1 k − 1
The Big Swap
Suppose that x and y are two integer variables. How can I swap the values of x and y without using a third temporary variable? (The usual way to do this swap is to do something like
temp ← x x ← y y ← temp.
Here we want to avoid using this extra variable. What can we do instead to achieve this swap?)
Solution: I’ll just indicate how you can do this below. I will slightly abuse the notation and use x and y to denote the values of the variables. The idea is that in each step, we will perform one addition or subtraction, and in the last step, we will negate one number.
x y =⇒^
x x + y =⇒^
−y x + y =⇒^
−y x =⇒^
y x
Note that, on a computer, this might run a chance of an integer overflow, i.e. if x and y are large integers, then computing the sum x + y might give a result that cannot be stored in an integer variable. (Of course, this is always true anytime an addition/subtraction operation is performed (or, indeed a mul- tiplication or shift operation, etc.). The result might be too big to store in an integer variable in that programming language/operating system.)