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];
}   

C data types and range

Monday, June 25, 2012

Character testing & conversion

int isalnum(int c) -- True if c is alphanumeric.
int isalpha(int c) -- True if c is a letter.
int isascii(int c) -- True if c is ASCII .
int iscntrl(int c) -- True if c is a control character.
int isdigit(int c) -- True if c is a decimal digit
int isgraph(int c) -- True if c is a graphical character.
int islower(int c) -- True if c is a lowercase letter
int isprint(int c) -- True if c is a printable character
int ispunct (int c) -- True if c is a punctuation character.
int isspace(int c) -- True if c is a space character.
int isupper(int c) -- True if c is an uppercase letter.
int isxdigit(int c) -- True if c is a hexadecimal digit 

Character Conversion:
int toascii(int c) -- Convert c to ASCII .
tolower(int c) -- Convert c to lowercase.
int toupper(int c) -- Convert c to uppercase.

Saturday, April 14, 2012

Algorithm (Lab report 06)

Theory: Graph is very important in graphics programming. In mathematics graph has many applications. In mathematics and computer science, graph theory is the study of graphs, mathematical structures used to model pair wise relations between objects from a certain collection. A "graph" in this context is a collection of "vertices" or "nodes" and a collection of edges that connect pairs of vertices. A graph may be undirected, meaning that there is no distinction between the two vertices associated with each edge, or its edges may be directed from one vertex to another; see graph (mathematics) for more detailed definitions and for other variations in the types of graph that are commonly considered. Graphs are one of the prime objects of study in discrete mathematics.
Apparatus:
        i.     Computer.
        ii.    Coding Language.
        iii.   Compiler (Code block, Dev c/c++, Turbo c/c++ etc.)
        iv.   Coding skill.
