You are on page 1of 10

Homework 2 CSE 458

PART - A

Q.1 Write an algo for post order traversing in a binary tree using stack?

Answer:-

POSTORD(INFO,LEFT,RIGHT,ROOT)

A binary tree T is in memory. This algorithm does a post order traversal of T, applying an
operation PROCESS to each of its nodes. An array STACK is used to temporarily hold the
address of nodes.

Algorithm:-

1. [Push NULL onto STACK and initialize PTR.]

Set TOP := 1,STACK[1]:= NULL and PTR := ROOT.

2. [Push left –most path onto STACK.]

Repeat steps 3 to 5 while PTR=! NULL:

3. Set TOP := TOP+1 and STACK [TOP]:= PTR.

[Pushes PTR on STACK .]

4. If RIGHT[PTR]=! NULL , then:[Push on STACK.]

Set TOP := TOP +1 and STACK [TOP]:= -RIGHT[PTR].

[End of if structure.]

5. Set PTR:=LEFT[PTR].[ updates pointer PTR.]

[End of step 2 loop.]

6. Set PTR := STACK [TOP] and TOP := TOP-1.

[Pops node from STACK,]

7. Repeat while PTR>0:

A) Apply PROCESS to INFO[PTR]

B) Set PTR := STACK [TOP] and TOP := TOP -1.


[Pops node from STACK.]

[End of loop.]

8. if PTR <0 , then:

a) Set PTR := -PTR.

b) Go to step 2.

[End of if structure.]

9 .Exit.

Questi 2 Use quick sort algorithm to sort: 36, 15,40,1,60,20,55,25,50,20


Is it a stable sorting algorithm? Comment?

Quick sort
Ans 2 36,15,40,1,60,20,55,25,50,20
60,15,40,1,36,20,55,25,50,20
1,15,20,20,25,36,40,50,55,60
1,15,20,
Time Complexity
Worst case: •
==> list is already sorted 2n
Average case: •
n2
n log
Statistically, quick sort has been found to be one of the fastest algorithm

it is not stable sorting algo because in this we use 20- 2 times.

Q.3 Model a divide and conquer algorithm to multiply two large integers using only additions
and shift operations.
Answer:

Divide and Conquer :

• To solve a problem we can break it into smaller sub problems,

solve each one recursively, and then merge the solutions


• Have already seen some examples: Merge sort, Quick sort,

• Here we see two examples that have applications in security

of communication (cryptography)

Example 1: Multiplication of large integers :

• Suppose we are dealing with integers that have hundreds of

bits (e.g. 256 or 512 bits).

• Such integers are too big to fit into one memory word. Need

To design an algorithm for multiplication

• The naive algorithm for addition takes O(n) steps if the integers are n bits each.

• For multiplication, the elementary algorithm takes O(n2) steps.

• Goal: do it faster, i.e. o(n2).

• Suppose that I and J are the two n bit integers to be multiplied.

• Say I = w · 2n/2 + x and J = y · 2n/2 + z.

J= w x

I= y z

• Now it is easy to see that I ·J = w ·y ·2n+(w ·z +x·y )2n/2+xz.

• To multiply by 2n only needs to shift-left n bits; each shift left

Takes O(1) time.

• So to multiply by 2n, and 2n/2 (for the second term), and add the results: O(n) time.

• We have 4 multiplications of integers of n/2 bits each: w · y, w · z, x · y, and x · z.

• So, the time required for multiplying I and J is:

T (n) =4T (n/2) + O(n).

• Using master theorem: T (n) ∈ Θ(n2)

• But this is not better than the naive algorithm!! What should

we do?
• The bottleneck here is: too many recursive calls; so try to

reduce the number of instances of size n/2

• Observation: Let r = (w+x) (y+z) = w·y+ (w·z+x·y)+x·y.

• So r contains all the 4 terms we need to compute I · J, but

not individually.

• What if we compute p = w · y and q = x · y, too? Then we

have:

– (w · z + x · y) = r − p − q

–w·y=p

–x·y=q

• So the recursive formula for the time is:

T (n) = 3T (n/2) + O(n)

• Using Master theorem: T (n) ∈ Θ(nlog2 3). Thus:

Theorem: We can multiply two n bit integers in O(n1.585) time.

PART - B

