0% found this document useful (0 votes)
3 views92 pages

Data Structure II (Tree & Graphs) Basic Solution

Soln

Uploaded by

viratharsh267
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views92 pages

Data Structure II (Tree & Graphs) Basic Solution

Soln

Uploaded by

viratharsh267
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 92

Trees and Graphs

1. A perfect binary tree has every leaf on the same level, and every non-leaf node
has two children. A perfect binary tree with k leaves contains how many
nodes?
(a) k (b) k2
(c)log2 k (d) 2k -1
Answer: D
Solution:
Maximum number of nodes in perfect binary tree = 20 + 21 + 22 + ⋯ + 2𝑚 = 2𝑚 +1 − 1.

Let k =2m = number of leaves.

Then 2(m+1) – 1 =2⋅2m −1 = 2k–1; which is the total number of nodes.

2. A complete n-ary tree is one in which every node has 0 or n children. If x is the
number of internal nodes of a complete n-ary tree, the number of leaves in it is
given by
(a) x (n – 1) + 1 (b) xn – 1
(c) xn + 1 (d) x (n + 1)
Answer: A
Solution: If we take n = 2 then we have a complete binary tree in which every
node has 0 or 2 children. In this case, if the number of internal nodes is x then
number of leaves = x + 1. So, option A is satisfied.
3. A k-ary tree is a tree in which every node has at most k children. In a k-ary
tree with n nodes and height h, which of the following is an upper bound for
the maximum number of leaves node?
𝑛
(a) 𝑙𝑜𝑔𝑘 𝑛 (b)
𝑙𝑜𝑔 𝑘 𝑛

(c)𝑘𝑕 (d)𝑕𝑘
Answer: C
Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 1


32 = 9 = kh
so, number of leaves nodes is kh.
Answer: C

4. A Binary tree in which every non-leaf node has non-empty left and right sub
trees is called a strictly binary tree. Such a tree with 10 leaves
(a) has 19 nodes (b)has 16 nodes
(c)has 15 nodes (d) has 20 nodes
Solution:
Strictly Binary tree with 10 leaves

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 2


Total number of nodes= 19
Answer is A

5. What is the minimum and maximum height of a binary tree that contains 100
nodes? (Assume root at height 0)
(a) Min: 10; Max 99
(b) Min: 6; Max 50
(c) Min: 10; Max 50
(d)Min: 6; Max 99
Solution:
To be minimum height, tree should be complete binary tree.
 log 2 𝑛 + 1  − 1 𝑤𝑕𝑒𝑟𝑒 𝑕𝑒𝑖𝑔𝑕𝑡 𝑜𝑓 𝑟𝑜𝑜𝑡 = 0
In this case height =
 log 2 𝑛 + 1  , 𝑕 𝑟𝑜𝑜𝑡 = 1
Minimum height
= log 2 101 − 1 =7 – 1 =6
To be maximum height, tree should be chain,
Maximum height =100-1 =99
Answer is D
6. The level of a node is the length of the path (or number of edges) from the root
to that node. The level of a tree is equal to the level of its deepest leaf. A binary
tree has level k. Which represents
1. The maximum possible number of nodes, and
2. The minimum possible number of nodes in the tree
(a) 2k+1 , 2k + 1
(b) 2 k+1 , k

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 3


(c) 2 k+1 , k
(d) 2 k+1 -1, k + 1
(2017-3)

7. If you know that a tree is a BST, which of the following is always sufficient to
reconstruct it?
i) Pre-order traversal
ii) In-order traversal
iii) Post-order traversal
iv)Level-ordertraversal
(a) i & ii both
(b) ii & iii both
(c) iv only
(d) Either of i or iii

8. Given a binary tree with in-order sequence as g d h b e i a f j c and Pre-order

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 4


sequence a b d g h e i c f j. What is the post-order sequence of this tree?
(a) ghd i ebjfca
(b) ghde i bjfca
(c) jgdhbe i afc
(d) ghd i ebcfja

9. Given a binary tree with in-order sequence as


Inorder: g d h b e i a f j c and
Level order: a b c d e f g h i j.
What is the post-order sequence of this tree?
(a) ghdiebjfca
(b) ghde i bjfca
(c) jgdhbe i afc
(d) ghd i ebcfja
Answer: A
Solution:
We know that 1st element of level order traversal is the root element. And by
inorder traversal, we can say that the elements before root (in inorder) all will
be at left of the root and the elements after root (in inorder) all will be at right

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 5


of the root in tree.
So, we will do it recursively. Then we get following tree:

So, post-order will be: g h d i e b j f c a


10. Given a binary tree with in-order sequence as “ABICDGFHE”, and post-order
sequence as “AICBGHFED” respectively. What is the pre-order sequence of this
tree?
(a) DABICEFGH
(b) DBACIEFGH
(c) DEFHGBCIA
(d)DEFGHBACI

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 6


11. The following keys {2, 26, 10, 27, 20, 15, 42}are to be inserted into given
unlabeled tree such that it becomes a binary search tree

What keys will be at leaf nodes?


(a) 2 and 15 only
(b)15 and 27 only
(c) 2,15 and 27 only
(d) None
Answer: C

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 7


12. Consider the binary search tree shown below, after deleting 23 from the binary
search tree which parent → child pair does not occur in the tree?

(a) 25 → 27
(b) 27 → 11
(c) 11 → 7
(d) 7 → 9

Answer: B
13. The in-order traversal of a binary tree is HFIEJGZ, and the post-order traversal
of the same tree is HIFJZGE. What will be the total number of nodes in the left
sub tree of root of the given tree?

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 8


(a) 1
(b) 2
(c) 3
(d) 4

Solution: Inorder : H|F|I|E|J|G|Z

(L Rt R)

Pre order: E F H I G J Z
(Rt L R)
Number of nodes of left – subtree of root
=3
Answer is C
14. Consider a Binary Tree. Let A and B be two nodes in the tree. It is given that B
is the inorder successor of A, and A has 2 children. Which of the following is
true about B?
(A) B has no right child
(B) B has no left child
(C) B has both children
(D) None of the above
(2017-10)
Answer: B
Solution:
Since B is successor of A, then definitely B has no left child as if it does have a left
child then in that case B won‟t be successor of A as in inorder traversal, we follow
Left Node Right approach and if left child does exist then in that case that left child
will be inorder successor of A and not B.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 9


15. The Following keys 4, 10, 3, 8, 5, 6, 25 are inserted in that order into an
initially empty AVL tree. Total how many single and double rotations are
required?_______

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 10


16. Consider the three trees drawn below.
Which of the below trees is/are not AVL trees?

