Friday, November 25, 2011

Introduction to Algorithms - The Reading Notes: Part 7, The Quick Sort

Now comes our famous QuickSort. It's famous because it's quick, so how quick it is? The answer from the theoretical analysis is Θ(n ln n) in average. OK, you may argue that the MergeSort and HeapSort we talked before also run in O(n ln n) , why this one is quicker? The answer is hidden in the big O notation, we usually don't write the constant factor in the function. In fact, QuickSort has a smaller hidden factor than others which distinguishes in practice QuickSort from the others. However, QuickSort can run, in the worst case, in O(n2) time. Now let's see how to write some code to do QuickSort.

As a starting point, we write our unit tests. As usual, we only need to add test cases in SortingTest.h and implement them in SortingTest.cpp.

    CPPUNIT_TEST( testQuickSort_0 );
    CPPUNIT_TEST( testQuickSort_1 );
    CPPUNIT_TEST( testQuickSort_Dec_0 );
    CPPUNIT_TEST( testQuickSort_Dec_1 );
...
    void testQuickSort_0();
    void testQuickSort_1();
    void testQuickSort_Dec_0();
    void testQuickSort_Dec_1();

The implementation of the tests which is quite straightforward, actually, same
as tests for other sorting algorithms except the method used to sort, here we 
use QuickSort::Sort(v): 



    // Setup Array.
...
    QuickSort::Sort(v);
...
    // Test Assertions.

Now if we try to compile, we will get some compiling errors, of course. We do
not have our QuickSort class created. So now it's the time to create it. 



    class QuickSort 
    {
        public:
            static void Sort(std::vector &v, bool bNonDecreasing = true);
        protected:
            static int  Partition(std::vector &v, int p, int r, bool bNonDecreasing);
            static void DoSort(std::vector &v, int p, int r, bool bNonDecreasing);
    };

The QuickSort provides exactly the same interface as other sorting algorithms which is the Sort method. As usual, we allow to sort in both non-decreasing and decreasing orders.

The DoSort method will recursively call itself to sort two sub-arrays partitioned by the method Partition.

Write the QuickSort class, compile it with scons, then run the unit tests. Failed!! Double check the code, then fix all the bugs until all unit tests pass. Below is the resulting code.

void QuickSort::Sort(std::vector &v, bool bNonDecreasing) 
{
    if (v.size() <= 1 )
    {
        return;
    }

    DoSort(v, 0, v.size()-1, bNonDecreasing);
}

int QuickSort::Partition(std::vector &v, int p, int r, bool bNonDecreasing)
{
    int pivot = v[r];
    int i = p-1; // i marks the end of the left partition.

    // j markes the end of the right partition
    for (int j = p; j <= r-1;  j++)
    {
        if ((v[j] < pivot &&  bNonDecreasing) ||
            (v[j] > pivot && !bNonDecreasing))
        {
            i++;
            int t = v[i];
            v[i]  = v[j];
            v[j]  = t;
        }
    }
    v[r] = v[i+1];
    v[i+1] = pivot;

    return i+1;
}

void QuickSort::DoSort(std::vector &v, int p, int r, bool bNonDecreasing)
{
    std::cout << "Sorting sub array: " << p << ":" << r << std::endl;
    if (p < r)
    {
        int q = Partition(v, p, r, bNonDecreasing);
        DoSort(v, p, q-1, bNonDecreasing);
        DoSort(v, q+1, r, bNonDecreasing);
    }
}


About this document ...

ITA Reading Notes This document was generated using the LaTeX2HTML translator Version 2008 (1.71)
Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -html_version 4.0,math -no_navigation -scalable_fonts -split 0 -no_auto_link ITAReadingNotes-Main
The translation was initiated by Bing Han on 2011-11-25

Sunday, November 20, 2011

Introduction to Algorithms - The Reading Notes: Part 6, The Heap Sort

Heap Sort

The basic idea of Heap Sort is to sort with help of a special data structure called heap. A heap is a binary tree where each note in the tree contains a value. If the value of each node is no less than it's children, we call it a Max-Heap. If the value of each node is no more than it's children, we call it a Min-Heap.

Don't be confused by name heap, which is also used commonly as a notion of a region of memory space where program can dynamically allocate.

Heap is very interesting, on one hand, it's a binary tree, so it benefits binary tree properties such as it's height grows logarithmically with the number of nodes it has; on the other hand, it has one important order relationship between parent and child nodes.

As usual, we won't discuss in detail the theoretical aspect of the algorithm, instead, we focus on the implementation of the algorithm.

Before implementing the algorithm, let's first setup the unit test. As we already has our test infrastructure for previous sorting algorithms we discussed, we simply need to add some test cases inside.

For Heap Sort, I'm going to create a class with name HeapSort which provides a Sort method as other sort algorithms. So in the CppUnitTest/SortingTest.h, I add some test cases and their declarations:

CPPUNIT_TEST( testHeapSort_0 );
    CPPUNIT_TEST( testHeapSort_1 );
...
    void testHeapSort_0();
    void testHeapSort_1();

Then I define those tests (methods) in the CppUnitTest/SortingTest.cpp:

