MCA-4 MC0080 I

download MCA-4 MC0080 I

of 15

Transcript of MCA-4 MC0080 I

  • 7/27/2019 MCA-4 MC0080 I

    1/15

    NAME - KRUSHITHA.V.P

    ROLL NO. - 520791371

    ASSIGNMENT SET 1

    SUBJECT - MC0080

    ANALYSIS AND DESIGN

    OF ALGORITHMS

    1

  • 7/27/2019 MCA-4 MC0080 I

    2/15

    Master of Computer Applications (MCA) Semester 4MC0080

    Assignment No: 01Subject Name: Analysis and Design of Algorithms

    2. Explain the concept of Notation in algorithms

    The Notation provides simultaneously both asymptotic lower bound andasymptoticupper bound for a given function.

    Let f(x) and g(x) be two functions, each from the set of natural numbers or

    positive real numbers to positive real numbers. Then f(x) is said to be(g(x)) (pronounced as big theta ofg ofx) if, there exists positive constants

    C1, C2and ksuch that C2 g(x) f(x) C1g(x)for allx k.

    Here the last inequalities represent two conditions to be satisfied

    simultaneously like.,

    C2g(x) f(x)and f(x) C1 g(x)

    So, the following theorem without proof, which relates the three functions

    Theorem: For any two functions f(x) and g(x), f(x) = if and only if

    f (x) = O (g(x)) and f(x) = where f(x) and g(x) are nonnegative..

    Let us consider the following example:-

    Let f(n) = 1+2 + . + n, n 1. Show that f(n) = .

    Solution:- First of all, we find an upper bound for the sum .

    For n 1, consider

    1+2 + + n n + n + + n = n. n = n2.

    This implies that f(n) = O(n2). Next, we obtain a lower bound for the sum.

    So,We have

    1 + 2 + + n = 1+2 + +

    2

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image210.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image08022.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image18327.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image18326.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image08019.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image08018.jpg
  • 7/27/2019 MCA-4 MC0080 I

    3/15

    = .

    This proves that f(n) = W (n2).

    So by the above. Theorem f(n) = q (n2).

    5. For a binomial tree Bk Prove that(i) there are 2knodes

    (ii) the height of the trees is k

    (iii) there are exactly nodes at depth ifor I = 0, 1, , k

    Proof: The proof is by induction on k. For each property, the basis is the

    binomial tree B0 . Verifying that each property holds for B0 is trivial.

    For the inductive step, we assume that the lemma holds for B k1.

    1. Binomial tree Bk consists of two copies of Bk1, and so Bk has

    2k1+2k1 = 2knodes.

    2. Because of the way in which the two copies ofBk1are linked to form

    Bk, the maximum depth of a node in Bk is one greater than maximum

    depth in B k1.By the inductive hypothesis, this maximum depth is (k1)

    + 1 = k.

    3. Let D(k, i) be the number of nodes at depth i of binomial tree Bk. Since

    Bkis composed of two copies ofBk1 linked together, a node at depth i

    in Bk1 appears in Bk once at depth and once at depth i + 1. In other

    words, the number of nodes at depth i in Bk is the number of nodes at

    depth i in Bk1 plus the number of nodes at depth i 1 in Bk1. Thus,

    3

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image0307.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image0289.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2101.gif
  • 7/27/2019 MCA-4 MC0080 I

    4/15

    (by the inductive hypothesis)

    (formula)

    .

    6. What do you mean by Fibonacci heaps? Explain their structure?

    Fibonacci heap is a collection of min-heap-ordered trees. The trees in aFibonacci heap are not constrained to be binomial trees, however. This is theexample of Fibonacci heap.

    Unlike trees within binomial heaps, which are ordered, trees within Fibonacciheaps are rooted but unordered. As Figure 4.3(b) shows, each node xcontains a pointerp [x] to its parent and a pointer child [x] to any one of itschildren. The children ofxare linked together in a circular, doubly linked list,which we call the child list ofx. Each child y in a child list has pointers left [y]and right [y] that point to ys left and right siblings, respectively. If node y isan only child, then left [y] = right [y] = y. The order in which siblings appearin a child list is arbitrary.

    4

  • 7/27/2019 MCA-4 MC0080 I

    5/15

    Two other fields in each node will be of use. The number of children in the

    child list of node x is stored in degree[x]. The Boolean-valued field mark[x]

    indicates whether nodexhas lost a child since the last time xwas made the

    child of another node. Newly created nodes are unmarked, and a node x

    becomes unmarked whenever it is made the child of another node.

    A given Fibonacci heap H is accessed by a pointer min [H] to the root of a

    tree containing a minimum key; this node is called the minimum node of

    the Fibonacci heap. If a Fibonacci heap H is empty, then min [H] = NIL.

    The roots of all the trees in a Fibonacci heap are linked together using their

    left and right pointers into a circular, doubly linked list called the root list of

    the Fibonacci heap. The pointer min [H] thus points to the node in the root list

    whose key is minimum. The order of the trees within a root list is arbitrary.

    We rely on one other attribute for a Fibonacci heap H: the number of nodescurrently in H is kept in n[H].

    Potential function

    For a given Fibonacci heap H, we indicate by t (H) the number of trees in theroot list of H and by m(H) the number of marked nodes in H. The potential ofFibonacci heap H is then defined by

    5

  • 7/27/2019 MCA-4 MC0080 I

    6/15

    (a)

    For example, the potential of the Fibonacci heap shown in above figure is

    5+2.3 = 11. The potential of a set of Fibonacci heaps is the sum of the

    potentials of its constituent Fibonacci heaps. We shall assume that a unit of

    potential can pay for a constant amount of work, where the constant is

    sufficiently large to cover the cost of any of the specific constant-time pieces

    of work that we might encounter.

    We assume that a Fibonacci heap application begins with no heaps. The

    initial potential, therefore, is 0, and by equation (a), the potential is

    nonnegative at all subsequent times.

    Maximum degree

    The amortized analyses we shall perform in the remaining sections of thisunit assume that there is a known upper bound D(n) on the maximum degree

    of any node in an n-node Fibonacci heap.

    3. Describe the concept of quicksort with suitable examples

    Quick Sort

    The purpose of discussing the Quick Sort Algorithm is to discuss its analysis.

    The Quick-Sort algorithm is obtained by exploring another possibility of

    dividing the elements such that there is no need of merging, that is

    Partition a [1..n] into sub arrays and such that

    all elements in are larger than all elements in .

    Recursively sort and .

    (nothing to combine / merge. A is already sorted after sorting and )

    Pseudo code for QUICKSORT:

    6

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image16411.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1622.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1641.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image16211.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1621.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1601.gif
  • 7/27/2019 MCA-4 MC0080 I

    7/15

    The algorithm PARTITION, which is called by QUICKSORT, is defined after ashort while.

    Then, in order to sort an array A of n elements, we call QUICKSORT with thethree parameters A, 1 and n QUICKSORT (A, 1, n).

    If and is time, we again get the recurrence. If T (n) denotes thetime taken by QUICKSORT in sorting an array of n elements.

    . Then after solving the recurrence we get the running

    time as

    The problem is that it is hard to develop partition algorithm which alwaysdivides A in two halves.

    Average running time

    Average time depends on the distribution of inputs for which we take theaverage.

    If we run QUICKSORT on a set of inputs that are already sorted, the

    average running time will be close to the worst-case.

    7

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image195.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image193.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image189.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1871.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1851.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1831.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1791.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1771.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1751.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1732.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1712.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1692.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image1672.gif
  • 7/27/2019 MCA-4 MC0080 I

    8/15

    Similarly, if we run QUICKSORT on a set of inputs that give good splits,

    the average running time will be close to the best-case.

    If we run QUICKSORT on a set of inputs which are picked uniformly at

    random from the space of all possible input permutations, then the

    average case will also be close to the best-case.

    So, under the assumption that all input permutations are equally likely, the

    average time of QUICKSORT is (intuitively). Is this assumptionrealistic?

    Not really. In many cases the input is almost sorted: think of rebuildingindexes in a database etc.

    Randomization Quick Sort*

    Running time of normal deterministic algorithm only depends on the input.

    Running time of randomized algorithm depends not only on input but also on

    the random choices made by the algorithm.

    Running time of a randomized algorithm is not fixed for a given input!

    Randomized algorithms have best-case and worst-case running times, but the

    inputs for which these are achieved are not known, they can be any of the

    inputs.

    We are normally interested in analyzing the expected running time of a

    randomized algorithm, that is the expected (average) running time for all

    inputs of size n

    Randomized Quick sort

    We can enforce that all n! permutations are equally likely by randomly

    permuting the input before the algorithm.

    Most computers have pseudo-random number generator random (1, n)

    returning random number between 1 and n.

    Using pseudo-random number generator we can generate a random

    permutation (such that all n! permutations equally likely) in O (n) time:

    8

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image203.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2011.gif
  • 7/27/2019 MCA-4 MC0080 I

    9/15

    Choose element inA[1] randomly among elements inA [1n],

    Choose element inA[2] randomly among elements in A [2..n], choose

    Element inA [3] randomly among elements inA [3..n] and so on.

    Alternatively we can modify PARTITION slightly and exchange last element

    in A with random element in A before partitioning.

    Expected Running Time of Randomized Quick sort

    Let T(n) be the running of RANDQUICKSORT for an input of size n.

    Running time of RANDQUICKSORT is the total running time spent in allPARTITION calls.

    PARTITION is called n times

    The pivot element r is not included in any recursive calls.

    One call of PARTITION takes time plus time proportional to the number ofiterations FOR-loop.

    In each iteration of FOR-loop we compare an element with the pivotelement.

    9

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image209.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image207.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image205.jpg
  • 7/27/2019 MCA-4 MC0080 I

    10/15

    If X is the number of comparisons performed in PARTITION over theentire execution of RANDQUICKSORT then the running time is

    O (n + X).

    E [T (n) ] = E [O (n+X) ] = n + E [X]

    To analyze the expected running time we need to compute E[X]

    To computeXwe usez1, z2, znto denote the elements in A wherez1 is theith smallest element. We also usezij to denote {zI, zj+1, . zj}.

    Each pair of elements zi and zj are compared at most once (when either ofthem is the pivot)

    where

    To compute Pr [zi compared to zj] it is useful to consider when two elementsare not compared.

    Ex: Consider an input consisting of numbers 1 through n.

    Assume first pivot it 7 first partition separates the numbers into set

    .

    In partitioning 7 is compared to all numbers. No number from the first set willever be compared to a number from the second set.

    In general once a pivot , is chosen we know that zi and zjcannotlater be compared.

    10

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image221.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image219.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image217.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image213.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image211.gif
  • 7/27/2019 MCA-4 MC0080 I

    11/15

    On the other hand ifzi is chosen as pivot before any other element in zij thenit is compared to each element inzij. Similar forzj.

    7 and 9 are compared because 7 is first item from Z 7, 9 to be chosen as pivotand 2 and 9 are not compared because the first pivot in Z2, 9 is 7.

    Prior to an element in Zij being chosen as pivot the set Zij is together in the

    same partition any element in Zij is equally likely to be first element

    chosen as pivot the probability thatzIorzj is chosen first inZij is

    We now have:

    Since best case is and therefore .

    Next time we will see how to make quick sort run in worst-casetime.

    4. What do you mean by linked lists? Explain with example?

    Linked lists

    A linked list is a data structure in which the objects are arranged in a linear

    order. Unlike an array, though, in which the linear order is determined by the

    array indices, the order in a linked list is determined by a pointer in each

    object. Linked lists provide a simple, flexible representation for dynamic sets.

    11

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image252.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image250.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2481.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2461.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image244.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image242.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image240.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image238.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image236.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2341.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2321.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image2291.gifhttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image229.gif
  • 7/27/2019 MCA-4 MC0080 I

    12/15

    Each element of a doubly linked list L is an object with key field and two

    other pointer fields: next and prev. The object may also contain other satellite

    data. Given an element x in the list, next [x] points to its successor in the

    linked list, and prev [x] points to its predecessor. If prev [x]= NIL, the

    elementxhas no predecessor and is therefore the first element, or head, of

    the list. If next [x] = NIL, the elementxhas no successor and is therefore thelast element, or tail, of the list. An attribute head [L] points to the first

    element of the list. IF head [L] = NIL, the list is empty.

    A list may have one of several forms. It may be either singly linked or doubly

    linked, it may be sorted or not, and it may be circular or not. If a list is singly

    linked, we omit the prev pointer in each element. If a list is sorted, the

    linear order of the list corresponds to the linear order of keys stored in

    elements of the list; the minimum element is the head of the list, and the

    maximum element is the tail. If the list is unsorted, the elements can appear

    in any order. In a circular list, the prev pointer of the head of the list points

    to the tail, and the next pointer of the tail of the list points to the head. The

    list may thus be viewed as a ring of elements.

    A doublylinkedlistLrepresentingthedynamicset{1, 4, 9, 16}.

    Each element in the list is an object with fields for the key and pointers

    (shown by arrows) to the next and previous objects. The next field of the tail

    and the prev field of the head are NIL, indicated by a diagonal slash. The

    attribute head [L] points to the head.

    Following the execution of LIST-INSERT (L, x), where key [x] = 25, the linked

    list has a new object with key 25 as the new head. This new object points to

    the old head with key 9.

    The result of the subsequent call LIST-DELETE (L, x), where x points to the

    object with key 4.

    Searching a linked list

    12

  • 7/27/2019 MCA-4 MC0080 I

    13/15

    The procedure LIST-SEARCH (L, k) finds the first element with key k in list Lby a simple linear search, returning a pointer to this element. If no objectwith key kappears in the list, then NIL is returned.

    LIST-SEARCH (L, 4)returns a pointer to the third element, and the call LIST-SEARCH (L, 7) returns NIL.

    LIST SEARCH (L, k)

    1.x head [L]

    2. whilex NIL and key [x] k

    3. dox next [x]

    4. returnx

    To search a list of n objects, the LIST-SEARCH procedure takes time inthe worst case, since it may have to search the entire list.

    Inserting into a linked list

    Given an element xwhose key field has already been set, the LIST-INSERTprocedure splicesxonto the front of the linked list

    LIST INSERT (L, x)

    1. next [x] head [L]

    2. ifhead [L] NIL

    3. then prev[head[L]] x

    4. head [L] x

    5. prev [x] NIL

    Deleting from a linked list

    The procedure LIST-DELETE removes an elementxfrom a linked L. It must begiven a pointer to x, and it then splices x out of the list by updatingpointers. If we wish to delete an element with a given key, we must first callLIST-SEARCH to retrieve a pointer to the element.

    LIST DELETE (L, x)

    1. ifprev [x] NIL

    13

    http://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image004411.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image010112.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image01088.jpghttp://resources.smude.edu.in/slm/wp-content/uploads/2010/07/clip-image004312.jpg
  • 7/27/2019 MCA-4 MC0080 I

    14/15

    2. then next [prev[x]] next [x]

    3. else head [L] next [x]

    4. ifnext[x] NIL

    5. then prev [next[x]] prev [x]

    14

  • 7/27/2019 MCA-4 MC0080 I

    15/15

    15