(A) i only (B) ii only (C) iii only (D) i and ii only
Answer: i, iii
Solution: (i) It doesn’t represent AVL tree because 6 should not be right childe
of 7.

So, (i) & (iii) are not AVL Tree.

17. Consider the following function do Something:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 11


int do Something (struct TreeNode *tree)
{
int x, y;
if (tree == NULL) return -1;
else {
x = 1 + doSomething (tree.getLeft());
y = 1 + doSomething (tree.getRight());
if (x >=y) return x;
else return y;
}
}
Method doSomething returns -1 for an empty tree. What does method
doSomething do when invoked for a nonempty tree?
(a) It returns the largest value in the binary search tree.
(b) It returns the number of nodes in the sub tree that has the greatest number
of nodes.
(c) It returns the level of the tree.
(d) It returns 1 plus the level of the tree.
Answer: C
Solution:
The idea is to start from the root and level as 1. If the key is null, return -1. Else
recursively call for left and right subtrees with level as level + 1.
18. What does following code return?
void doSomething (struct myp1 *p1)
{
if(p1 == NULL) return 0;
else if(p1->left == NULL && p1->right==NULL) return 1;
else return doSomething (p1->left) + doSomething (p1->right);
}
(a) Number of nodes in the binary tree.
(b) Number of leaf nodes in the binary tree.
(c) Number of non-leaf nodes in the binary tree.
(d) height of the binary tree.
Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 12


First traversal left,then right, if left >right
So, return left +1;
or return right +1:
means it return height of binary tree.
Answer:D

19. How many binary trees with 3 nodes, A, B, and C when traversed in post-order
will give the sequence A, B, C?
(a) 3
(b) 7
(c) 5
(d) 4
Solution: Post Order – A, B, C
(L R Rt)

Answer is C

20. What is the maximum numbers of levels (root is in level 1)of an AVL tree with 7
nodes?
(a)3
(b)4
(c)5
(d)6
Answer: B
Solution: The AVL tree of 7 elements with maximum levels:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 13


21. If you know that a tree is a BST, which of the following is always sufficient to
reconstruct it?
1. Pre-order traversal
2. In-order traversal
3. Post-order traversal
4. Level-order traversal:
(a) 1 and 4 only (b) 2 and 4 only
(c) 1, 2 and 3 only (d) 1, 3 and 4 only
Solution: If we have in-order and, one of the preorder or post-order then we
can construct a tree. In case of BST, sorted order is in-order. So, to construct a
tree, we should have either pre- order or post order. But if I know only in-order
then we are not able to construct a tree.
Correct Answer will be
1) Pre- order 3) Post – order and 4) level-order
Answer is D
22. Consider the following tree

If the post-order traversal gives a b-cd* +, then the label of the nodes 1, 2, 3,...

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 14


will be(respectively)
(a) +, -, *, a, b, c, d (b) a, -, b, +, c, *, d
(c) a, b, c, d, -, *, + (d) -, a, b, +, *, c, d

Solution:

Post –order traversal (L R Rt)


4 5 2 6 7 3 1
But given post –order is
a b - c d * +
 1=+, 2 = - ,
3= *, 4=a,
5=b, 6=c, 7= d
1, 2, 3,4,5,6,7 will be
+, -, *, a, b, c, d
Answer is A
23. Consider the following binary search tree:

Which one best describes the ranges of possible values for X and Y if
duplicates are not allowed?
(a)X >22 and Y >77
(b) 22 < X <44 and Y <77
(c) 22 < X <44 and Y >77
(d) 22 < X <44 and 44 < Y <77
Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 15


Since X is left child of 44 & Right child of 22
 22< X < 44-----(1)
