algo excersise

Embed Size (px)

Citation preview

  • 8/6/2019 algo excersise

    1/30

    Solutions to Chapter One Questions1. ___________ is the process of executing a correct program on data sets and measuring the time andspace it takes tocompute the results?Ans. Profiling2.Define algorithm? What are its properties?Ans.

    An algorithm is a set of instructions that provide step-by-step specifications to perform a task.The

    properties of an algorithm are:

    Input: Specifies the data set that is applied to the algorithm to check its validity.

    Output: Specifies the data set that is produced as a result of the algorithm execution.

    D efiniteness: S pecifies t hat the i nstructions descri bed in the algori t hm should be w ell defined andshould not create any ambiguity.

    T ermination: Specifi es that the instruct ions described in the algori thm must contain a proper termination condition.

    E ffectiveness: Specifies that the algorithm take less time and less memory space duringits execution.3.What is debugging and what is profiling?Ans.Debugging is the process of identifying and fixing the errors in a program. Errors in a program can beidentified byexecuting the program with a sample dataset.Profiling is the process of measuring theperformance of the program by executing it on different data sets. Performanceof a programis measured by recording the time and memory space that the program takes during its execution.4.

    One of the properties of an algorithm is beauty (true/false)Ans. False

    Solutions to Chapter Tw o Questions1.G ive at least 5 real life examples where we use stack operations.Ans.The real life examples of stacks are:

    B angles in a hand: The bangles wore in a hand follow last-in-first-out (LIFO) strategy of stack. The bangle that you wear first is the last one to be taken out while removing all thebangles from the hand. The bangle that isworn last is the first one to be taken out.

    Same circumference circular r ings in a pole: The rings having same ci rcumferenceplaced into a pole also follow LIFO str ategy.The to pm ost ri ng, w hic h wa s th e la st to be placed in the pole, is the first one to be taken out.

  • 8/6/2019 algo excersise

    2/30

    Sacks full of w heat placed one over other : The sack at the top is removed first and thesack at the bottom is removed last.

    T he bolts scre w ed to a single nut: When the bolts are screwed to a single nut, the lastscrewed bolt is unscrewed first and the bolt that

    was screwed first is unscrewed in the last. B attery cells in a torch: The battery cells in a torch also follow the same LIFO strategy of stack

    2.G ive at least 5real life examples

    where queue isused.Ans.Real life examplesof queue are:

  • 8/6/2019 algo excersise

    3/30

    A queue of people at ticket-w

    indow

    : The person whocomes first gets

    the ticket first.Theperson who iscoming last isgetting the ticketsin last. Therefore,

  • 8/6/2019 algo excersise

    4/30

    it follows first-in-first-out(FIFO)strategy of queue.

    V ehicles on toll-tax bridge

    : The vehicle thatcomes first to thetoll tax boothleaves theboothfirst. The vehicle

  • 8/6/2019 algo excersise

    5/30

    that comes lastleaves last.Therefore, itfollows first-in-first-out(FIFO)strategy of queue.

    Phone ans w eringsystem: The person whocalls first gets a

  • 8/6/2019 algo excersise

    6/30

    response first fromthephoneansweringsystem. Theperson who callslast gets the

    response last.Therefore, itfollowsfirst-in-first-out (FIFO)strategy of queue.

  • 8/6/2019 algo excersise

    7/30

    L uggagechecking

    machine: Luggagechecking

    machine checksthe luggage firstthatcomes first.Therefore, it

  • 8/6/2019 algo excersise

    8/30

    follows FIFOprinciple of queue.

    Patientsw

    aitingoutside thedoctor's clinic

    : The patient whocomes first visitsthe doctor first,and the patientwho comes last

  • 8/6/2019 algo excersise

    9/30

    visits the doctor last. Therefore, itfollows the first-in-first-out (FIFO)strategy of queue.3.

    Name 10situations that canbe represented bymeans of graphs.Explain what

  • 8/6/2019 algo excersise

    10/30

    each vertex andedge represents.Ans.The situations thatcan berepresented by

    means of graphsare:E x a m p l e

    V e r t e x re p r e s e n t

  • 8/6/2019 algo excersise

    11/30

    s E d g e r ep r e s e n t sD i s p l a c e m e nt P o s i t i o n o rs t a t e D i s t a n ce b e t w e e n t h

    e t w o v e r t i ce s connectedthrough theedgeS h o r t e st P a t h C i t

  • 8/6/2019 algo excersise

    12/30

    y D i s t a n c eb e t w e e n

    t h e t w o v e r t i c e s connected through theedgeC i t y M a p

    P l a c e s o f t he c i t y D i s t an c e b e t w e en t h e t w o v e r t i c e s connect

  • 8/6/2019 algo excersise

    13/30

    ed through thee d g e A i r r o u te D i f f e r e n t ai r p o r t s D i s ta n c e b e t w e en t h e t w o v

    e r t i c e s c o n n e c ted through theedgePopulationGrowthper y ear Populati

  • 8/6/2019 algo excersise

    14/30

    on for an y earVariation inthe population between thetwo vertices connectedthrough theedgeIn

    mathematical expressionsV a l u eC o n d i t i o ns a n d c a l cu l a t i o n s Dail

  • 8/6/2019 algo excersise

    15/30

    y temperaturereportTemperature foraspecifictimeVariation inthe temperature

    betweenthe vertices connectedthroughtheedgeChild-parent relationshi

  • 8/6/2019 algo excersise

    16/30

    pN a m e o f t h ep e r s o n R e l a t i on s h i p O r g a n iz a t i o n c h a rt D e s i g n a t io n O r d e r o r

    f l o w C h e m ic a ls t r u c t u r e C h em i c a le l e m e n t s B o n d

  • 8/6/2019 algo excersise

    17/30

    i n g b e t w e e nt h e e l e m e n t s

    4.Draw a connectedgraph that

    becomesdisconnectedwhen any edge isremoved from it.Ans.

  • 8/6/2019 algo excersise

    18/30

    The followingfigure shows agraph thatbecomesdisconnectedwhen any edge is

    removed from it:DisconnectedGraph5 .

  • 8/6/2019 algo excersise

    19/30

    Draw all trees of nlabeled verticesfor n=1,2,3,4 and5.Ans.The following

    figures show thetrees for variousvalues of n, wheren represents thenumber of vertices

  • 8/6/2019 algo excersise

    20/30

    in a tree.For n=1For n=2For n=3There can be anumber of treeswith 3labeled vertices.

    Some of theexamples of thetrees with 3labeled verticesare

  • 8/6/2019 algo excersise

    21/30

    For n=4Again,

    there can be anumber of trees

    with 4 labeledvertices. Some of the examples of the trees with 4

  • 8/6/2019 algo excersise

    22/30

    labeledverticesare:For n=5Again, there

    can be a number of trees with 5

    labeled vertices.Some of theexamples of thetrees with 5

  • 8/6/2019 algo excersise

    23/30

    labeledverticesare

    6 .Sketch all binary

    trees withsix pendentedges.Ans.

  • 8/6/2019 algo excersise

    24/30

    The followingfigures showbinary trees withsix pendentedges:

  • 8/6/2019 algo excersise

    25/30

  • 8/6/2019 algo excersise

    26/30

  • 8/6/2019 algo excersise

    27/30

  • 8/6/2019 algo excersise

    28/30

    Binar y Tree withSix Pendent Edges

  • 8/6/2019 algo excersise

    29/30

    The precedingfigures, A, B, andC, show threeexamples of binary treeshaving six

    pendentedges. All thebinary treeshavingsix leaf nodes come

  • 8/6/2019 algo excersise

    30/30

    under thiscategory