DAA 2marks With Answers

download DAA 2marks With Answers

of 11

Transcript of DAA 2marks With Answers

  • 7/26/2019 DAA 2marks With Answers

    1/11

    Unit I

    Algorithm:

    An algorithm is a sequence of nonambiguous instructions for solving a problem in a finite

    amount of time. An input to an algorithm specifies an instance of the problem the algorithm

    solves.

    How can you classify the algorithms?

    Among several ways to classify algorithms, the two principal alternatives are:

    . to group algorithms according to types of problems they solve

    . to group algorithms according to underlying design techniques they are based upon

    ADT

    An abstract collection of objects with several operations that can be performed on them is called

    an abstract data type (ADT. !he list, thestack, the queue, thepriority queue, and the dictionaryare important e"amples of abstract data types. #odern object$oriented languages support

    implementation of A%!s by means of classes.

    Algorithm Design Technique:

    An algorithm design technique (or &strategy' or &paradigm' is a general approach to solving

    problems algorithmically that is applicable to a variety of problems from different areas of

    computing.

    Pseudocode:

    Pseudocode is a mi"ture of a natural language and programming languagelie constructs.

    )seudocode is usually more precise than natural language, and its usage often yields moresuccinct algorithm descriptions.

    Flow Chart:

    Flowchartis a method of e"pressing an algorithm by a collection of connected geometric shapes

    containing descriptions of the algorithm*s steps.

    General Plan for Analyzing the Time fficiency of !onrecursi"e Algorithms

    #$ %ecide on a parameter (or parameters indicating an input*s si+e.

    %$ dentify the algorithm*s basic operation. (As a rule, it is located in the innermost loop.

    &$ -hec whether the number of times the basic operation is e"ecuted depends only on the si+e of

    an input. f it also depends on some additional property, the worst$case, average$case, and, ifnecessary, best$case efficiencies have to be investigated separately.

    '$ et up a sum e"pressing the number of times the algorithm*s basic operation is e"ecuted.

    ($ /sing standard formulas and rules of sum manipulation either find a closed form formula for

    the count or, at the very least, establish its order of growth.

    General Plan for Analyzing the Time fficiency of )ecursi"e Algorithms

    #$ %ecide on a parameter (or parameters indicating an input*s si+e.

  • 7/26/2019 DAA 2marks With Answers

    2/11

    %$ dentify the algorithm*s basic operation.

    &$ -hec whether the number of times the basic operation is e"ecuted can vary on different

    inputs of the same si+e0 if it can, the worst$case, average$case, and best$case efficiencies must beinvestigated separately.

    '$ et up a recurrence relation, with an appropriate initial condition, for the number of times the

    basic operation is e"ecuted.($ olve the recurrence or, at least, ascertain the order of growth of its solution.

    Two measures of efficiency:

    !here are two inds of algorithm efficiency: time efficiency and space efficiency. Time efficiency

    indicates how fast the algorithm runs0spaceefficiency deals with the e"tra space it requires.

    *asic +,eration:

    Analgorithm*s time efficiency is principally measured as a function of its input si+e by counting

    the number of times its basic operation is e"ecuted. A basic operation is the operation that

    contributes the most to running time. !ypically, it is the most time$consuming operation in the

    algorithm*s innermost loop.

    +rder of Growth:

    !he established framewor for analy+ing time efficiency is primarily grounded in the order of

    growth of the algorithm*s running time as its input si+e goes to infinity. !he notations O, _, and_

    are used to indicate and compare the asymptotic orders of growth of functions e"pressingalgorithm efficiencies.

    Unit II

    *rute Force:

    Brute force is a straightforward approach to solving a problem, usually directly based on the

    problem statement and definitions of the concepts involved.1ive 2"ample

    -aster Theorem:

    ff (n) _(nd) where d 3 4 in recurrence (5.6, then

  • 7/26/2019 DAA 2marks With Answers

    3/11

    Analogous results hold for the O and notations, too.

    Closest.Pair Pro/lem

    !he closest$pair problem calls for finding the two closest points in a set of npoints using 7rute

    8orce approach. t is the simplest of a variety of problems in computational geometry that deals

    with pro"imity of points in the plane or higher$dimensional spaces.

    Con"e0 1ull Pro/lem:

    8inding the conve" hull for a given set of points in the plane or a higher dimensional space is one

    of the most important9some people believe the most important9problems in computationalgeometry. !his prominence is due to a variety of applications in which this problem needs to be

    solved, either by itself or as a part of a larger tas.

    Con"e0:

    A set of points (finite or infinite in the plane is called convex if for any two pointsp and q in the

    set, the entire line segment with the endpoints atp and qbelongs to the set.

    Con"e0 1ull:

    !he convex hull of a set of points is the smallest conve" set containing . (!he &smallest'

    requirement means that the conve" hull of must be a subset of any conve" set containing .

    Exhaistive Search:

    Exhaustive search is simply a brute$force approach to combinatorial problems. t suggestsgenerating each and every element of the problem domain, selecting those of them that satisfy all

    the constraints, and then finding a desired element (e.g., the one that optimi+es some objective

    function.1ive 2"ample

    1amiltonian Circuit:

    A Hamiltonian circuit is defined as a cycle that passes through all the vertices of the graph

    e"actly once. Here ource and %estination of a verte" is the same verte".

    General Plan for Di"ide and Conquer -ethod:

    #$ A problem is divided into several subproblems of the same type, ideally of about equal si+e.

    %$ !he subproblems are solved (typically recursively, though sometimes a different algorithm is

    employed, especially when subproblems become small enough.

    &$ f necessary, the solutions to the subproblems are combined to get a solution to the original

    problem.

    1ive 2"ample

  • 7/26/2019 DAA 2marks With Answers

    4/11

    *inary Tree

    A binary tree T is defined as a finite set of nodes that is either empty or consists of a root and

    two disjoint binary trees T! and T" called, respectively, the left and right subtree of the root.1ive 2"ample tree

    Tree Tra"ersals:6. n thepreorder traversal, the root is visited before the left and right subtrees are visited

    (in that order.

    . n the inorder traversal, the root is visited after visiting its left subtree but before visitingthe right subtree.

    ;. n the postorder traversal, the root is visited after visiting the left and right subtrees (in

    that order.

    -erge sort

    #ergesort is a divide$and$conquer sorting algorithm. t wors by dividing an input array into two

    halves, sorting them recursively, and then merging the two sorted halves to get the original array

    sorted. !he algorithm*s time efficiency is in (n

    log n) in all cases.

    2uic3 4ort

    $uicksort is a divide$and$conquer sorting algorithm that wors by partitioning its input elements

    according to their value relative to some preselected element.

  • 7/26/2019 DAA 2marks With Answers

    5/11

    +*4T:

    An o,timal /inary search treeis a /inary search treefor which the nodes are arranged on

    levels such that the treecost is minimum. an o,timal /inary search tree(*4T, sometimescalled a weight$balanced /inary tree, is a /inary search treewhich provides the smallest

    possible searchtime (or e"pected searchtime for a given sequence of accesses (or access

    probabilities.

    Transiti"e Closure:

    !he transitive closure of a directed graph with n vertices can be defined as the n n booleanmatri" T @ ti&B, in which the element in the ith row and the&th column is 6 if there e"ists a

    nontrivial path (i.e., directed path of a positive length from the ith verte" to the &th verte"0

    otherwise, ti& is 4.

    Digra,h:

    A digra,his short for directed graph, and it is a diagram composed of points

    called "ertices(nodes and arrows called arcsgoing from a verte" to a verte".

    1ive e"ample

    Ad5acency -atri0:

    An ad5acency matri0is a square matri0used to represent a finite graph. !he elements of

    the matri0indicate whether pairs of vertices are adjacent or not in the graph.

    Distance -atri0:

    a distance matri0is a matri0(two$dimensional array containing the distances, taen

    pairwise, between the elements of a set. %epending upon the application involved,

    the distancebeing used to define this matri0may or may not be a metric.

    Greedy Technique:!he greedy technique suggests constructing a solution to an optimi+ation problem through asequence of steps, each e"panding a partially constructed solution obtained so far, until a

    complete solution to the problem is reached. =n each step, the choice made must be feasible,

    locally optimal, and irre'ocable

    Prims lgorithm:

    rim*s algorithm is a greedy algorithm for constructing a minimum spanning tree of a weighted

    connected graph. t wors by attaching to a previously constructed subtree a verte" closest to thevertices already in the tree.

    6rus3al7s Algorithm:+ruskal*s algorithm is another greedy algorithm for the minimum spanning tree problem. t

    constructs a minimum spanning tree by selecting edges in nondecreasing order of their weights

    provided that the inclusion does not create a cycle.

    Di53stra7s Algorithm:

    Di&kstra*s algorithm solves the singlesource shortestpath problem of finding shortest paths

    from a given verte" (the source to all the other vertices of a weighted graph or digraph. t wors

  • 7/26/2019 DAA 2marks With Answers

    6/11

    as )rim*s algorithm but compares path lengths rather than edge lengths. %ijstra*s algorithm

    always yields a correct solution for a graph with nonnegative weights.

    1uffman Tree:

    A -uffman tree is a binary tree that minimi+es the weighted path length from the root to the

    leaves of predefined weights. !he most important application of Huffman trees is Huffmancodes.

    1uffman Code:

    A -uffman code is an optimal prefi"$free variable$length encoding scheme that assigns bit

    strings to symbols based on their frequencies in a given te"t. !his is accomplished by a greedy

    construction of a binary tree whose leaves represent the alphabet symbols and whose edges are

    labeled with 4*s and 6*s.

    Difference /etween Dynaimc ,rogramming and Greedy a,,roach

    Greedy Programming A greedy algorithm is one which finds optimal solution at each and every stage with thehope of finding global optimum at the end.

    Dynamic Programming A Dynamic algorithm is applicable to problems that exhibit Overlappingsubproblemsand Optimal substructureproperties.

    Difference The main difference is that, the choice made by a greedy algorithm may depend on choicesmade so far but not on future choices or all the solutions to the sub problem. It iteratively makesone greedy choice after another, reducing each given problem into a smaller one. In other words,a greedy algorithm never reconsiders its choices. This is the main difference from dynamicprogramming, which is exhaustive and is guaranteed to find the solution. After every stage,dynamic programming makes decisions based on all the decisions made in the previous stage,and may reconsider the previous stages algorithmic path to solution.

    Distinguish /etween Dynamic ,rogramming and Di"ide and Conquer

    http://en.wikipedia.org/wiki/Overlapping_subproblemshttp://en.wikipedia.org/wiki/Overlapping_subproblemshttp://en.wikipedia.org/wiki/Optimal_substructurehttp://en.wikipedia.org/wiki/Overlapping_subproblemshttp://en.wikipedia.org/wiki/Overlapping_subproblemshttp://en.wikipedia.org/wiki/Optimal_substructure
  • 7/26/2019 DAA 2marks With Answers

    7/11

    Does Prim7s algorithm always wor3 correctly on gra,hs with negati"e edge weights8

    Pro"e that any weighted connected gra,h with distinct weights has e0actly one minimum

    s,anning tree$

    Cets assume that the graph has #! $ #!6 and #!. Cet 2 be the set of edges present in

    #! but not in #!6.

    -onsider #!6. f this is a minimum spanning tree, adding an edge to it should create a cycle.

    -onsider adding an edge DeD from 2. Add DeD to #!6. !his would create a cycle. Hence, this newtree ( say ! is just 6 edge away from being a #!. !o mae a #E#/# spanning tree out of

    it, you have to remove the most e"pensive edge in the cycle. 7ecause all the edges have different

    weights, the most e"pensive edge will be only one of its ind. f DeD is the most e"pensive edge,then, you donDt get multiple #!s. f DeD is not the most e"pensive edge, then #!6 was not a

    #E#/# spanning tree.

    Unit I9

    Iterati"e Im,ro"ement

    !he iterati'eimpro'ement technique involves finding a solution to an optimi+ation problem bygenerating a sequence of feasible solutions with improving values of the problem*s objective

    function. 2ach subsequent solution in such a sequence typically involves a small, locali+ed

    change in the previous feasible solution. Fhen no such change improves the value of the

    objective function, the algorithm returns the last feasible solution as optimal and stops.

    4im,le0 -ethod:

    !hesimple. method is the classic method for solving the general linear programming problem. twors by generating a sequence of adjacent e"treme points of the problem*s feasible region with

    improving values of the objective function.

    #a"imum 8low )roblem:

  • 7/26/2019 DAA 2marks With Answers

    8/11

    !he ma.imumflo% problem ass to find the ma"imum flow possible in a networ, a weighted

    directed graph with a source and a sin.

    Ford.Ful3erson -ethod:

    !he/ord/ulkerson method is a classic template for solving the ma"imumflow problem by the

    iterative$improvement approach. !heshortestaugmentingpath method implements this idea bylabeling networ vertices in the breadth$first search manner. !he 8ord$8ulerson method also

    finds a minimum cut in a given networ.

    !aximum cardinality matching

    A ma.imum cardinality matching is the largest subset of edges in a graph such that no two edges

    share the same verte". 8or a bipartite graph, it can be found by a sequence of augmentations of

    previously obtained matchings.

    4ta/le -arriage Pro/lem:

    !hestable marriage problem is to find astable matching for elements of two n element sets

    based on given matching preferences. !his problem always has a solution that can be found bythe 0alehapley algorithm.

    -an.o,timal

    t assigns to each man the highest$raned woman possible under any stable marriage.

    oman.o,timal

    t assigns to each man the highest$raned man possible under any stable marriage.

    *loc3ing ,air

    A pair (m, %), where m 1, % 2, is said to be a bloc"ing pair for a marriage matching# ifman m and woman % are not matched in#but they prefer each other to their mates in#

    !aximum !atching #heorem:

    A matching # is a ma"imum matching if and only if there e"ists no augmenting path with

    respect to#.

    *i,artite Gra,h:

    n a bipartite graph, all the vertices can be partitioned into two disjoint sets 3 and 4, not

    necessarily of the same si+e, so that every edge connects a verte" in one of these sets to a verte"

    in the other set.

    Preflow:

    preflow is a flow that satisfies the capacity constraints but not the flow$conservationrequirement. Any verte" is allowed to have more flow entering the verte" than leaving it.A

    preflowpush algorithm moves the e"cess flow toward the sin until the flow$conservation

    requirement is reestablished for all intermediate vertices of the networ.

    !ax$Flow !in$%ut #heorem

    !he value of a ma"imum flow in a networ is equal to the capacity of its minimum cut.

  • 7/26/2019 DAA 2marks With Answers

    9/11

    Augmenting Path -ethod:

    =n each iteration, we can try to find a path from source to sin along which some additional flow

    can be sent. uch a path is called flow augmenting. f a flow$augmenting path is found, weadjust the flow along the edges of this path to get a flow of an increased value and try to find an

    augmenting path for the new flow. f no flow$augmenting path can be found, we conclude that

    the current flow is optimal. !his general template for solving the ma"imum$flow problem iscalled the augmenting$path method& #his is also "nown as Ford$Ful"erson method&

    Properties of digraph:

    t contains e"actly one verte" with no entering edges0 this verte" is called the source and

    assumed to be numbered 6.

    t contains e"actly one verte" with no leaving edges0 this verte" is called the sin" and

    assumed to be numbered n.

    !he weight ui& of each directed edge (i, & ) is a positive integer, called the edge capacity.

    (!his number represents the upper bound on the amount of the material that can be sent

    from i to& through a lin represented by this edge.

    Simplex tableau

    2ach e"teme point can be represented by a simplex tableau, a table storing the information about

    the basic feasible solution corresponding to the e"treme point.

    )equirements of the standard form in sim,le0 method:

    t must be a ma"imi+ation problem.

    All the constraints (e"cept the nonnegativity constraints must be in the form of linear

    equations with nonnegative right$hand sides.

    All the variables must be required to be nonnegative.

    0treme Point Theorem:

    Any linear programming problem with a nonempty bounded feasible region has an optimal

    solution0 moreover, an optimal solution can always be found at an e"treme point of the problem*s

    feasible

    region.

    Unit 9

    Tri"ial ;ower *ound

    A tri'ial lo%er bound is based on counting the number of items in the problem*s input that mustbe processed and the number of output items that need to be produced.

    Information.theoretic ;ower *ound:

    An informationtheoretic lo%er bound is usually obtained through a mechanism of decision trees.

    !his technique is particularly useful for comparison based algorithms for sorting and searching.

    Decision Tree

  • 7/26/2019 DAA 2marks With Answers

    10/11

    An informationtheoretic lo%er bound is usually obtained through a mechanism of decision trees.

    2ach internal node of a binary decision tree represents a ey comparison indicated in the node,

    e.g., k 5 k. !he node*s left subtree contains the information about subsequent comparisons madeif k 5 k, and its right subtree does the same for the case of k 6k 2ach leaf represents a possible

    outcome of the algorithm*s

    run on some input of si+e n.

    Decision tree for finding a minimum of three num/ers:

    .

    Ad"ersary -ethod:

    !he ad'ersary method for establishing lower bounds is based on following the logic of amalevolent adversary who forces the algorithm into the most time$consuming path.

    Class P

    -lass is a class of decision problems that can be solved in polynomial time by (deterministicalgorithms. !his class of problems is calledpolynomial.

    1ive e"ample

    Class !P

    -lass7 is the class of decision problems that can be solved by nondeterministic polynomial

    algorithms. !his class of problems is called nondeterministicpolynomial.

    !P.Com,lete

    A decision problemD is said to be'P$complete if:

    #$ it belongs to class7

    %$ every problem in7 is polynomially reducible toD

    %'F$satisfiability problem

    #he %'F$satisfiability problem is 7complete. !he -E8$satisfiability problem deals with

    boolean e"pressions. 2ach boolean e"pression can be represented in conjunctive normal form,

    and ass whether or not one can assign values true andfalse to variables of a given booleane"pression in its -E8 form to mae the entire e"pression true.

    Difference /etween /ac3trac3ing and /ranch.and./ound

  • 7/26/2019 DAA 2marks With Answers

    11/11

    A,,ro0imation Algorithms

    Appro"imation algorithms are often used to find appro"imate solutions to difficult problems ofcombinatorial optimi+ation. !he performance ratio is the principal metric for measuring the

    accuracy of such appro"imation algorithms.

    Gra,h Coloring Pro/lem

    8or a given graph, find its chromatic number, which is the smallest number of colors that need to

    be assigned to the graph*s vertices so that no two adjacent vertices are assigned the same color.