void SortingTest::testHeapSort_0()
{
    int iArray[] = {10, 5, 7, -2, 19, 50, 300, 320, 160, 1000};
    std::vector v(iArray, iArray + sizeof(iArray) / sizeof(int));
    HeapSort::Sort(v);
    CPPUNIT_ASSERT_EQUAL(v[0], -2);
    CPPUNIT_ASSERT_EQUAL(v[1], 5);
    CPPUNIT_ASSERT_EQUAL(v[2], 7);
    CPPUNIT_ASSERT_EQUAL(v[3], 10);
    CPPUNIT_ASSERT_EQUAL(v[4], 19);
    CPPUNIT_ASSERT_EQUAL(v[5], 50);
    CPPUNIT_ASSERT_EQUAL(v[6], 160);
    CPPUNIT_ASSERT_EQUAL(v[7], 300);
    CPPUNIT_ASSERT_EQUAL(v[8], 320);
    CPPUNIT_ASSERT_EQUAL(v[9], 1000);
}


void SortingTest::testHeapSort_1()
{
...
}

Now I can start coding the HeapSort class. First, I declare the main interface method, which is Sort for the HeapSort class, then I declare some helper methods Sort will use.

Same as described in the ITA, we will construct a binary tree directly on the input array, by using the index of an item in the array. So item 0 is always the root of the heap tree. Then, item 1 and 2 are two children of item 0, item 3 and 4 are two children of item 1, item 5 and 6 are children of item 2, and so on. In short, given a node i , we know that it's left child is 2*i + 1 and it's right child is (i + 1)*2 , if they exist, where i starts from 0. As for it's parent, it depends. If i = 0 , then it's the root node, so it does not really have a parent, otherwise, the parent node's index can be computed as (i - 1)/2 .

We declare Parent, Left and Right methods to do the computation described above.
Also, the Heap Sort is based on first building a MaxHeap ( in case we need to sort non-decreasingly, otherwise, we build a MinHeap), then each time takes the root (which is the largest item in the heap), then reconstruct the heap. So we will also declare two auxiliary methods BuildMaxHeap and MaxHeapify to do the job. Methods for building and reconstructing MinHeap are also provided. The whole HeapSort class declaration looks like follows:


class HeapSort 
{
    public:
    static void Sort(std::vector &v, bool bNonDecreasing = true);

    protected:
    static int  Parent(const int i) { return ((i > 0) ? (i - 1) >> 1: 0); }
    static int  Left(const int i)   { return (i << 1) + 1; }
    static int  Right(const int i)  { return (i + 1) << 1; }

    static void BuildMaxHeap(std::vector &v);
    static void BuildMinHeap(std::vector &v);
    static void MaxHeapify(std::vector &v, const int i, const int iHeapSize);
    static void MinHeapify(std::vector &v, const int i, const int iHeapSize);
};
Now comes the time to implement those methods. We will implement them in  a bottom-up way. So, first look at the MaxHeapify method, it takes  three parameters, a vector representing the heap (or a binary tree to be  built to a heap) and a integer i  which is an index in the vector, representing a sub-tree rooted at i . Also the heap size is provided as the third parameter. MaxHeapify assumes that two sub-trees rooted at both of i 's children are already MaxHeaps, only i  is possibly violating the MaxHeap property. So the goal is just to make a MaxHeap rooted at i . To this end, it simply recursively floats down the content of node i  until a proper place is found. "Float down" is just an exchange of i  with the maximum of i , Left(i ) and Right(i ). Of cause, in case that the maximum is i  itself, nothing needs to be done. The code reads as follows:


void HeapSort::MaxHeapify(std::vector &v, const int i, const int iHeapSize)
{
    int l = Left(i);
    int r = Right(i);

    int largest = i;

    if ( l < iHeapSize && v[l] > v[i])
    {
        largest = l;
    }

    if ( r < iHeapSize && v[r] > v[largest])
    {
        largest = r;
    }

    if (largest != i)
    {
        int temp = v[largest];
        v[largest] = v[i];
        v[i] = temp;

        MaxHeapify(v, largest, iHeapSize);
    }
}
Next, we will implement BuildMaxHeap. With the help of MaxHeapify, this  is fairly straightforward.  We simply need to first MaxHeapify the sub-trees at the lowest level  (actually the next to lowest, as the lowest are leaf nodes that does not  have any child so trivially they are MaxHeap). As a result, we can  start MaxHeapify the sub-trees from the middle of the vector.  The trick is, given a vector of size n , if we count from the end of the vector, the first item that may have a child always appears no early than the n/2 th item (think why?).  As a result, we have the following simply implementation of BuildMaxHeap:   

void HeapSort::BuildMaxHeap(std::vector &v)
{
    for (int i = (v.size() >> 1); i >= 0; i--)
    {
        MaxHeapify(v, i, v.size());
    }
}
We can implement similar methods for MinHeap too.

Now that we have everything in hand, let's implement the final method Sort. We start by building a MaxHeap (or MinHeap), then each time, we exchange the root of the heap (item 0) with the last item in the heap. This exchange does two things: put the current largest at the end of the vector and put a smaller one at the heap root, breaking heap property. Then we use MaxHeapify to reconstruct the heap.
Once the heap is reconstructed, we have again the largest item at root, then we simply repeat the exchange-reconstruct. The code looks like the following, sorting by non-decreasing or non-increasing order are supported with parameter bNonDecreasing, as other sort algorithms:


void HeapSort::Sort(std::vector &v, bool bNonDecreasing) 
{
    if (v.size() <= 1 )
    {
        return;
    }
    
    if (bNonDecreasing)
    {
        BuildMaxHeap(v);
    }
    else
    {
        BuildMinHeap(v);
    }

    int iHeapSize = v.size();
    
    for (int i = v.size() - 1; i >=1; i--)
    {
        int temp = v[0];
        v[0] = v[i];
        v[i] = temp;

        iHeapSize--;
        if (bNonDecreasing)
        {
            MaxHeapify(v, 0, iHeapSize);
        }
        else
        {
            MinHeapify(v, 0, iHeapSize);
        }
    }
}


