Sunday 30 June 2013

differences between Graphs and Trees? [excluded Graphical Concept]

Topic
Graph
Tree


Rules & Restriction:
In general graph doesn’t really have any rules and restrictions.
Tree is the restricted specialized version of graph with numerous rules specially how to connect nodes.

Direction :
Graph can have Uni-directional or Bi-directional paths between nodes.
Tree has linear direction (only one path between each two vertices) that comes along with “Parent-children” relationship.

*point to be noted: one child can have only one parent (as it is in real.)

Circles and all :
Can be cyclic (or acyclic) or have loop, circuit and self loop.
Tree doesn't have any circles back or self loop or circuit or cycles.

Recursive Data :
No such thing.
Tree is Recursive Data Structure due to the nodes with children.

DAG:
Can only be cyclic or acyclic
Tree fits within the category of Directed Acyclic Graphs in short DAG (graph that has no cycles).

Complexity:
Graphs are more complex due to all the loops and cycles it got.

Trees are really easy to understand.
Connection:
In graph objects connects themselves by Links.

*in mathematical abstractions it calls Vertices or in between same pair it calls edges.

Tree is actually Minimally connected graph. In easy English its: one path between each two vertices.
Outlook:
It’s more likely a diagrammatic overview.
It’s more likely a flow looking overview.
(e.g. flow chart)

Root node:
No such thing.
Exactly only one root node.

Parent child relation:
No such thing.
In tree, there is parent child relationship for the sake of flow so there can be the direction top to bottom or vice versa.

Traversal kinds:
Graph traversed by DFS (depth First Search) and BFS (Breadth First Search) algorithm.
3 types:
a. Pre order
b. In order
c. post order
*all of these are in DFS or BFS algo.

Different types:
Basically only two types can be highlighted:
a. Directed graph
b. Undirected graph

There are actually too many. Like, Binary tree, Binary Search Tree, AVL tree, Heaps etc.
Number of edges:
No boundary.
Always have to have n-1 numbers of edges.

Model type:
Network model.
Hierarchical model




Wednesday 26 June 2013

what is Stack and Queue? [ simplest yet every detail]

Stack:
a list of elements where insertion (push) or deletion (pop) is and can only be done at one same end.
the bookish name of it is LIFO. stands for Last In First Out.

special terminology:

  1. push: to insert any element into the stack.
  2. pop  ; to delete any element from the stack.

example:
its like a cookie jar. Top most cookie in the jar is only accessible.

application:
the whole "UNDO" concept or the thing we do by pressing CTRL+ Z is done by stack, or like in Maze puzzles.



Queue;
linear list of elements where insertion (rear)  and deletion (front) can only be done by different ends.
bookish name of it is FIFO, stands for First In First Out. queue has head and tail.

special terminology:

  1. enqueue (rear) : to operate insertion operation.
  2. dequeue (front): to operate deletion operation.

example:
like people waiting in a line for movie tickets, new ( or last) person will stand at the end of the line every time. or like a fancy restaurant where i can enter and get out using 2 different way, and can't mess it up :P .

application:
like in BFS( breadth first search) or DFS( deapth first search).

Algorithm: Pseudo-code to find the Smallest element form a list of items stored in a linear array.

a nonempty array DATA with N numerical values is given. an algorithm to find the location LOC and the value MIN of the smallest element of array DATA. a variable K is gonna be use as counter.

  1. set K :=1, LOC :=1 and MIN :=DATA[0].    [ initialization ]
  2. While K<=N, repeat step 3
  3. IF MIN > DATA[K], then set LOC :=K and MIN :=DATA[K]                                         ELSE K :=K+1.
  4. Write: LOC, MIN
  5. Exit.

difference between Algorithm, Pseudo-code and Program? [ simple and short but yet everything ]

algorithm: ( set of logic statement/method ) :
is a detailed sequences of steps from beginning to end to carry out the whole process.
logic or the basic plan of the action, behind of getting something done.it has rules to follow.

