JAJAH Development Blog

Blogs by JAJAH Developers

Archive for the ‘data structures’ Category

How to add a parameter to a Predicate

Monday, January 14th, 2008

When using a “Find” method in a generic List, there is a need to implement a Predicate(T) delegate that defines the conditions of the element to search for. a not good MSDN example can be found here. why not good? because usually we want to add extra parameters to the Predicate(T) delegate.
A good way of doing it is by using Anonymous Method as below:

ourList.Find(delegate(OurObject objectItem)
{
return objectItem.StringToFind == stringToCheck;
}
);
a good reference for Anonymous Method can be found here

BTW, this post was written with a great assistance of our Telephony team )

Fibonacci Heap and Amortized Time

Tuesday, January 1st, 2008

In every system we design and develop, choosing the data structure that fits to our needs is one of the most important decisions we made. Data structures not only fits to our storage need, it also affects our system complexity and its performance.
Performance is a big word, when it refers to data structures, we usually discuss the worst-case sequence and according to that we determine it.
but, in some cases the worst-case happens infrequently, and in most cases (also the average case) our performance is extremely different then the worst-case.
This kind of analyses is being called Amortized Analysis. Amortized analysis refers to the average running time and not the worst case running time.

Fibonacci Heap is a great example for amortized analysis. a glance in the table below shows its advantages versus a linked list

  Linked List Fibonacci Heap
insert O(1) O(1)
access min O(1) O(1)
delete min O(n) O(log n)*
decrease key O(1) O(1)*
delete O(n) O(log n)*
merge O(1) O(1)

(*) Amortized time

How we accomplish this?
Fibonacci Heap is heap data structure consisting of a forest of trees. Its implementation discussed in details in this Wikipedia article and also being demonstrated in .
The main idea of it, is keeping the level of trees in a way that in the average case we will have less operations to do. only once in a while we will merge the trees and organize them to a binomial heap (this operation is being called CONSOLIDATE). since we kept the level of the trees, the CONSOLIDATE operation will take O(log n) in the average case and only infrequently it will take O(n).
Also, in some cases we can control the phase of the worst case and force it to happen. it can help us to improve our system performance by forcing CONSOLIDATE operation during an off-peak.

The use of Fibonacci Heaps improves the asymptotic time of Dijkstra’s algorithm for computing shortest paths in a graph and Prim’s algorithm for computing a minimum spanning tree of a graph.

Maybe some day in the future, Microsoft or Sun will choose to add this fascinating data structure to there implemented collections.

How to shuffle a list

Thursday, December 13th, 2007

usually, most of the manipulations that are being done with lists are sorting. therefore most of the .Net Framework solutions are for doing that. But what if we want to shuffle a list instead of sorting it?
well, I have bump into that problem while ago, and try to come up with the most elegant solution for doing it.

my first approach was using the existing .Net Framework solutions for sorting and manipulate them in a way to provide a shuffling solution.

so I did as follow:

I have created a RandomComparer who implements IComparer<int> with the following compare method:

public int Compare(int x, int y)
{
if (x == y) return 0;
else return random.Next(2) == 0 ? 1 : -1;
}

Then, I shuffled the list while using the RandomComparer I have created:

List<int> sortedList = GetSortedList();
List<int> shuffledList;
shuffledList = sortedList.Sort(RandomComparer.Instance);

Looks like an elegant solution? well, it has 2 problems:

  1. performance - sorting a list takes in the worst case O(n lgn) while shuffling can take O(n)
  2. after running this code in load test I have noticed that once in ~200 times, a System.ArgumentException is being thrown saying: “IComparer did not return zero when Array.Sort called x. CompareTo(x). x”.
    notice that the Compare method described above returns 0
    if(x == y). after investigating this I figure that in some cases there is 3 parameters comparison, and some times we get:
    a > b, b > c, c> a. which makes no sense…

So, the best way I come up with is a simple implementation of a shuffle method that picks a random value from the sorted list and add it to the shuffled list:

public List<int> Shuffle(List<int> sortedList)
{
List<int> shuffledList = new List<int>(sortedList.Count);
int count = sortedList.Count;
for (int i = 0; i < count; i++)
{
int j = random.Next(sortedList.Count);
shuffledList.Add(sortedList[j]);
sortedList.Remove(sortedList[j]);
}
return shuffledList;
}

notice that this solution takes in the worst case O(n)

Jajah is the VoIP player that brought you web-activated telephony.