Analysis of Algorithms

Document Sample
Analysis of Algorithms Powered By Docstoc
					Stacks (Background)

            Trees     1
Applications of Stacks
    Direct applications
         Page-visited history in a Web browser
         Undo sequence in a text editor
         Chain of method calls in the Java Virtual
    Indirect applications
         Auxiliary data structure for algorithms
         Component of other data structures

 Dr.Alagoz                 Trees                      2
Abstract Data Types (ADTs)
  An abstract data          Example: ADT modeling a
  type (ADT) is an          simple stock trading system
  abstraction of a                 The data stored are buy/sell
  data structure                    orders
  An ADT specifies:                The operations supported are
       Data stored                   order buy(stock, shares, price)
       Operations on the             order sell(stock, shares, price)
        data                          void cancel(order)
       Error conditions           Error conditions:
        associated with               Buy/sell a nonexistent stock
                                      Cancel a nonexistent order

 Dr.Alagoz                  Trees                                   3
The Stack ADT
   The Stack ADT stores                Auxiliary stack
   arbitrary objects                   operations:
   Insertions and deletions               object top(): returns the
   follow the last-in first-out            last inserted element
   scheme                                  without removing it
   Think of a spring-loaded               integer size(): returns the
   plate dispenser                         number of elements
   Main stack operations:
                                          boolean isEmpty():
        push(object): inserts an
         element                           indicates whether no
                                           elements are stored
        object pop(): removes and
         returns the last inserted