About this document ...

ITA Reading Notes This document was generated using the LaTeX2HTML translator Version 2008 (1.71)
Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -html_version 4.0,math -no_navigation -scalable_fonts -split 0 -no_auto_link ITAReadingNotes-Main
The translation was initiated by Bing Han on 2011-11-20

Introduction to Algorithms - The Reading Notes: Part 5, Maximun Sum Sub-Array (DAC)

To be completed.

Saturday, November 12, 2011

Fedora 16 Release

Fedora 16 released on November 8, 2011!

I tried to download the 3.5GB DVD image  with firefox under windows. I started the download quite late last night, as it takes about 2 hours to finish, I paused it and shut down my PC then went to bed. I resumed it this morning and it finished later, but surprisingly, the resulted .iso file has 6GB!!

I don't know how the firefox downloader worked on it but I do know, I don't have a DVD on which I can write a 6GB image. However, instead delete the bad file directly, I'm curious to see how bad it is. Interestingly, I can mount it with a WinISO virtual driver and then I can open it. Selecting all items in the image, the total size seems correct, i.e. about 3.5GB.

Of cause, the SHA256 checksum failed. (See here on how to verify the image)

Finally, I switched to Fedora and let wget get the things done and it did it correctly.

So, now I have the DVD image to write and install ! :D

Saturday, June 11, 2011

Fedora 15!

Released at 24 May, the Lovelock  is another great step to a better user experience.  I installed it today and was shocked by the revolutionary changes to the GNOME desktop, now it's the GNOME shell that I'm interacting with.

It may seem unfamiliar and even irritating when I found no minimize button and everything in my Desktop folder does not show up on the desktop and I have to find applications in another place. But, but, but after I read the release note which is well prepared for the situation that a user like me can have, it answered me all my questions, and in one hour, I started to love it! Fedora name this 15th release Lovelock, does that mean something?


The Super key is really handy, it calls up immediately all windows opened for you to select one. Also it brings the launch bar to front so launching an application can be done instantly with the advantage that it does not take any space when you are working in the application.

Also the Firefox 4, just great! I love it, especially the Sync.

As a programmer, I have the gcc 4.6 in this new release, I have vim 7.3, I have sons 2.0.1 ...

All in all, I love the Lovelock, better than ever.

Introduction to Algorithms - The Reading Notes: Part 4, Maximun Sum Sub-Array (BF)

Maximum Sub Sub-Array is an interesting problem. It goes like this, I'm given a series of values and I'm asked to find a continuous sub-series that has the maximum sum. 

For example, if I'm given a series of value -3, 2, 5, 6, -1, 2, -5, 5, then I find that the sub-series 2,5,6 gives 13 which quite large, but 2, 5, 6, -1, 2 sums up even more, it gives 14. If I look further, I find that the sub-series 2, 5, 6, -1, 2, -5, 5 gives the same maximum of 14. So we may have more than one maximum and our goal is to find any one of them.

