Tuesday, November 18, 2008

Algorithms & Data Structures - Data Structures And Operations

Objectives
At the end of the class learner will be able to answer the :

  • Insert, delete, and process nodes in an AVL tree
  • Understand the operation of the AVL tree ADT
  • Write application programs using an AVL tree


  • AVL Tree


    Introduction
    In 1962, two Russian mathematicians, G.M Adelson-velskii and E.M Landis, created the balanced binary tree structure that is named after them-the AVL tree.

    Definition
    An AVL tree is a height-balanced binary search tree.

    AVL Balance Factor:
    The formula to calculate the height-balance is :
    │ HL – HR │ <=1 Where HL is the height of the left subtree and HR is the height of the right subtree

    The balance factor for any node in an AVL tree must be +1, or -1,we will use the descriptive identifiers
    • LH for left hight(+1) to indicate that the left subtree is higher than the right subtree.
    • EH for even high(0) to indicate that the subtrees are the same height, and
    • RH for right high(-1) to indicate that the left subtree is shorter than the right subtree.


    Example of an AVL tree



    Balancing Trees
    Whenever we insert or delete a node the tree is unbalance. To make it balance we have to perform the rotations either to the left or to the right.

    There are four cases required to rebalance the tree they are:
    1. Left of Left
    2. Right of Right
    3. Right of left
    4. Left of right




    Left of Left - Single Rotation right:

    Right of Right - Single Rotation Left:

    Right of Left - Double Rotation Right:


    Left of Right - Double Rotation Right:


    AVL tree Implementation

    AVL Tree Insert:

    AVL Tree Left Balance:

    Rotate AVL Tree Right and Left:

    AVL Tree delete:




    Wednesday, November 12, 2008

    Algorithms & Data Structures - AVL TREE

    • A balance binary search tree.
    • The best search time, that is O(log N) search times

    • An AVL tree is defined to be a well-balanced binary search tree in which each of its nodes has the AVL property.
    • The AVL property is that the heights of the left and right sub-trees of a node are either equal or if they differ only by 1




    Keeping the Tree Balance
    • the height of the right subtree and height of the left subtree for any node cannot differ by more than one.
    • This process is usually done through rotation.

    • Diagram 1

    • A single counter clockwise rotate will balance it.
    • s_ right_rotate() - because it involves the right subtree.

    • Diagram 2

    • A single counter clockwise rotate will balance it.
    • s_ left_rotate() - because it involves the left subtree.

    • Diagram 3

    • A double clockwise rotate will balance it.
      • 1st - single clockwise rotation with the parent of the inserted node (this produces the tree in diagram 1)
      • 2nd - single counter clockwise rotation on the root
    • d_ right_rotate() - because it involves the right subtree.

    • Diagram 4

    • A double clockwise rotate will balance it.
      • 1st - single counter clockwise rotation with the parent of the inserted node (this produces the tree in diagram 2)
      • 2nd - single clockwise rotation with the root.
    • d_ left_rotate() - because it involves the left subtree.



    Example:
    Given these data: build an AVL tree.
    12 , 10 , 3 , 23 , 11, 14 , 2 , 4 , 1
    Click the picture below to see the answer! ^_^

    Sunday, November 2, 2008

    Algorithms & Data Structures - Sorting 1 and Analysis

    • Sort problem is obvious to all
    • Several distinct algorithms to solve it
    • Algorithms use different data structures
    • Algorithm performance varies widely
    • See Chapters 1, 2, and 7
      • For now, skip ShellSort and QuickSort
    • Bubble Sort
    • Insertion Sort
    • Merge Sort
    • And analysis as we go…


    BubbleSort
    procedure BubbleSort( var A : InputArray; N : int);
    var
    j, P : integer;
    begin
    for P := N to 2 by -1 do begin
    for j := 1 to P - 1 do begin
    if A[ j ] > A[ j + 1 ] then
    Swap( A[ j ], A[ j + 1 ] );
    end; {for}
    end; {for}
    end;

    Template BubbleSort class
    template<class T> class CBubbleSort {
    public:
    void Sort(T *A, int N)
    {
    for(int P = N-1; P >= 1; P--)
    {
    for(int j = 0; j<=P-1; j++) { if(A[j + 1] < t =" A[j">Utilizing it…

    // Data …
    vector<double> data;

    // fill in the data…

    // Instantiate the sort object…
    CBubbleSort<double> sort;

    sort.Sort(&data[0], data.size());

    Running time for BubbleSort
    How many times does c,d get executed?
    for P := N to 2 by -1 do begin
    for j := 1 to P - 1 do begin
    if A[ j ] > A[ j + 1 ] then
    Swap( A[ j ], A[ j + 1 ] );
    end; {for}
    end; {for}

    Running time for BubbleSort
    How many times does c,d get executed?

    So, we could say:


    Big-Oh notation
    Definition: T(N)=O( f(N) ) if there are positive constants c and n0 such that T(N) <= cf(N) when N >= n0
    Another notation:

    Intuitively what does this mean?

    Asymptotic Upper Bound


    Example of Asymptotic Upper Bound




    Is BubbleSort O(N2)?
  • Can we find a c and n0?:





  • Some rules to simplify things…
    • Loops:
      • Running time is body time times # iterations
    • Nested loops:
      • Analysis from the inside out…
    • Statements:
      • Assume time of 1 each
        • Unless the statement is an algorithm
    • So, we could say:



    Usage


    Exercise in Big O-notation


    What’s the minimum time?


    Omega notation







    Asymptotic Lower Bound


    What these tell us
  • O(f(N)) - Upper bound on running timg
  • Ω(g(N)) - Lower bound on running time


  • What about the “constants”?
    • Algorithm 1: O(N), actual time is:
      • T(N)=1,000,000N
    • Algorithm 2: O(N2), actual time is:
      • T(N)=10N2
      • When is algorithm 2 faster?


    Why do we care?
    • After all, computers just keep getting faster, don’t they?
      • Is today’s slow algorithm acceptable tomorrow?
    • Algorithm 1: O(N2)
    • Algorithm 2: O(N)
      • If your computer is suddenly 10 times faster, can you handle 10 times as much data in the same time?


    Faster Computer or Algorithm?
    √ What happens when we buy a computer 10 times faster?


    Constant running time


    Theta Notation


    Big O, Omega, Theta


    In general:
    O is used to denote the upper bound of the complexity
    Ω is used to denote the lower bound of the complexity
    Ø is used to denote the tight bound of the complexity

    Example, the general sorting problem has the time complexity Ω(nlogn). Any sorting based on compare-interchange takes c.nlogn time for some constant c.

    Sorting problem has a time complexity O(nlogn)
    (since there is an algorithm which sorts in O(nlogn) time)

    Aside: Can we make this faster in some cases?
    procedure BubbleSort( var A : InputArray; N : int);
    var
    j, P : integer;
    begin
    for P := N to 2 by -1 do begin
    for j := 1 to P - 1 do begin
    if A[ j ] > A[ j + 1 ] then
    Swap( A[ j ], A[ j + 1 ] );
    end; {for}
    end; {for}
    end;

    Insertion Sort
    procedure InsertionSort( var A : InputArray; N : integer);
    var j, P : integer; Tmp : InputType;
    begin
    for P := 2 to N do begin
    j := P;
    Tmp := A[ P ];

    while j > 1 and Tmp < A[ j - 1 ] do begin
    A[ j ] := A[ j - 1 ];
    j := j - 1;
    end; {while}
    A[ j ] := Tmp;
    end; {for}
    end;

    Worst case analysis
    • N outer loops
    • Each item moved maximum distance
      • For item i, that would be i-1, right?



    Best case analysis
    • N outer loops
    • Each is not moved at all
      • For item i, 0 moves, which is constant time.



    Worst-Case and Average-Case Analysis
    • Worst-case analysis
      • Big O, the upper bound, of the running time of any input of size N.
    • Average-case analysis
      • Some algorithms are fast in practice.
      • The analysis is usually much harder.
      • Need to assume that all input are equally likely.
      • Sometimes, it is not obvious what is the average.


    InsertionSort Average Case
  • On average, how much would you have to move a item to sort the list?
  • For item i, the minimum is 0 and the maximum is i-1. So, the average is i/2



  • Insertion Sort Average Case More formal solution
    • For sorting, we assume randomly ordered data.
    • Inversions:
      • An inversion in an array of numbers is any ordered pair (i, j) having the property that
      • i < j and A[i] > A[j]
    • How many inversions in this list:
      • 12, 17, 5, 7, 21, 1, 8
      • How many are possible altogether?


    Inversions
    √ The maximum inversions for a list of size N is N(N-1)/2
    √ Theorem: The average number of inversions in an array of N distinct numbers is N(N-1)/4

    Average case for exchanging adjacent elements
    √ When we exchange adjacent elements we remove exactly one inversion
    • 12, 17, 5, 7, 21, 1, 8
    √ We have to remove on average N(N-1)/4 inversions=Ω(N2)

    Mergesort
    procedure MergeSort( var A : InputArray; N : int);
    Begin
    MSort(A, 1, N)
    end;

    Procedure MSort(var A : InputArray; F, L : int);
    var q: int;
    Begin
    if F < L then begin
    q := (F + L) / 2
    MSort(A, F, Q);
    MSort(A, Q+1, L);

    Merge(A, F, Q, L);
    end;
    End;



    How long does merge take?
    • One of the earliest sorting sorting algorithms, invented by John von Neumann in 1945.
    • Mergesort is Divide-and-conquer Algorithm
      • Given an instance of the problem to be solved, split this into several, smaller, sub-instances (of the same problem) independently solve each of the sub-instances and then combine the sub-instance solutions so as to yield a solution for the original instance.
      • The problem is divided into smaller problems and solved recursively.
      • Quicksort and heapsort are also such algorithms





    Running Time
  • T(1)=O(1)
  • T(N)=T(N/2)+T(N/2)+O(N)


  • Mergesort
    • O(n log n) worst-case running time
    • Same ops done regardless of input order
    • Mergesort is Divide-and-conquer Algorithm
    • Copying to and from temporary array
      • Extra memory requirement
      • Extra work







    Into the wild: AdSense for feeds

    We've been hinting at this for awhile, but it's finally time to spill the beans: Starting next week, we'll be rolling out AdSense for feeds to a small group of publishers, in anticipation of a full launch to all FeedBurner and AdSense publishers "coming soon". If you start seeing "Ads by Google" on an ad in a feed somewhere, that'd be us.

    So what will this mean for you? Well, publishers already in the FeedBurner Ad Network will continue to see premium CPM ads directly sold onto their content, but with the added bonus of contextually targeted ads that will fill up the remainder of their inventory. That means you get the best of both worlds: a dedicated Google sales force that knows how and why to sell onto your content, with the added revenue that full back-fill coverage provides. And with AdSense, you'll know that your back-filled ads are using the strongest contextual ad engine, ensuring the most relevant and profitable ads are delivered to your subscribers. And yes, ads are also sold via Google's AdWords program.

    For publishers who are not yet placing ads in their feeds, any publisher who meets the requirements to join the AdSense program will also be able to use AdSense for feeds. You will be able to manage your feed ad units directly from AdSense Setup tab, and track performance right on the AdSense Report tab. You can slice, dice, mix, or mash your tracking across feed units and content units, or keep them totally separate. You're in control. You can still control the frequency and rules around when ads appear in your feeds, without having to mess with templates on your content management system.

    You might be wondering what you'll need to do to use AdSense for feeds. You'll learn more about the details when we fully launch, but here are the basics: you will need to sign up for AdSense if you haven't already, and you will want to set up your AdSense channels for "placement targeting" in order to make sure that advertisers can target your syndicated content specifically. As a publisher, you will remain be in control of the campaigns that are targeted at your feed by harnessing the power of Ad Review Center.

    And, this is just the beginning of the chocolaty goodness that will come from ongoing integration effort with Google - there are many more "things" and "stuff" yet to come, as we mentioned a few weeks back.

    We'll give you the full details on AdSense for feeds, including supported formats, how to sign up, etc., etc. when we're ready for the full launch to all publishers. In the meantime, FeedBurner feeds will continue to be fed as usual, and we'll be reaching out to select publishers individually to try out AdSense for feeds.

    http://blogs.feedburner.com/feedburner/archives/2008/05/into_the_wild_adsense_for_feed_1.php