Q.4 Analyze the following properties of BFS and DFS for an Acyclic Tree without making
any assumptions.
a.) Optimality
b.) Completeness
c.) Space Complexity
d.) Time Complexity
Propose an algorithm which is a hybrid of both BFS and DFS and ensures better characteristics
compared to both BFS and DFS.

Breadth-first search (BFS)Description

A simple strategy in which the root is expanded first then all the root successors areexpanded
next, then their successors.We visit the search tree level by level that all nodes are expanded at a
given depth before any nodes at the next level are expanded.Order in which nodes are expanded. 
Performance Measure:
Completeness
:it is easy to see that breadth-first search is complete that it visit all levels given that dfactor is
finite, so in some d it will find a solution.
Optimality:
breadth-first search is not optimal until all actions have the same cost.

Space complexity and Time complexity:

 
Consider a state space where each node as a branching factor b, the root of the tree generates b
nodes, each of which generates b nodes yielding b2 each of these generates b3 and so on

.In the worst case, suppose that our solution is at depth d, and we expand all nodes but the last
node at level d, then the total number of generated nodes is: b + b2 + b3 + b4 +bd+1 – b =
O(bd+1), which is the time complexity of BFS.

As all the nodes must retain in memory while we expand our search, then the space complexity
is like the time complexity plus the root node = O(bd+1).

Depth-first search (DFS )Description:

DFS progresses by expanding the first child node of the search tree that appears and thus going
deeper and deeper until a goal node is found, or until it hits a node that has no children. Then
the search backtracks, returning to the most recent node it hasn’t finished exploring.
Order in which nodes are expanded  

Performance Measure:

: Completeness :
DFS is not complete, to convince yourself consider that our search start expanding the left sub
tree of the root for so long path (may be infinite) when different choice near the root could lead
to a solution, now suppose that the left sub tree of the root has no solution, and it is unbounded,
then the search will continue going deep infinitely, in this case we say that DFS is not complete.

Optimality:
Consider the scenario that there is more than one goal node, and our search decided to first
expand the left sub tree of the root where there is a solution at a very deep level of this left sub
tree, in the same time the right sub tree of the root has a solution near the root, here comes the
non-optimality of DFS that it is not guaranteed that the first goal to find is the optimal one, so we
conclude that DFS is not optimal

Time complexity:
Consider a state space that is identical to that of BFS, with branching factor b, and we start the
search from the root .
In the worst case that goal will be in the shallowest level in the search tree resulting in generating
all tree nodes which are O( bm) .

Space Complexity:

Unlike BFS, our DFS has a very modest memory requirements, it needs to story only the path
from the root to the leaf node, beside the siblings of each node on the path, remember that BFS
needs to store all the explored nodes in memory.
DFS removes a node from memory once all of its descendants have been expanded .With
branching factor b and maximum depth m, DFS requires storage of only bm + 1 nodes which are
O(bm) compared to the O(bd+1) of the BFS.

Combination of BFS and DFS:


Iterative deepening depth-first search (IDS)
Description:
It is a search strategy resulting when you combine BFS and DFS, thus combining the advantages
of each strategy, taking the completeness and optimality of BFS and the modest memory
requirements of DFS.
IDS works by looking for the best search depth d, thus starting with depth limit 0 and make a
BFS and if the search failed it increase the depth limit by 1 and try a BFS again with depth 1 and
so on – first d = 0, then 1 then 2 and so on – until a depth d is reached where a goal is found.

Performance Measure:
Completeness:
IDS is like BFS, is complete when the branching factor b is finite.
Optimality:
IDS is also like BFS optimal when the steps are of the same cost.

Time Complexity:
One may find that it is wasteful to generate nodes multiple times, but actually it is not that costly
compared to BFS, that is because most of the generated nodes are always in the deepest level
reached, consider that we are searching a binary tree and our depth limit reached 4, the nodes
generated in last level = 24 = 16, the nodes generated in all nodes before last level = 20 + 21 + 22
+ 23= 15
Imagine this scenario, we are performing IDS and the depth limit reached depth d, now if you
remember the way IDS expands nodes, you can see that nodes at depth d are generated once,
nodes at depth d-1 are generated 2 times, nodes at depth d-2 are generated 3 times and so on,
until you reach depth 1 which is generated d times, we can view the total number of generated
nodes in the worst case as:
N(IDS) = (b)d + (d – 1)b2 + (d – 2)b3 + …. + (2)bd-1 + (1)bd = O(bd)
If this search were to be done with BFS, the total number of generated nodes in the
Worst case will be like:
 N(BFS) = b + b2 + b3 + b4 + …. bd + (bd + 1 – b) = O(bd + 1)
