•  
  • Programming (118)

Memcached

Categories: Linux, Programming, Scalability
Tags: No Tags
Comments: 1 Comment
Published on: October 24, 2011

Memcached is Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, page rendering or simply anything you like to store temporarily.

Memcached is simply a Key/Value store. You can see it as a standalone distributed hash table or dictionary. Memcached doesn’t know what your date is like, all it does is to store key value pairs with expiration and using LRU (Least recently used) algorithm to maintain the cached items.

What makes Memcached cool is that it provides scalability. How does it do this? By hashing algorithms that its clients implements. There are two phase of hashing, one of which happens at the client and the other happens at the server. Therefore, eventually all the memcached clients implement some hashing algorithm in order to benefit from the memcached’s distributed nature. I will mention this in a bit.

Having said that memcached is a distributed hash table (key value store), servers are disconnected from each other, and usually they are unaware of each there. There is no communication between memcached servers, any synchronization or broadcasting. This increases the flexibility to be able to scale out the memcached servers. If you are running low on resources on a memcached server, you can add another memcached server, and you can keep adding more cache servers as you need. You should pay attention that, if you don’t add cache servers, your cached item will start to be dropped out of memcached as the cache becomes full and as I mentioned Least recently used algorithm is used to drop the oldest cached items. This is also called Eviction.

In computer science world, one of the most important notations is Algorithmic complexity. Example: searching for an item, sorting a collection etc. Memcached considers this and implements a O(1), constant time, key value store. This means that storing an item to cache and extracting an item from the cache is constant time operation, which is very fast. This is achieved by implementing a good hash code method that doesn’t cause collisions.

On another note, Memcached storage of cached items is not traversable/iterable. You cannot traverse the whole cache.

Memcached is awesome! But not for every architecture.

  • You have objects larger than 1MB.
    • Memcached is not for large media and streaming huge blobs.
  • You have keys larger than 250 chars.
    • Memcached doesn’t support more than 250 chars.
  • If you want persistence or a database. You might consider MemcacheDB which provides persistence for Memcached.
  • You’re running in an insecure environment. Memcached doesn’t have any authentication or authorization system.

As I mentioned Memcached has two-stage hashing. It behaves as a giant hash table, looking up key = value pairs. Give it a key, and set or get some arbitrary data. That is it really. That is all it does.

When doing a memcached lookup, first the client hashes the key against the whole list of servers that you need to introduce to your clients. Once it has chosen a server after the first hashing procedure, the client then sends its request, and the server that was chosen does an internal hash key lookup for the actual item data. This enables the client to know which cache server to query again, when the item that was sent to cache is requested.

You need to understand that memcached is not redundant. There is no notion of replication or communication between cache servers; they are unaware of each other. Their only purpose is to store an item and give back an item. If one of your cache servers fails, you will lose all your data within that cache server. You will have to remove the cache server that failed from the list of the cache servers of your clients, ie: configuration.

Moreover, when one of your cache fail and you want to add another one, or you remove your cache from your clients, that will cause a big problem which is all your data, cached items will be invalid. This is due to double hashing mechanism, which clients, uses to hash the items based on the servers. So all your cache will be invalid, and you will have a spike. In order to avoid this, you will have to start a new node and assign the IP address of the dead node to the newly created node; this will prevent all your data to be invalid. Yet another way to solve this problem would be to use Consistent Hashing , in order to avoid computation of hash values of all the data.

Memcached operations aim to be atomic. All individual commands sent to memcached are atomic.

Memcached mimics organization of data via namespaces and it only stores objects. On the other hand, Microsoft App Fabric Cache provides notion of regions or sections. This allows you to keep the same Type of items in independent caches. Moreover, you can store strong Type instead of objects with AppFabric Cache, which I think these features are great.

Memcached is fast. It utilizes highly efficient, non-blocking networking libraries to ensure that memcached is always fast even under heavy load. In other words, in circumstances where your database might be falling over, memcached won’t be. Which is precisely what memcache was designed to do: to take the load off of your database, which for the majority of popular web applications is the biggest performance bottleneck and risk to scalability.