and, Y is right child of 44 and Y is Parent of their right child 77.
44< Y < 77------(2)
from (1) & (2), (d) is correct.
Answer is D
24. Which statement is true after deleting 11 from this binary tree(node to be
deleted is replaced by its in-order predecessor?

(a) The value of the root is 4. The left child of the 6 node is null.
(b) The value of the root is 7. The 9 node is a leaf.
(c) The value of the root is 12. The 15 node is its right child.
(d) The value of the root is 9. The right child of the 6 node is 7.

Solution:
Inorder predecessor,just small element from 11.
so just smallest element from 11 is 9.
so 11 is replace by 9.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 16


So, root of this tree is 9, and right child of node 6 is 7.
Answer: D

25. Consider the AVL tree in the figure below which is not balanced after some
insertion.

Perform the appropriate rotation to restore the height balance of the tree. What
is the level order traversal of the tree after it has been balanced?
(a) R E X C M S Y A H P F
(b) R M X E P S Y C H A F
(c) M E R C H P X A F S Y
(d) E C M A H P F R X S Y

Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 17


LR: - RR+LL
RR perform on child Right become root and root become left.

than LL rotation on R(parent )

So, level order traversal is


M E R C H P X A F S Y.
Answer:C

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 18


For next five questions consider the following binary search tree

50

25 80

20 35 70 90

15 23
30 45 65 75 100

5 18
68 95 150

26. If the value 46 is inserted into this tree, which node becomes its parent?
_________
Solution:
If the value 46 is inserted into this tree so 46 will become right child of 45
because it greater than 45.
Answer: 45 become parent of 46.

27. If the value 64 is inserted into this tree, which node becomes its parent?
_________
Solution:
If value 64 is inserted into this tree than 64 is greater than 50, and less than
80, and less than 70 and less than 65 , so it become left child of 65.
Answer:65

28. If we delete node 65, which node should be its replacement node?__________

solution:
If we delete 65, then 68 at its place.
Answer:68.

29. If we delete node 90, which node should be its replacement node, when we
replace in order successor?___________
Solution:
Delete node 90, So order successor of 90 is 95.
than node 95 replaced by node 90.
Answer: 95.

30. If we delete the root node 50, which node should be selected as its replacement
node so that the fewest number of changes are made to the tree? __________
Solution:
When we delete Root, then it can replace by either it inorder successor or
Preorder.
BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 19
Inorder predecessor of 50 is 45.
Inorder successor of 50 is 65.
So, if it replaces by 45, so just replace it and no other changes required after
that and if it replace by 65, then 2 interchange require Ist 68 take it place than
65 take - place of 50.
Answer:45

31. Suppose we are storing binary tree in an array. What is the largest array that
might be necessary to hold a binary tree with 5 elements? ________
Solution:

If binary tree is like this.


So each node has two children in binary tree.
so binary tree node insert like this

So, size of array is 1+2+4+8+16= 31


So, maximum size of array is 31.
Answer:31

32. How many n node binary trees with items 1, 2, ..., n have identical post order
and in-order traversals?
(a) 0 (b) 1 (c) n (d) n!
Answer: D
Solution:
A binary tree can have same Post-order and Inorder traversal if there is no
right sub-tree anywhere that means every root either has left child or is the
final node(leaf). That means this binary tree is just a list, hence total ways of
arranging are n!

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 20


33. The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e
c f g, respectively. The Post-order traversal of the binary tree is:
(a) d e b f g c a
(b) e d b g f c a
(c) e d b f g c a
(d) d e f g b c a
Solution:
Inorder : traversal of binary tree.
dbeafcg
preorder traversal :-
abdecfg

post order (left right root) d e b f g c a.


Answer:A
34. The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25,
23, 39, 35, 42. Which one of the following is the post-order traversal sequence
of the same tree?
(a) 10, 20, 15, 23, 25, 35, 42, 39, 30
(b) 15, 10, 25, 23, 20, 42, 35, 39, 30
(c) 15, 20, 10, 23, 25, 42, 35, 39, 30
(d) 15, 10, 23, 25, 20, 35, 42, 39, 30
Solution:
Preorder traversal :
30, 20, 10, 15, 25, 23, 39, 35, 42.
Inorder traversal of binary search tree is sorted.
10,15,20,23,25,30,35,39,42.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 21


Post order:
15 10 23 25 20 35 42 39 30
Answer:D
35. The post-order traversal sequence of a binary search tree is:
1,4,3,6,8,5,11,10,12,21,31,30,20,9. Which one of the following is the pre-
order traversal sequence of the same tree?
(a) 9,5,3,1,4,8,6,20,12,10,11,30,21,31
(b) 9,5,3,4,1,8,6,12,20,10,11,21,30,31
(c)1,4,3,5,9,20,12,10,11,30,21,31,8,6
(d)None of the above
solution:
Post order traversal :
1,4,3,6,8,11,10,12,21,30,20,9
In order traversal :-
1, 3, 4, 5, 6, 8, 9, 10, 11, 12 ,20, 21, 30, 31

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 22


preorder traversal :
9 5 3 1 4 8 6 20 12 10 11 30 21 31
Answer:A

36. A binary search tree is generated by inserting in order of the following integers:
50,15,62,5,20,58,91,3,8,37,60,24. The number of nodes in the left sub tree
and right sub tree of the root respectively is
(a) (4,7)
(b) (7,4)
(c) (8,3)
(d) (3,8)
Solution:
50,15,62,5,20,58,91,3,8,37,60,24.
The number of nodes in the left sub tree.

Number of node in left subtree :- 7


Number of node in right subtree:- 4
Answer:B

37. While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 23


tree (BST) in the sequence shown, the element in the lowest level is_______
Solution:
71, 65, 84, 69, 67, 83

So element at lowest is 67.


Answer: 67

38. The following numbers are inserted into an empty binary search tree in the
given order:10,1,3,5,15,12,16. What is the height of the binary search tree (the
height is the max distance of a leaf node from the root)?
(a) 2 (b) 3 (c) 4 (d)
1
Solution:
10,1,3,5,15,12,16

So height of tree is =3
Answer: B

39. Suppose that we have numbers between 1 and 1000 in a binary search tree

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 24


and we want to search for the number 363. Which of the following sequences
cannot be the sequence of nodes visited in the search?
(a) 2, 252, 401, 398, 330, 344, 397, 363
(b) 924, 220, 911, 244, 898, 258, 362, 363
(c) 2, 399, 387, 219, 266, 382, 381, 278, 363
(d) 925, 202, 911, 240, 912, 245, 363
Solution:

202 240
925 911

then range is 240> x>911. and 912 is not in range.


Answer: D

40. Suppose that you are searching for the key 70 in a binary search tree Which of
the following sequences is/are the sequence of nodes visited in the search?
1. 77, 41, 99, 20, 85, 70
2. 99, 10, 80, 20, 60, 70
3. 5, 10, 80, 40, 32, 50, 70
4. 22, 58, 81, 70
(a)Only 2 and 4
(b) Only 3
(c) Only 1, 2 and 4
(d) Only 3 and 4

Solution:

(1)77, 41, 99, 20, 85, 70.


41
77
41 <x<77.
So 99 can’t be in this range so 1 is not valid sequence.
(b) check for option 2.
99, 10 , 80, 20, 60, 70.

so to can be search in this sequence.

(3)check for option 3.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 25


40>x<80
So 32 95 not in this sequence.
So sequence 3 is not possible.
(4) 22, 58, 81, 70

22 58
81
So 70 is in this sequence.
so sequence 4 is also possible.
Answer:A

41. Consider the Binary Search Tree built by inserting the following sequence of
integers, one at a time: 5, 4, 7, 9, 8, 6, 2, 3, 1. If the node containing 5 were
removed from the tree, what would be the right child of the node containing 2?
(a) Null
(b) The node containing 3.
(c) The node containing 4.
(d) The node containing 6.
Solution:
5, 4, 7, 9, 8, 6, 2, 3, 1

If remove 5 , than if either replace by inorder successor or predecessor.


So it replace by 6 or by 4.
but right child of 2 is still 3.
Answer:B

42. Consider the following binary search tree

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 26


How many possible insertion orders (i.e., permutations) of the keys that could
have produced this BST? _______
Solution:
It take(logn) time,because AVL tree is balance, so for finding in order
successor and predecessor take (logn ).
Answer:B

43. What is the time complexity for removing the root element from an AVL tree
containing N elements?
(a)θ(n)
(b)θ (logn)
(c) θ (n2)
(d) θ (nlog n)
Answer: B
Solution:
The rotation operations (left and right rotate) take constant time as only few
pointers are being changed there. Updating the height and getting the balance
factor also take constant time. So the time complexity of AVL delete remains
same as BST delete which is O(h) where h is height of the tree. Since AVL tree
is balanced, the height is O(Logn). So time complexity of AVL delete is O(Log n).
44. In the balanced binary tree (AVL) in the figure given below, how many nodes
will become unbalanced when a node W is inserted as a child of the node
“g”?____

Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 27


so, node c, b, and a is unbalanced because cost of node c has =2
cost of b has = 2
cost of a = 2
Answer: 3
45. Consider the balanced AVL tree constructed from the following (ordered)
sequence of values: 2 4 8 5 7 9 How many rotations were needed while adding
all the values to keep the tree balanced(here consider double rotation as a
single rotation)? _______

Solution:
248579

(1) RR :- RR rotation .

(2)LR Rotation :- RR+LL

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 28


than LL:-

(3) RR Rotation

So, number of rotations is 3.


Answer:3

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 29


46. Consider the following AVL tree

After deleting 31 from the tree by replacing it by its inorder predecessor. What
is the pre-order traversal sequence after deletion?
(a) 10, 19, 21, 25, 35,40, 49, 60
(b) 10, 21, 19, 35, 40, 60, 49, 25
(c) 25, 19, 49, 10, 21, 40, 60, 35
(d) 25, 19, 10, 21, 49, 40, 35, 60
Solution:

It is AVL tree , it is unbalanced at node 21. So it is LL rotation.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 30


Now preorder traversal:
Root Left Right.
25 19 10 21 49 40 35 60
Answer:D

47. What does following code return?


int doSomething(struct node *p1){
int left, right;
if(p1 == NULL) return 0;
left = doSomething(p1->left);
right = doSomething(p1->right);
if(left > right) return left +1;
else return right + 1;
}
(a) Number of nodes in the binary tree.
(b) Number of leaf nodes in the binary tree.
(c) Number of non-leaf nodes in the binary tree.
(d) height of the binary tree.
Solution:
First traversal left,then right , if left >right
so return left +1;
or return right +1:
means it return height of binary tree.
Answer:D

48. What does following code return?


void doSomething (struct myp1 *p1)
{
if(p1 == NULL) return 0;
else if(p1->left == NULL && p1->right==NULL) return 0;
else return 1+ doSomething (p1->left) + doSomething (p1->right);
}
(a) Number of nodes in the binary tree.
(b) Number of leaf nodes in the binary tree.
(c) Number of non-leaf nodes in the binary tree.
(d) height of the binary tree.
Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 31


p1→left = = Null and p1→Right = = Null
so return 1.
than p1→Right
so it return 3
so left and non-leaf both are 3.
so take some other example .

so it return 2 means it return number of non-leaf node in the binary tree.


Answer:C

49. What value does the following function returns called with the root of the
following binary tree as parameter?_________

int doSomething(struct Node *p1)


{
if (p1 == NULL) return 0;
else if (p1->left == NULL && p1->right == NULL) return 1;
else if (p1->left==NULL) return doSomething(p1->right) + 1;

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 32


else if (p1->right == NULL) return doSomething(p1->left) + 1;
return min(doSomething(p1->left), doSomething(p1->right)) + 1;
}
Solution:

so minimum is 2 at root
so 2+1 =3
Answer: 3
50. What sequence of values does the following function print called with the root
of the following binary tree as parameter?

int doSomething (struct Node *p1)


{
if (p1 != NULL)
{
printf("%d", p1->data);

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 33


doSomething(p1->left);
printf("%d", p1->data);
doSomething(p1->right);
}
}
(a)24,9, 3, 11, 4,4 ,3,9,15,15,24
(b)24,9,3,1,1,3,4,4,15,15,9,24
(C)24, 9, 3,1,1,3,4,4,9,15,15,24
(d) 24,9,3,1,1,3,9,4,4,15,15,24
Answer: C
Solution:

24 9 3 1 1 3 4 4 9 15 15 24
So possible sequence is
24,9,3,1,1,3,4,4,9,15,15,24.
Answer:C
51. Consider a level order traversal of the following binary tree. Which node is the
last node enqueued before the node containing y is dequeued?

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 34


(a) The node containing c.
(b) The node containing o.
(c) The node containing m.
(d) The node containing s.
Answer: D
Solution: parent is deleted,if all child node is visited so before y is dequeued
node S is enqueued.
52. If we insert n items into a BST, and these items are in sorted order (from least
to greatest), what is the total runtime of all n of these insertions?
(a)O(n)
(b) O(n2)
(c) O (loglog n)
(d) O (logn)
Solution:
If 1st node isinserted, then comparison 1.
If 2nd node is inserted, then comparison 2.
and nth node insert then comparison =n
If tree is binary search tree
so, 1+2+3…n
𝑛(𝑛+1)
so = O(n2)
2
so, complexity is O(n2)
Answer: B
53. What function is performed by the code given below?
struct BTNode{
struct BTNode * left;
struct BTNode * right;
int value;
};
int func(struct BTNode * root) {
int i_left, i_right;
if(root == NULL)
return 0;
else{
i_left = func(rootleft);
i_right = func(rootright);
if(i_left > i_right)
return (i_left+1);

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 35


else
return (i_right+1);
}

(a) Find number of nodes in the tree


(b) Find depth of the tree
(c) Find number of leaves in the tree
(d) None of the above

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 36


54. This function SumElements finds the sum of all the elements in the binary
tree.
Complete the code
struct BTNode{
struct BTNode * left;
struct BTNode * right;
int value;
};
int SumElements(struct BTNode * root){
if(root == NULL)
return 0;
else
return ________
}
(a) rootdata + SumElements(rootleft)
(b) rootdata + SumElements(rootright)
(c) rootdata + SumElements(rootleft) + SumElements(rootright)
(d) SumElements(rootleft) + SumElements(rootright)

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 37


55. The given code checks whether the given binary tree is a Binary search tree or
not. Assume that we have FindMaxand FindMinthat return the min or max
integer value from a nonempty tree.
struct BTNode{
struct BTNode * left;
struct BTNode * right;
int value;
}
int isBST(struct BTNode * root){
if(root == NULL)
return 1;
if(rootleft !=NULL &&FindMin(rootleft > rootdata) // P
return 0;
if(rootright !=NULL &&FindMax(rootright < rootdata)// Q
return 0;
if(!isBST(rootleft) || !isBST(rootright)) return 0;
return 1;
}
(a) No Logical error
(b) Error in line “P”
(c) Error in line “Q”
(d) Error in both lines “P” and “Q”

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 38


56. What does the following code mystery compute when called with the root node
of a binary tree?
struct node
{
int data;
struct node* left;
struct node* right;
};
void mystery(struct node* node)
{
if (node==NULL) return;
else
{
struct node* temp;
mystery(node-->left);
mystery(node-->right);
temp = node-->left;

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 39


node->left = node->right;
node->right = temp;
}
}
(a) Interchanged left and right children of some non-leaf nodes.
(b) Interchanged left and right children of all non-leaf nodes.
(c) Interchanged parent and right children of all non-leaf nodes.
(c) Interchanged parent and left children of all non-leaf nodes.
Solution:

The above 3 line of code interchange left and right children of all non- leaf
nodes.
Answer is B
Consider the following next three questions:

57. What will be the printed value when called printOrder1with the root node of a
given binary tree?
void printOrder1(struct node* node)
{
if (node == NULL)
return;
printOrder1(node->left);

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 40


printOrder1(node->right);
printf("%d\t ", node->data);
}
(a) 5 15 12 9 23 50 52 49 20
(b) 20 9 49 5 12 23 52 15 50
(c) 5 9 12 15 20 23 49 50 52
(d) 20 9 5 12 15 49 23 52 50
Solution: PrintOrder1 return post-order traversal of given tree.
Printed value will be
5 15 12 9 23 50 52 49 20
Answer is A

58. What will be the printed value when called printOrder2with the root node of a
given binary tree?
voidprintOrder2(struct node* node)
{
if (node == NULL)
return;
printOrder2(node->left);
printf("%d\t ", node->data);
printOrder2(node->right);
}

(a) 5 15 12 9 23 50 52 49 20
(b) 20 9 49 5 12 23 52 15 50
(c) 5 9 12 15 20 23 49 50 52
(d) 20 9 5 12 15 49 23 52 50
Solution: PrintOrder2 return In-order traversal of given tree.
Printed value will be
5 9 12 15 20 23 49 50 52
Answer is C
59. What will be the printed value when called printOrder3with the root node of a
given binary tree?

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 41


void printOrder3(struct node* node)
{
if (node == NULL)
return;
printf("%d\t ", node->data);
printOrder3(node->left);
printOrder3(node->right);
}
(a) 5 15 12 9 23 50 52 49 20
(b) 20 9 49 5 12 23 52 15 50
(c) 5 9 12 15 20 23 49 50 52
(d) 20 9 5 12 15 49 23 52 50
Solution: PrintedOrder3 return pre-order traversal of given tree.
Printed value will be
20 9 5 12 15 49 25 52 50
Answer is D
60. What does the following Mystery function do when called with the root node of
a given binary tree?______

int Mystery(struct node *node)


{
if(node == NULL)
return 0;
int old_val = node->data;
node->data = Mystery(node->left) + Mystery(node->right);
return node->data + old_val;
}
Solution: The given function sum the key of all node.
returned value= 8-4-2+10+6+7+5
=2+10+18
=30

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 42


Answer is 30
61. If the seven keys A, B, C, D, E, F, and G have been inserted into a binary
search
tree and C is in node x, then where could D be?
i. The parent of x.
ii. The grandparent of x.
iii. The right child of x.
iv. The left child of the right child of x.
v. The right child of the right child of x.
(a) Only i.
(b) Only i and ii.
(c)Only i, ii, and iii.
(d) Only i, ii, iii, and iv.
Solution: Order of Insertion
ABCDEFG

(i) D could of parent of x.

D>C
(ii) D Could be grand – parent of x

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 43


it is true.
(iii) D could be right child of x

It is true.
(iv) D could be left child of the right child of x.

It is true.
(v) D couldn’t be the right child of the right child of x, because there doesn’t
exist any letter which is greater than c and less than D.
Answer is D

62. Which of these is not a binary search tree?

Solution:
In BST, parent node > left child
Parent node < Right child
(a)

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 44


(b) ,(c),& (d) are BST.
Answer is A

63. Suppose that we have numbers between 1 and 1000 in a binary search tree
and
want to search for the number 363. Which of the following sequences couldnot
be the sequences of nodes examined?
(I) 2,252,401,398,330,344,397,363.
(II) 924,220,911,244,898,258,362,363.
(III) 925,202,911,240,912,245,363.
(IV) 2,399,387,219,266,382,381,278,363.
(V) 935,278,347,621,299,392,358,363.
(a) I, II and III only (b) III and V only
(c) I and II only (d) I, II and IV only
Solution: We want to search for the 363.
I. 2,252,401,398,330,344,397,363

It is true.
II. 924,220,911,244,898,258,362,363
363<924, 220<363 (let x=363)

It is true.
III:- 925,202,911,240,912,245,363

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 45


912 should be before 911.
IV:- 2,399,387,219,266,382,381,278,363.

It is true.

299 should be before 347.


Answer is B

64. The following items are inserted into a binary search tree: 3, 6, 5, 2, 4, 7, 1.
which node is the deepest?
(a) 1 (b) 3 (c) 4 (d) 7
Solution: Order of insertion in BST
3, 6, 5, 2, 4, 7, 1
 BST will be

Answer is C

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 46


65. What binary search tree is obtained after the root of this tree is deleted?

i) ii)

(a)Only I
(b) Only II
(c) both I and II
(d) neither I nor II
Solution:

In- order : 3 5 6 7 10 11 14 17
If we delete 7 then there are two possible candidate to be root ( in place of 7 )
either in –order successor i.e. 10 or in-order predecessor i.e . 6.
Case I : - When we take in-order successor as a root then tree will be

Case II: When we take in-order predecessor as a root then tree will be

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 47


Answer is C
66. Which of these is not an AVL tree?

Solution:

A BST is AVL iff balance of every node of tree is 0, 1, or -1.


Answer is D
For next two questions consider that the following keys are inserted keys into an
AVL tree in the Following order:40, 20, 15, 25, 30, 80, 75, 95, 90, 35, 100
67. Which key would be in the root of the tree after inserting all the keys?
(a)75 (b) 40
(c) 35 (d) 30
Solution: 40, 20, 15, 25, 30, 80, 75, 95, 90, 35, 100

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 48


RL


Insert 90
RL

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 49


root of the tree =75


Answer A

68. What is the height of the above AVL tree?(Assume root at height zero)
(a) 3 (b) 4 (c) 5 (d) 6
Solution: Height of above tree=3
Answer is A

69. Consider an AVL tree of height 5(Assume root at height zero). What is the
smallest
number of keys it can store? _____
Solution:
Height =5 (height of root =0)

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 50


minimum number of node =20
2ndMethod :
Recurrence relation for finding minimum number of node
T(n) =T(n-1) +T(n-2) +1
T(0)=1, T(1) =2, T(2) =4
T(3) =T(2) +T(1) +1=7
T(4) =T(3) +T(2) +1 =7+4+1 =12
T(5) =T(4) +T(3) +1= 12+7+1 =20
Answer is 20

70. Which of the following complexity analysis is/are correct?


1. The worst case running time for building a binary search tree is O(nlogn).
2. The worst case running time for building anAVL tree is O(nlogn).
3. The worst case running time for building a binary search tree is Ɵ(n2).
4. The worst case running time for building anAVL tree is Ɵ(n2).
(a) 2and 3 only
(b) 3 and 4 only
(c) 1 and 2 only
(d) 1 and 4 only
Solution: The worst case running time for building BST is Ɵ (n2) .Because for
worst case, BST may form chain forms, then complexity will be Ɵ (n2). The
worst case running time for building AVL tree is O(nlogn) .For every insertion

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 51


logn time required, since.AVL tree is balanced BST during insertion , depth of
tree will be always logn.
Complexity = Ɵ(nlogn)
Answer is A
71. Items 7, 3, 11, 9, and 13 are inserted into an AVL tree. What happens when 12
is inserted?
(a) a single rotation between some node and its left child is performed
(b) a single rotation between some node and its right child is performed
(c) a double rotation with a node, its left child, and a third node is performed
(d) a double rotation with a node, its right child, and a third node is performed
Solution:

Now, 12 is insertion

After RR Rotation , tree will be

= A single rotation between some node and its right child is performed.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 52


Answer is B
72. The following items are inserted into an AVL tree: 1, 2, 3, 8, 6. how many
rotations are performed?
(a) no rotations
(b) 1 single rotation only
(c) 1 double rotation only
(d) 1 single rotation and 1 double rotation
Solution:
Order of insertion 1 2 3 8 6

1 single rotation and 1 double rotation


Answer is D
73. In how many ways we can insert the elements {1, 2, . . . , 7} into an empty AVL
tree so that we don’t have to perform any rotations on it?___________
Solution: You should insert in the order { 4,2, 6, 1, 3, 5, 7} to make an AVL
tree.

The ordering of {2,6} and the ordering of {1,3, 5, 7} do not matter.


two case of { 2, 6}
and 4b case of {1,3, 5, 7}
total number of cases= 24! =48
Answer is 48

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 53


74. The keys 1, 2 3....n are inserted randomly into a binary search tree. What is
Theprobability that right- subtree of root contains exactly n-2 keys
(a) 1/n2 (b) 1/n
(c)1/(n+1) (d) (n-2)/n
Solution: All possible cases for cardinality of right subtree ={0,1,2,3…….., n-1}
Required probability = 1/n
Answer is B
Graph
75. Suppose we perform a breadth-first search of this graph starting at node 4. Which nodes
could be the last node visited by the search?

(A) 6 (B) 2 (C) 8 (D) 7

Answer: B

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 54


Consider the following graph for next Two questions:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 55


76. [MSQ]

Which of the following discovering sequences is/are possible if we search the graph by
BFS and use B as the source vertex?

(a) B,C,E,A,F,G,J,D,H,I (b) B,A,C,E,D,F,J,G,I,H

(c) B,E,C,A,F,G,J,H,D,I (d) B,A,E,C,F,D,J,G,H,I

Answer:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 56


BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 57
BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 58
BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 59
77. [MSQ]

Which of the following discovering sequences is impossible if we search the graph by


DFS?

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 60


(a) A, D, F, H, G, J, I, C, E, B (b) H, F, G, J, I, B, A, C, E, D

(c) B, E, J, I, H, G, F, A, D, C (d) B, A, C, E, F, G, J, H, D, I

Answer: C

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 61


78. Given the following graph, which sequence can be a DFS and BFS sequence both?

(A)DHBFACGE (B)DHBFAGEC (C)DBHFACGE (D)DHBFAGCE

Answer: D

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 62


79. Match the properties to the trees (Note: G is a graph and T is its spanning tree/DFS
tree/BFS tree)

1. In a spanning tree (a) every node at distance d in G is at depth d in T.

2. In a DFS(b) there is exactly one path from any node in T to the root

3. In a BFS (c) every pair of neighbors in G is on a path from the root in T

(A) 1-c, 2-a, 3-b (B) 1-b, 2-c, 3-a (C) 1-a, 2-b, 3-c (D) None

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 63


80. Suppose that during an execution of depth-first search in a digraph G, dfs(v) is called
after dfs(w) is called, but before dfs(w) returns. Which of the following must be true of
the graph G?

(A)There exists a directed path from v to w.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 64


(B) There exists a directed path from w to v.

(C) There does not exist a directed path from v to w.

(D)There exists a directed cycle containing both v and w

Answer: B

If we create recursive tree of DFS function, we will observe if there exist a path from
any vertex v to other vertex w then dfs(v) is called after dfs(w) but before dfs(w)
returns.

81. Select the correct statement

1. For a directed graph, the absence of back edges with respect to a BFS tree implies that
the graph is acyclic.

2. The depth of any DFS tree rooted at a vertex is at least as much as the depth of any
BFS tree rooted at the same vertex.

3. There is no edge in an undirected graph that jumps more than one level of any BFS
tree of the graph.

4. In an unweighted graph where the distance between any two vertices is at most T,
any BFS tree has depth at most T, but a DFS tree might have larger depth.

(A)2 an d 3 only (B) 3 and 4 only

(C) 2, 3 and 4 only (D) 1, 2 and 3 only

Ans: C
1. FALSE. It is true that the absence of back edges with respect to a DFS tree implies
that the graph is acyclic. However, the same is not true for a BFS tree. There may be
cross edges which go from one branch of the BFS tree to a lower level of another branch
of the BFS tree. It is possible to construct a cycle using such cross edges (which decrease
the level) and using forward edges (which increase the level).

2. TRUE. Since all vertices are connected by a path with at most T edges, and since BFS
always finds the path with the fewest edges, the BFS tree will have depth at most T. A
DFS tree may have depth up to V −1 (for example, in a complete graph).

3. TRUE. If such an edge existed, it would provide a shorter path to some node than the
path found by BFS (in terms in the number of edges). This cannot happen, as BFS

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 65


always finds the path with the fewest edges.

4. Ref Point(2).

82. Which sequence corresponds to that of depth first search for the graph given below?
The search starts at vertex 0 and lexicographic ordering is assumed for the edges
emanating from each vertex.

(A) 0 1 2 4 3 5 (B) 0 1 2 5 4 3

(C) 0 1 2 3 4 5 (D) 0 1 3 4 2 5

[43-2017]

83. Given a rooted tree, one desires to find the shortest path from the root to a given node
v. Which algorithm would one use to find this shortest path?

(A) DFS (B) BFS

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 66


(C) Either BFS or DFS (D) neither BFS nor DFS

Answer: C

A tree has a unique path between any two pairs of nodes. Any traversal strategy would
give us the path (which is the shortest path).

84. Consider an undirected graph G. Let T be a depth first search traversal tree. Let u be a
vertex and v be the first unvisited vertex after visiting u. Which of the following
statements is always true?

(A) (u, v) must be an edge in G

(B) (u, v) must be an edge and v is a descendant of u in T

(C) if (u, v) is not an edge, u and v have the same parent.

(D) if (u, v) is not an edge, then u is a leaf.

Ans:

In DFS, if 'v' is visited


after 'u', then one of the following is true.
1) (u, v) is an edge.
u
/ \
v w
/ /\
x y z

2) 'u' is a leaf node.