ITA mentions the brutal force algorithm that has Ω(n^2) running time, it says the algorithm is to calculate sums of all sub-series possible (there is O(n^2) number of sub-series) and if we can evaluate each sub-series in constant time then we can have Ω(n^2) algorithm, but it does not say how. ITA introduces also a very fast (O(nlgn) algorithm based on DaC (Divide and Conquer). 

I understand that this ITA chapter is for DaC so no need to talk more details on this brutal force algorithm and it's put in the exercise. So, let's finish the Ω(n^2) brutal force algorithm first then we will see how to implement the DaC one.

So you already know the number of sub-series is O(n^2), suppose we have a n-length series, but why?  The answer is this number equals to the number of ways you have when you try to select any two numbers in the series (those two numbers defines the start and end of the sub-series), that is n(n-1)/2.

Now comes to the question how we calculate the sum of each sub-series. If we just simply calculate sums for all sub-series, then we have to sum n sub-series of length 1, n-1 sub-series of length 2, n-2 sub-series of length 3, ..., 1 sub-series of length n. In total, we need to do add n+2(n-1)+3(n-2)...+ n times, which is equivalent to sum(for i = 1 to n) (i*(n-i+1)),  which renders our algorithm O(n^3) in terms of time consumption.

Now we need to do more to get a faster algorithm. Just one more glance at our previous O(n^3) algorithm we find that the sub-series have many common parts and we add those parts repeatedly many times once for each sub-series. That's the key to reduce running time.

I will now use a n*n matrix to save sums of each sub-series, say, [0-1], [0-2]... [1,2], [1,3]... Also each single element represents also a sub-series of length 1, they are also stored in the matrix [0,0], [1,1] ... So only the upper triangle of the matrix is used. The advantage to save the sums in the matrix is that it's very easy to find a sub-series that can be used to build the next sum, because we know that:
sum(sub[i, j]) = sum(sub[i, j-1]) + v[j] = sum(sub[i, j-1]) + sub[j, j]

OK, that's enough for the theory, let's see the code. But wait a moment, before implementing the algorithm, let's put in place the unit test!

I'll use another class for the MaxSubArray unit test, separated from that of the sort algorithms. With CppUnit, I just simply create a .h and .cpp for the new tests, no other configuration is needed.

MiscTest.h

   1:  #ifndef MISCTEST_H  
   2:  #define MISCTEST_H  
   3:  #include <cppunit/extensions/HelperMacros.h> 
   4:    
   5:    
   6:  class MiscTest : public CppUnit::TestFixture { 
   7:    
   8:      CPPUNIT_TEST_SUITE( MiscTest); 
   9:      CPPUNIT_TEST( testMaxSubArray_0); 
  10:      CPPUNIT_TEST_SUITE_END(); 
  11:    
  12:      private: 
  13:      public: 
  14:    
  15:      void setUp(); 
  16:      void tearDown(); 
  17:    
  18:      // Test cases. 
  19:      void testMaxSubArray_0(); 
  20:    
  21:  }; 
  22:  #endif 


MiscTest.cpp
   1:  #include "MiscTest.h" 
   2:  #include "MaxSubArray.h" 
   3:    
   4:  using namespace Common::Algorithm::MaxSubArray; 
   5:    
   6:  // Registers the fixture into the 'registry' 
   7:  CPPUNIT_TEST_SUITE_REGISTRATION( MiscTest ); 
   8:    
   9:  void MiscTest::setUp() 
  10:  { 
  11:  } 
  12:  void MiscTest::tearDown() 
  13:  { 
  14:  } 
  15:    
  16:  // Test normal case. 
  17:  void MiscTest::testMaxSubArray_0() 
  18:  { 
  19:      int iArray[] = {-10, 5, 7, -2, 19, 50, -30, 32, -24, 18}; 
  20:      std::vector<int> v(iArray, iArray + sizeof(iArray) / sizeof(int)); 
  21:    
  22:      int rStart = 0; 
  23:      int rEnd = 0; 
  24:      int rSum = 0; 
  25:      MaxSubArray::CalcBF(v, rStart, rEnd, rSum); 
  26:       
  27:      CPPUNIT_ASSERT_EQUAL( 1, rStart); 
  28:      CPPUNIT_ASSERT_EQUAL( 7, rEnd); 
  29:      CPPUNIT_ASSERT_EQUAL(81, rSum); 
  30:    
  31:  } 

Finally, I can start implementing the algorithm. Then I run the unit test each time I think my algorithm will work, then fix bugs and retry. Here is the final version that passes the test above:

   1:  void MaxSubArray::CalcBF(std::vector<int> &v, int &oStart, int &oEnd, int &oSum) 
   2:  { 
   3:      // Trivial cases, size = 0 or 1. 
   4:      if (v.size() == 0) 
   5:      { 
   6:      oStart = 0;  
   7:      oEnd = 0; 
   8:      oSum = 0; 
   9:      return; 
  10:      } 
  11:    
  12:      if (v.size() == 1) 
  13:      { 
  14:      oStart = 0; 
  15:      oEnd = 0; 
  16:      oSum = v[0]; 
  17:      return; 
  18:      } 
  19:    
  20:      oSum= 0; 
  21:      oStart = 0; 
  22:      oEnd = 0; 
  23:      std::vector<int> vResult; 
  24:    
  25:      // Initialize vResult. 
  26:      for (size_t i = 0; i < v.size(); i++) 
  27:      { 
  28:      for (size_t j = 0; j < v.size(); j++) 
  29:      { 
  30:          if (i == j) 
  31:          { 
  32:          vResult.push_back(v[i]); 
  33:          } 
  34:          else 
  35:          { 
  36:          vResult.push_back(0); 
  37:          } 
  38:      } 
  39:      } 
  40:    
  41:    
  42:      // Calculate the results. 
  43:      // We use a matrix to store sum of all intervals 
  44:      // 00   01   02   03   04 
  45:      // 10   11   12   13   14 
  46:      // 20   21   22   23   24 
  47:      // 30   31   32   33   34 
  48:      // 40   41   42   43   44 
  49:      // M[i,i] = v[i] 
  50:      // M[i,j] = sum(i,j) for 0 <= i < j <= v.size().  
  51:      // It takes O(n^2) time. 
  52:      for (size_t i = 0; i < v.size(); i++) 
  53:      { 
  54:      for (size_t j = 0; j < v.size(); j++) 
  55:      { 
  56:          if (i < j) 
  57:          { 
  58:          vResult[ i * v.size() + j] = vResult[ i * v.size() + j - 1 ] + 
  59:                                       vResult[ j * v.size() + j]; 
  60:          if (oSum < vResult[i * v.size() +j]) 
  61:          { 
  62:              oSum = vResult[i * v.size() + j]; 
  63:              oStart = i; 
  64:              oEnd = j; 
  65:          } 
  66:          } 
  67:      } 
  68:      } 
  69:  } 

Let discuss the faster version that takes only O(nlgn) time, in the next post. Thank you and like to know your feedback.

Thursday, June 2, 2011

Introduction to Algorithms - The Reading Notes: Part 3, Merge Sort

Merge sort is a better sorting algorithm than insertion sort in terms of worst case running time. It's also the algorithm that I ran into when I have my interview for my current job.

The ITA book uses it as an example of the Divide and Conquer philosophy which basically divides a big problem into several smaller ones then solve each of them first, when the smaller problems are solved, the big problem becomes easier to solve. This usually results in a recursive algorithm as the case of the merge sort. I'm not sure it's a good place to introduce this concept in the beginning part of the book, the learning curve is quite steep.

The merge sort contains two parts, the divide or recursive part and the merge part. The idea is to divide the whole list to sort into two lists each of half the original size and sort the two lists separately, then merge the two sorted smaller lists into one sorted list to get the problem solved. The magic is to repeat this procedure for the two smaller lists too, so they became four even smaller lists, and so on.

The merge part is quite straightforward, compare the elements at the head position of two sorted list, and put the larger (or smaller) into the result list until one of the list contains no element, then we can safely put the rest of the other list into the result list. Only one thing to remark, the vector constructor who takes to iterators s and e means to construct a vector that contains elements from s to e-1 of the original vector.

   1:  void MergeSort::Merge(std::vector<int> &v, int p, int q, int r, bool bNonDecreasing) 
   2:  { 
   3:      std::cout << "Merging sub array: " << p << ":"<< q-1 << " and " << q << ":" << r-1 << std::endl; 
   4:      std::vector<int> vSub1(v.begin()+p, v.begin()+q); 
   5:      std::vector<int> vSub2(v.begin()+q, v.begin()+r); 
   6:      std::cout << "vSub1.size: " << vSub1.size() << ", vSub2.size: "<< vSub2.size() << std::endl; 
   7:      int iLastValue = (bNonDecreasing ? INT_MAX : INT_MIN); 
   8:       
   9:      vSub1.push_back(iLastValue); 
  10:      vSub2.push_back(iLastValue); 
  11:    
  12:      int i = 0; 
  13:      int j = 0; 
  14:      for (int k = p; k < r; k++) 
  15:      { 
  16:      if ( bNonDecreasing && (vSub1[i] < vSub2[j]) || 
  17:          !bNonDecreasing && (vSub1[i] > vSub2[j])) 
  18:      { 
  19:         v[k] = vSub1[i]; 
  20:         i++; 
  21:      } 
  22:      else 
  23:      { 
  24:          v[k] = vSub2[j]; 
  25:          j++; 
  26:      } 
  27:    
  28:      } 
  29:  } 


The recursive part is a bit tricky. Personally, I prefer to give a unique interface to the users of my sorting library, so I'd like to call my interface MergeSort::Sort(input_vector_to_sort, order). Then I found that I need a helper method that can be called recursively and I named it as DoSort(). DoSort() takes two additional parameters, the start and the end position in the list that defines the sub-list to be sorted. It starts by dividing the input vector into two smaller ones then it calls itself recursively for each smaller vector, finally it merges the two smaller vector (sorted now).

   1:  void MergeSort::DoSort(std::vector<int> &v, int p, int r, bool bNonDecreasing) 
   2:  { 
   3:      std::cout << "Sorting sub array: " << p << ":" << r << std::endl; 
   4:      if (p < r) 
   5:      { 
   6:      int q = (p+r)/2;  
   7:      std::cout << "Dividing sub array: " << p << ":" << q  << ", " << q+1 << ":" << r << std::endl; 
   8:      DoSort(v, p, q, bNonDecreasing); 
   9:      DoSort(v, q+1, r, bNonDecreasing); 
  10:      Merge(v, p, q+1, r+1, bNonDecreasing); 
  11:      } 
  12:  } 

Finally, the interface method is fairly simple:

   1:  void MergeSort::Sort(std::vector<int> &v, bool bNonDecreasing)  
   2:  { 
   3:      if (v.size() < 1 ) 
   4:      { 
   5:      return; 
   6:      } 
   7:    
   8:      DoSort(v, 0, v.size()-1, bNonDecreasing); 
   9:  } 

Wednesday, May 11, 2011

Introduction to Algorithms - The Reading Notes: Part 2, Insertion Sort

Insertion Sort is probably the simplest algorithm so it is used in many books as the first example. ItA discusses this minion algorithm right in the Part I Foundations, Chapter 2 Getting Started.

The aim of the algorithm is to sort a set of keys in a certain order: You have some keys as input then the algorithm will give you a ordered list of your keys. Here is how the insertion sort works: suppose all the keys are in an input list, then the algorithm starts working on the the keys one by one in a certain order, for example from left to right. Each time it works on a key, it save it's value, then it looks all keys already processed (they are to the left of the current key), from right to left, by comparing the current key with them and moving the processed keys one place to the right if it should be placed after the current key, according to the ordering rule. This process ends until when an already processed key that should be placed to the left of the current key or there is no more already processed keys. Then we put the current key in the place of the last already processed key we found. Now that we find a good place for the current key, we can start processing the next unprocessed key.

The algorithm is simple, code the pseudo code in C++ is straightforward. What I'd to say here is how I set up the whole project with CppUnit and SCons.

CppUnit is very handy for unit test, especially when I'm writing libraries. Just as in this ITA project, I'd like to put all the algorithms in some libraries, I don't need to write a separate main cpp file just to test these algorithms, instead, I setup a CppUnit testing framework for this. It's easier to maintain. For how to install/use CppUnit, here is the CppUnit Documentation.

First, I implement the Insertion Sort algorithm in InsertSort.cpp and .h files and I put them in a folder named Sorting. With TDD in mind, I write only the class declaration but leaving the method empty.

Then I setup the CppUnit framework: I need three files: SortingTest.h and .cpp, where I'll implement test code for all the sorting algorithms I'll write; TestMain.cpp. where is the "main" function of the test code. The beauty of using CppUnit is this "main" function has only infrastructure interest, it's not related to the particular test cases. So once setup, I don't need to change it. I'll only work on the SortingTest class.

Now I will write a test case:

   1:  // Test normal case.
   2:  void SortingTest::testInsertSort_0()
   3:  {
   4:      int iArray[] = {10, 5, 7, -2, 19, 50, 300, 320, 160, 1000};
   5:      std::vector<int> v(iArray, iArray + sizeof(iArray) / sizeof(int));
   6:      Common::Algorithm::Sort::InsertSort(v);
   7:      CPPUNIT_ASSERT_EQUAL(v[0], -2);
   8:      CPPUNIT_ASSERT_EQUAL(v[1], 5);
   9:      CPPUNIT_ASSERT_EQUAL(v[2], 7);
  10:      CPPUNIT_ASSERT_EQUAL(v[3], 10);
  11:      CPPUNIT_ASSERT_EQUAL(v[4], 19);
  12:      CPPUNIT_ASSERT_EQUAL(v[5], 50);
  13:      CPPUNIT_ASSERT_EQUAL(v[6], 160);
  14:      CPPUNIT_ASSERT_EQUAL(v[7], 300);
  15:      CPPUNIT_ASSERT_EQUAL(v[8], 320);
  16:      CPPUNIT_ASSERT_EQUAL(v[9], 1000);
  17:  }


The aim of the test case is to test the basic function of the algorithm, I initialize a vector with 10 integers, then I call the InsertSort(), finally, I test that the vector is sorted by examining that each element has the expected value. CppUnit provides a macro CPPUNIT_ASSERT_EQUAL for this kind of test.

Now that I have the algorithm code (although not yet complete), I have the testing code, I can compile both and run my test. I use SCons for this. SCons is very convenient yet powerful.  Basically, you write a configuration file usually named as SConstruct, inside you tell scons how to build your project.

   1:  Library(target='Sorting/Sorting', \ 
   2:          source=Glob('Sorting/*.cpp')) 
   3:  Program(target='CppUnitTest/TestMain', \ 
   4:          source=Glob('CppUnitTest/*.cpp'), \ 
   5:          LIBS=['cppunit','Sorting'], \ 
   6:          LIBPATH='Sorting', \ 
   7:          CPPPATH=['Sorting', 'CppUnitTest']) 

The Library keyword at line 1 specifies a library to build, the source code are all the .cpp files in the Sorting folder.

The test code should be built with the Program keyword, as in line 3. It needs all source code in the CppUnitTest folder and two libraries: the CppUnit library and the Sorting library that I'm testing. The Sorting library is in my local folder so I need to tell SCons where to find it with LIBPATH keyword as in line 6 above. Finally, the test code need the Sorting library header to compile, I'll tell SCons to find the Sorting headers in the Sorting folder with CPPPATH keyword as in line 7.

OK, now I save the Sconstruct file then I type scons. It compiles my Sorting library and the testing code.

I can run the test immediately and it fails evidently. Now you can start to write the InsertSort() method, but personally, I will write some more test cases before writing the InsertSort() method. For example, I will write one case when the input is empty, another when all inputs have the same value, etc.

Finally, I write my InsertSort() method and correct all failing cases, once all test cases pass, I'm quite confident that my InsertSort() works correctly.

Here is the how the InsertSort() is implemented:

   1:  void Sort::InsertSort(std::vector<int> &v)  
   2:  { 
   3:      int key = 0; 
   4:    
   5:      // Trivial case, nothing to do.  
   6:      if (v.size() <= 1) 
   7:      {    
   8:          return; 
   9:      }    
  10:    
  11:      for (int j = 1; j < v.size(); j++) 
  12:      {    
  13:          key = v[j]; 
  14:          int i = j-1; 
  15:          while (i >= 0 && v[i] > key)                                                                                             
  16:          {    
  17:              v[i+1] = v[i]; 
  18:              i--; 
  19:          }    
  20:          v[i+1] = key; 
  21:      }    
  22:  } 
  23:    

Modification: When I started to write the merge sort, I found it could be better to rename the class and the methods, and finally, I ended up with a solution that gives each sorting algorithm a class and they all have the same Sort() method declaration.

   1:  void InsertSort::Sort(std::vector<int> &v, bool bNonDecreasing) 




Monday, May 9, 2011

Introduction to Algorithms - The Reading Notes: Part 1, Introduction

I started reading this book recently and I will share my understandings and thoughts with you.

I will discuss the problems in the book and I'd like to inject in particular more on the software engineering aspects of those algorithms: how to implement them so they can be used in a real-life software project to solve practical problems.

Finally, I will code those algorithms in C++ and build the code under Fedora Linux with Scons. I will also adopt the TDD methodology (TDD stands for Test Driven Development) and use CppUnit for unit testing.

Here are some useful links for this project:
The code formatter, it formats the source code into HTML so they look better when put in the blog. Currently it formats C#, VB and some other languages but not C/C++. I'll continue looking for one but before I find something, I'll use this one for the moment.

The SCons user's guide.

The CppUnit Documentation.



Saturday, May 7, 2011

A Real-life Story about Return by Reference - Part II

Now we know that we should follow some "best practices" when dealing with return by reference:


1. Use toto aToto = getToto() or toto aToto(getToto()), don't write toto aToto; aToto = getToto();


2. Don't return a reference to a local variable, except when you know what you are doing 


3. Let member function return a reference to a member variable when the performance is really an issue, and call it with toto &aTotoRef = aToto.getTotoRef() to really take advantage of return by reference. Otherwise, return by value. Always take care of the variable holding the reference result, use it only during the life time of the object itself. 


There is one last thing you may want to know, that is the RVO does not work all the time, as a result it cost you one more construction/destruction of your object when you return it by value. I won't say it's a penalty you have to pay, it's just the fair cost you have to pay as the compiler can not help you more because your code asks for more. So when RVO does not work? Well, when the compiler can not decide which is the object it needs to return at compile time. Here is an example:

toto getTotoWithParam(bool first)
{
    if (first)
    {
        toto aToto;
        return aToto;
    }
    else
    {
        toto bToto;
        return bToto;
    }
}

Then, when calling this function, it will show you that the RVO is not apllied.

toto a = getTotoWithParam(false);
toto constructor.
toto copy constructor.
toto destructor.
toto destructor.



The object is constructed inside the function then used to copy construct the object holding the result.  Compared with the getToto() function, one more object (temporary object) is created.

toto aToto = getToto():
toto constructor.
toto destructor.

So that all about the return by value/reference that I want to tell you, I hope you now know what to do given a certain situation.

Good luck and I'd like to know what you are thinking about this.

Sunday, April 24, 2011

Books to read (once more)

Mathematics, Data Structures & Algorithms

Discrete Mathematics and Its Applications (Fifth Edition), Kenneth H.Rosen

Concrete Mathematics : A Foundation for Computer Science (Second Edition), Ronald L. Graham / Donald E. Knuth / Oren Patashnik


Introduction to Algorithms (Second Edition), Thomas H. Cormen / Charles E. Leiserson / Ronald L. Rivest / Clifford Stein

Data Structures, Algorithms, and Applications in C++, Sartaj Sahni
Data Structures & Program Design In C (Second Edition), Robert Kruse / C.L. Tondo / Bruce Leung


The Art of Computer Programming
Donald E.Knuth

Compiler

Compilers: Principles, Techniques, and Tools (2nd Edition)
[Alfred V. Aho / Ravi Sethi / Jeffrey D. Ullman]

Advanced Compiler Design and Implementation
[Steven S. Muchnick]

Operating System

Operating System Concepts (Now we have the 7th Edition)
[Abraham Silberschatz / Peter Baer Galvin / Greg Gagne]

Operating Systems : Design and Implementation (Now we have the 3rd Edition)
[Andrew S. Tanenbaum / Albert S. Woodhull]


C++

C++ Primer
Lippman / Josee Lajoie]

