Professional Documents
Culture Documents
RA17T3A27 - Home Work 2 - CSE458 - 2nd Assgn 7450070092 Roll t27
RA17T3A27 - Home Work 2 - CSE458 - 2nd Assgn 7450070092 Roll t27
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:-
[End of if structure.]
[End of loop.]
b) Go to step 2.
[End of if structure.]
9 .Exit.
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
Q.3 Model a divide and conquer algorithm to multiply two large integers using only additions
and shift operations.
Answer:
of communication (cryptography)
• Such integers are too big to fit into one memory word. Need
• The naive algorithm for addition takes O(n) steps if the integers are n bits each.
J= w x
I= y z
• So to multiply by 2n, and 2n/2 (for the second term), and add the results: O(n) time.
• But this is not better than the naive algorithm!! What should
we do?
• The bottleneck here is: too many recursive calls; so try to
not individually.
have:
– (w · z + x · y) = r − p − q
–w·y=p
–x·y=q
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.
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.
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).
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.
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:
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.
When n is composite, let p be the smallest prime factor of n and let k = n/p. Then
0 < p < n and