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

    Wednesday, October 15, 2008

    Multimedia Storage Devices

    Multimedia storage and retrieval
    Magnetic media technology

  • hard disk
  • Redundant Array of Inexpensive Disks (RAID)

  • Optical media technology
  • large capacity but slower access near-line mass-storage (jukeboxes)
  • CD-ROM
  • CD-WORM
  • CD-WR (erasable CD)


  • CD-ROM
    CD-ROM - Compact Disc Read Only memory.
    CD-ROM has several advantages and few disadvantages.

    Advantages:
    a) CD-ROM can hold about 650 megabytes of data, the equivalent of thousands of floppy disc.
    b) CD-ROM are not damaged by magnetic fields or the x-rays.
    c) The data on a CD-ROM can be accessed much faster than on a tape.

    Disadvantage :
    CD-ROM are 10 to 20 times slower than hard discs.

    CD-ROM Capacity
    The capacity of a CD-ROM depends on the drive.
    Almost all CD-ROM drives will handle up to 620 megabytes without any problems.
    Many newer drives can read discs with over 700 megabytes.
    The fundamental unit of data on a CD-ROM is the sector.
    Every CD-ROM is composed of a given amount of a sector.

    Example of Storage in Multimedia
    • CD-RW Rewritable Compact Discs
      • Rewritable-- Data can be Overwritten Directly and Repeatedly
      • 650MB Storage Capacity/74 Minutes Digital Audio Recording Time
      • Playable on All CD-ROM Drives With MultiRead Functionality
    • CD-R Recordable Compact Discs;
      • Offer high-density storage and superior audio recording.
      • 650MB/ 74 Min. Capacity


    RAID Technology
    RAID = Redundant Array of Inexpensive Disks.
  • RAID is a new technology that provides a potential alternative to mass storage combined with high throughput and reliability.
  • It is a set of disk drives viewed by the user as one or more logical drives.
  • Data is distributed across the set of drives in a pre-defined manner.

  • There are 8 discrete levels of RAID functionality
    1) Level 0 : Disk Striping
    2) Level 1 : Disk Mirroring
    3) Level 2 : Bit Interleaving and Header Error Correction (HEC) Parity
    4) Level 3 : Bit Interleaving and XOR Parity
    5) Level 4 : Block Interleaving with XOR Parity
    6) Level 5 : Block Interleaving with Parity Distribution
    7) Level 6 : Fault tolerant system
    8) Level 7 : Heterogeneous system

    1) RAID Level 0 - Disk Striping

    Disk Striping for RAID Level 0

    RAID Level 0 is based on distribution of data across multiple drives connected to a single disk controller

    Characteristics/Advantages
    1) RAID 0 implements a striped disk array, the data is broken down into blocks and each block is written to a
    separate disk drive.
    2) I/O performance is greatly improved by spreading the I/O load across many channels and drives
    3) Best performance is achieved when data is striped across
    multiple controllers with only one drive per controller

    2) RAID Level 1 - Disk Mirroring

    Disk Layout in RAID Level 1

    RAID Level 1 focuses on fault tolerance in addition to striping. Disk Layout in RAID Level 1

    3) RAID Level 2 - Bit Interleaving and HEC Parity

    RAID 2 disk subsystem contain multiple drives connected to a disk controller, with either single or multiple channels.

    4) RAID Level 3 - Bit Interleaving with XOR Parity

    RAID 3 introduces parity to the model by interleaving the data at a bit level across several drives similar to data striping.

    5) RAID Level 4 - Block Interleaving with XOR Parity

    Very similar to RAID 3 except that striping is done at block level across several drives.

    6) RAID Level 5 - Block Interleaving with Parity Distribution

    Parity is distributed across various drives.
    Removing a potential bottleneck.

    7) RAID level 6 - Fault-Tolerant System

    Improvement over RAID 5 model through the addition error recovery information.

    8) RAID Level 7 - Heterogeneous System

    Allows each individual drive to access data as fast as possible by incorporating a few crucial features.