Tuesday, June 18, 2013

PHP call by value and call by reference

<?php
function increment_value($y) {
    $y++;
    echo $y."<br/>";
    }

function increment_reference(&$y) {
    $y+=10;
    echo $y."<br/>";
    }
$x = 1;
increment_value($x); // prints '2'
echo $x."<br/>"; // prints '1'
increment_reference($x); // prints '2'
echo $x."<br/>"; // prints '2'
?>

Monday, April 1, 2013

Mind reader

I simply try to develop a little application using HTML/CSS/JavaScript and jQuery.Open it in a Web browser and Try it now.

Wednesday, December 19, 2012

Find shortest paths between all pairs of vertices in a graph.

Floyd-Warshall Algorithm
It is one of the easiest algorithms, and just involves simple dynamic programming.
The algorithm can be read from this wikipedia page.


#define SIZE 31
#define INF 1e8
double dis[SIZE][SIZE];
void init(int N)
{
for(k=0;k<N;k++)
for(i=0;i<N;i++)
dis[i][j]=INF;
}
void floyd_warshall(int N)
{
int i,j,k;
for(k=0;k<N;k++)
    for(i=0;i<N;i++)
        for (j=0;j<N;j++)
        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}

int main()
{
//input size N
init(N);
//set values for dis[i][j]
floyd_warshall(N);
}

Friday, August 24, 2012

টাইম কমপ্লেক্সিটি শিখতেই হবে

তাহলেই বুঝতে পারবেন আপনার অ্যালগোরিদমটি অন্যদের চেয়ে কতটা ভাল কাজ করে।
ইনপুটের সাপেক্ষে-
Order of Growth n Time (ms) Comment
O(1) 1000 1 Excellent, almost impossible for most cases
O(log n) 1000 9.96 Very good, example: Binary Search
O(n) 1000 1000 Normal, Linear time algorithm
O(n log n) 1000 9960 Average, this is usually found in sorting algorithm such as Quick sort
O(n^2) 1000 1000000 Slow
O(n^3) 1000 10^9 Slow, btw, All Pairs Shortest Paths algorithm: Floyd Warshall, is O(N^3)
O(2^n) 1000 2^1000 Poor, exponential growth… try to avoid this. Use Dynamic Programming if you can.
O(n!) 1000 uncountable Typical NP-Complete problems.
সময়ের সাপেক্ষে-
Order of Growth Time (ms) Max Possible n Comment
O(1) 60.000 ms Virtually infinite Best
O(log n) 60.000 ms 6^18.000 A very very large number
O(n) 60.000 ms 60.000 Still a very big number
O(n log n) 60.000 ms ~ 5.000 Moderate, average real life size
O(n^2) 60.000 ms 244 small
O(n^3) 60.000 ms 39 very small
O(2^n) 60.000 ms 16 avoid if you can
O(n!) 60.000 ms 8 extremely too small.
মনে রাখতে হবে কমপ্লেক্সিটির ক্রম- constant < log n < n < n log n < n^2 < n^3 < 2^n < n!

Monday, August 20, 2012

Merge sort

# include<stdio.h>
# include<iostream.h>
# include<conio.h>
#define max 5000
void MergeSort(int low, int high);
void Merge(int low, int mid , int high);
int a[max];
int b[max];
int main(void)
{
int i,n,high,low;
cout<<"How many element:";
cin>>n;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
high=n;
low=0;
for(i=1;i<=n;i++)
    {
    MergeSort(low,high);
    cout<<a[i]<<"\t";
    }
getch();
return 0;
}
void MergeSort(int low, int high)
{
int mid;
if(low<high)
    {
    mid=(low+high)/2;
    MergeSort(low, mid);
    MergeSort(mid+1,high);
    Merge(low, mid, high);
    }
}
void Merge(int low, int mid, int high)
{
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
    {
    if(a[h]<=a[j])
        {
        b[i]=a[h];
        h=h+1;
        }
    else
        {
        b[i]=a[j];
        j=j+1;
        }
    i=i+1;
    }
if(h>mid)
    {
    for(k=j; k<=high; k++)
        {
        b[i]=a[k];
        i=i+1;
        }
    }
else
    {
    for(k=h; k<=mid; k++)
        {
        b[i]=a[k];
        i=i+1;
        }
    }
for(k=low; k<=high; k++)
a[k]=b[k];
}