pseudo-code: ( function of describing/outlining the algorithm ) :
pseudo code is kinda like "intermediary" between algorithm and implemented program.
algorithm is the semantic of the process while pseudo code is the syntax to communication sake to solve any problem or do any task. no rules

program:
a detailed set of instruction for a machine (in this case the computer itself) to carry out the task.
its the real implement of computer algorithms. program start working after when the  algorithm is done making.


in short:
algorithm is the "skeleton" of the plan/task and program  is like "giving it it's shape" and pseudo code is telling the program how the algorithm wants the plan/ task to turn out to be.

among these three PSEUDO-CODE is the most easiest for human brain to understand or to make.

Tuesday 25 June 2013

what is Doubly and Circular linked list? [ simple explanation ]

doubly linked list:
linked list containing nodes that carries two pointer one (left side one) for the previous node and the other for (right side one) the next node. so Doubly liked list's node carries 3 field.
  1. link to point the previous node
  2. integer value carrying field 
  3. link to point the next node.
this picture might help to understand :






circular linked list:
in short, linked list of which 1st node's link is stored in the link part of the last node.


little help with a diagram:


heads up:
non circular is mostly known as acyclic 

Algorithm: Pseudo-code to find a particular item form a list of items stored in a linear array.

a nonempty linear array DATA with N elements in it and a ITEM of information / element are given. a linear search algorithm is to find the ITEM in DATA and if so then location LOC of ITEM in DATA to print out.

  1. set K :=1 and LOC:=0. [ initialize ]
  2. while LOC=0 and K<=N, repeat step 3 and 4.
  3. if ITEM = DATA[K], then set LOC :=K.
  4. set K:=K+1. [increment counter ] [end of step 2 loop ]
  5. If : LOC=0 then:  Write: ITEM isn't in the array DATA.                                                       Else: Write: LOC is the location of ITEM..
  6. Exit.



differences between Array and Linked list? [with simple explanation ]

topics
Array
Linked list

1.continuity
Array has continuous memory.
Linked list may or may not be continuous. It’s not really necessary for any linked list to fulfill continuity condition.

2.insertion/
deletion
Quite difficult.
Pretty much easier. Easy insertion and deletion is sort of the main base on why linked list concept was built.

3.space wastage
Spaces get to waste quite often.
There actually no chance for it.


4.expensity
Comparatively expensive.
Less expensive.

5.size accuracy
Size in array is always fixed.
Not fixed. (unless anything explicitly announced.)

6.extra memory requirement
Extra memory doesn’t really needed.
Linked list stores bit extra memory due to the pointer that comes along at the end of its’ nodes.

7.space expansion
Can’t be extended or reduced.
Linked list is quite flexible in this case.

8.access timing
Same amount of time takes to access each element.


Different amount of time takes to access each element.

9.accessing point
Directly reachable to the exact element.
Have to go through all the nodes come on the way to go the certain node’s information.

10.movement requirement
Needs movement of elements for insertion and deletion.

No needed.
11. space requirement
Requires less space as only the information shall be stored.
Comparatively takes more space as the pointers are stored along with every information.

what is two-dimensional [ 2D ] and three dimensional [ 3D ] array [and subscript] ?

in short that array where elements are referenced by respectively by Two and Three subscripts.


*subscript:
in basic programming language, subscript is like a linear array inside a non-linear array.




Difference between pass by Value and pass by Reference? [ simple explanation both in English and Alien accent :P +tip ]

alright about this, 2 explanation helped a lot. i'm gonna post both of em.


a. in english:
lets say i kinda want to share a web page with you.
now if i give you the URL of the web site, you can use that URL and see the very same web page from your home pc, that i can see it from mine home pc. now if you choose to delete the URL the thing you actually doing is destroying the reference to that web page, but the page is still there and i still can have access to it.
so this will be passing by Reference.

now how about if i take a print out of that very same page and give it to you, then as you have guessed it will be passing by Value. now you only have  isolated copy of the original 'thing', you wont be seeing any subsequent changes if i make after that little gift incident. and vise versa, any change you make on your print out copy wont change the original copy. now destroying or deleting your copy only will delete a copy version of the original 'thing',original web page shall remain intact.