Algorithm: (Insert a graph)
Step01. Procedure DIJKSTRA SINGLE SOURCE SP(V;E;w; s)
Step02. begin
Step03. VT := fsg;
Step04. for all v 2 (V �� VT ) do
Step05. if (s; v) exists set l[v] := w(s; v);
Step06. else set l[v] := 1;
Step07. while VT 6= V do
Step08. begin
Step09. _nd a vertex u such that l[u] := minfl[v]jv 2 (V �� VT )g;
Step 10. VT := VT [ fug;
Step 11. for all v 2 (V �� VT ) do
Step 12. l[v] := minfl[v]; l[u] + w(u; v)g;
Step 13. endwhile
Step 14. end DIJKSTRA SINGLE SOURCE SP

Source code 01: (Insert a graph)
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<math.h>
#define IN 99
#define N 6
int dijkstra(int cost[][N], int source, int target);
int dijsktra(int cost[][N],int source,int target)
{
    int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;
    char path[N];
    for(i=1;i< N;i++)
    {
        dist[i] = IN;
        prev[i] = -1;
    }
    start = source;
    selected[start]=1;
    dist[start] = 0;
    while(selected[target] ==0)
    {
        min = IN;
        m = 0;
        for(i=1;i< N;i++)
        {
            d = dist[start] +cost[start][i];
            if(d< dist[i]&&selected[i]==0)
            {
                dist[i] = d;
                prev[i] = start;
            }
            if(min>dist[i] && selected[i]==0)
            {
                min = dist[i];
                m = i;
            }
        }
        start = m;
        selected[start] = 1;
    }
    start = target;
    j = 0;
    while(start != -1)
    {
        path[j++] = start+65;
        start = prev[start];
    }
    path[j]='\0';
    strrev(path);
    printf("%s", path);
    return dist[target];
}
int main()
{
    int cost[N][N],i,j,w,ch,co;
    int source, target,x,y;
    printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n");
    for(i=1;i< N;i++)
    for(j=1;j< N;j++)
    cost[i][j] = IN;
    for(x=1;x< N;x++)
    {
        for(y=x+1;y< N;y++)
        {
            printf("Enter the weight of the path between node %d and %d: ",x,y);
            scanf("%d",&w);
            cost [x][y] = cost[y][x] = w;
        }
        printf("\n");
    }
    printf("\nEnter The Source:");
    scanf("%d", &source);
    printf("\nEnter The target");
    scanf("%d", &target);
    co = dijsktra(cost,source,target);
    printf("\nShortest Path: %d\n",co);
    getch();
    return 0;
}
Result:
Sample Input:
Shortest Path Algorithm(DIJKSRTRA's ALGORITHM

Enter the weight of the path between node 1 and 2: 1
Enter the weight of the path between node 1 and 3: 2
Enter the weight of the path between node 1 and 4: 3
Enter the weight of the path between node 1 and 5: 4
Enter the weight of the path between node 2 and 3: 5
Enter the weight of the path between node 2 and 4: 6
Enter the weight of the path between node 2 and 5: 7
Enter the weight of the path between node 3 and 4: 8
Enter the weight of the path between node 3 and 5: 9
Enter the weight of the path between node 4 and 5: 10
Enter the source: 3
Enter the target : 3

Sample output:  
D
Shortest path: 0

Caution: You must change this code. And also change the test value when you print it......Try to make change or modify this.

Saturday, March 31, 2012

Algorithm (Lab report 05)

Theory: The merge sort is very important to sorting algorithm. It can sort different two (Arrays) elements with Divide, conquer and combine approach. Merge sort is very predictable. It makes between 0.5*lg(n) and lg(n) comparisons per element, and between lg(n) and 1.5*lg(n) swaps per element. The minima are achieved for already sorted data; the maxima are achieved, on average, for random data. If using Θ(n) extra space is of no concern, then merge sort is an excellent choice: It is simple to implement, and it is the only stable O(n·lg(n)) sorting algorithm. Note that when sorting linked lists, merge sort requires only Θ(lg(n)) extra space (for recursion).

Apparatus:
           i.   Computer.
          ii.   Coding Language.
         iii.   Compiler (Code block, Dev c/c++, Turbo c/c++ etc.)
         iv.   Coding skill.
Algorithm: (Computational (MERGE-SORT(A,p,q,r)

Step01
:  if p <r
Step02:   then q ← (r + p)/2
Step03:   MERGE-SORT(A, p, q )
Step04:   MERGE-SORT(A,q+1,r)
Step05:   MERGE(A, p, q, r)
Step06:    n1←q-p+1
Step07:    n2←r-q
Step08:    create arrays L[1...N1+1] and R[1...N2+1]
Step09:    for i←1 to N1
Step10:   do L[i] ← A[p+i-1]
Step11:   for j ← 1 to n2
Step12:   do R[j] ← A[q+j]
Step13:   L[N1+1] ← ∞
Step14:   R[N2+1] ← ∞
Step15:   i ← 1
Step16:   j ← 1
Step17:   for k ← p to r
Step18:   do if L[i] ≤ R[j]
Step19:   then A[k] ← L[i]
Step20:   i ← i+1
Step21:   else A[k] ← R[j]
Step22:    j ← j+1


Source code 01:
(Random merge sort)
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<time.h>
#include<stdlib.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 low=0;
int high;
int main(void)
{
char check='y';
int i,n;
clock_t start, finish;
while(check=='y')
  {
  textmode(C80);
  cout<<"How many number do you generate(Merge sort):";
  cin>>n;
  for(i=0; i<n; i++)
    {
    a[i]=random(n);
    }
  start=clock();
  high=n;
  for(i=0; i<n; i++)
    {
    MergeSort(low,high);
    cout<<a[i]<<"\t";
    }
  finish=clock();
  cout<<"\nTotal time is:"<<(finish-start)/CLK_TCK<<" second";         check=getche();
}
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];
}                                                            Result:
Sample Input 01:
How many number do you generate(Merge sort): 10
Sample Output 01:
0          0          0          0          1          2          3          3          5          7
Total time is 0 second.
===========================================================
Sample Input 02:
How many number do you generate(Merge sort): 14
Sample Output 02:
0          0          0          0          1          2          3          3          4          4          6          7          9
9
Total time is 0 second.

Caution: You must change this code. And also change the test value when you print it......Try to make change or modify this.

Friday, March 23, 2012

Algorithm (Lab report 04)

Theory:
The exponential differential equations which are the topic of this thesis will be defined in terms of invariant differential forms on commutative algebraic groups, in particular semiabelian varieties. The theory of the equations depends on a good understanding of the algebraic subgroups of commutative algebraic groups.

The main work of the thesis, establishing the theory of exponential   differential equations, Differential equations contains background material from differential algebra and algebraic geometry, and the final gives an application.

Apparatus:
        i.     Computer.
        ii.    Coding Language.
        iii.   Compiler (Code block, Dev c/c++, Turbo c/c++ etc.)
        iv.   Coding skill.
Algorithm: (Computational (Exponential) of  X^n)
Step 01: Exponential (X^n)
Step 02: m=n; pow=1; z=X;
Step 03: while(m>0) do
Step 04: while(m mod 2==0) do
Step 05:  {
Step 06       m=(m/2); z=z^2
Step 07:      m=m-1;
Step 08:     pow=pow*z;
Step 09:   }
Step 10: return pow;

Source code 01: (Exponential)
# include<stdio.h>
# include<math.h>
# include<conio.h>
int main()
{
int i,test;
float x,total=0;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
  scanf("%f",&x);
  total+=pow(x,i);
}
printf("%.0f",total);
getch();
return 0;
}
Source code 02: (Exponential)
# include<stdio.h>
# include<math.h>
# include<conio.h>
int main()
{
int i,test;
float x,y,total=0;
scanf("%d",&test);
for(i=1;i<=test;i++)
  {
  scanf("%f%f",&x,&y);
  total+=pow(x,y);
}
printf("Total: %.0f",total);
getch();
return 0;
}


Result 01:
Sample Input                                           Sample output

3
10                                                                           Total: 27480
20
30


Result 02:
Sample Input                                           Sample output

3

10    1                                                                       Total: 27480
20    2
30    3

Caution: You must change this code. And also change the test value when you print it......Try to make change or modify this.