w
/ \
x v
/ /\
u y z

In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited
children (u is leaf), then control goes back to parent and parent then visits next unvisited children.

85. Consider a graph G. Let T be a BFS tree with root r. Let d(u,v) denote the length of the
shortest path between the nodes u and v. If v is visited before u in the breadth first

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 67


search traversal, which of the following statements is true?

(A) d(r, v) > d(r, u)

(B) d(r, v) = d(r, u)

(C) d(r, v) < d(r, u)

(D) insufficient information to comment on d(r, v) and d(r, u)

Answer is (C). BFS is used to count shortest path from source (If all path costs are 1)
Now, if u is visited before v it means 2 things: Either u is closer to v, or, If u & v are
same distance from r, then our BFS algo chose to visit u before v.

86. Consider the following graph:

The incident matrix of the given graph is

1 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0
1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0
(a) (b) (c) (d)
1 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1
0 0 1 1 1 0 1 0 0 0 1 1 0 0 1 1

Answer: C

Solution: The incident Matrix of the given graph is

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 68


87. We apply the algorithm DFS to the complete graph Kn. How many non-

isomorphic trees can we obtain, Depending on the initial vertex?

(a)n! (b) (n-1)! (c)nn-2 (d) None of these

Answer: B

Solution:

ABC, ACB

