Sunday, 18 August 2013

time complexity: definition of Big O notation O(n)[ linear, constant, logarithmic,quadratic], Big Omega notation Ω(n) and Big Theta notation Θ(n)

if you are here for a quick lesson, you can just go through this para rather than the whole post,but still i believe you do know what is best for you:

  • best-case runtime        : O(1) [ shortest and probably most accurate]
  • average-case runtime  : O(n/2)=O(n) [expected value of the function in most cases]
  • worst-case runtime      : O(n)  [maximum value of the function for any possible input]
  • O(n) = log2(n) grows mostly the slowest
  • exponential function 2n grows most rapidly
  • polynomial function ngrows just according to its exponent c


->Big O notation  [a.k.a O(n) ] :
talking of computer science, describing  any sort of run time or required time its a common way for. in bookish way, it describes the growth of several different cases of orders for different algorithms.

{**not necessary for newbies: as like in monotonic function for f(n) and g(n). from any positive integer to another positive integer it can be said that f(n) = O(g(n)). [where constants,  c>0 & n0>0, so that
f(n) ≤ c * g(n), for all n ≥ n0] this means f(n) can't get to grow faster than g(n) or in other way g(n) indicates the upper bound for f(n) [ when n is way too large, n→∞ ]  }

dramatic explanation: 

for f(n) = O(g(n)) relation -

example: 

you can easily prove n2 + 2 n + 1 = O(n2). all you have to do is to  find c and n0,that:
n 2 + 2 n + 1 ≤ c*n2
Let n0=1, then for n ≥ 1
1 + 2 n + n2 ≤ n + 2 n + n2 ≤ n2 + 2 n2 + n 2 = 4 n2
Therefore, c = 4.

**heads up: big O notation isn't symmetric, i.e.  n = O(n2) but n2 ≠ O(n)


Kinds:


a. linear time : T(n) = O(n):    if the algorithm's execution is directly proportional to the input size. it means if the time grows linearly as input size grows bigger.

examples:
  • in array like, Linear Search, Traverse, Minimum finding
  • in arraylist like, Contains method
  • in queue like, Contains method

b. fixed (constant) time:   T(n) = O(1):   if any algorithm regarding of its input taking size, runs in a constant time, maintains O(1) notational time complexity.
examples:
  • fixed size stack(e.g. push, pop methods)  or queue (e.g. enqueue, dequeue methods)
  • accession to any element of the array

c. Logarithmic Time: T(n) = O(log n):    when the time execution is proportional to its input size logarithm.
examples:
  • binary search
  • guessing game / 20 questions game / "guess-what-number-i-am-thinking" game
**heads up:   always log(n) < n, but in case of n -> ∞, O( log n ) won't use the whole input of the algorithm it runs in

d. Quadratic Time: T(n) = O( n):   time execution is proportional to the square of the size of its input.

examples:
  • insertion sort
  • bubble sort
  • selection sort


->Big Omega notation  [a.k.a Ω(n) ] :
Omega or more precisely Capital Omega notation is use for indicating lower bound. its like  f(n) = Ω( g(n) ) when there exist constant c that f(n) ≥ c*g(n) for for all sufficiently large n.

if it seems hard to get, you actually need it for making better algorithm skills, so you can just memorize the example i've given bellow for now:
examples:
  • n = Ω(1)
  • n2 = Ω(n)
  • n2 = Ω( n log(n) )

->Big Theta notation  [a.k.a Θ(n) ] :
big Theta can be used for both upper and lower bounds. its like f(n) = Θ( g(n) ) if and only f(n) = O( g(n) ) and f(n) = Ω( g(n) )

same goes to big Theta as big Omega, you can just memorize it for a quick start.
example:
  • 2n = Θ(n)
  • n2 + 2 n + 1 = Θ( n2)

thank you :)


Friday, 16 August 2013

Google's feature: How to Set Timer in Google?

Google features a cool feature call set timer. pretty much same as our cell phone, or stopwatch offers.

diagram:



 try it ->
  • go to Google
  • write " set timer N minute" [ N= number of time u wish to set ]
  • hit "ENTER"
or just click here


Thursday, 8 August 2013

Source code: Conversion of ASCII Value to Character and Character to ASCII Value using C++ [ user friendly, easy, object oriented ]

