Sunday, 26 February 2017

Space Complexity, Time Complexity of Algorithms




Space Complexity:

The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity.
Auxiliary Space is the extra space or temporary space used by an algorithm.
Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input.
For example, if we want to compare standard sorting algorithms on the basis of space, then Auxiliary Space would be a better criteria than Space Complexity. Merge Sort uses O(n) auxiliary space, Insertion sort and Heap Sort use O(1) auxiliary space. Space complexity of all these sorting algorithms is O(n) though.

Time Complexity of Algorithms

Time complexity of an algorithm signifies the total time required by the program to run to completion. The time complexity of algorithms is most commonly expressed using the big O notation.
Time Complexity is most commonly estimated by counting the number of elementary functions performed by the algorithm. And since the algorithm's performance may vary with different types of input data, hence for an algorithm we usually use the worst-case Time complexity of an algorithm because that is the maximum time taken for any input size.

Calculating Time Complexity

Now lets tap onto the next big topic related to Time complexity, which is How to Calculate Time Complexity. It becomes very confusing some times, but we will try to explain it in the simplest way.
Now the most common metric for calculating time complexity is Big O notation. This removes all constant factors so that the running time can be estimated in relation to N, as N approaches infinity. In general you can think of it like this :
statement;
Above we have a single statement. Its Time Complexity will be Constant. The running time of the statement will not change in relation to N.

for(i=0; i < N; i++)
{
  statement;
}
The time complexity for the above algorithm will be Linear. The running time of the loop is directly proportional to N. When N doubles, so does the running time.

for(i=0; i < N; i++) 
{
  for(j=0; j < N;j++)
  { 
    statement;
  }
}
This time, the time complexity for the above code will be Quadratic. The running time of the two loops is proportional to the square of N. When N doubles, the running time increases by N * N.

while(low <= high) 
{
  mid = (low + high) / 2;
  if (target < list[mid])
    high = mid - 1;
  else if (target > list[mid])
    low = mid + 1;
  else break;
}
This is an algorithm to break a set of numbers into halves, to search a particular field(we will study this in detail later). Now, this algorithm will have a Logarithmic Time Complexity. The running time of the algorithm is proportional to the number of times N can be divided by 2(N is high-low here). This is because the algorithm divides the working area in half with each iteration.

void quicksort(int list[], int left, int right)
{
  int pivot = partition(list, left, right);
  quicksort(list, left, pivot - 1);
  quicksort(list, pivot + 1, right);
}
Taking the previous algorithm forward, above we have a small logic of Quick Sort(we will study this in detail later). Now in Quick Sort, we divide the list into halves every time, but we repeat the iteration N times(where N is the size of list). Hence time complexity will be N*log( N ). The running time consists of N loops (iterative or recursive) that are logarithmic, thus the algorithm is a combination of linear and logarithmic.
NOTE : In general, doing something with every item in one dimension is linear, doing something with every item in two dimensions is quadratic, and dividing the working area in half is logarithmic.

Types of Notations for Time Complexity

Now we will discuss and understand the various notations used for Time Complexity.
  1. Big Oh denotes "fewer than or the same as" <expression> iterations.
  2. Big Omega denotes "more than or the same as" <expression> iterations.
  3. Big Theta denotes "the same as" <expression> iterations.
  4. Little Oh denotes "fewer than" <expression> iterations.
  5. Little Omega denotes "more than" <expression> iterations.

Understanding Notations of Time Complexity with Example

O(expression) is the set of functions that grow slower than or at the same rate as expression.
Omega(expression) is the set of functions that grow faster than or at the same rate as expression.
Theta(expression) consist of all the functions that lie in both O(expression) and Omega(expression).
Suppose you've calculated that an algorithm takes f(n) operations, where,
f(n) = 3*n^2 + 2*n + 4.   // n^2 means square of n
Since this polynomial grows at the same rate as n^2, then you could say that the function f lies in the set Theta(n^2). (It also lies in the sets O(n^2) and Omega(n^2) for the same reason.)
The simplest explanation is, because Theta denotes the same as the expression. Hence, as f(n) grows by a factor of n^2, the time complexity can be best represented as Theta(n^2).






Saturday, 25 February 2017

Algorithms for circular header linked list

Algorithm for traversing circular header linked list

Algorithm for Searching operation in circular header linked list

Algorithm for Deletion operation in circular header linked list


Circular list with TAIL pointer

Circular list with TAIL pointer

In single linked list problem occurs when we want to insert node at the last of the list to solve this problem alternative is maintain a TAIL pointer instead of head pointer .This TAIL pointer  points to the last node of the list using this pointer we can directly access the last node without traversing the whole list.


Header Linked List

Header Linked List
Definition:
 A header linked list is linked list that always contain a special note at the front of the list, this special node is called headed node. It does not contain actual data item included in the list but usually contains some useful information about the entire linked list.

Such as
  • ·      Total number of nodes in the list .
  • ·      pointer  to the last node in the list or to the last node accessed the header node in the list is never deleted that always point to the first node in the list.

 Widely used types of header linked list are:

1.A Grounded Header list is the header list where the last node contains the null pointer . grounded comes from the fact that many text use the electrical ground symbol to indicate the null pointer.
  • ·      Location Of first node is written as:

        PTRàLINK(HEAD)
        Where as in singly linked list we write it as PTRàHEAD
  • ·      Condition to check empty header linked list:

         LINK(HEAD)=NULL





 2. circular header list is a header list where the last more points back to the header node .the term node by itself normally refer to an ordinary node not  the header node, when used with header list. Thus the first node in header is the node following the header node and the list.