Memcached is simple and easy to deploy. It does not require a lot of technical knowledge to use or use effectively – it just does what it is supposed to.

Compressing large values is a great way to get more out of your memory and network communication/bandwidth. Compression can save a lot of memory for some values, and also potentially reduce latency as smaller values are quicker to fetch over the network.

Most clients support enabling or disabling compression by threshold of item size, and some on a per-item basis. Smaller items won’t necessarily benefit as much from having their data reduced, and would simply waste CPU.

Main Operations to work with Memcached is as follows:

Storing an item to database, you can pass values for datetime or timespan for expiration of the object being set.

Remove an item from the cache.

Increment and decrement given a keys value.

TryGet or Get is used to get an object from the cache.

Some of the clients allows retrieving multiple elements from the cache.

 

On another note, you can read about Microsoft AppFabric Cache.

Java Set Operations

Categories: Programming
Tags:
Comments: No Comments
Published on: October 7, 2011

Union:

c1.addAll(c2);

Intersection:

c1.retainAll(c2)

Difference

c1.removeAll(c2)

Subset/Superset check

c1.containsAll(c2)

Eclipse Shortcuts

Categories: Programming
Tags: No Tags
Comments: No Comments
Published on: October 4, 2011

I just learned that you can see all the eclipse shortcuts by pressing;

ctrl + shift and L keys. and you ll see the list of all the shortcuts.

C# Synchronizing a method

Categories: Programming
Tags: No Tags
Comments: No Comments
Published on: September 12, 2011

C# provides several synchronization primitives such as lock, monitor, mutex, reader writer locks, semaphore, Interlocked etc.

if you want to synchronize your method :

[MethodImpl(MethodImplOptions.Synchronized)]
public void DoSomething()
{
…..
}

Even though this is not a very common practice, your method will be synchronized.

Best Practices and Hidden Features of C#

Categories: Programming
Tags: No Tags
Comments: No Comments
Published on: August 10, 2011

Best Practices and Hidden Features of C#

While working with files, folders and IO, C# provides several helper classes for the job one of which is Path class. While most of the developers still uses string operations to combine the paths, Path class provides a Combine method to get the job done.

Ex:

var  path = Path.Combine(@”c:\csv\”, “foo”); // return a string of the new path.

Path class also provides many other methods to access to many functionality of the path information, such as full path, extension, file name, directory name and so on.

C# introduces ?? operator, null-coalescing, to evaluate expressions for their null value. if the expression on the left is null, expression on the right will be evaluated. ?? operator comes in very handy while working with databases due to the results can include null values. Moreover, you can chain many ?? null-coalescing operators together in the same statement. Using null-coalescing you can as well lazily initialize your objects.

ex:

var res = obj ?? new YourObject();

Casting is an expensive and tedious process. C# introduces is and as operators to simplify casting. When you are doing a cast, there is a possibility of an exception being thrown, to avoid this, you can use is and as operators. is operator is used to check the type of an object, if(myObject is MyObject) like this. This operation returns true or false. There is no exception thrown. Likewise with as operator, if the cast fails, CLR wont throw an exception, instead it will return false. If the cast is successful, you will get the casted object.

C# provides automatic properties which is a good start while coding. Most of the time if you might need encapsulate some logic within the properties you can easily change it through automatic properties. Properties are an important concept to implement encapsulation and properties provides a good starting point for encapsulation.

ex :

public string Name {set;get;}

While working with generics you will encounter some cases to assign values to your generic types, and when you wont know what type you will be expecting and what to assign to your variables, default(T) comes in handy. In the run time, CLR finds out the default value of the generic type, if it s an object, null, if it s value type, whatever the default value of it will be used by default method.

While working with string data, you will need to escape characters like \ or spaces, verbatim string comes in handy.

ex:

string path = @”c:\csv\foo.csv”; // if you dont use @, you will have to escape \.

Nullable types, is a great feature of C#, while working with databases or APIs, sometimes, you get no result, null values, and using value types, you will get exceptions most of the time for having null values, this is the time you will need to use nullable types.

Environment class provides several utilities and most of them are system independent. Some of the methods are:

Environment.FailFast(string); //This is used fail fast, right away, reporting to event log with the exception.
Environment.NewLine; // Provides new line, system independent
There are several other cool methods about the system information provided by Environment class.

Using statement and directive provides several benefits one of them is to make the object used within the using statement to be eligible for garbage collection. When the object is out of scope, object becomes eligible for garbage collection and CLR invokes the finalizer or the dispose method of the object.

Ex:

using(ISession session = new Session()){} // when the session object is out of scope, you wont need to explicitly call the dispose method of the object, CLR finds it and executes it for you for garbage collection.
Another feature of using is you can create shortcuts for long named objects as follows:

using dict = Dictionary<MyLongNamedObject, ANotherLongNamedObjectOrCollection>;

There are several approach for string comparison and below is an efficient way of doing it :

myString.Equals( theirString, StringComparison.OrdinalIgnoreCase ); // return boolean. If you convert both of your strings to upper or lower, you are creating extra objects which will as well needed to be recycled etc. So this is an efficient way of comparing strings in C#

Most of the time developers check if the string is null or empty seperately. C# has a string method to check if the string is empty or null as follows :

string.IsNullOrEmpty(myString);// return boolean.

Yet another problem is while the string is null or white space, OK you can trim the string and check the white spaces, which creates new string, C# provides method to check if the string is null or contain white space(s) via

string.IsNullOrWhiteSpace(myString);

 

Windows Server AppFabric

Categories: Programming, Scalability, Web
Tags: ,
Comments: 1 Comment
Published on: July 19, 2011

Caching and reliable hosting is one the important elements for high available and scalable applications. Microsoft delivers Microsoft server appfabric. Two major components are Caching and Hosting. OK, i dont much know about hosting except the fact that it uses Microsoft Activation service, and I think, IIS is using it internally. Again, I m not very familiar with it. But you can learn more in the following URL:
Microsoft AppFabric
and Microsoft provides a training kit which is really useful :
AppFabric Traning Kit

As far as caching, AppFabric Cache used to be velocity cache. It s more than a distributed key value pair, it has many other features such as tags, where you can tag the key value pairs and then you can get them by tag. Moreover, you can create regions to store the relative items within an organization. For example: You can have a student cache, teacher cache, classes cache, etc. You dont have to stick everything into a single cache.

Just like other cache services, the API provides, Add (Adds an item to Cache), Get(Gets an item from cache), Put(Adds or updates an item), Remove methods(Removes an item from cache). Moreover, you can handle concurrency with Optimistic or Pessimistic Concurrency. Usually, you would like to define these in an interface and wrap the Factory Method of Data Classes. In terms of configuration, there are several configuration parameters for appfabric cache, during development, I usually prefer storing the configuration in the dedicated config file, such as cache.config. Moreover, in your application you would prefer to wrap the datacachefactory class, instantiate it and keep it live as long as the application lives.

You can set expiration time for the items in the cache, default is 10 minutes. You can set it so that it doesn’t expire etc.

There is cache eviction, which occurs when the machine the cache is on, runs out of memory. The cache items becomes evicted and drops. Least recently used algorithm is used for this implementation.

While developing code with cache, it is always good to do defensive programming, because there might be several problems, such as items might expire, cache servers might go down etc. So it is very desirable to consider all these failures.

One important point is that, every item you store in the cache should be serializable. All the primitive type in C#.Net are serializable, if you are creating your own types, make sure you make them serializable, there are couple ways to do that, such as implementing ISerializable interface or by using DataContract and DataMember attributes (Which WCF uses). Once your items are serializable you can store them in the cache.

Usual usage of cache is to be placed in front of database. So each request doesnt go to database but instead, it gets the data from cache which increases the response time of the application. This is just one of the usage of cache. Cache basically takes the load of the database.

Binary Search Tree Implementation via C#

Categories: Algorithms, Programming
Tags:
Comments: No Comments
Published on: July 10, 2011