The C++ Programming Language (Special Edition)
[Bjarne Stroustrup]


Effective C++ (Now we have the 3rd Edition)
More Effective C++
Scott Meyers

C++ Gotchas: Avoiding Common Problems in Coding and Design
Stephen C.Dewhurst

Modern C++ Design: Generic Programming and Design Patterns Applied
Andrei Alexandrescu

ASM

The Art of Assembly Language
Randall Hyde  (x86)

Introduction to 80x86 Assembly Language and Computer Architecture
Richard C. Detmer

UNIX Programming

Advanced Programming in the UNIX Environment
[W. Richard Stevens]

The UNIX Programming Environment
[Brianw. Kernighan]

UNIX Network Programming, Volume 1 : The Sockets Networking API (Third Edition)
[W. Richard Stevens / Bill Fenner / Andrew M. Rudoff]

UNIX Network Programming Volume 2 : Interprocess Communications (Second Edition)
[W. Richard Stevens]

Computer Networks 

Computer Networks (Fourth Edition)
[Andrew S. Tanenbaum]

TCP/IP Illustrated, Volume 1 : The Protocols
TCP/IP Illustrated, Volume 2 : The Implementation
TCP/IP Illstrated, Volume 3 : TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols
[W. Richard Stevens]


Internetworking with TCP/IP Vol I : Principles, Protocols, and Architecture (Third Edition)
Internetworking with TCP/IP Vol II : Design, Implementation, and Internals (Second Edition)
Internetworking with TCP/IP Vol III : Client-Server Programming and Applications, BSD Socket Version (Second Edition)
Internetworking with TCP/IP Vol III : Client-Server Programming and Applications, Windows Sockets Version
[Douglas E. Comer / David L. Stevens]

