Tree Depth-Theory of Complexity and Algorithms-Assignment Solution, Exercises of Advanced Algorithms

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

2011/2012

Uploaded on 07/17/2012

toshi-sabri
toshi-sabri 🇮🇳

4.4

(20)

44 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A Question of Some Depth
Let Tbe a k-ary tree, i.e. each internal node of Thas at most kchildren.
Suppose that the depth of Tis d(the maximum depth of any node of Tis
d). What is the maximum number of external nodes (leaves) that Tcan have?
What is the maximum total number of nodes that Tcan 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 kchildren. Each of these children
will have kchildren of their own, so there will be k2children at depth 2 in T.
Similarly, each of these k2nodes will have kchildren, so there are k2·k=k3
vertices at depth 3, and so forth. The leaves of the tree will have depth d.
Therefore, there will be kdnodes (leaves) at depth d. So the maximum number
of leaves for a k-ary tree with depth dis kd.
Then the maximum total number of nodes is then
1 + k+k2+k3+···+kd=kd+1 1
k1.
Note: For your information, here we show that (assuming k6= 1)
1 + k+k2+k3+···+kd=kd+1 1
k1.
(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+kd1+· · · +k2+k+ 1.
Then
kS =kd+1 +kd+· · · +k3+k2+k .
Therefore
kS S= (kd+1 +kd+· · · +k2+k)(kd+kd1+· · · +k+ 1)
and hence
(k1)S=kd+1 1.
So (dividing by (k1) which is allowed since k6= 1) we get that
S=kd+1 1
k1.
1
docsity.com
pf2

Partial preview of the text

Download Tree Depth-Theory of Complexity and Algorithms-Assignment Solution and more Exercises Advanced Algorithms in PDF only on Docsity!

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

S =

kd+1^ − 1 k − 1

docsity.com

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.)

docsity.com