So, number of spanning trees if n = 3, is 2.

ABCD
ACBD
ACDB
ADCB
ABDC
ADBC
number of spanning trees =6
The number of non-isomorphic tree, after applying DFS on Kn, if initial vertex is
given. For starting vertex 1, For other vertex, (n-1)!. number of spanning trees
=1* (n-1)!.

Answer is (n-1)!

88. How many different BFS output are possible for following graph considering 1 as a
source
vertex? _______

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 69


Answer: 9

Solution:

1 as source vertex hence.

1 2 3 4 5 6 8 7 9

1 2 4 3 5 6 8 7 9

1 3 4 2 6 7 8 5 9

1 3 2 4 6 5 8 7 9

1 2 4 3 5 8 6 9 7

1 4 2 3 6 8 5 7 9

1 4 2 3 8 6 5 9 7

1 4 3 2 6 8 5 7 9

1 4 3 2 8 6 5 9 7

hence total 12-3 =9

Answer:9

89. [MSQ]

Consider the following graph

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 70


Which of the following is/are possible output of DFS ?

(a) ABEHGDCFJ (b) ABDCFGHEJ

(c) ACDEHJBFG (d) ABDEHJGFC

Answer: a, b, d

Solution:

ABEHGDCFJ is valid.

ABDCFGHEJ is valid.