If we consider a realistic numbers, and use b = 10 and d = 5, then number of generated nodes in
BFS and IDS will be like
N(IDS) = 50 + 400 + 3000 + 20000 + 100000 = 123450
N(BFS) = 10 + 100 + 1000 + 10000 + 100000 + 999990 = 1111100
 
BFS generates like 9 time nodes to those generated with IDS.

Space Complexity:
IDS is like DFS in its space complexity, taking O(bd) of memory

Conclusion:
We can conclude that IDS is a hybrid search strategy between BFS and DFS inheriting their
advantages.
•IDS is faster than BFS and DFS.
•It is said that “IDS is the preferred uniformed search method when there is alarge search space
and the depth of the solution is not known”
Q.5 Imagine a binary Tree with following abstract operations:

a.) get Root


b.) () -> gives the reference of the root Node of the tree.
c.) left() -> gives the reference to the left child a node. If there is no left child, left() returns
NULL reference.
d.) right() -> gives the reference to the right child of a node. If there is no right child, right()
returns NULL reference.

With these abstract functions model an algorithm to find the height of the tree?

Answer:-

A binary tree is an important type of structure which occurs very often. It is characterized by the
fact that any node can have at most two branches, i.e.,there is no node with degree greater than
two. For binary trees we distinguish between the subtree on the left and on the right, whereas for
trees the order of the subtreewas irrelevant. Also a binary tree may have zero nodes. Thus a
binary tree is really a different object than a tree.

Definition: A binary tree is a finite set of nodes which is either empty or consists of a root and
two disjoint binary trees called the left subtree and the right subtree.

Operations
Operations on a binary tree require comparisons between nodes. These comparisons are made
with calls to a comparator, which is a subroutine that computes the total order (linear order) on
any two values. This comparator can be explicitly or implicitly defined, depending on the
language in which the BST is implemented

Searching
Searching a binary tree for a specific value can be a recursive or iterative process. This
explanation covers a recursive method.
We begin by examining the root node. If the tree is null, the value we are searching for does not
exist in the tree. Otherwise, if the value equals the root, the search is successful. If the value is
less than the root, search the left sub tree. Similarly, if it is greater than the root, search the right
sub tree. This process is repeated until the value is found or the indicated subtree is null. If the
searched value is not found before a null sub tree is reached, then the item must not be present in
the tree
# 'node' refers to the parent-node in this case
def search_ binary _tree(node, key):
if node is None:
return None # key not found
if key < node .key:
return search _binary _tree(node .left Child, key)
else if key > node .key:
return search _binary _tree(node .right Child, key)
else: # key is equal to node key
return node .value # found key
height(node) = max(height(node .L), height(node. R)) + 1
height(node):
if node == null:
return -1
else:
max(height(node .L), height(node. R)) + 1
If you wanted the number of nodes the code would be:

height(node):
if node == null:
return 0
else:
max(height(node .L), height(node. R)) + 1

Q.6 Show that for any Non empty Binary Tree T, if “ n” denotes the number of terminal
nodes and “ N” denotes the number of nodes of degree 2, then the following equation is
valid----------
N = n-1;

Answer:- An integer n ≥ 2 is prime if and only if all the intermediate binomial coefficients
are divisible by n.

Proof: When p is prime, p divides

 for all 0 < k < p


because it is a natural number and the numerator has a prime factor p but the
denominator does not have a prime factor p.

When n is composite, let p be the smallest prime factor of n and let k = n/p. Then
0 < p < n and

otherwise the numerator k(n−1)(n−2)×...×(n−p+1) has to be divisible by n = k×p,


this can only be the case when (n−1)(n−2)×...×(n−p+1) is divisible by p. But n is
divisible by p, so pdoes not divide n−1, n−2, ..., n−p+1 and because p is prime, we
know that p does not divide (n−1)(n−2)×...×(n−p+1) and so the numerator cannot
be divisible by n.

so here it is proved N=n-1 is valid..

You might also like