Database

An Introduction to Database Systems (Seventh Edition)
[C. J.Date]

Database System Concepts (Fourth Edition)
[Abraham Silberschat / Henry F.Korth / S.Sudarshan]

Software Engineering

Code Complete, Steven C. McConnell

Pragmatic programmer: from journeyman to master, Andrew Hunt and David Thomas
Peopleware:Productive Projects and Teams, Tom DeMarco, Timothy Lister    
 

Wednesday, March 9, 2011

A Real-life Story about Return by Reference - Part I

Recently I did some new development on our software. As a standard process, I submitted my work to a code review. One of my colleagues suggested me, for one of my method, using a reference as the return type instead of an object, for example:
class toto ()
...

toto    getToto();
toto &getToto();

His concern is the that return by reference is more efficient than return by value. Personally, I do not think much about efficiency but more on the correctness and clearness of the code, so for a long time, I return everything by value except for very rare cases such as overloading the operator << , etc. His suggestion triggered me to investigate more this point.

Let's consider return by value first so we have something to compare with. So how to write a function that returns an object by value? Well it's simple.
toto getToto()
{
     toto myToto;
     // Here I can work with myToto.
     return myToto;
}
Or if I really have very simple work to do with the myToto, I can write also:
toto getToto()
{
     return toto();
}