ACDEHJBFG is not valid.

ABDEHJGFC is valid.

ACDEHJBFG is not valid because in backtracking if we backtrack then after D, G must be traversed
but it traverses F hence not possible output.

Answer: C

90. Consider a following directed graph:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 71


A breadth-first search traversal of the graph above will visit the nodes in which one of
the following orders? (Alphabetical order is used when there is more than one option.)

(a) ABEGFCHD (b) ABDGFECH

(c) ABDGEFCH (d) AGBDEFCH

Solution:

BFS so option B and C, D both are there but we have to choose alphabetic order hence (c) is
correct.

Answer: C

91. [MSQ]
Which of the following statements are correct?
(a) For a directed graph, the absence of back edges with respect to a BFS tree implies
that the graph is acyclic.
(b) There is no edge in an undirected graph that jumps more than one level of any BFS
tree of the graph.
(c) The depth of any DFS tree rooted at a vertex is at least as much as the depth of any
BFS tree rooted at the same vertex.
(d) None of the above
Answer: b, c

Solution:

(a) For a directed graph, the absence of bake edges with respect to a BFS tree implies that the graph
is acyclic (False)..

Reason: Because, there may be cross edges which go form one branch of the BFS tree to a lower
level of another branch of the BFS tree. It is possible to construct a cycle using such cross edges
(Which decrease the level) and using forward edges (which increase the level).

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 72