location of the first node is:
  • ·      Location Of first node is written as:

        PTRàLINK(HEAD)
  • ·      Condition to check empty Circular header linked list:

           LINK(HEAD)=HEAD

                          





Advantage: the main advantage of using head and node in a linked list if that you can avoid the special testing case while inserting and deleting nodes from the linked list.

  • The special cases inserting or deleting a node either at or from the beginning of the list or from the empty list. In single linked list special cases were handed separately.
  • As header linked list always contains at least one node insertion and deletion to take place only after the head and the special cases of inserting for deletion at the front will never occur to perform the operation on a header linked list.
  • While travelling in order to point to the first we have to use the statement similarly by inserting or deleting a node from sorted header linked list we need to maintain pointer.

Introduction to Circular linked list

Introduction to Circular linked list

Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list.

In singly linked list last node contains null pointer. In single linked list each node has a unique predecessor and each node has a unique successive. the main drawback of a singly linked list is that from a given node we can access all the nodes that follow it but not the one proceeding to it to solve this problem we make a slight modification in singly linked list by replacing the null pointer in the last node of the list with the address of the first node.Such  linked list is called Circular List.

Friday, 24 February 2017

Searching operation on header linked list


Searching operation on header linked list 

Algorithm:SEARCH(HEAD,ITEM)- given a non empty unsorted header linked list LIST in memory and address of the first node is stored in LINK(HEAD) .this algorithm find and returns the location LOC of the note where item is first appeared in the list if search  is successful or set Loc = Null if  search is unsuccessful .here  LOC is a local variable
1. LOC àNULL
2. PTRàLINK(HEAD)
[ search for item ]
3. repeat step 4  for while PTR≠ NULL and ITEM ≠INFO(PTR)
4. PTRàLINK(PTR)
[ end of step 3 loop]
5.  if ITEM =INFO(PTR) then [successful]
PTRàLOC
[end of if structure]

6. Return LOC



Deletion operation on header linked list

Algorithm:DELETE (HEAD ,AVAIL, LOC):- given list be a header linked list in memory and LINK(HEAD) pointer pointing to its first node this algorithm delete a node pointed by LOC where we are using local variable PTR to find the desired node to be deleted and PREVLOC  to keep track of its predecessor . The local variable AVAIL point to the first node of the free storage list.

      [ empty list?]
1.  if LINK(HEAD) = NULL then
 write “underflow in linked list”
 return PTR
[end of if structure]
2. PTRàLINK(HEAD)
[ set PTR to LINK(HEAD) i.e. first node of header linked list and PREVLOC to head ]
PREVLOC àHEAD
3. [ find location so as to find predecessors location that is PREVLOC]
 Repeat while PTR≠ LOC and  LINK (PTR)≠ NULL
[update pointers]
a) PREVLOC àPTR
b) PTR àLINK(PTR)
[end of step 3 loop]
4. If PTR≠LOC
 then write “ node not found”
 return
[end of if structure]
5. [ delete node appointed by LOC]
If PREVLOC=NULL  then
[deleting first node]
LINK(HEAD)àLINK(PTR)
else
LINK(PREVLOC) àLINK(LOC)
[end of its structure]
6. [ Return deleted notes to free storage list]
a)LINK(LOC) àLINK(AVAIL)[here AVAIL is also a headerd linked list of available nodes]
b)LINK(AVAIL) àLOC
7. Return

Header Linked List


 


Header Linked List
Definition:
 A header linked list is linked list that always contain a special note at the front of the list, this special node is called headed node. It does not contain actual data item included in the list but usually contains some useful information about the entire linked list.

Such as
  • ·      Total number of nodes in the list .
  • ·      pointer  to the last node in the list or to the last node accessed the header node in the list is never deleted that always point to the first node in the list.

 Widely used types of header linked list are:

1.A Grounded Header list is the header list where the last node contains the null pointer . grounded comes from the fact that many text use the electrical ground symbol to indicate the null pointer.
  • ·      Location Of first node is written as:

        PTRàLINK(HEAD)
        Where as in singly linked list we write it as PTRàHEAD
  • ·      Condition to check empty header linked list:

         LINK(HEAD)=NULL



 


 2. circular header list is a header list where the last more points back to the header node .the term node by itself normally refer to an ordinary node not  the header node, when used with header list. Thus the first node in header is the node following the header node and the list.
location of the first node is:
  • ·      Location Of first node is written as:

        PTRàLINK(HEAD)
  • ·      Condition to check empty Circular header linked list:

           LINK(HEAD)=HEAD

                          





Advantage: the main advantage of using head and node in a linked list if that you can avoid the special testing case while inserting and deleting nodes from the linked list.

  • The special cases inserting or deleting a node either at or from the beginning of the list or from the empty list. In single linked list special cases were handed separately.
  • As header linked list always contains at least one node insertion and deletion to take place only after the head and the special cases of inserting for deletion at the front will never occur to perform the operation on a header linked list.
  • While travelling in order to point to the first we have to use the statement similarly by inserting or deleting a node from sorted header linked list we need to maintain pointer.