Basically, we have the following ways to use the functions:
toto aToto = getToto();  // get a toto and hold it with aToto
toto aToto(getToto());   // construct aToto with the result of getToto with its copy constructor.
getToto();                      // get a toto and do nothing with it. Not useful by valid call.
toto aToto; aToto = getToto(); // assign the result of getToto to an existing object.


That is not all the story yet, we still have another way to call the getToto: 
const toto &aRefToto = getToto();
What we are doing here is to bind a reference to a constant to the result of getToto, which is a temporary object so we are not allowed to call it without the 'const' like:  toto &aRefToto = getToto(); (at least we cannot do it now with most modern compilers. ).


So which one is more efficient ? It depends on the compiler. Actually, this is a feature usually named as RVO, Return Value Optimization. C++ standard leaves this to the implementation of the compiler. Modern compilers such as gcc does quite well so all the first three examples and the last one have the same best performance: only one object is created in the function, so the constructor and destructor get called only once. Only the fourth example cost more, which is caused by the assignment.

So here is the output of a test program:
toto aToto = getToto():
toto constructor.
toto destructor.

toto aToto(getToto());   
toto constructor.
toto destructor.

getToto():
toto constructor.
toto destructor.

toto aToto; aToto = getToto(); 
toto constructor.
toto constructor.
toto assignment.
toto destructor.
toto destructor.