Below is my binary search tree implementation (Not self balancing). Binary search trees are very common data structure and being used by many algorithms. Furthermore, binary search trees can be adopted to solve many algorithms, problems and puzzles.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FiratAtagun.DataStructures
{
    public abstract class Node :  IEquatable, IEquatable> where T : IComparable
    {
        public T Value { set; get; }

        protected Node(T value)
        {
            Value = value;
        }

        protected Node()
        {

        }

        public override string ToString()
        {
            return Value.ToString();
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public bool Equals(T other)
        {
            return Value.Equals(other);
        }

        public bool Equals(Node other)
        {
            return Value.Equals(other.Value);
        }

        public static bool operator <(Node left, Node right)
        {
            if(left.Value.CompareTo(right.Value) < 0)
            {
                return true;
            }

            return false;
        }

        public static bool operator >(Node left, Node right)
        {
            if (left.Value.CompareTo(right.Value) > 0)
            {
                return true;
            }

            return false;
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FiratAtagun.DataStructures
{
    public class TreeNode : Node where T : IComparable
    {

        public TreeNode LeftChild { set; get; }
        public TreeNode RightChild { set; get; }

        public TreeNode()
        {
        }

        public TreeNode(T value): base(value)
        {
        }
    }
}

using System;
using System.Collections.Generic;

namespace FiratAtagun.DataStructures
{
    public class BinaryTree : IEnumerable, IEnumerable> where T: IComparable
    {

        public TreeNode Root { set; get; }

        public BinaryTree()
        {

        }

        public BinaryTree(IEnumerable collection)
        {
            foreach (var item in collection)
            {
                Insert(item);
            }
        }

        public void Insert(T value)
        {
           Insert(new TreeNode(value));
        }

        public void Insert(TreeNode treeNode)
        {
            if (ReferenceEquals(Root, null))
            {
                Root = treeNode;
                return;
            }

            var currentNode = Root;

            var node = treeNode;

            while (true)
            {
                if (node < currentNode)
                {

                    if (ReferenceEquals(currentNode.LeftChild, null))
                    {
                        currentNode.LeftChild = node;
                        break;
                    }
                    currentNode = currentNode.LeftChild;
                    continue;
                }

                if (node > currentNode)
                {

                    if (ReferenceEquals(currentNode.RightChild, null))
                    {
                        currentNode.RightChild = node;
                        break;
                    }
                    currentNode = currentNode.RightChild;
                    continue;
                }
            }
        }

        public void InsertRecursively(T value)
        {
            var node = new TreeNode(value);

            if(ReferenceEquals(Root, null))
            {
                Root = node;
                return;
            }

            var currentNode = Root;

            InsertRecursively(currentNode, node);
        }

        public void InsertRecursively(TreeNode currentNode , TreeNode node)
        {
            if(node < currentNode)
            {
                if(ReferenceEquals(currentNode.LeftChild,null))
                {
                    currentNode.LeftChild = node;
                    return;
                }
                InsertRecursively(currentNode.LeftChild, node);
                return;
            } 

            if(node > currentNode)
            {
                if(ReferenceEquals(currentNode.RightChild, null))
                {
                    currentNode.RightChild = node;
                    return;
                }
                InsertRecursively(currentNode.RightChild, node);
                return;
            }
        }

        public int NumberOfNodes
        {
            get { return NumberOfNodesMethod(); }
        }

        private int NumberOfNodesMethod()
        {
            if(ReferenceEquals(Root,null))
            {
                return 0;
            }

            return NumberOfNodesImpl(Root);
        }

        private static int NumberOfNodesImpl(TreeNode treeNode)
        {
            if(ReferenceEquals(treeNode, null))
            {
                return 0;
            }

            var count = 1 + NumberOfNodesImpl(treeNode.LeftChild) + NumberOfNodesImpl(treeNode.RightChild);

            return count;
        }

        public int Height
        {
            get { return GetHeight() -1; }
        }

        private int GetHeight()
        {
            if(ReferenceEquals(Root, null))
            {
                return 0;
            }

            return GetHeight(Root);
        }

        private static int GetHeight(TreeNode node)
        {
            if(ReferenceEquals(node, null))
            {
                return 0;
            }

            return 1 + Math.Max(GetHeight(node.LeftChild), GetHeight(node.RightChild));
        }

        public void PreOrderTraversal()
        {
            if(ReferenceEquals(Root, null))
            {
                return;
            }

            var currentNode = Root;

            PreOrderTraversal(currentNode);
        }

        public void PreOrderTraversal(TreeNode node)
        {
            if(ReferenceEquals(node, null))
            {
                return;
            }

            Console.WriteLine(node.Value);
            PreOrderTraversal(node.LeftChild);
            PreOrderTraversal(node.RightChild);
        }

        public void InOrderTraversal()
        {
            if(ReferenceEquals(Root, null))
            {
                return;
            }

            InOrderTraversalImpl(Root);
        }

        public void InOrderTraversalImpl(TreeNode node)
        {
            if(ReferenceEquals(node, null))
            {
                return;
            }

            InOrderTraversalImpl(node.LeftChild);
            Console.WriteLine(node.Value);
            InOrderTraversalImpl(node.RightChild);
        }

        public void PostOrderTraversal()
        {
            if(ReferenceEquals(Root,null))
            {
                return;
            }

            PostOrderTraversalImpl(Root);
        }

        public void PostOrderTraversalImpl(TreeNode node)
        {
            if(ReferenceEquals(node, null))
            {
                return;
            }

            PostOrderTraversalImpl(node.LeftChild);
            PostOrderTraversalImpl(node.RightChild);
            Console.WriteLine(node.Value);
        }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        IEnumerator> IEnumerable>.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}

LinkedList Notes

Categories: Algorithms, Programming
Tags:
Comments: 2 Comments
Published on: July 7, 2011

LinkedList is a common data structure being used. One of the difference between linked list and array based structures are, linked list doesn’t require contiguous memory space. Moreover, expansion within array based data structures bases on a load factor that s determined by the engineers who build the structures, and expansion of arrays usually twice the size of the current size. During expansion of the array, CLR creates a new array with the new size and then copy all the entities from one array to another. Even though this seems to be a an easy operation, as you data grows, it might become slow. Imagine, your array size becomes 5 million elements, next expansion would require contiguous memory to hold 10 million blocks. So this operation becomes very expensive. Benefit of linked list in the aspect is that, it doesn’t require contiguous memory space, nodes can be anywhere withing the Virtual Memory. Since we keep pointers to the nodes, that is sufficient to access any node which is linked by another node. Therefore the issue we mentioned above doesn’t happen with linked list.

There can be several algorithms and problems based on linked list due to the fact that linked list is convenient for multiple pointer usages, recursion, iteration, reversing etc.
Some of the examples are follows and the best way to practice them is to write code for it after understanding them.

If there is a cycle in a given linked list, you can find it by marking the visited nodes as visited, if you see a visited node again, you halt. This solution requires you to modify the Node structure and add a bolean to it. Another solution would be to have two pointers at the beginning, one that moves one by one node, and another pointer, that moves 2 nodes at a time. And if there is a cycle in the linked list they will meet both pointers will eventually meet.

A way to find out the middle element of a linked list is to use two pointers, one that moves one by one and another that moves two by two. Once the one that moves two nodes at a time reaches to the end, first node comes to the middle of the linked list.

To check if a given linked list is palindrome, divide the list into two parts. Reverse the first part and compare with the second part node by node. If they are equal that, linked list contains a palindrome. There are several different approaches to solve palindrome problem both recursively and iteratively.

Given two singly linked lists in sorted order, which forms a Y shape, you can reverse both of the linked lists and compare each element one by one by keeping two pointers. Once you reach the nodes that don’t intersect. Previous nodes are the intersection points.

Given a sorted singly linked, it s possible to remove the duplicates with a single iteration, by keeping two pointers, one to current node and one to the next node. If the next node is equal to current node, connect the current node to next node’s next node.

Given an unsorted singly linked list, it s possible to remove duplicates by using a dictionary. Store elements in the dictionary as you iterate the list. Each time you move to a node, check the dictionary, if the element is in the dictionary already, that s duplicate. Another way is to sort and do the above algorithm.

Given a linked list of even and odd numbers, you can separate the even numbers from the odd ones as follows. Using two pointers, one to point to the head and another to point to the tail, start iterating from both ends, if first pointer’s node is even and second pointer node is odd then swap them. Otherwise keep moving the pointers until you reach a odd or even number. Then swap them until two pointers overlap.

Given a linked list in which you need to set the last node as the head of the linked list. You traverse the linked list once. Once you reach to the end, you also keep a previous pointer to find the node that will qualify to last node. Last node next pointer will be assigned to the head pointer and then will qualify to be the head node. Tail node will be the second pointer node, whose next pointer will point to null.

Below is an implementation of a linked list that I have quickly written. Syntax hightlighter is not working perfect as you can see. Sorry about that.

using System;
using System.Collections;
using System.Collections.Generic;

namespace FiratAtagun.DataStructureTests.LinkedList
{
    public class Node : IEquatable where T : IComparable
    {
        public Node Next;

        public Node()
        {
        }

        public Node(T data)
        {
            Data = data;
        }

        public Node(Node data)
        {
            Data = data.Data;
        }

        public Node(T data, Node next)
        {
            Data = data;
            Next = next;
        }

        public T Data { set; get; }

        #region IEquatable Members

        public bool Equals(T other)
        {
            return Data.Equals(other);
        }

        #endregion
    }

    public enum InsertionMethod
    {
        Default,
        Recursive,
        Iterative
    }

    public class SinglyLinkedList : ICollection> where T : IComparable
    {
        private int _count;
        private Node _head;
        private Node _iterator;

        public SinglyLinkedList()
        {
        }

        public SinglyLinkedList(T data)
        {
            _head = new Node(data);
        }

        public SinglyLinkedList(Node node)
        {
            _head = node;
        }

        public SinglyLinkedList(IEnumerable collection)
        {
            foreach (T item in collection)
            {
                Insert(item);
            }
        }

        #region ICollection> Members

        public void Add(Node item)
        {
            Insert(item);
        }

        public void Clear()
        {
            _head = null;
        }

        public bool Contains(Node item)
        {
            Node found = Find(item);
            return found != null ? false : true;
        }

        public void CopyTo(Node[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { return _count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(Node item)
        {
            return Delete(item);
        }

        public IEnumerator> GetEnumerator()
        {
            _iterator = _head;
            while (_iterator != null)
            {
                yield return _iterator;
                _iterator = _iterator.Next;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public void Insert(T data, InsertionMethod insertionMethod = InsertionMethod.Default)
        {
            _count++;
            Insert(new Node(data), insertionMethod);
        }

        public void Insert(Node node, InsertionMethod insertionMethod = InsertionMethod.Default)
        {
            if (insertionMethod == InsertionMethod.Default || InsertionMethod.Iterative == insertionMethod)
            {
                InsertIteratively(node);
                return;
            }

            InsertRecursively(node);
        }

        private void InsertRecursively(Node node)
        {
            if (_head == null)
            {
                _head = node;
                return;
            }

            Node currentNode = _head;
            InsertRecursively(node, currentNode, null);
        }

        private void InsertRecursively(Node node, Node currentNode, Node previousNode)
        {
            if (currentNode != null)
            {
                InsertRecursively(node, currentNode.Next, currentNode);
                return;
            }

            currentNode = node;
            previousNode.Next = currentNode;
        }

        private void InsertIteratively(Node node)
        {
            if (_head == null)
            {
                _head = node;
                return;
            }

            Node currentNode = _head;

            while (currentNode.Next != null)
            {
                currentNode = currentNode.Next;
            }

            currentNode.Next = node;
        }

        public bool Delete(Node node)
        {
            return true;
        }

        public Node Find(T element)
        {
            foreach (var item in this)
            {
                if (element.Equals(item.Data))
                    return item;
            }
            return null;
        }

        public Node Find(Node node)
        {
            foreach (var element in this)
            {
                if (node.Data.Equals(element.Data))
                    return element;
            }
            return null;
        }

        public void InsertAfter(T data, Node node)
        {
        }

        public void InsertBefore(T data, Node node)
        {
        }

        public bool Replace(T data, Node node)
        {
            return true;
        }

        public bool Contains(T item)
        {
            return Contains(new Node(item));
        }
    }
}

MinHeap Implementation via C#

Categories: Algorithms, Programming
Tags:
Comments: No Comments
Published on: July 1, 2011

Heap data structure, Binary Heap, is quite useful to keep the min and max access in constant time,O(1). Moreover, Binary heaps can be used to implement priority queues.

Binary heap
implementation is quite easy. Below is the code for MinHeap . I need to add the HeapSort which is an efficient comparison based algorithm that takes O(nlogn) for sorting a sequence, Heap sort can be done in place. Basic idea behind Heap sort is to remove the minimum, then take the right most leaf (which is the last element in the array, due to the nature of this structure, which should be a complete tree) and then call heapify method each time after removing the min, removing n nodes takes O(n) time and for each node, we need to call heapify method which cost O(logn), hence overall running time of this algorithm is O(nlogn).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FiratAtagun.DataStructures.Heap
{
    public class MinHeap : IEnumerable
    {

        private int[] _collection;
        private int _currentCapacity;
        private const int DefaultCapacity = 20;
        private int _index = 0;

        public MinHeap()
        {
            _collection = new int[DefaultCapacity];
            _currentCapacity = DefaultCapacity;
        }

        public int GetParent(int i, out int index)
        {
            double element = i;
            index = (int)Math.Floor((element - 1)  / 2);

            if(index < 0)
            {
                return 0;
            }

            return _collection[index];
        }

        public int GetLeftChild(int i, out int index)
        {
            index = (2 * i) + 1;
            return _collection[index];
        }

        public int GetRightChild(int i, out int index)
        {
            index = (2 * i) + 2;
            return _collection[index];
        }

        public void Insert(int i)
        {
            _collection[_index] = i;

            HeapifyUp(_index);
            _index++;
        }

        private int _currentIndex;

        private void HeapifyUp(int element)
        {
            int parentIndex;

            int parent = GetParent(element, out parentIndex);

            if (_collection[element] < parent)
            {
                Swap(ref _collection[parentIndex], ref _collection[element]);

            }
            else
            {
                return;
            }

            // Swapped, current element is at parentIndex now, and _index is the parent of the element

            HeapifyUp(parentIndex);

        }

        public void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        public int Count
        {
            get { return _index - 1; }
        }

        public int Minimum
        {
            get { return _collection[0]; }
        }

        public int ExtractMinimum()
        {
            int minimum = _collection[0];

            _collection[0] = _collection[--_index];
            _collection[_index] = default(int);

            Heapify();

            return minimum;
        }

        public void Heapify()
        {
            _currentIndex = 0;
            Heapify(_currentIndex);
        }

        public void Heapify(int index)
        {

            int leftChildIndex;
            int rightChildIndex;

            int leftChild = GetLeftChild(index, out leftChildIndex);
            int rightChild = GetRightChild(index, out rightChildIndex);

            if(leftChild == 0 && rightChild == 0)
            {
                return;
            }

            int replacingElement = 0;

            if (leftChild < rightChild)
            {
                replacingElement = leftChildIndex;
            }
            if (leftChild > rightChild)
            {
                replacingElement = rightChildIndex;
            }

            if (leftChild == rightChild)
            {
                replacingElement = leftChildIndex;
            }

            Swap(ref _collection[index], ref _collection[replacingElement]);

            Heapify(replacingElement);
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            foreach (var i in _collection)
            {
                yield return i;
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}

Equilibrium index of an array

Categories: Algorithms, Programming
Tags: No Tags
Comments: No Comments
Published on: June 30, 2011

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A:
Median of two sorted arrays

A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0

3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4] + A[5]=0

7 is not an equilibrium index, because it is not a valid index of array A.

Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.

Solution : Iterate through the array and find the sum of all elements, lets call it WholeSum. Then start iterating from left to right, while iterating store the addition of the elements in another variable called left sum. From WholeSum we subtract LeftSum and we get RightSum, if LeftSum is equal to RightSum, that s out Equilibrium index of the array. It takes O(n).

page 1 of 12»

Welcome , today is Tuesday, February 7, 2012

Bad Behavior has blocked 250 access attempts in the last 7 days.