Short Online Q&A for DataStructure

Embed Size (px)

Citation preview

  • 8/12/2019 Short Online Q&A for DataStructure

    1/22

    THE INDIAN ENGINEERING COLLEGE

    Vadakkangulam

    DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

    TREC / ME DEGREE COMPUTER SCIENCE AND ENGINEERING

    CS91! DATA STRUCTURES AND ALGORITHMS

    S"#$% &u'(%)#n( and An(*'$( + Ma$k( ,

    P$'-a$'d .0

    ANON 2 3ENIFER0 L'4%u$'$/CSE

  • 8/12/2019 Short Online Q&A for DataStructure

    2/22

    UNIT I COMPLE5IT6 ANAL6SIS 7 ELEMENTAR6 DATA STRUCTURES

    12 8"a% d# a(m-%#%)4 n#%a%)#n m'an(

    Asymptotic notations are terminology that is introduced to enable us to make

    meaningful statements about the time and space complexity of an algorithm. Thedifferent notations areBig Oh notationOmega notationTheta notation.

    2 8"a% a$' %"' :a()4 a(m-%#%)4 ';;)4)'n4 4la(('(

    The various basic efficiency classes areConstant : !ogarithmic : log n!ogarithmic : log n

    !inear : n"#log#n : nlog n$uadratic : n%

    Cubic : n&

    'xponential : %n

    (actorial : n)

  • 8/12/2019 Short Online Q&A for DataStructure

    3/22

    2 8"a% )( aB'$ag' 4a(' ';;)4)'n4

    The average case efficiency of an algorithm is its efficiency for an average caseinput of si1e n. 2t provides information about an algorithm behavior on a 3typical or3random input.

    2 8"a% )( am#$%)'d ';;)4)'n42n some situations a single operation can be expensive, but the total time for theentire se4uence of n such operations is al0ays significantly better that the 0orst caseefficiency of that single operation multiplied by n. this is called amorti1ed efficiency.

    92 8"a% )( %"' ;#$mula u('d )n Eu4l)d( alg#$)%"m ;#$ ;)nd)ng %"' g$'a%'(% 4#mm#n

    d)B)(#$ #; %*# num:'$(

    'uclid5s algorithm is based on repeatedly applying the e4uality6cd*m,n+7gcd*n,m mod n+ until m mod n is e4ual to -, since gcd*m,-+7m.

    12 D';)n' a$%)4ula%)#n -#)n%(2

    2f a graph is not biconnected,the vertices 0hose removal 0ould disconnect thegraph are kno0n as articulation points.

    112 G)B' (#m' '>am-l' #; NP 4#m-l'%' -$#:l'm(2

    i. 8amiltonian circuit.ii. Travelling salesmen problemsiii. !ongest path problemsiv. Bin packingv. 9napsack problemvi. 6raph colouring problem

    12 8"a% )( %"' $'4u$$'n4' $'la%)#n %# ;)nd #u% %"' num:'$ #; mul%)-l)4a%)#n( and %"'

    )n)%)al 4#nd)%)#n ;#$ ;)nd)ng %"' n!%" ;a4%#$)al num:'$

    The recurrence relation and initial condition for the number of multiplications is *n+7*n#+; for n

  • 8/12/2019 Short Online Q&A for DataStructure

    4/22

    1?2 8"a% )( %"' '>-l)4)% ;#$mula ;#$ %"' n%" F):#na44) num:'$

    The formula for the nth (ibonacci number is given by(*n+7 =>*?n # ?n+@here? 7*;>+=%

    ? 7*#>+=%

    1@2 D';)n' Da%a S%$u4%u$'(

    ata tructures is defined as the 0ay of organi1ing all data items that consider notonly the elements stored but also stores the relationship bet0een the elements.

    12 D';)n' L)nk'd L)(%(

    !inked list consists of a series of structures, 0hich are not necessarily adacent inmemory. 'ach structure contains the element and a pointer to a structure containing itssuccessor. @e call this the"ext Dointer. The last cell5s"ext pointer points to "E!!.

    12 S%a%' %"' d);;'$'n% %-'( #; l)nk'd l)(%(The different types of linked list include singly linked list, doubly linked list andcircular linked list.. !ist the basic operations carried out in a linked listThe basic operations carried out in a linked list include:/ Creation of a list/ 2nsertion of a node/ eletion of a node/ odification of a node/ Traversal of the list

    192 L)(% #u% %"' adBan%ag'( #; u()ng a l)nk'd l)(%/ 2t is not necessary to specify the number of elements in a linked list during itsdeclaration/ !inked list can gro0 and shrink in si1e depending upon the insertion and deletionthat occurs in the list/ 2nsertions and deletions at any place in a list can be handled easily and efficiently/ A linked list does not 0aste any memory space

    2 L)(% #u% %"' a--l)4a%)#n( #; a l)nk'd l)(%

    ome of the important applications of linked lists are manipulation ofpolynomials, sparse matrices, stacks and 4ueues.

    12 S%a%' %"' d);;'$'n4' :'%*''n a$$a( and l)nk'd l)(%(

    Arrays !inked !istsi1e of an array is fixed i1e of a list is variable2t is necessary to specify thenumber of elements duringdeclaration.

    2t is not necessary to specify thenumber of elements duringdeclaration

  • 8/12/2019 Short Online Q&A for DataStructure

    5/22

    2nsertions and deletions aresome0hat difficult

    2nsertions and deletions are carriedout easily

    2t occupies less memory than alinked list for the same number ofelements

    2t occupies more memory

    2 8"a% d# #u m'an : :alan4'd %$''(

    Balanced trees have the structure of binary trees and obey binary search treeproperties. Apart from these properties, they have some special constraints, 0hich differfrom one data structure to another. 8o0ever, these constraints are aimed only at reducingthe height of the tree, because this factor determines the time complexity.'g: AF! trees, play trees.

  • 8/12/2019 Short Online Q&A for DataStructure

    6/22

    UNIT II HEAP STRUCTURES

    12 D';)n' H'a-2

    A heap is a partially ordered data structure, and can be defined as a binary treeassigned to its nodes, one key per node, provided the follo0ing t0o conditions are meti.The tree5s shape re4uirement#The binary tree is essentially complete, that is all

    the leaves are full except possibly the last level, 0here only some rightmost leaves 0ill bemissing.

    ii.The parental dominance re4uirement#The key at each node is greater that ore4ual to the keys of its children

    2 8"a% a$' %"' -$#-'$%)'( #; :)na$ "'a-

    i+ tructure Dropertyii+ 8eap Order Droperty

  • 8/12/2019 Short Online Q&A for DataStructure

    7/22

    iii.Adding a ne0 item to the set

    92 G)B' %"$'' -$#-'$%)'( #; "'a-(The properties of heap are

    There exists exactly one essentially complete binary tree 0ith Hn5 nodes.

    2ts height is e4ual to log%n The root of the heap is al0ays the largest element A node of a heap considered 0ith all its descendants is also a heap

    12 G)B' %"' ma)n -$#-'$% #; a "'a- %"a% )( )m-l'm'n%'d a( an a$$a2

    A heap can be implemented as an array by recording its elements in the top#do0n,left#to#right fashion. 2t is convenient to store the heap5s elements in positions through nof such an array. 2n such a representation

    i. The parental node keys 0ill be in the first n=% positions of the array, 0hile theleaf keys 0ill occupy the last n=% positions

    ii.The children of a key in the array5s parental position Hi5 *IiIn=%+ 0ill be in

    positions %i and %i;and correspondingly, the parent of the key in position Hi5*%IiIn+ 0illbe in position i=%.

    112 8"a% a$' %"' %*# al%'$na%)B'( %"a% a$' u('d %# 4#n(%$u4% a "'a-

    The t0o alternatives to construct a heap are Bottom#up heap construction Top#do0n heap construction

    12 8"a% )( %"' alg#$)%"m %# d'l'%' %"' $##%( k' ;$#m %"' "'a-

    A!6OJ2T8i.'xchange the root5s key 0ith the last key 9 of the heapii.ecrease the heap5s si1e by oneiii.38eapify the smaller tree by sifting 9 do0n the tree exactly in the same 0ay

    as bottom#up heap construction. Ferify the parental dominance for 9: if it holds stop theprocess, if not s0ap 9 0ith the larger of its children and repeat this operation until theparental dominance holds for 9 in its ne0 position.

    1

  • 8/12/2019 Short Online Q&A for DataStructure

    8/22

    to place G in its correct spot along a path from the root containing minimum children.This general strategy is kno0n as percolate do0n.

    1?2 8"a% )( m'an% : Im-l)4)% H'a-(

    A particularly simple and beautiful implementation of the heap structure is theimplicit heap. ata is simply put into an array and the childrens ofelementxLiM are defined to be the elementsxL%iM andxL%i;M. Thus the parent of theelement ccan be found at c=% *integer division+.

    1@28"a% )( a (k'* "'a-

    A heap data structure that is stored in a binary tree *not necessarily complete andbalanced+. The insertion and deletion of elements in the tree come from merging t0o

    ske0 heaps together in such a 0ay that the heap property is preserved and the tree doesnot degrade to a linear tree.

    12 H#* %# m'$g)ng (k'* "'a-(

    uppose 0e are merging a heap containing the elements %, >, and N 0ith a heapcontaining the elements and P.

    N Q# merge #< P = R =

    > % i. 2dentify the root, thus N becomes the ne0 root and the left subtree of the heap 0ith root N becomes the right subtree of

    the other heap:N % Q# merge #< P *to form the left subtreeR = of the ne0 ske0 heap+ >

    ii.

    N = R P > R Q# merge #< %

    iii. N = R P > = R %

  • 8/12/2019 Short Online Q&A for DataStructure

    9/22

    12 D';)n' Fa:)na44) H'a-2

    The (ibonacci heap is a data structure that supports all the basic heap operationsin O*+ amorti1ed time, 0ith the exception of deleteSmin and delete, 0hich take O *log n+amorti1ed time. 2t immediately follo0s that the heap operations in ikstras algorithm0ill re4uire a total of O*U'U ; UFU log UFU+ time.

    192 8"a% )( La m'$g)ng

    T0o heaps are merged only 0hen it is re4uired to do so. This is similar to la1ydeletion. (or la1y merging, merges are cheap, but because la1y merging does not actuallycombine trees, the deleteSmin operation could encounter lots of trees, making thatoperation expensive. Any one deleteSmin could take linear time, but it is al0ays possibleto charge the time to previous merge operations. 2n particular, an expensive deleteSminmust have been preceded by a large number of unduly cheap merges, 0hich have beenable to store up extra potential.

    2 8$)%' %"' am#$%)'d anal()( #; La .)n#m)al &u'u'(To carry out the amorti1ed analysis of la1y binomial 4ueues, 0e 0ill use the same

    potential function that 0as used for standard binomial 4ueues. Thus, the potential of ala1y binomial 4ueue is the number of trees.

    12 .)n#m)al "'a-(J

    A set of binomial trees satisfying the follo0ing:. 'ach binomial tree in 8 is heap#ordered:

    the key of a node is greater than or e4ual to the key of its parent%. There is at most one binomial tree in 8 0hose root has a given degree

    28$)%' %"' D)Kk(%$a( ("#$%'(% -a%" alg#$)%"m

    !et 6 7 *F,'+ be a 0eighted *0eights are non#negative+ undirected graph, let s F. @ant to find the distance *length of the shortest path+, d*s,v+ from s to every othervertex.

  • 8/12/2019 Short Online Q&A for DataStructure

    10/22

    UNIT III SEARCH STRUCTERS

    12 8"a% )( :)na$ ('a$4"

    Binary search is a remarkably efficient algorithm for searching in a sorted array. 2t0orks by comparing a search key 9 0ith the arrays middle element ALmM. if they match

    the algorithm stopsK other0ise the same operation is repeated recursively for the first halfof the array if 9 Q ALmM and the second half if 9 < ALmM.

    9AL-MVVVALm#M ALmM ALm;MVVVALn#M

    search here if 9QALmM search here if 9

  • 8/12/2019 Short Online Q&A for DataStructure

    11/22

    trees 0ere invented in ZP% by t0o Jussian scientists, 6..Adelson#Felsky and'..!andis, after 0hom the data struture is named.

    2 D';)n' AVL T$''2

    An empty tree is height balanced. 2f T is a non#empty binary tree 0ith T! and

    TJ as its left and right subtrees, then T is height balanced ifi+ T! and TJ are height balanced andii+ [h! # hJ[I @here h! and hJ are the heights of T! and TJ respectively.

    2 8"a% a$' %"' Ba$)#u( %$an(;#$ma%)#n -'$;#$m'd )n AVL %$''

    .single rotation # single ! rotation # single J rotation%.double rotation #!J rotation #J! rotation

    92 8"a% a$' %"' 4a%'g#$)'( #; AVL $#%a%)#n(

    !et A be the nearest ancestor of the ne0ly inserted nod 0hich has the balancing

    factor \%. Then the rotations can be classified into the follo0ing four categories:!eft#!eft: The ne0ly inserted node is in the left subtree of the left child of A.Jight#Jight: The ne0ly inserted node is in the right subtree of the right child of A.!eft#Jight: The ne0ly inserted node is in the right subtree of the left child of A.Jight#!eft: The ne0ly inserted node is in the left subtree of the right child of A.

    12 8"a% d# #u m'an : :alan4' ;a4%#$ #; a n#d' )n AVL %$''

    The height of left subtree minus height of right subtree is called balance factor of anode in AF! tree.The balance factor may be either - or ; or #.The height of an emptytree is #.

    112 8$)%' a:#u% %"' ';;)4)'n4 #; AVL %$''(

    As 0ith any search tree , the critical characteristic is the tree5s height. The tree5sheight is bounded above and belo0 by logarithmic functions. The height Hh5 of any AF!tree 0ith Hn5 nodes satisfies the ine4ualities

    log% n I h Q .-> log%*n;%+ .&%NNThe ine4ualities imply that the operations of searching and insertion are ]*log n+ in

    the 0orst case. The operation of key deletion in an AF! tree is more difficult thaninsertion, but it turns out to have the same efficiency class as insertion i.e., logarithmic

    12 8"a% )( %"' m)n)mum num:'$ #; n#d'( )n an AVL %$'' #; "')g"% "

    The minimum number of nodes *h+, in an AF! tree of height h is given

    by *h+7*h#+;*h#%+;. (or h7-, *h+7.1

  • 8/12/2019 Short Online Q&A for DataStructure

    12/22

    serves as the root of a subtree 0ith keys bet0een 9 W 9% and the rightmost child servesas the root of a subtree 0ith keys greater than 9%. The last re4uirement of %#& trees is thatall its leaves must be on the same level, a %#& tree is al0ays height balanced. %#& trees0ere introduced by ^ohn 8opcroft in ZN-.

    1=2 8"a% d# #u m'an : !

    1=2 D';)n' .!%$'' #; #$d'$ M2

    A B#tree of order is a tree that is not binary 0ith the follo0ing structuralproperties:/ The root is either a leaf or has bet0een % and children.

    / All non#leaf nodes *except the root+ have bet0een _=%` and children./ All leaves are at the same depth.

    1?2 8"a% a$' %"' a--l)4a%)#n( #; .!%$''

    / atabase implementation/ 2ndexing on non primary key fields

    1@2 D';)n)%)#n #; a $'d!:la4k%$''

    A red#black tree is a binary search tree 0hich has the follo0ingred-black properties

    :. 'very node is either red or black.%. 'very leaf *"E!!+ is black.&. 2f a node is red, then both its children are

    black.

    . 'very simple path from a node to adescendant leaf contains the same number ofblack nodes.

  • 8/12/2019 Short Online Q&A for DataStructure

    13/22

    A basic red#black tree

    Basic red#black tree0ith the ('n%)n'lnodesadded.2mplementations of thered#black treealgorithms 0ill usuallyinclude the sentinelnodes as a convenientmeans of flagging thatyou have reached a leafnode.

    They are the "E!!black nodes of property%.

    12 D';)n' (-la %$''2

    A splay tree is a binary search tree in 0hich restructuring is done using a schemecalled splay. The splay is a heuristic method 0hich moves a given vertex v to the root ofthe splay tree using a se4uence of rotations.

    12 8"a% )( %"' )d'a :'")nd (-la)ng

    playing reduces the total accessing time if the most fre4uently accessed node ismoved to0ards the root. 2t does not re4uire to maintain any information regarding theheight or balance factor and hence saves space and simplifies the code to some extent.

    192 L)(% %"' %-'( #; $#%a%)#n( aBa)la:l' )n S-la %$''2

    !et us assume that the splay is performed at vertex v, 0hose parent andgrandparent are p and g respectively. Then, the three rotations are named as:i. ig: 2f p is the root and v is the left child of p, then left#left rotation at p 0ouldsuffice. This case al0ays terminates the splay as v reaches the root after thisrotation.ii. ig#ig: 2f p is not the root, p is the left child and v is also a left child, then a left#left rotation at g follo0ed by a left#left rotation at p, brings v as an ancestor of gas 0ell as p.

  • 8/12/2019 Short Online Q&A for DataStructure

    14/22

    iii. ig#ag: 2f p is not the root, p is the left child and v is a right child, perform aleft#right rotation at g and bring v as an ancestor of p as 0ell as g.

    2 D';)n' :$u%' ;#$4' (%$)ng ma%4")ng2

    The brute force string matching has a given string of n characters called the text

    and a string of m characters called the pattern, find a substring of the text that matches thepattern. And find the index 2 of the leftmost character of the first matching substring inthe text.

    12 8"a% a$' %"' adBan%ag'( #; :$u%' ;#$4' %'4"n)u'

    The various advantages of brute force techni4ue arei. Brute force applicable to a very 0ide variety of problems. 2t is used for manyelementary but important algorithmic tasksii.(or some important problems this approach yields reasonable algorithms of atleast some practical value 0ith no limitation on instance si1eiii.The expense to design a more efficient algorithm may be unustifiable if only a

    fe0 instances of problems need to be solved and a brute force algorithm can solvethose instances 0ith acceptable speediv. 'ven if inefficient in general it can still be used for solving small#si1einstances of a problemv. 2t can serve as a yardstick 0ith 0hich to udge more efficient alternatives forsolving a problem

    2 8"a% a$' %"' -$#-'$%)'( #; :)na$ "'a-

    i+ tructure Dropertyii+ 8eap Order Droperty

  • 8/12/2019 Short Online Q&A for DataStructure

    15/22

    UNIT IV GREED6 7 DIVIDE AND CON&UER

    12 G)B' %"' g'n'$al -lan ;#$ d)B)d'!and!4#nu'$ alg#$)%"m(2The general plan is as follo0si.A problems instance is divided into several smaller instances of the same problem,ideally about the same si1eii.The smaller instances are solved, typically recursivelyiii.2f necessary the solutions obtained are combined to get the solution of the originalproblem

    2 S%a%' %"' Ma(%'$ %"'#$'m and )%( u('2

    2f f*n+ ]*nd+ 0here d - in recurrence e4uation T*n+ 7 aT*n=b+;f*n+, then]*nd+ if aQbd T*n+

    ]*ndlog n+ if a7bd

    ]*nlogba+ if a

  • 8/12/2019 Short Online Q&A for DataStructure

    16/22

    @2 8"a% )( a %$'' 'dg' and 4$#(( 'dg'

    2n the breadth first search forest, 0henever a ne0 unvisited vertex is reached forthe first time, it is attached as a child to the vertex from 0hich it is being reached. uchan edge is called tree edge. 2f an edge is leading to a previously visited vertex other thanits immediate predecessor, that edge is noted as cross edge.

    2 8"a% )( %$an(;#$m and 4#nu'$ %'4"n)u'

    The group of design techni4ues that are based on the idea of transformation iscalled transform and con4uer techni4ue because themethods 0ork as t0o stageprocedures. (irst in the transformation stage, the

    problem5s instance is modified to be more amenable *agreeable+ to the solution.Then in the second or con4uering stage, it is solved.

    2 8"a% )( g$''d %'4"n)u'

    6reedy techni4ue suggests a greedy grab of the best alternative available in thehope that a se4uence of locally optimal choices 0ill yield a globally optimal solution to

    the entire problem. The choice must be made as follo0si.(easible : 2t has to satisfy the problem5s constraintsii.!ocally optimal : 2t has to be the best local choice among all feasible choices

    available on that step.iii.2rrevocable : Once made, it cannot be changed on a subse4uent step of the

    algorithm

    92 8"a% )( a (%a%' (-a4' %$''

    The processing of backtracking is implemented by constructing a tree of choicesbeing made. This is called the state#space tree. 2ts root represents a initial state before thesearch for a solution begins. The nodes of the first level in the tree represent the choices

    made for the first component of the solution, the nodes in the second level represent thechoices for the second component and so on.

    12 8"a% )( a -$#m)()ng n#d' )n %"' (%a%'!(-a4' %$''

    A node in a state#space tree is said to be promising if it corresponds to a partiallyconstructed solution that may still lead to a complete solution.

    112 8"a% )( a n#n!-$#m)()ng n#d' )n %"' (%a%'!(-a4' %$''

    A node in a state#space tree is said to be promising if it corresponds to a partiallyconstructed solution that may still lead to a complete solutionK other0ise it is called non#promising.

    12 8"a% d# l'aB'( )n %"' (%a%' (-a4' %$'' $'-$'('n%

    !eaves in the state#space tree represent either non#promising dead ends orcomplete solutions found by the algorithm.

    1

  • 8/12/2019 Short Online Q&A for DataStructure

    17/22

    2n the maority of cases, a state#space tree for backtracking algorithm isconstructed in the manner of depth#first search. 2f the current node is promising, its childis generated by adding the first remaining legitimate option for the next component of asolution, and the processing moves to this child. 2f the current node turns out to be non#promising, the algorithm backtracks to the node5s parent to consider the next possible

    solution to the problem, it either stops or backtracks to continue searching for otherpossible solutions.

    1=2 8"a% )( a ;'a():l' (#lu%)#n and *"a% )( an #-%)mal (#lu%)#n

    2n optimi1ation problems, a feasible solution is a point in the problem5s searchspace that satisfies all the problem5s constraints, 0hile an optimal solution is a feasiblesolution 0ith the best value of the obective function.

    1?2 D';)n' D)B)d' and C#nu'$ alg#$)%"m

    ivide and Con4uer algorithm is based on dividing the problem to be solved intoseveral, smaller sub instances, solving them independently and then combining the sub

    instances solutions so as to yield a solution for the original instance.1@2 M'n%)#n (#m' a--l)4a%)#n #; D)B)d' and C#nu'$ alg#$)%"m

    a. $uick ort b. erge ort c. Binary search

    12 8"a% a$' %"' %*# (%ag'( ;#$ "'a- (#$%

    tage : Construction of heap tage % : Joot deletion "# times

    12 8"a% )( d)B)d' and 4#nu'$ (%$a%'g

    2n divide and con4uer strategy the given problem is divided into smallerDroblems and solved recursively. The con4uering phase consists of patching together the

    ans0ers . ivide and con4uer is a very po0erful use of recursion that 0e 0ill seemany times.

    192 8"a% d# #u m'an : ('-a$a%' 4"a)n)ng

    eparate chaining is a collision resolution techni4ue to keep the list of all elementsthat hash to the same value. This is called separate chaining because each hash tableelement is a separate chain *linked list+. 'ach linked list contains all the elements 0hosekeys hash to the same index.

    2 8$)%' %"' adBan%ag' and D)(adBan%ag'( #; ('-a$a%' 4"a)n)ng2

    Adv:

    / ore number of elements can be inserted as it uses linked lists.is Adv./ The elements are evenly distributed. ome elements may have moreelements and some may not have anything./ 2t re4uires pointers. This leads to slo0 the algorithm do0n a bit because ofthe time re4uired to allocate ne0 cells, and also essentially re4uires theimplementation of a second data structure.

  • 8/12/2019 Short Online Q&A for DataStructure

    18/22

  • 8/12/2019 Short Online Q&A for DataStructure

    19/22

    UNIT V D6NAMIC PROGRAMMING AND .ACTRACING

    12 D';)n' G$a-"2

    A graph 6 consist of a nonempty set F 0hich is a set of nodes of the graph, a set '0hich is the set of edges of the graph, and a mapping from the set for edge ' to a set of

    pairs of elements of F. 2t can also be represented as 67*F, '+.2 8"a% )( m'an% : (%$#ngl and 8''kl 4#nn'4%'d )n a g$a-"

    An undirected graph is connected, if there is a path from every vertex to everyother vertex. A directed graph 0ith this property is called strongly connected.

    @hen a directed graph is not strongly connected but the underlying graph isconnected, then the graph is said to be 0eakly connected.

  • 8/12/2019 Short Online Q&A for DataStructure

    20/22

    2 8"a% )( a -$#m)()ng n#d' )n %"' (%a%'!(-a4' %$''

    A node in a state#space tree is said to be promising if it corresponds to a partiallyconstructed solution that may still lead to a complete solution.

    92 8"a% )( a n#n!-$#m)()ng n#d' )n %"' (%a%'!(-a4' %$''

    A node in a state#space tree is said to be promising if it corresponds to a partiallyconstructed solution that may still lead to a complete solutionK other0ise it is called non#promising.

    12 8"a% d# l'aB'( )n %"' (%a%' (-a4' %$'' $'-$'('n%

    !eaves in the state#space tree represent either non#promising dead ends orcomplete solutions found by the algorithm.

    112 8"a% )( dnam)4 -$#g$amm)ng and *"# d)(4#B'$'d )%

    ynamic programming is a techni4ue for solving problems 0ith overlappingsubproblems. These subproblems arise from a recurrence relating a solution to a given

    problem 0ith solutions to its smaller subproblems only once and recording the results in atable from 0hich the solution to the original problem is obtained. 2t 0as invented by aprominent E. athematician, Jichard Bellman in the Z>-s.

    12 8"a% )( :a4k%$a4k)ng

    Backtracking constructs solutions one component at a time and such partiallyconstructed solutions are evaluated as follo0s

    i. 2f a partially constructed solution can be developed further 0ithoutviolating the problem5s constraints, it is done by taking the first remaining

    legitimate option for the next component.ii.2f there is no legitimate option for the next component, no alternatives for the

    remaining component need to be considered. 2n this case, the algorithm backtracks toreplace the last component of the partially constructed solution 0ith its next option.

    1

  • 8/12/2019 Short Online Q&A for DataStructure

    21/22

    tuple as its *2;+st coordinate. 2f such an element does not exist, the algorithm backtracksto consider the next value of xi, and so on.

    1?2 G)B' a %'m-la%' ;#$ a g'n'$)4 :a4k%$a4k)ng alg#$)%"m2

    A!6OJ2T8

    Backtrack*GL..iM+ ==6ives a template of a generic backtracking algorithm ==2nput GL..iMspecifies the first 2 promising components of a solution ==Output All the tuplesrepresenting the problem5s solution if GL..iM is a solution 0rite GL..iM else for eachelement xLi; Mconsistent 0ith GL..iM and the constraints do GLi;M x

    Backtrack*GL..i;M1@2 8$)%' a $'4u$()B' alg#$)%"m ;#$ (#lB)ng T#*'$ #; Han#) -$#:l'm2 A!6OJ2T8

    To move n

  • 8/12/2019 Short Online Q&A for DataStructure

    22/22

    i.A 0ay to provide, for every node of a state space tree, a bound on the best valueof the obective function on any solution that can be obtained by adding furthercomponents to the partial solution represented by the node.

    ii.The value of the best solution seen so far

    2 8"a% )( kna-(a4k -$#:l'm6iven n items of kno0n 0eights 0i and values vi, i7,%,V,n, and a knapsack of capacity@, find the most valuable subset of the items that fit the knapsack. 2t is convenient toorder the items of a given instance in descending order by their value#to#0eight ratios.Then the first item gives the best payoff per 0eight unit and the last one gives the 0orstpayoff per 0eight unit.