b.this one is in alien language:
pass by value is using a copy of the original value stored in another memory. like allocate some more memory Then copy the value in that memory and Then use that exact memory through the way of the certain function / method. original variable aint affected by the function at all.

now about pass by reference is using the actual memory storing that value. if function changes the value then that change is immediate and preserved when the function exits / executed.



in short : pass by Reference is like having a phone number of someone, and pass by Value is like having a photo of that certain someone.

Tip:
pasing by Reference makes a faster program execution.

source code: Polish notation validity check ( parenthesis check ) using C++ ( stack) [ the most simplest ]

#include<iostream>
using namespace std;

char a[100], i=0;


void push(char e)

{
a[++i]=e;
}

void pop()

{
i--;
}

int main()

{ cout<<"assign the equation: ";
char b[100];
gets(b);


for(int x=0; b[x]; x++) 
{
if(b[x]=='{' || b[x]=='(' || b[x]=='[') {push(b[x]);}

else if(b[x]==')' && a[i]=='(') {pop();}


else if(b[x]=='}' && a[i]=='{') {pop();}


else if(b[x]==']' && a[i]=='[') {pop();}

}


if(i==0) {cout<<"\nah, good this a Valid one !!\n\n";}


else {cout<<"\nahah no luck kiddo! this aint a Valid one\n\n";}
}

source code: Pass by Value and Pass by Reference (pointer array) using C++ [ shown either way ;) ]

#include<iostream>
using namespace std;

class ptrcls{
public:
ptrcls()
{
int length;
cout <<"length of the array?\nans:"; cin>> length;

int numbers[100];

cout <<"\nfill up the array:";
for(int x=0; x<length; x++)
{
cin>> numbers[x];
}

int* ptr; 
// Declaring a pointer with the same data type as integar array.
   
ptr= numbers;
// Assigning the numbers' address of array to pointer, no need of "&" before the numbers
// since the "numbers" array already starts at the first memory location.

cout << "\nPassing an array using a pointer as reference:\n";
cout <<"ptr: ";
for(int i=0; i<length; i++)
{
cout <<*ptr <<", ";
ptr++;
}
cout <<endl;

cout << "\nPassing an array using the subcript operator:\n";
for(int i=0; i<length; i++)
{
cout << numbers[i] <<", ";
}
}
};

int main()
{
ptrcls obj;
cout <<endl;
}

Monday 24 June 2013

What is the difference between Local, Non-Local and Global variables? [ in brief + short tips ]

local variables:
local variables are the one whose scope is only till the end of the function/method.

global variables:
global variables are the one whose scope is through the way to the end of the program.

non-local:
technically global and non-local variables are same.

Tips:
global or local variable of any type can't be both  at the same time, but they sure can share the same name. it wont cause any trouble, almost all languages, it's legal but still better to avoid. but there is a 'but'. if a global and a local variable shares the same name, the local one will hide/omit the global.

source code: Multidimensional [2D] Array using C++ [ the most easiest, visual friendly and user friendly as always]

#include<iostream>
using namespace std;

void display() 

{
int r,c; int ary[100][100];

cout <<"row ?\nans:"; cin>> r; cout <<endl;

cout <<"coulmn ?\nans:"; cin>> c; cout <<endl;

cout <<"put the numbers and keep pressing ENTER afterwards:\n";
for (int x=0; x<r; x++)
{
for (int y=0; y<c; y++)
cin>> ary[x][y];
}

cout <<endl;


cout <<"your matrix:\n";

for (int x=0; x<r; x++)
{
for (int y=0; y<c; y++)
{
cout<<ary[x][y] <<" ";
}
cout<<endl;
}

}

int main()

{
display();
}

Saturday 22 June 2013

How to calculate log base 2 using a scientific calculator?

Our scientific calculator actually has 2 buttons are for Logarithm calculation, [see the photo]

1. "log" button: is to calculate log base 10 (known as Common Logarithm)
2. "ln" button: is to calculate log base e (known as Natural Logarithm)