#include <iostream>
using namespace std;

void a2c()
{
int ch;
cout <<"enter the ASCII value: ";
cin>> ch;
cout <<"\ncharacter: "<< static_cast<char>(ch)<<endl<<endl;
}

void c2a()
{
char ing;
cout <<"\n\n\nenter the character: ";
cin>> ing;
cout <<"\nASCII: " <<static_cast<int>(ing)<<endl<<endl;
}

int main()
{
char c;
cout <<"pick what you want: \n";
cout <<"a. ASCII to Character\n";
cout <<"b. Character to ASCII\npick: ";
cin>> c;

switch(c)
{
case 'a': a2c(); break;
case 'b': c2a(); break;
}
}

Wednesday, 7 August 2013

source code: Prime number check using C++ [ user friendly, object oriented, easy likewise forever]

#include<iostream>
using namespace std;

void prime()
{
int x, n= 3, tick, c;
cout <<"number of prime numbers needed: "; cin>> x;

if( x >= 1 )
{
cout <<"\nFirst " <<x <<" prime numbers:\n" <<2 <<endl;
}

for(tick= 2; tick<= x;)
{
for(c= 2; c<= n -1; c++)
{
if (n%c == 0) break;
}

if (c == n)
{
cout <<n <<endl;
tick++;
}
n++;
}
}

int main()
{
prime();
return 0;
}

Sunday, 28 July 2013

Binary Tree representation using Array:

a Binary Tree T can be represented by Array in 2 different way;
  1. Sequential representation (via singular linear array)
  2. Linked representation


Sequential representation:
if T is a complete Binary tree, then its-
  • root R will be stored in array element T[0]
  • if K number of node occupies, will be stored in array element T[K]
  • left child will be stored in array element T[ 2*K ]
  • right child will be stored in array element T [ 2*K+1 ]
** if T[ 1 ] = NULL, then it is an empty Tree.


diagrammatic explanation:










Linked representation:
if K is the correspond  location pointer :
  • need 3 parallel array INFO, LEFT and RIGHT
  • data at node will be stored in array element INFO[K]
  • location of left child of node N will be stored in array element LEFT[ K ]
  • location of right child of node N will be stored in array element RIGHT [ K ]
  • root R of T will be stored in ROOT
diagrammatic explanation:





various standard ways of Binary Tree Traversal:

3 standard ways are out there for traversing a Binary Tree. these ways or ( you may call it Algorithms) are given billow:
  • PreOrder
  • InOrder
  • PostOrder

working process: if the tree is T with root R:

PreOrder:
  • process the root R
  • traverse the Left SubTree of R in PreOrder
  • traverse the Right SubTree of R in PreOrder
InOrder:
  •  traverse the Left SubTree of R in InOrder
  •  process the root R
  •  traverse the Right SubTree of R in InOrder
PostOrder:
  • traverse the Left SubTree of R in PostOrder
  • traverse the Right SubTree of R in PostOrder
  • process the root R

example:
lets consider an  mathematical equation 
       [ a + ( b - c ) ] * [ ( d - e ) / ( f + g -h ) ]
now the orders will be like these:
PreOrder   :        * + a - b c / - d e - + f g h 
InOrder     :        a + b - c * d - e / f  + g - h
PostOrder  :        a b c - + d e - f g + h - / * 



Saturday, 27 July 2013

difference between Full Binary Tree and Complete Binary Tree

topics
Full Binary Tree
Complete Binary Tree
1.node assemble
it's a regular binary tree with no odd number of leaves.
Every node or level of a tree is completely filled exception can happen only in the last level.

 2.last node
Last and each node must have either 0 (zero) or 2 (two) children.
Children on the last level node must start from the left most position with no spot left unoccupied in between any tow form the left counting.

3.application
 n/a
 Some specific data structure type (i.e. Heaps) need to be in Complete Binary Tree shape, when they need not to be a Full Binary Tree.

4.formula   
If one can find any of

  •  Number of Total Nodes or
  •  Number of Laves or
  •  Number of Internal Nodes
can find the other 2 at instant.

 In complete binary tree there is no relating connection among these 3 attributes.
5. leaf level
Not necessarily have to be in the same depth.
Tree must have the entire leaves node in the exact same depth.

6. diagram