(b) There is no edge in an undirected graph that jumps more than one level of any BFS tree of
graph.

(True) .

Reason: If such an edge existed, it would provide a shorter path to some node than the path found
by BFS (in terms of number of edges). This cannot happen as BFS always finds the path with
fewest edges.

(c) The depth of any DFS tree rooted at a vertex is at least as much as the depth of any BFS tree
rooted at the same vertex. (True).

Reason: Since BFS finds paths using the fewest number of edges, the BFS depth of any vertex is at
least as small as the DFS depth of the same vertex.

Thus, the DFS tree has a greater or equal path.

Answer: b, c

92. Suppose that you perform a DFS on an undirected graph G = (V, E) and for each vertex
u ∈ V. You compute the discovery time d[u] and finish time f[u]. Let u be a descendant
of v in the DFS tree. What can be said about the relative order of d[u], f[u], d[v], and
f[v]? ___________

(a) d[v] < d[u] < f[u] < f[v]. (b) d[u] < d[v] < f[v] < f[u].

(c) d[v] > d[u] > f[u] > f[v]. (d) d[v] < d[u] < f[v] < f[u].

Solution:

DFS an undirected graph G= (V, E) and for each vertex uv. you compute the
discovery

time d[u] and finish time f[u] . u be a descendant of v in DFS tree. So here is DFS we

know parent discover first then child but finish after child. as here v is parent and u is
child. Order will be d[v] < d[u] < f[u] < f[v].

Answer: A

93. Consider a following directed graph:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 73


A depth-first search traversal of the graph above will visit the nodes in which one of
the following orders? (Alphabetical order is used when there is more than one option.)

(a) ABDGEFCH (b) ABDEFCHG (c) ABEGFCHD (d) ABDFCHEG

Answer: None
Solution: All are incorrect.
DFS order: ABEFCHDG

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 74


94. Consider the following directed graph:

Suppose a depth first traversal of this graph is performed starting vertex ‘a’,

assuming that whenever there is a choice, the vertex earlier in the alphabetical order is
to be chosen. Then:

I. Number of tree edges is T= _____________

II. Possible Number of back edges in T= ____________

III. Possible Number of forward edges in T= _________

IV. Possible Number of cross edges in T= ___________

Solution:
I :- Number of tree edge = 5
Tree edges will be

II :- Possible back edge is „ca‟


 number of back edges =1
III :- Possible forward edge are „af‟ , „ae‟
number of forward edges =2
IV :- Possible cross edges is „db‟, ec
 number of cross edges = 2

95. Consider the following directed graph:

Suppose a breadth first traversal of this graph is performed with starting vertex ‘a’,

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 75


assuming that whenever there is a choice, the vertex earlier in the alphabetical order is
to be chosen. Then:

I. Number of tree edges is T= _____________


II. Possible Number of back edges in T= ____________
III. Possible Number of forward edges in T= _________
IV. Possible Number of cross edges in T= ___________
Solution: I :- Number of tree edges= 5
After applying BFS, traversed path (tree ) are

II :- Possible back edges are „ac‟


Number of back edges =1
III:- In BFS traversal, there are no forward edge=0
IV:- Possible cross edges are bd, ce, ef ,be
Number of cross edges =4

96. Let A be the adjacency matrix of the graph G given below. For n ≥ 1, let pn denote the
entry of An in the row labeled a and the column labeled c. Select the correct answer:

(A) p1 = 0, p2 = 1, p3 = 1 (B) p1 = 0, p2 = 2, p3 = 2

(C) p1 = 0, p2 = 2, p3 = 2 (D) p1 = 0, p2 = 2, p3 = 3

Solution:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 76


a b c d e f g h i

a 0 1 0 0 0 1 1 1 0

b 1 0 1 0 0 0 0 0 0

c 0 1 0 1 0 0 0 1 1

d 0 0 1 0 1 0 0 0 0

e 0 0 0 1 0 1 1 0 1

f 1 0 0 0 1 0 0 0 0

g 1 0 0 0 1 0 0 1 1

h 1 0 1 0 0 0 1 0 1

i 0 0 1 0 1 0 1 1 0

Path of length 2 are “abc” and “ahc”.

p2=2

Path of length 3 are ahic, aghc, andagic

P3 =3, P2=2, P1=0

Answer is D

97. Examine this graph given as an adjacency list:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 77


Which of the following is the possible output of DFS? Whenever you have a choice of
which vertex to visit next, take the lowest vertex in alphabetical order.

(A) a b d c e (B) a b c e d (C) a c e b d (D) e d b a c

[Q65- 2019]

Solution: a, b, c, e, d

Answer is B

98. Let M = [mi,j] be the adjacency matrix of a normal graph (that is, not a digraph). If m1,2 =
1, then what is m2,1?

(A) -1 (B)0

(C)1 (D) Not enough information to tell

Answer: C

Since graph is undirected, therefore if there is an edge from vertex 1 to vertex 2 then definitely
there will be an edge from vertex 2 to vertex 1 and hence m2,1= 1.

99. If G is a directed graph with 20 vertices, how many Boolean values will be needed to
represent G using an adjacency matrix?

(A) 400 (B) 40 (C) 200 (D) 20

Ans: A

Since there are 20 vertices therefore the matrix size will be 20x20 and hence we will
need 400 values.

100. Which choice lists all vertices that have incorrect rows in the adjacency matrix for this
graph?

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 78


(A) I (B) I K (C) B (D) B E F I K

Ans: D

Entries for vertex B is wrong as clearly edge B-I has weight 3 and meanwhile matrix has entry 4.
Vertex E has 3 entries and E has degree 2 hence it is wrong. Same reason for vertices F, I and K as
well.

101. Four graphs are represented using an adjacency matrix as shown below. Which of the
following adjacency matrix represents a connected graph?

Ans:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 79


102. Change two zeros to ones in the following matrix so that it is the adjacency matrix of a
tree. How many ways can this be done? __________

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 80


103. [MSQ]

Consider the following directed acyclic graph:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 81


Which of the following sequence is/are the correct topological sorting?

(a)1 2 3 4 5 (b) 1 2 3 5 4 (c) 1 3 2 4 5 (d) 1 2 4 3 5

Answer: a, b

104. Consider the following directed acyclic graph:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 82


How many different topological orderings are possible for the above graph?
___________

Answer: 13

Solution:

105. [MSQ]

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 83


Consider the following directed acyclic graph G:

For the above graph which of the following order is/are correct topological ordering?

(a) 1 2 3 4 5 6
(b) 1 2 3 4 6 5
(c) 1 3 2 4 6 5
(d) 1 3 2 4 5 6
Answer: a, b, c, d

Solution:

106. Consider the following adjacency list representation of graph G:

Its adjacency matrix representation will be

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 84


0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1
1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1
(a) 0 1 0 1 1 (b) 0 1 1 1 0 (c) 0 1 0 1 0 (d) 0 1 0 1 0
0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1
0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1

Answer: D

Solution: The equivalent graph and adjacency matrix are shown below:

and

107. Consider the following adjacency list representation of graph G:

How many strongly connected component does G have? __________

Answer: 4

Solution: By above adjacency list, the following graph is constructed.

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 85


The different strongly connected component is

So, number of SCC = 4.

Data for next four questions: Consider the following simple directed graph G:

Suppose a breadth first traversal of this graph is performed with starting vertex ‘a’, assuming
that whenever there is multiple choice, the vertex earlier in the alphabetical order is to be
chosen.

Common Solution: The tree of BFS traversal of given graph is shown below:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 86


108. Which of the following tree(s) represents BFS tree for the given condition?

(a)I only (b) II only (c) Both (d)None

Answer: A

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 87


109. How many forward edges are there? _________

Answer: 0

110. Which of the following sets represents back edges?

(a){ } (b) {ei, di, cg} (c) {ia, jh} (d) {ia}

Answer: c

111. Which of the following sets represents cross-edges?

(a){ag, ai} (b) {ei, di, cg} (c) {ei, bi, cg} (d) {cg,
ei}

Answer: B

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 88


Data for next four questions: Consider the following simple directed graph G:

Suppose a depth first traversal of this graph is performed starting vertex ‘a’.
Assuming that whenever there is multiple choice, the vertex earlier in the alphabetical order
is to be chosen.

Common Solution: The tree of DFS traversal of given graph is shown below:

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 89


112. Which of the following sets represents tree edges?

(a) {ab, be, ei, ad, dh, hj, ac, cg, cf}

(b){ad, ac, cf, ag, dh, hj, ab, be, be}

(c){ab, ac, ad, ag, bi, be, cf, dh, hj}

(d) Both a & b

Answer: A

113. Which of the following sets represents back edges?

(a) {ia, jh} (b){ia} (c){ia, jh, di} (d) {}

Answer: A

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 90


114. Which of the following sets represents cross-edges?

(a){ag, di} (b){bi, ag} (c) {ai, bi} (d) {di}

Answer: D

115. Which of the following sets represents forward edges?

(a){ag, di} (b){bi, ag} (c) {ai, bi} (d) {}

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 91


Answer: B

BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 92

You might also like