Now to calculate log base 2, you can use any of these two, just that you will need to convert it into base 2.

You can do this by dividing your result by the "log" or "ln" of base 2. 

Let me give an example for each case.

Example:
If you to calculate log(x) base 2, then calculate log(x) base 10 then divide it by log2.
Or,
to calculate ln(x) base 2, do it with base 10 then divide it by ln2.

Calculation example:
log8 base 2 is 3. using calculator is should be log8 base 10 / log2.


Thank you :)
by Hasin Arefin Khan

Thursday 20 June 2013

source code: Binary search using C++ [the most easiest way and user friendly as well]



#include<iostream>

 using namespace std;

int main()

{
   int  n, search, ary[100];

   cout<<"Enter number of elements: "; cin >>n;


   int beg= 0, end= n - 1, mid= (beg+end)/2;


   cout<<"Enter the elements: ";


   for (int x=0; x<n; x++)

      cin >>ary[x];

   cout<<"Enter the integer to find: "; cin>>search;



   while(beg <= end)

   {
      if (ary[mid] == search)
  {
 cout<<search <<" found at location:" <<mid+1 <<endl;
         break;
  }
           
      else if(ary[mid] < search)
      {
  beg= mid + 1;
       
      }
      else
         end= mid - 1;

      mid= (beg + end)/2;

   }
   if (beg > end)
      cout <<"no luck kid !";

   return 0;  

}

Wednesday 19 June 2013

source code: Linear search using C++ [easy and user friendly]


#include <iostream>

using namespace std;

class lin

{
public:
void ls()
{
int ary[100],c,search,number;
cout <<"Enter the number of elements in array you want:"; cin>>number;

cout <<"Input " <<number <<" numbers in the array:" <<endl;

for(c=0; c<number; c++)
{
cin >> ary[c];
}
cout << "Enter the number what to search" <<endl; cin >> search;

for(c=0; c<number; c++)

{
if(ary[c]==search)
{
cout <<search<<" is present at location "<<++c<<endl; break;
}
else if(c==number)
{
cout<<search<<" is not present in array.\n";
}
}

}
};

int main()
{
lin a;
a.ls();
}

source code: Boolean Flag using C++ (total / average price) [easy and user friendly]


#include <iostream>
using namespace std;

int main (){

bool proceed= true;
    int items= 0,p;
    double price, totalPrice=0;
    cout<<"Program To Calculate and show the Average and Total Price Of Items \n\n";

    while(proceed)

    {
        cout<<"Enter price of item: "; cin>>price;
        totalPrice += price;
        items++;


        cout<<"Anymore item? Enter 1 for yes OR 0 for no: "; cin>>p;


try

{
if(p>1)
{
throw 1;
}else{proceed=p;}
}
catch(...)
{
cout<<"aA ah, i said 1 for YES and 0 for NO, OONNLLYY !!\n\nAnymore item? Enter 1 for yes OR 0 for no: "; cin>>p;
}

    }

    cout<<"\n\nNumber of items: "<<items <<"\nThe Average Price Of The Items Is "<<totalPrice/items;

    cout<<"\nThe Total Price of the Items Is "<<totalPrice <<"\n\nthank you for using this software :)\n\n";

}


source code: Bubble sort using C++ [easiest and user friendly]


#include <iostream>

using namespace std;


void bubbleSort(int *array,int length)
{
    int i,j;
    for(i=0; i<length; i++)
    {
        for(j=0; j<i; j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i];
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
cout<<"bubble sorted look of you array:";
for(i=0;i<length;i++)
        cout<<array[i]<<' ';
cout<<endl;
}

int main()

{
int length; int a[10];
cout<<"how long array you want:\nans: ";
cin>>length;
 
cout<<"\ninput the elements and hit ENTER afterwards:\nans: \n";

for(int x=0; x<length; x++)
cin>> a[x];

cout<<"the array you just entered:\n";

for(int x=0; x<length; x++)
cout<< a[x]<<' ';
cout<<endl;



    bubbleSort(a, length);

}