Dr.Alagoz                      Trees                              4
Stack Interface in Java
                       public interface Stack {
 Java interface
 corresponding to          public int size();
 our Stack ADT             public boolean isEmpty();
 Requires the
 definition of class       public Object top()
                               throws EmptyStackException;
 Different from the        public void push(Object o);
 built-in Java class       public Object pop()
 java.util.Stack                throws EmptyStackException;

 Dr.Alagoz             Trees                             5
    Attempting the              In the Stack ADT,
    execution of an             operations pop and
    operation of ADT may        top cannot be
    sometimes cause an          performed if the
    error condition, called     stack is empty
    an exception                Attempting the
    Exceptions are said to      execution of pop or
    be “thrown” by an           top on an empty
    operation that cannot       stack throws an
    be executed                 EmptyStackException
 Dr.Alagoz              Trees                   6
Queues (Background)

           Trees      7
The Queue ADT
  The Queue ADT stores arbitrary           Auxiliary queue
  objects                                  operations:
  Insertions and deletions follow             object front(): returns the
  the first-in first-out scheme                element at the front without
  Insertions are at the rear of the            removing it
  queue and removals are at the               integer size(): returns the
  front of the queue                           number of elements stored
                                              boolean isEmpty(): indicates
  Main queue operations:
                                               whether no elements are
       enqueue(object): inserts an            stored
        element at the end of the
        queue                              Exceptions
       object dequeue(): removes and         Attempting the execution of
        returns the element at the front       dequeue or front on an
        of the queue                           empty queue throws an
Dr.Alagoz                          Trees                              8
Applications of Queues
    Direct applications
         Waiting lists, bureaucracy
         Access to shared resources (e.g., printer)
         Multiprogramming
    Indirect applications
         Auxiliary data structure for algorithms
         Component of other data structures

 Dr.Alagoz                 Trees                       9
Array-based Queue
   Use an array of size N in a circular fashion
   Two variables keep track of the front and rear
     f index of the front element
     r index immediately past the rear element
   Array location r is kept empty

                    normal configuration
            0 1 2     f                          r

             wrapped-around configuration
            0 1 2     r                f
Dr.Alagoz                     Trees                  10
Queue Operations
   We use the           Algorithm size()
   modulo operator       return (N - f + r) mod N
   (remainder of
                        Algorithm isEmpty()
   division)             return (f = r)

            0 1 2   f                 r
            0 1 2   r           f

Dr.Alagoz               Trees                       11
Queue Operations (cont.)
  Operation enqueue          Algorithm enqueue(o)
  throws an exception if      if size() = N - 1 then
  the array is full              throw FullQueueException
  This exception is            else
  implementation-                Q[r]  o
  dependent                      r  (r + 1) mod N

             0 1 2   f                   r
             0 1 2   r             f

 Dr.Alagoz                 Trees                      12
Queue Operations (cont.)
   Operation dequeue       Algorithm dequeue()
   throws an exception      if isEmpty() then
   if the queue is empty       throw EmptyQueueException
   This exception is         else
   specified in the            o  Q[f]
   queue ADT                   f  (f + 1) mod N
                               return o

             0 1 2   f                  r
             0 1 2   r             f
 Dr.Alagoz                 Trees                    13
Queue Interface in Java
                       public interface Queue {
 Java interface
 corresponding to          public int size();
 our Queue ADT             public boolean isEmpty();
 Requires the              public Object front()
 definition of class           throws EmptyQueueException;
 EmptyQueueException       public void enqueue(Object o);
 No corresponding          public Object dequeue()
 built-in Java class            throws EmptyQueueException;

 Dr.Alagoz             Trees                                14
Application: Round Robin
           We can implement a round robin scheduler using a
           queue, Q, by repeatedly performing the following
      1.      e = Q.dequeue()
      2.      Service element e
      3.      Q.enqueue(e)

                                     The Queue

                 1. Deque the     2 . Service the    3. Enqueue the
                 next element     next element      serviced element


 Dr.Alagoz                        Trees                                15
Trees (Background)
                 Make Money Fast!

        Stock          KG            Bank
        Fraud      programming      Robbery

                Trees                         16
What is a Tree
   In computer science, a
   tree is an abstract model                         Computers”R”Us
   of a hierarchical
   A tree consists of nodes           Sales                   Manufacturing        R&D
   with a parent-child
                             US          International     Laptops    Desktops
        Organization charts
        File systems        Europe           Asia        Canada
        Programming

 Dr.Alagoz                        Trees                                       17
Tree Terminology
   Root: node without parent (A)                Subtree: tree consisting of
   Internal node: node with at least            a node and its
   one child (A, B, C, F)                       descendants
   External node (a.k.a. leaf ): node                       A
   without children (E, I, J, K, G, H, D)
   Ancestors of a node: parent,
   grandparent, grand-grandparent,              B                   C       D
   Depth of a node: number of
   ancestors                            E           F           G       H
   Height of a tree: maximum depth
   of any node (3)
   Descendant of a node: child,             I       J   K
   grandchild, grand-grandchild, etc.

 Dr.Alagoz                      Trees                           subtree
Tree Abstract Data Types
    We use positions to abstract             Query methods:
    nodes                                       boolean isInternal(p)
    Generic methods:                            boolean isExternal(p)
         integer size()                        boolean isRoot(p)
         boolean isEmpty()                  Update method:
         Iterator elements()                   object replace (p, o)
         Iterator positions()               Additional update methods
    Accessor methods:                        may be defined by data
         position root()                    structures implementing the
         position parent(p)                 Tree ADT
         positionIterator children(p)

 Dr.Alagoz                           Trees                               19
  Preorder (Depth-first) Traversal
  A traversal visits the nodes of a tree       Algorithm preOrder(v)
  in a systematic manner. In a preorder
  traversal, a node is visited before its         visit(v)
  descendants                                     for each child w of v
  1.Visit the root.                                   preorder (w)
  2.Traverse the left subtree.
  3.Traverse the right subtree.
Application:Print structured documentMoney Fast!
                2                               5                            9
           1. Motivations                           2. Methods           References

                                    6                  7             8
       3                    4
                                   2.1 Stock         2.2 KG       2.3 Bank
    1.1 Greed        1.2 Avidity
                                     Fraud          programming   Robbery

    Dr.Alagoz                           Trees                                 20
 Postorder Traversal
  A node is visited after its descendants .   Algorithm postOrder(v)
  1.Traverse the left subtree.                   for each child w of v
  2.Traverse the right subtree.
                                                     postOrder (w)
  3.Visit the root.
Application: compute space used by files in a
  directory and its subdirectories
                              Data Structure Course

                3                           7
           Homeworks                             Project             Lectures

       1                2       4                 5              6
     Hw1               Hw k

    Dr.Alagoz                       Trees                                 21
  Inorder Traversal
  In an inorder traversal a node is
  visited after its left subtree and             Algorithm inOrder(v)
  before its right subtree                          if hasLeft (v)
  1.Traverse the left subtree.                          inOrder (left (v))
  2.Visit the root.
  3.Traverse the right subtree
Application: draw a binary tree                     if hasRight (v)
          x(v) = inorder rank of v                      inOrder (right (v))
         y(v) = depth of v
                        2                        8

                    1           4        7           9

                            3        5

        Dr.Alagoz                        Trees                           22
Binary Trees
  A binary tree is a tree with the                   Applications:
  following properties:                                  arithmetic expressions
       Each internal node has at most two               decision processes
        children (exactly two for proper
        binary trees)                                    searching
       The children of a node are an                                 A
        ordered pair
  We call the children of an internal
  node left child and right child
  Alternative recursive definition: a                 B                       C
  binary tree is either
       a tree consisting of a single node, or
       a tree whose root has an ordered         D            E           F            G
        pair of children, each of which is a
        binary tree

                                                          H       I

 Dr.Alagoz                           Trees                                        23
Arithmetic Expression Tree
    Binary tree associated with an arithmetic expression
         internal nodes: operators
         external nodes: operands
    Example: arithmetic expression tree for the
    expression (2  (a - 1) + (3  b))

                                                   

                    2           -               3       b

                            a        1

 Dr.Alagoz                          Trees                   24
Decision Tree
    Binary tree associated with a decision process
         internal nodes: questions with yes/no answer
         external nodes: decisions
    Example: dining decision

                           Want a fast meal?
                     Yes                            No

          How about coffee?               On expense account?
             Yes            No             Yes               No

       Emmi            Aya               Kuheylan        Café De Paragon
 Dr.Alagoz                       Trees                             25
Print Arithmetic Expressions
    Specialization of an inorder                Algorithm printExpression(v)
        print operand or operator
                                                   if hasLeft (v)
         when visiting node                            print(“(‟‟)
        print “(“ before traversing left
                                                       inOrder (left(v))
        print “)“ after traversing right          print(v.element ())
                                                   if hasRight (v)
                       +                               inOrder (right(v))
                                                     print (“)‟‟)

2            -             3        b
                                                    ((2  (a - 1)) + (3  b))
         a        1
 Dr.Alagoz                                  Trees                           26
Properties of Proper Binary Trees
    Notation                         Properties:
      n number of nodes                e = i + 1
      e number of
                                       n = 2e - 1
        external nodes
                                       h  i
      i number of internal
        nodes                          h  (n - 1)/2
      h height                         e  2

                                       h  log2 e

                                       h  log2 (n + 1) - 1

 Dr.Alagoz                   Trees                      27
BinaryTree ADT
   The BinaryTree ADT              Update methods
   extends the Tree                may be defined by
   ADT, i.e., it inherits          data structures
   all the methods of              implementing the
   the Tree ADT                    BinaryTree ADT
   Additional methods:
        position left(p)
        position right(p)
        boolean hasLeft(p)
        boolean hasRight(p)
Dr.Alagoz                  Trees                       28
Evaluate Arithmetic Expressions
   Specialization of a postorder         Algorithm evalExpr(v)
   traversal                                 if isExternal (v)
        recursive method returning               return v.element ()
         the value of a subtree              else
        when visiting an internal                x  evalExpr(leftChild (v))
         node, combine the values
                                                  y  evalExpr(rightChild (v))
         of the subtrees
                                                    operator stored at v
                         +                        return x  y

                                
                                                The result is: ???
    2            -           3       2

             5       1
 Dr.Alagoz                            Trees                               29
Array-Based Representation of
Binary Trees
 nodes are stored in an array
                                                                2                                 3
                                                                     B                        D

     let rank(node) be defined as follows:
           rank(root) = 1                              4                    5       6                     7
                                                            E            F                C            J
           if node is the left child of parent(node),
                rank(node) = 2*rank(parent(node))
           if node is the right child of parent(node),
                                                                10                   11
                rank(node) = 2*rank(parent(node))+1
                                                                     G           H
Dr.Alagoz                             Trees                                                       30
Euler Tour Traversal (*)
   Generic traversal of a binary tree
   Includes a special cases the preorder, postorder and inorder traversals
   Walk around the tree and visit each node three times:
     on the left (preorder)

     from below (inorder)

     on the right (postorder)


                        L      R                           
                    2                     -             3       2

                                    5           1

 Dr.Alagoz                              Trees                           31
Template Method Pattern (*)
 Generic algorithm that    public abstract class EulerTour {
 can be specialized by       protected BinaryTree tree;
 redefining certain steps    protected void visitExternal(Position p, Result r) { }
 Implemented by means of     protected void visitLeft(Position p, Result r) { }
 an abstract Java class      protected void visitBelow(Position p, Result r) { }
                             protected void visitRight(Position p, Result r) { }
 Visit methods that can be   protected Object eulerTour(Position p) {
 redefined by subclasses         Result r = new Result();
 Template method eulerTour       if tree.isExternal(p) { visitExternal(p, r); }
      Recursively called on the              else {
       left and right children                   visitLeft(p, r);
      A Result object with fields               r.leftResult = eulerTour(tree.left(p));
       leftResult, rightResult and               visitBelow(p, r);
       finalResult keeps track of                r.rightResult = eulerTour(tree.right(p));
       the output of the                         visitRight(p, r);
       recursive calls to eulerTour
                                                 return r.finalResult;

 Dr.Alagoz                            Trees                                              32
Specializations of EulerTour(*)
  We show how to                       public class EvaluateExpression
  specialize class                                      extends EulerTour {
  EulerTour to evaluate                    protected void visitExternal(Position p, Result r) {
  an arithmetic                               r.finalResult = (Integer) p.element();
  expression                               }
  Assumptions                              protected void visitRight(Position p, Result r) {
       External nodes store                  Operator op = (Operator) p.element();
        Integer objects                       r.finalResult = op.operation(
                                                                (Integer) r.leftResult,
       Internal nodes store
                                                                (Integer) r.rightResult
        Operator objects
        supporting method
        operation (Integer, Integer)

 Dr.Alagoz                                  Trees                                              33
Linked Structure for Trees(*)
   A node is represented by
   an object storing                     
        Element
        Parent node
        Sequence of children       B
   Node objects implement                                                   
   the Position ADT
                                                A           D       F

    A            D              F

             C         E                                                
                                                        C           E
 Dr.Alagoz                              Trees                           34
Linked Structure for Binary Trees (*)
  A node is represented
  by an object storing                         
       Element
       Parent node
       Left child node                        B
       Right child node
  Node objects implement                  
  the Position ADT

                 B                  A                  D

        A                  D                                         

                   C           E               C               E

 Dr.Alagoz                         Trees                           35
Solved Problems & HWLA
 1. A) Describe the output of the following series of stack operations on a single,
 initially empty stack: push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1),
 pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop().

Dr.Alagoz                                   Trees                                              36
Solved Problems & HWLA
 1.B) Describe the output of the following series of queue operations on a single,
   initially empty queue: enqueue(5), enqueue(3), dequeue(), enqueue(2), enqueue(8), dequeue(),
 dequeue(), enqueue(9), enqueue(1), dequeue(), enqueue(7), enqueue(6), dequeue(), dequeue(),
 enqueue(4), dequeue(), dequeue(). Solution The head of the queue is on the left.

Dr.Alagoz                                 Trees                                          37
Solved Problems & HWLA
     1.C) Describe in pseudo-code a linear-time algorithm for reversing a queue Q. To
access the queue, you are only allowed to use the methods of queue ADT. Hint : Consider
using an auxiliary data structure.
Solution We empty queue Q into an initially empty stack S, and then empty S back into Q.
Algorithm ReverseQueue(Q)
Input: queue Q
Output: queue Q in reverse order
S is an empty stack
while (! Q.isEmpty()) do
while (! S.isEmpty()) do

    Write a java program for a), b) and c) the above stack operation.
Assume initially empty stack. Maximum stack size is 5 elements. The program should give „the stack
is full‟ , and “the stack is empty” messages when more than 5 consecutive push() and pop () is
made, respectively. Make other assumptions if needed..

    Dr.Alagoz                                  Trees                                          38
Solved Problems & HWLA
        4.1)    (a) A .   (b) G, H, I , L , M, and K . 4.3)   4

        4.5) Proof is by induction. The theorem is trivially true for h = 0.        Assume
        true for h = 1, 2, ..., k . A tree of height k +1 can have two subtrees of height at
        most k . These can have at most 2^ (k +1) - 1 nodes each by the induction
        hypothesis. These 2^ (k +2) -2 nodes plus the root prove the theorem for height k
        +1 and hence for all heights.

        4.7) This can be shown by induction. In a tree with no nodes, the sum is zero, and in
        a one-node tree, the root is a leaf at depth zero, so the claim is true.
        Assume that the theorem is true for all trees with at most k nodes.
        Consider any tree with k +1 nodes. Such a tree consists of an i node left subtree
        and a k - i node right subtree. By the inductive hypothesis, the sum for the left
        subtree leaves is at most one wrto the left tree root. Because all leaves are one
        deeper with respect to the original tree than wrto the subtree, the sum is at most
        1/ 2 wrto the root. Similar logic implies that the sum for leaves in the right subtree
        is at most 1/ 2 , proving the theorem.
        The equality is true if and only if there are no nodes with one child. If there is a node
        with one child, the equality cannot be true because adding the second child would
        increase the sum to higher than 1. If no nodes have one child, then we can find and
        remove two sibling leaves, creating a new tree. It is easy to see that this new tree
        has the same sum as the old. Applying this step repeatedly, we arrive at a single
        node, whose sum is 1. Thus the original tree had sum 1.
   Solve Exercises in Chapter 4 : 2, 4, 6, 8, 9, 19, 44, 48,51
    Dr.Alagoz                                    Trees                                        39
Take Home Quiz

    Write a short paper on one of the followings:
    Binary search tree, AVL-tree, Splay tree and B-tree.
    (Word document, 12 times new roman, at most 2 pages)

    Name your document as Lastname.Name-CourseCode.doc
    Email your document to me anytime before October 28, 2009 (Sharp).
    Subject: CourseCode-HW1

Dr.Alagoz                             Trees                              40