75
Consider the directed graph shown in the figure below . Th ere are multiple shortest paths between vertices S and T. Which one will be reported by Dijstra?s shortest path algorithm? Assume that in any iteration the shortest path to a verte! v is updated only when a strictly shorter path to v is discovered. "A# SDT "$# S$DT "C# SACDT "D# SAC%T  Answer "D# GATE2012

DataStructure GATE

  • Upload
    uanuli

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 1/75

Consider the directed graph shown in the figure below. There are multiple

shortest paths between vertices S and T. Which one will be reported by

Dijstra?s shortest path algorithm? Assume that in any iteration the shortest

path to a verte! v is updated only when a strictly shorter path to v isdiscovered.

"A# SDT

"$# S$DT

"C# SACDT

"D# SAC%T

 Answer "D#

GATE2012

Page 2: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 2/75

Suppose a circular &ueue of capacity "n ' (# elements is implemented with

an array of n elements. Assume that the insertion and deletion operation

are carried out using )%A) and *)+,T as array inde! variables

respectively. -nitially )%A) *)+,T /. The conditions to detect &ueuefull and &ueue empty are

"A# *ull0 ")%A)1(# mod n *)+,T empty0 )%A) *)+,T

"$# *ull0 ")%A)1(# mod n *)+,T empty0 "*)+,T1(# mod n )%A)

"C# *ull0 )%A) *)+,T empty0 ")%A)1(# mod n *)+,T

"D# *ull0 "*)+,T1(# mod n )%A) empty0 )%A) *)+,T

 Answer "A#

Page 3: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 3/75

The height of a tree is defined as the number of edges on the longest

path in the tree. The function shown in the pseudocode below is

invo2ed as height "root# to compute the height of a binary tree rooted

at the tree pointer root.

Page 4: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 4/75

The appropriate expression for the two boxes B1 and B2 are

"A# $( 0 "( 1 height"n34right## $5 0 "( 1 ma!"h(h5##

"$# $( 0 "height"n34right## $5 0 "( 1 ma!"h(h5##

"C# $( 0 height"n34right# $5 0 ma!"h(h5#"D# $( 0 "( 1 height"n34right## $5 0 ma!"h(h5#

 Answer "A#

The bo! $( gets e!ected when left subtree of n is ,677 and right sbtree

is not ,677. -n this case height of n will be height of right subtree plus

one.

The bo! $5 gets e!ecuted when both left and right sbtrees of n are not

,677. -n this case height of n will be ma! of heights of left and rightsbtrees of n plus (.

Page 5: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 5/75

 An undirected graph 8"9 %# contains n " n 4 5 # nodes named v( v5 :.vn.

Two nodes vi vj are connected if and only if / ; <i ' j< ; 5. %ach edge "vi

vj # is assigned a weight i 1 j. A sample graph with n = is shown below.

GATE2011

What will be the cost of the minimum spanning tree (!T" of such a graph

with n nodes#

"A# (>(5"((n5 ' @n#

"$# n5 ' n 1 (

"C# n ' ((

"D# 5n 1 (

Page 6: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 6/75

Binimum spanning tree for 5 nodes would be

"v(# "v5# Total weight

Binimum spanning tree for nodes would be

"v(# "v5# < "v# Total weight 1 = E

Binimum spanning tree for = nodes would be

"v(# "v5# "v=# < "v# Total weight 1 = 1 (

Binimum spanning tree for @ nodes would be

"v(# "v5# "v=# < "v# < "v@# Total weight 1 = 1 1 F 5(

Binimum spanning tree for nodes would be

"v(# "v5# "v=# "v# < "v# < "v@# Total weight 1 = 1 1 F 1 (/ (

We can observe from above e!amples that when we add 2th node theweight of spanning tree increases by 5235. 7et T"n# be the weight of

minimum spanning tree. T"n# can be written as

T"n# T"n3(# 1 "5n35# for n 4 5

T"(# / T"5# / and T"5#

The recurrence can be written as sum of series "5n ' 5# 1 "5n3=# 1 "5n3# 1"5n3F# 1 :. and solution of this recurrence is n5 ' n 1 (.

Page 7: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 7/75

The length of the path from v@ to v in the BST of previous &uestion

with n (/ is

"A# ((

"$# 5@"C# (

"D# =(

 Answer0 "C#

 Any BST which has more than @ nodes will have the same distance

between v@ and v as the basic structure of all BSTs "with more than @nodes# would be following.

Page 8: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 8/75

 Answer0 "$#

Page 9: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 9/75

 A binary tree is ma!3heap if it is a complete binary tree "A complete binary

tree is a binary tree in which every level e!cept possibly the last is

completely filled and all nodes are as far left as possible# and it follows

the ma!3heap property "value of each parent is greater than or e&ual tothe values of its children#.

 A# is not a ma!3heap because it is not a complete binary tree

$# is a ma!3heap because it is complete binary tree and follows ma!3heapproperty.

C# is not a ma!3heap because F is a chile of @ in this tree so violates the

ma!3heap property.

D# is not a ma!3heap because F is a chile of @ in this tree so violates the

ma!3heap property. There are many other nodes in this tree which

violate ma!3heap property in this tree.

Page 10: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 10/75

GATE2010

Consider a complete undirected graph with verte! set G/ ( 5 =H. %ntry

Wij in the matri! W below is the weight of the edge Gi jH.

What is the minimum possible weight of a spanning tree T in this graph

such that verte! / is a leaf node in the tree T?"A# E

"$# F

"C# I"D# (/

 Answer "D#

To get the minimum spanning tree with verte! / as leaf first remove /th row and /th

column and then get the minimum spanning tree "BST# of the remaining graph.

+nce we have BST of the remaining graph connect the BST to verte! / with the

edge with minimum weight "we have two options as there are two (s in /th row#.

Page 11: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 11/75

-n the graph given in previous &uestion what is the minimum possible

weight of a path J from verte! ( to verte! 5 in this graph such that J

contains at most edges?

"A# E

"$# F

"C# I

"D# (/

 Answer "$#Jath0 ( 34 / 34 = 34 5

Weight0 ( 1 = 1

Page 12: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 12/75

The degree se&uence of a simple graph is the se&uence of the degrees of

the nodes in the graph in decreasing order. Which of the following

se&uences can not be the degree se&uence of any graph?

-. E @ = = 5 (--. 5 5

---. E = = 5 5

-9. F E E = 5 ( (

"A# - and --

"$# --- and -9"C# -9 only

"D# -- and -9

 Answer "D#

-n se&uence -9 we have a verte! with degree F which is not possible in a

simple graph "no self loops and no multiple edges# with total verte! count

as F. Ba!imum possible degree in such a graph is E.

-n se&uence -- four vertices are connected to other vertices but

remaining = vertices have degrees as 5 and 5 which are not possiblein a simple graph "no self loops and no multiple edges#.

Page 13: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 13/75

Consider a $13tree in which the ma!imum number of 2eys in a node is @.

What is the minimum number of 2eys in any non3root node?

"A# (

"$# 5

"C# "D# =

 Answer "$#

Since the ma!imum number of 2eys is @ ma!imum number of children a

node can have is . $y definition of $ Tree minimum children that a node

can have would be >5 . Therefore minimum number of 2eys that anode can have becomes 5 "3(#.

Page 14: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 14/75

The following C function ta2es a simply3lin2ed list as input argument. -t

modifies the list by moving the last element to the front of the list and

returns the modified list. Some part of the code is left blan2.

typedef struct node

G

  int valueK

  struct node Lne!tK

H,odeK

 

,ode Lmovetofront",ode Lhead#G

  ,ode Lp L&K

  if ""head ,6770 << "head34ne!t ,677##

return headK

  & ,677K p headK

  while "p34 ne!t M,677#G

  & pK

  p p34ne!tK

  H

   

  return headKH

Page 15: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 15/75

Choose the correct alternative to replace the blan2 line.

"A# & ,677K p34ne!t headK head pK

"$# &34ne!t ,677K head pK p34ne!t headK

"C# head pK p34ne!t &K &34ne!t ,677K

"D# &34ne!t ,677K p34ne!t headK head pK

 Answer"D#

When the while loop ends & contains address of second last node and pcontains address of last node. So we need to do following things after while

loop.

i# Set ne!t of & as ,677 "&34ne!t ,677#.

ii# Set ne!t of p as head "p34ne!t head#.

iii# Ba2e head as p " head p#Step "ii# must be performed before step "iii#. -f we change head first then

we lose trac2 of head node in the original lin2ed list.

Page 16: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 16/75

 A hash table of length (/ uses open addressing with hash function h"2#2

mod (/ and linear probing. After inserting values into an empty hash

table the table is as shown below

Which one of the following choices gives a

possible order in which the 2ey values could

have been inserted in the table?

"A# = =5 = @5 5

"$# = =5 5 @5 =

"C# = = =5 5 @5

"D# =5 = 5 = @5

 Answer "C#

The se&uence "A# doesnNt create the hash table as the element @5 appears before 5in this se&uence.

The se&uence "$# doesnNt create the hash table as the element appears before =

in this se&uence.

The se&uence "C# creates the hash table as =5 5 and = appear before @5 and

and = appears before .

The se&uence "D# doesnNt create the hash table as the element appears before 5in this se&uence.

Page 17: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 17/75

Oow many different insertion se&uences of the 2ey values using the

same hash function and linear probing will result in the hash table

shown before?

"A#(/

"$# 5/

"C# /

"D# =/

 Answer "C#

-n a valid insertion se&uence the elements =5 5 and = must appear

before @5 and and = must appear before .

Total number of different se&uences M ! @ /

-n the above e!pression M is for elements =5 5 and = as they canappear in any order and @ is for element = as it can appear at @

different places.

GATE $! 200%

Page 18: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 18/75

GATE $! 200%

The 2eys (5 (F ( 5 5 @ and (@ are inserted into an initially empty

hash table of length (/ using open addressing with hash function h"2# 2

mod (/ and linear probing. What is the resultant hash table?

 Answer "C#

Page 19: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 19/75

+pen addressing or closed hashing is a method of collision resolution in

hash tables.

With this method a hash collision is resolved by probing or searching

through alternate locations in the array "the probe se&uence# until eitherthe target record is found or an unused array slot is found which

indicates that there is no such 2ey in the table.

Well 2nown probe se&uences include0

linear probing in which the interval between probes is fi!ed'often at (.

quadratic probing in which the interval between probes increases linearly

"hence the indices are described by a &uadratic function#.

double hashing in which the interval between probes is fi!ed for eachrecord but is computed by another hash function.

Page 20: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 20/75

Consider a binary ma!3heap implemented using an array. Which one of the

following array represents a binary ma!3heap?

"A# 5@(5(((/F(=

"$# 5@(=(((/F(5"C# 5@(=(((/F(5

"D# 5@(=(5((/F(

 Answer "C#

 A tree is ma!3heap if data at every node in the tree is greater than or e&ual

to itNs childrenN s data.

-n array representation of heap tree a node at inde! i has its left child at

inde! 5i 1 ( and right child at inde! 5i 1 5.

5@

> P

  (= (

  > P > P

  ( (/ F (5

Page 21: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 21/75

What is the content of the arra& after two delete operations on the correct

answer to the pre'ious uestion# 

"A# (=((5(/F

"$# (=(5(F(/"C# (=(F(5(/

"D# (=((5F(/

 Answer"D#

*or Oeap trees deletion of a node includes following two operations.(# )eplace the root with last element on the last level.

5# Starting from root heapify the complete tree from top to bottom..

7et us delete the two nodes one by one0

Page 22: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 22/75

Page 23: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 23/75

What is the ma!imum height of any A973tree with E nodes? Assume that

the height of a tree with a single node is /.

"A# 5

"$# "C# =

"D# @

 Answer"$#

 A97 trees are binary trees with the following restrictions.

(# the height difference of the children is at most (.

5# both children are A97 trees

GATE 200)

Page 24: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 24/75

GATE 200)

$onsider the following $ program that attempts to locate an element x in an

arra& *+, using binar& search- The program is erroneous- 

.n which of the following contents of * and x does the program fail#

"A# Q is R( 5 = @ E F I (/ and ! ; (/

"$# Q is R( @ E I (( ( (@ (E (I and ! ; (

"C# Q is R5 5 5 5 5 5 5 5 5 5 and ! 4 5

"D# Q is R5 = F (/ (5 (= ( (F 5/ and 5 ; ! ; 5/ and ! is even

 Answer "C#

Page 25: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 25/75

The above program doesnNt wor2 for the cases where element to be

searched is the last element of QR or greater than the last element "or

ma!imum element# in QR. *or such cases program goes in an infinite

loop because i is assigned value as 2 in all iterations and i never

becomes e&ual to or greater than j. So while condition never becomesfalse.

Page 26: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 26/75

/n pre'ious uestion the correction needed in the program to mae it

wor properl& is

"A# Change line to0 if "QR2 ; !# i 2 1 (K else j 23(K

"$# Change line to0 if "QR2 ; !# i 2 3 (K else j 21(K"C# Change line to0 if "QR2 ; !# i 2K else j 2K

"D# Change line E to0 H while ""QR2 !# "i ; j##K

 Answer "A#

Page 27: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 27/75

i3stra4s single source shortest path algorithm when run from 'ertex a in the

below graph computes the correct shortest path distance to

 

"A# only verte! a

"$# only vertices a e f g h

"C# only vertices a b c d

"D# all the vertices

 Answer "D#

Page 28: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 28/75

Dij2straNs single source shortest path is not guaranteed to wor2 for graphs with

negative weight edges but it wor2s for the given graph.

7et us see:

7et us run the (st pass

b (b is minimum so shortest distance to b is (.

 After (st pass distances are

c e 35.

e is minimum so shortest distance to e is 35

 After 5nd pass distances are

c f /.f is minimum so shortest distance to f is /

 After rd pass distances are

c g .

$oth are same let us ta2e g. so shortest distance to g is .

 After =th pass distances are

c h @c is minimum so shortest distance to c is

 After @th pass distances are

h 35

h is minimum so shortest distance to h is 35

Page 29: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 29/75

The following C function ta2es a single3lin2ed list of integers as a parameter and

rearranges the elements of the list. The function is called with the list containing the

integers ( 5 = @ E in the given order. What will be the contents of the list after

the function completes e!ecution?

struct node

G

  int valueK

  struct node Lne!tK

HK

void rearrange"struct node Llist#

G

  struct node Lp L &K

  int tempK

  if ""Mlist# << Mlist34ne!t#

returnK  p listK

  & list34ne!tK

  while"&#

G

  temp p34valueK

  p34value &34valueK  &34value tempK

  p &34ne!tK

  & p?p34ne!t0/K

  H

H

Page 30: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 30/75

"A# (5=@E

"$# 5(=@E

"C# (5@=E

"D# 5=@E(

 Answer "$#

The function rearrange"# e!changes data of every node with its ne!t node. -t starts

e!changing data from the first node itself.

Jlease write comments if you find any of the answers>e!planations incorrect or

you want to share more information about the topics discussed above.

Page 31: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 31/75

The Breadth 5irst !earch algorithm has been implemented using the ueue

data structure- .ne possible order of visiting the nodes of the following

graph is 

"A# B,+JU)

"$# ,UBJ+)

"C# UB,J)+"D# UB,J+)

 Answer "C#

GATE 2006

Page 32: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 32/75

GATE 2006

$onsider the following $ program segment where $ell7ode represents a node

in a binar& tree8 

Page 33: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 33/75

The value returned by 8et9alue"# when a pointer to the root of a binary

tree is passed as its argument is0

"A# the number of nodes in the tree

"$# the number of internal nodes in the tree"C# the number of leaf nodes in the tree

"D# the height of the tree

 Answer "C#

Page 34: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 34/75

7et w be the minimum weight among all edge weights in an undirected

connected graph. 7et e be a specific edge of weight w . Which of the

following is *A7S%?

"A# There is a minimum spanning tree containing e.

"$# -f e is not in a minimum spanning tree T then in the cycle formed by

adding e to T all edges have the same weight.

"C# %very minimum spanning tree has an edge of weight w .

"D# e is present in every minimum spanning tree.

 Answer "D#

"A# "$# and "C# are correct.

"D# is incorrect as there may be many edges of wight w in the graph and emay not be pic2ed up in some of the minimum spanning trees.

Page 35: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 35/75

Consider a hash table of siVe seven with starting inde! Vero and a hash

function "! 1 =#modE. Assuming the hash table is initially empty which of

the following is the contents of the table when the se&uence ( F (/ is

inserted into the table using closed hashing? ,ote that N denotes an

empty location in the table.

"A# F (/

"$# ( F (/

"C# (

"D# ( (/ F

 Answer "$#

Page 36: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 36/75

A complete n3ary tree is a tree in which each node has n children or no

Page 37: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 37/75

 A complete n3ary tree is a tree in which each node has n children or no

children. 7et - be the number of internal nodes and 7 be the number of leaves

in a complete n3ary tree. -f 7 =( and - (/ what is the value of n?

"A#

"$# =

"C# @

"D#

 Answer "C#

*or an n3ary tree where each node has n children or no children following

relation holds

7 "n3(#L- 1 (

Where 7 is the number of leaf nodes and - is the number of internal nodes.7et us find out the value of n for the given data.

7 =( - (/

=( (/L"n3(# 1 (

"n3(# =

n @

Page 38: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 38/75

The height of a binar& tree is the maximum number of edges in an&

root to leaf path- The maximum number of nodes in a binar& tree of

height h is8

"A# 5h 3("$# 5"h3(# ' (

"C# 5"h1(# 3(

"D# 5L"h1(#

 Answer "C#

Ba!imum number of nodes will be there for a complete tree.

,umber of nodes in a complete tree of height h ( 1 5 1 55 1 5L 1 :.

5h 5"h1(# ' (

The maximum number of binar& trees that can be formed with three

Page 39: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 39/75

The maximum number of binar& trees that can be formed with three

unlabeled nodes is8

"A# (

"$# @

"C# ="D#

 Answer "$#

The following postfi! e!pression with single digit operands is evaluated

Page 40: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 40/75

The following postfi! e!pression with single digit operands is evaluated

using a stac20

F 5 > 5 L 1 @ ( L 3

,ote that is the e!ponentiation operator. The top two elements of the stac2after the first L is evaluated are0

"A# (

"$# @ E

"C# 5

"D# ( @

 Answer "A#

Page 41: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 41/75

Page 42: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 42/75

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 postorder traversal of the binary tree is0

"A# d e b f g c a

"$# e d b g f c a"C# e d b f g c a

"D# d e f g b c a

 Answer "A#

8AT% CS 5//

Page 43: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 43/75

8AT% CS 5//

Consider the polynomial p"!# a/ 1 a(! 1 a5!5 1a! where ai M / for

all i. The minimum number of multiplications needed to evaluate p on an

input ! is0

"A#

"$# =

"C#

"D# I

 Answer "A#

Bultiplications can be minimiVed using following order for evaluation of the

given e!pression.

p"!# a/ 1 !"a( 1 !"a5 1 a!##

Page 44: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 44/75

To implement Dij2straNs shortest path algorithm on unweighted graphs so

that it runs in linear time the data structure to be used is0

"A# Uueue

"$# Stac2"C# Oeap

"D# $3Tree

 Answer"A#

The shortest path in an un3weighted graph means the smallest number of

edges that must be traversed in order to reach the destination in the graph.

This is the same problem as solving the weighted version where all the

weights happen to be (. -f we use Uueue "*-*+# instead of Jriority Uueue

"Bin Oeap# we get the shortest path in linear time +"<9< 1 <%<#. $asically wedo $*S traversal of the graph to get the shortest paths.

A h i li2 bi h b t i t d f 5 hild

Page 45: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 45/75

 A 3ary ma! heap is li2e a binary ma! heap but instead of 5 children

nodes have children. A 3ary heap can be represented by an array as

follows0 The root is stored in the first location aR/ nodes in the ne!t level

from left to right is stored from aR( to aR. The nodes from the second

level of the tree from left to right are stored from aR= location onward. Anitem ! can be inserted into a 3ary heap containing n items by placing ! in

the location aRn and pushing it up the tree to satisfy the heap property.

Which one of the following is a valid se&uence of elements in an array

representing 3ary ma! heap?

"A# ( @ F I

"$# I ( F @

"C# I F @ (

"D# I @ F (

 Answer "D#

Page 46: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 46/75

!uppose the elements 6 2 10 and 9 are inserted in that order into

the 'alid :; ar& max heap found in the abo'e uestion Which one of

the following is the seuence of items in the arra& representing the

resultant heap#

"A# (/ E I F ( @ 5 =

"$# (/ I F E @ = 5 (

"C# (/ I = @ E F 5 (

"D# (/ F I E 5 = ( @

 Answer"A#

Page 47: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 47/75

A i l t ti f U i t t 2 S( d S5 i i

Page 48: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 48/75

 An implementation of a &ueue U using two stac2s S( and S5 is given

below0

7et n insert and m ";n# delete operations be performed in an arbitrary

Page 49: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 49/75

7et n insert and m "; n# delete operations be performed in an arbitrary

order on an empty &ueue U. 7et ! and y be the number of push and pop

operations performed respectively in the process. Which one of the

following is true for all m and n?

"A# n1m ; ! ; 5n and 5m ; y ; n1m

"$# n1m ; ! ; 5n and 5m; y ; 5n

"C# 5m ; ! ; 5n and 5m ; y ; n1m

"D# 5m ; ! ;5n and 5m ; y ; 5n

 Answer"A#

The order in which insert and delete operations are performed matters here.

The best case: -nsert and delete operations are performed alternatively. -n every

delete operation 5 pop and ( push operations are performed. So total m1 n push

"n push for insert"# and m push for delete"## operations and 5m pop operations are

performed.

The worst case: *irst n elements are inserted and then m elements are deleted. -n

first delete operation n 1 ( pop operations and n push operation are performed.

+ther than first in all delete operations ( pop operation is performed. So total m 1

n pop operations and 5n push operations are performed "n push for insert"# and m

push for delete"##

Consider the following graph0

Page 50: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 50/75

Consider the following graph0

Page 51: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 51/75

Which one of the following cannot be the seuence of edges

added in that order to a minimum spanning tree using <rusal4s

algorithm#

"A# "aXb#"dXf#"bXf#"dXc#"dXe#

"$# "aXb#"dXf#"dXc#"bXf#"dXe#

"C# "dXf#"aXb#"dXc#"bXf#"dXe#

"D# "dXf#"aXb#"bXf#"dXe#"dXc#

 Answer "D#

The edge "d3e# cannot be considered before "d3c# in Yrus2alNs

 minimum spanning tree algorithm because Yrus2alNs algorithm pic2s the

edge with minimum weight from the current set of edges at each step.

GATE 200=

Page 52: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 52/75

GATE 200=

Which one of the following is a 2ey factor for preferring $3trees to binary

search trees for inde!ing database relations?

"a# Database relations have a large number of records

"b# Database relations are sorted on the primary 2ey

"c# $3trees re&uire less memory than binary search trees

"d# Data transfer form dis2s is in bloc2s.

 Answer "d#

 A dis2 bloc2 contains fairly large number of 2eys. 6nli2e $ST where each

node contains only one 2ey $3Tree is designed to contain large

number of 2eys so that tree height is small.

Oow many distinct binary search trees can be created out of = distinct 2eys?

Page 53: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 53/75

Oow many distinct binary search trees can be created out of = distinct 2eys?

"a# @

"b# (=

"c# 5="d# =5

 Answer "b#

Oere is a systematic way to enumerate these $STs. Consider all possible binary

search trees with each element at the root. -f there are n nodes then for each

choice of root node there are n ' ( non3root nodes and these non3root nodes mustbe partitioned into those that are less than a chosen root and those that are greater

than the chosen root.

7etNs say node i is chosen to be the root. Then there are i ' ( nodes smaller than i

and n ' i nodes bigger than i. *or each of these two sets of nodes there is a certain

number of possible subtrees.

7et t"n# be the total number of $STs with n nodes. The total number of $STs with i

at the root is t"i ' (# t"n ' i#. The two terms are multiplied together because the

arrangements in the left and right subtrees are independent. That is for each

arrangement in the left tree and for each arrangement in the right tree you get one$ST with i at the root.

Summing over i gives the total number of binary search trees with n nodes.

Page 54: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 54/75

Summing over i gives the total number of binary search trees with n nodes.

The base case is t"/# ( and t"(# ( i.e. there is one empty $ST and there is one

$ST with one node.

-n a complete 2 ary tree every internal node has e!actly 2 children The

Page 55: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 55/75

-n a complete 23ary tree every internal node has e!actly 2 children. The

number of leaves in such a tree with n internal nodes is0

"a# n2

"b# "n ' (# 21 ("c# n" 2 ' (# 1 (

"d# n"2 ' (#

 Answer "c#

Page 56: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 56/75

 A program J reads in @// integers in the range R/..(// e!epresenting the

scores of @// students. -t then prints the fre&uency of each score above

@/. What would be the best way for J to store the fre&uencies?

"a# An array of @/ numbers

"b# An array of (// numbers

"c# An array of @// numbers

"d# A dynamically allocated array of @@/ numbers

 Answer "a#

 An array of siVe @/ loo2s the best option to store number of students for

each score. We need to store fre&uencies of scores above @/. We can

ignore scores below @/ and to inde! the scores above @/ we can subtract

@/ from the score value>

An undirected graph 8 has n nodes. -ts adjacency matri! is given by an n Z

Page 57: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 57/75

 An undirected graph 8 has n nodes. -ts adjacency matri! is given by an n

n s&uare matri! whose "i# diagonal elements are /s and "ii# non3diagonal

elements are (s. which one of the following is T)6%?

"a# 8raph 8 has no minimum spanning tree "BST#"b# 8raph 8 has a uni&ue BST of cost n3(

"c# 8raph 8 has multiple distinct BSTs each of cost n3(

"d# 8raph 8 has multiple spanning trees of different costs

 Answer "c#

-f all non diagonal elements are ( then every verte! is connected to every other

verte! in the graph with an edge of weight (. Such a graph has multiple distinct BSTs

with cost n3(. See the below e!ample.

The connected graph0

$elow are three Binimum Spanning trees each of cost 5./.

Page 58: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 58/75

p g

Binimum Spanning Tree ( Binimum Spanning Tree 5

Binimum Spanning Tree

A J i it U i i l t d B O - iti ll it h @ l t

Page 59: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 59/75

 A Jriority3Uueue is implemented as a Ba!3Oeap. -nitially it has @ elements.

The level3order traversal of the heap is given below0

(/ F @ 5

Two new elements [( and [E are inserted in the heap in that order. The

level3order traversal of the heap after the insertion of the elements is0

"a# (/ F E @ 5 (

"b# (/ F E 5 ( @

"c# (/ F E ( 5 @

"d# (/ F E 5 ( @

 Answer "D#

+riginal Ba!3Oeap is0 After -nsertion of (. After -nsertion of E.

1 ! i +1 , d d ( i 3"

Page 60: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 60/75

1- !uppose &ou are gi'en an arra& s+1---n, and a procedure re'erse (si3"

which re'erses the order of elements in a between positions i and 3 (both

inclusi'e"- What does the following seuence

do

where 1 > >? n8re'erse (s 1 "@

re'erse (s 1 n"@

re'erse (s 1 n"@

(GATE $! 2000"

"a# )otates s left by 2 positions"b# 7eaves s unchanged

"c# )everses all elements of s

"d# ,one of the above

 Answer0 "a#

%ffect of the above reversals for any 2 is e&uivalent to left rotation of the array of

siVe n by 2.

-f we rotate an array n times for 2 ( to n we get the same array bac2.

e'ersal algorithm for arra& rotation

Write a function rotate"arrR d n# that rotates arrR of siVe n by d elements

Page 61: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 61/75

Write a function rotate"arrR d n# that rotates arrR of siVe n by d elements.

)otation of the above array by 5 will ma2e array

 

Algorithm8

rotate(arr[ ], d, n)reverse(arr[ ], l, d) ;

reverse(arr[ ], d + l, n);

reverse(arr[ ], l, n);7et A$ are the two parts of the input array where A arrR/..d3( and $ arrRd..n3(.

The idea of the algorithm is0)everse A to get Ar$. >L Ar is reverse of A L>

)everse $ to get Ar$r. >L $r is reverse of $ L>

)everse all to get "Ar$r# r $A.*or arrR R( 5 = @ E d 5 and n E

 A R( 5 and $ R = @ E

)everse A we get Ar$ R5 ( = @ E

)everse $ we get Ar$r R5 ( E @ = )everse all we get "Ar$r#r R = @ E ( 5

Page 62: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 62/75

The best data structure to chec whether an arithmetic

expression has balanced parentheses is a (GATE $! 2009"

a# &ueue

b# stac2

c# tree

d# list

 Answer"b#

Ce'el order tra'ersal of a rooted tree can be done b&

Page 63: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 63/75

&

starting from the root and performing (GATE $! 2009"

a# preorder traversal

b# in3order traversal

c# depth first search

d# breadth first search

 Answer"d#

Page 64: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 64/75

Ce'el .rder Tree Tra'ersal

7evel order traversal of a tree is breadth first traversal f or the tree.

%!ample Tree

 

7evel order traversal of the above tree is ( 5 = @

Gi h f ll i i (9:22 1::9 19 1 %D % 1%)%

Page 65: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 65/75

Gi'en the following input (9:22 1::9 1961 %D6% 1%)%

D161 D16: 91%%" and the hash function x mod 10 which

of the following statements are true#

i- %D6% 1%)% 91%% hash to the same 'alue

ii- 1961 D161 has to the same 'alue

iii- All elements hash to the same 'alue

i'- Each element hashes to a different 'alue(GATE $! 2009"

a# i only

b# ii only

c# i and ii onlyd# iii or iv

 Answer "c#

Page 66: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 66/75

ostorder tra'ersal of a gi'en binar& search tree T produces the following

seuence of e&s

10 % 2: 22 26 2= 1= =0 %= D0 90 2%

Which one of the following seuences of e&s can be the result of an in;order

tra'ersal of the tree T#

(GATE $! 200="

a# I (/ (@ 55 5 5@ 5E 5I =/ @/ / I@

b# I (/ (@ 55 =/ @/ / I@ 5 5@ 5E 5Ic# 5I (@ I (/ 5@ 55 5 5E =/ / @/ I@

d# I@ @/ / =/ 5E 5 55 5@ (/ I (@ 5I

 Answer "a#

-norder traversal of a $ST always gives elements in increasing order. Among all fouroptions a# is the only increasing order se&uence.

Page 67: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 67/75

Suppose the numbers E @ ( F / I = 5 are inserted in that order

into an initially empty binary search tree. The binary search tree uses the

usual ordering on natural numbers. What is the in3order traversal se&uence

of the resultant tree? "8AT% CS 5//#

a# E @ ( / 5 = F I

b# / 5 = ( @ I F E

c# / ( 5 = @ E F I

d# I F = 5 / ( @ E

 Answer "c#

-n3order traversal of a $ST gives elements in increasing order. So answer c

is correct without any doubt.

7et 8 be an undirected connected graph with distinct edge weight 7et

Page 68: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 68/75

7et 8 be an undirected connected graph with distinct edge weight. 7et

ema! be the edge with ma!imum weight and emin the edge with minimum

weight. Which of the following statements is false? "8AT% CS 5///#

"a# %very minimum spanning tree of 8 must contain emin"b# -f ema! is in a minimum spanning tree then its removal must

disconnect 8

"c# ,o minimum spanning tree contains ema!

"d# 8 has a uni&ue minimum spanning tree

 Answer "c#

"a# and "b# are always true.

"c# is false because "b# is true.

"d# is true because all edge weights are distinct for 8.

Consider an undirected unweighted graph 8 7et a breadth3first traversal

Page 69: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 69/75

Consider an undirected unweighted graph 8. 7et a breadth first traversal

of 8 be done starting from a node r. 7et d"r u# and d"r v# be the lengths of

the shortest paths from r to u and v respectively in 8. lf u is visited before

v during the breadth3first traversal which of the following statements is

correct? "8AT% CS 5//(#

a# d"r u# ; d "r v#

b# d"r u# 4 d"r v#

c# d"r u# ; d "r v#

d# ,one of the above

 Answer "c#

d"r u# and d"r v# will be e&ual when u and v are at same level otherwise

d"r u# will be less than d"r v#

Oow many undirected graphs "not necessarily connected# can be

Page 70: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 70/75

constructed out of a given set 9 G9 ( 9 5:9 nH of n vertices ?

"8AT% CS 5//(#

a# n"n3l#>5b# 5n

c# nM

d# 5"n"n3(#>5#

 Answer "d#

-n an undirected graph there can be ma!imum n"n3(#>5 edges. We can

choose to have "or not have# any of the n"n3(#>5 edges. So total number of

undirected graphs with n vertices is 5"n"n3(#>5#.

Consider the following graph

Page 71: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 71/75

 Among the following se&uences

-# a b e g h f --# a b f e h g

---# a b f h g e

-9# a f g h b e

Which are depth first traversals of the above graph? "8AT% CS 5//#a# - -- and -9 only

b# - and -9 only

c# -- --- and -9 only

d# - --- and -9 only

 Answer "d#

Consider the following C function.(GATE $! 200:"

Page 72: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 72/75

float f"float ! int y#

G

float p sK int iK

for  "s( p( i(K i ; yK i 11#

G

pL !>iK

s1pK

H

return sK

H*or large values of y the return value of the function f best appro!imates

a# !y

b# e!

c# ln"( 1 !#

d# !!

 Answer "b#

The function f"# is implementation of TaylorNs Series to calculates e!

e! ( 1 ! 1 !5>5M 1 !>M 1 333 Bore is the value of y more precise value

of e! will be returned by f"#

The elements 5 (@ 5/ / (5 5@ ( are inserted one by one in the

Page 73: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 73/75

given order into a Ba! Oeap. The resultant Ba! Oeap is.

 Answer "a#

 A single array AR(..BA\S-]% is used to implement two stac2s. The two

Page 74: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 74/75

g y R p

stac2s grow from opposite ends of the array. 9ariables top( and top5 "topl;

top 5# point to the location of the topmost element in each of the stac2s. -f

the space is to be used efficiently the condition for ^stac2 full[ is

"8AT% CS 5//=#a# "top( BA\S-]%>5# and "top5 BA\S-]%>51(#

b# top( 1 top5 BA\S-]%

c# "top( BA\S-]%>5# or "top5 BA\S-]%#

d# top( top5 3(

 Answer"d#

-f we are to use space efficiently then siVe of the any stac2 can be more

than BA\S-]%>5.

$oth stac2s will grow from both ends and if any of the stac2 top reaches

near to the other top then stac2s are full. So the condition will be top(

top5 3( "given that top( ; top5#

!uppose we run i3stra4s single source shortest;path algorithm on the

Page 75: DataStructure GATE

7/24/2019 DataStructure GATE

http://slidepdf.com/reader/full/datastructure-gate 75/75

3 g g

following edge weighted directed graph with 'ertex as the source- /n what

order do the nodes get included into the set of 'ertices for which the

shortest path distances are finaliFed# (GATE $! 2009"

a# J U ) S T 6

b# J U ) 6 S T

c# J U ) 6 T S

d# J U T ) 6 S