const toto &aRefToto = getToto();
toto constructor.
toto destructor.

It is amazing to know that when you call toto aToto = getToto() or toto aToto(getToto()), you have actually zero overhead, the constructor and destructor are called only once for the object that is created inside the function. Also it's amazing to know the difference (the penalty you are paying) when you write something like: toto aToto; aToto = getToto(); once more constructor and destructor and one more assignment! 

CONCLUSION ONE: Use toto aToto = getToto() or toto aToto(getToto()), don't write toto aToto; aToto = getToto();



Now let's consider the cases when we return by reference.
Things are a little different when it comes to return by reference. Obviously, you are not allowed to return the reference of a temporary object, so the following form is invalid:
toto &getTotoRef()
{
    return toto();
}
Now we only have on possibility to return a reference of an object:
toto &getTotoRef()
{
    toto aToto;
    return aToto;
}

As far as you remember that the reference is "just another name of the referred object", you may notice that  we are doing a dangerous thing here by returning a reference of a local object. Absolutely, you are right. A decent compiler will warn you that you are returning a reference to a local variable. Personally, I never return reference to a local object. This is just to demonstrate the efficiency aspect.

So here we have three ways to use the getTotoRef().
toto &aRefToto = getTotoRef(); // Don't do like this!
toto aToto = getTotoRef();          // construct with result of getTotoRef()
toto aToto(getTotoRef());            // construct with copy constructor.
getTotoRef();                              // useless, but valid.
toto aToto; aToto = getTotoRef(); // assign result of getTotoRef() to an existing variable.

Here is the output of the call sequence when the test program runs:

toto &aRefToto = getTotoRef():
toto constructor.
toto destructor.

toto aToto = getTotoRef();         
toto constructor.
toto destructor.
toto copy constructor.
toto destructor.

toto aToto(getTotoRef());        
toto constructor.
toto destructor.
toto copy constructor.
toto destructor.

getTotoRef();                          
toto constructor.
toto destructor.

toto aToto; aToto = getTotoRef(); 
toto constructor.
toto constructor.
toto destructor.
toto assignment.
toto destructor.


So if you use a reference to hold the result of getTotoRef, you have the maximal gain of return by reference: there is no overhead at all. Otherwise, either the copy constructor or the assignment need to be called to construct a new object based on the result of getTotoRef.  However, with the first form which is most efficient here, you are bound to have unexpected behavior later when you want to use your reference so don't do that.

CONCLUSION TWO: Don't return a reference to a local variable, except when you know what you are doing :D 

So why we have this feature to return a reference in C++? Well, this allows you to do something like:
toto &forwardToto(toto &comingToto)
{
    return comingToto;
}
So here you don't need to create any object. The interest here is that if a function returns a reference, it can appear at the left side of an operator, so the following is valid:
forwardToto() = aToto;
And opens the possibility to do something like cout << "blabla" << endl; because << is overloaded to return a reference. For more on how to overload the operator <<, you should take a/another look at the great book by Prata: C++ Primer Plus (5th Edition)


We can also profit this feature in a member function of a class, but with a lot of care. So think about the tradeoff first.
Suppose we have a class with a member variable, and we have a getter of this variable, so we can make the getter return a reference, like:
class totoClass
{
public:
    toto &getTotoRef();
    _myToto;
}

Then you have the following ways to use this getter:
toto bToto = aToto.getTotoRef();              // Construct bToto.
toto bToto(aToto.getTotoRef());                // Construct bToto.
toto &bToto = aToto.getTotoRef();           // bToto refers to aToto._myToto.
bToto = aToto.getTotoRef();   // assign aToto._myToto to an existing bToto.

What do we pay for each call? Here is the result:

toto bToto = aToto.getTotoRef();              
toto copy constructor.
toto destructor.

toto bToto(aToto.getTotoRef());    
toto copy constructor.
toto destructor.
            
toto &bToto = aToto.getTotoRef();           
** Nothing is called.

bToto = aToto.getTotoRef();   
toto assignment.

OK, not bad, we call once the copy constructor when we want to save the result in another object, fair enough. If we only get the reference as in the third example, we don't need to call any toto constructor/copy constructor/assignment/destructor. That is a big saving if these operations are costly themselves. However, you can do this ***only if you are able to make sure that your totoClass object lives longer than your variable holding the result. 


In comparison, tradition getters that return a value is not too bad. Here is what we have:

class totoClass
{
public:
    toto getToto(); // get by value.
    _myToto;
}

toto bToto = aToto.getToto()
toto copy constructor.
toto destructor.


toto bToto(aToto.getToto());       
toto copy constructor.
toto destructor.
        
const toto &bToto = aToto.getToto();   
toto copy constructor.
toto destructor.


bToto = aToto.getToto();   
toto copy constructor.
toto assignment.
toto destructor.

So the penalty is at least one copy constructor need to be called. We no longer have the amazing gain when we have with ordinary functions that return a local object by value. 

CONCLUSION THREE: Let member function return a reference to a member variable when the performance is really an issue, and call it with toto &aTotoRef = aToto.getTotoRef() to really take advantage of return by reference. Otherwise, return by value. Always take care of the variable holding the reference result, use it only during the life time of the object itself. 

Hope this make things clearer, then we can investigate even more in the next part, yes, the story does not end here.

Like to know what you are thinking about this.