Saturday, May 1, 2010

Animated Moving Car

// Add header files stdio.h  conio.h  graphics.h  dos.h  math.h
#include
#include
#include
#include
#include

void main()
{
int gd=DETECT,gm;
int i=0,m=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
while(!kbhit())
{
cleardevice();
i++;
if(i>getmaxx())
i-=670;
line(80+i,300,90+i,270);
line(90+i,270,120+i,270);
line(120+i,270,160+i,240);
line(160+i,240,230+i,240);
line(230+i,240,275+i,270);
line(275+i,270,310+i,270);
line(310+i,270,335+i,290);
line(335+i,290,335+i,300);
line(255+i,300,335+i,300);
line(180+i,300,210+i,300);
line(80+i,300,135+i,300);
arc(232+i,300,0,180,23);
arc(157+i,300,0,180,23);
circle(232+i,300,18);
circle(157+i,300,18);
pieslice(232+i,300,0+m,90+m,18);
pieslice(232+i,300,180+m,270+m,18);
pieslice(157+i,300,0+m,90+m,18);
pieslice(157+i,300,180+m,270+m,18);
if(m<-360)
m=90;
m-=3;
delay(5);
}
getch();
closegraph();
}

House

//Add header files  stdio.h  graphics.h  stdlib.h  math.h  conio.h
//Use DosBox 0.73 for Output
#include
#include
#include
#include
#include

void reset (int h[][2])
{
    int val[9][2] = {
            { 50, 50 },{ 75, 50 },{ 75, 75 },{ 100, 75 },
            { 100, 50 },{ 125, 50 },{ 125, 100 },{ 87, 125 },{ 50, 100 }
            };
    int i;
    for (i=0; i<9; i++)
    {
    h[i][0] = val[i][0]-50;
    h[i][1] = val[i][1]-50;
    }
}
void draw (int h[][2])
{
    int i;
    setlinestyle (DOTTED_LINE, 0, 1);
    line (320, 0, 320, 480);
    line (0, 240, 640, 240);
    setlinestyle (SOLID_LINE, 0, 1);
    for (i=0; i<8; i++)
    line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);
    line (320+h[0][0], 240-h[0][1], 320+h[8][0], 240-h[8][1]);
}
void rotate (int h[][2], float angle)
{
    int i;
    for (i=0; i<9; i++)
    {
    int xnew, ynew;
    xnew = h[i][0] * cos (angle) - h[i][1] * sin (angle);
    ynew = h[i][0] * sin (angle) + h[i][1] * cos (angle);
    h[i][0] = xnew; h[i][1] = ynew;
    }
}
void scale (int h[][2], int sx, int sy)
{
    int i;
    for (i=0; i<9; i++)
    {
    h[i][0] *= sx;
    h[i][1] *= sy;
    }
}
void translate (int h[][2], int dx, int dy)
{
    int i;
    for (i=0; i<9; i++)
    {
    h[i][0] += dx;
    h[i][1] += dy;
    }
}
void reflect (int h[][2], int m, int c)
{
    int i;
    float angle;
    for (i=0; i<9; i++)
        h[i][1] -= c;
    angle = M_PI/2 - atan (m);
    rotate (h, angle);
    for (i=0; i<9; i++)
        h[i][0] = -h[i][0];
    angle = -angle;
    rotate (h, angle);
    for (i=0; i<9; i++)
        h[i][1] += c;
}

void ini()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"..\\bgi");
}
void dini()
{
    getch();
    closegraph();
}
void main()
{

    int h[9][2],sx,sy,x,y,m,c,choice;
    do
    {
        clrscr();
        printf("1. Scaling about the origin.\n");
        printf("2. Scaling about an arbitrary point.\n");
        printf("3. Reflection about the line y = mx + c.\n");
        printf("4. Exit\n");
        printf("Enter the choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: printf ("Enter the x- and y-scaling factors: ");
                scanf ("%d%d", &sx, &sy);
                ini();
                reset (h);
                draw (h);getch();
                scale (h, sx, sy);
                cleardevice();
                draw (h);
                dini();
                break;

            case 2: printf ("Enter the x- and y-scaling factors: ");
                scanf ("%d%d", &sx, &sy);
                printf ("Enter the x- and y-coordinates of the point: ");
                scanf ("%d%d", &x, &y);
                ini();
                reset (h);
                translate (h, x, y);// Go to arbitrary point
                draw(h); getch();//Show its arbitrary position
                cleardevice();
                translate(h,-x,-y);//Take it back to origin
                draw(h);
                getch();
                cleardevice();
                scale (h, sx, sy);//Now Scale it
                draw(h);
                getch();
                translate (h, x, y);//Back to Arbitrary point
                cleardevice();
                draw (h);
                putpixel (320+x, 240-y, WHITE);
                dini();
                break;

            case 3: printf ("Enter the values of m and c: ");
                scanf ("%d%d", &m, &c);
                ini();
                reset (h);
                draw (h); getch();
                reflect (h, m, c);
                cleardevice();
                draw (h);
                dini();
                break;

            case 4: exit(0);
        }
    }while(choice!=4);
}

Animated Moving Fish

//add header files as stdio.h  time.h  conio.h  stdlib.h  dos.h  graphics.h
#include
#include
#include
#include
#include
#include

void main()
{
    int gd=DETECT,gm;
    int x=10,y=200,x1=675,y1=380;
    int stangle=35,endangle=140,radius=90;

    initgraph(&gd,&gm,"..\\bgi");

    while(!kbhit())
    {
        cleardevice();
        setbkcolor(BLACK);

        if(x<640)
        {
            x+=5;
            y+=1;
            arc(x,y,stangle,endangle+35,radius);
            arc(x,y-110,190,323,radius+2);
            circle(x+40,y-60,5);
            line(x-90,y-90,x-90,y-8);
        }
        else
        {
            x1-=5;
            y1-=1;
            arc(x1,y1,stangle-30,endangle+4,radius);
            arc(x1,y1-110,217,350,radius+2);
            circle(x1-40,y1-60,5);
            line(x1+90,y1-90,x1+90,y1-10);
        }
        setcolor(YELLOW);
        delay(90);
    }
    closegraph();
}

Animated Analog Clock

//add header files as stdio.h  conio.h  graphics.h  dos.h  math.h
#include
#include
#include
#include
#include

#define arg_sec M_PI/30
#define arg_hour M_PI/6
#define arg_min M_PI/360
void main()
{
  int gd=DETECT,gm,sec=0,hour,min,x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,i,dig_sec;
  char *k[12]={"1","2","3","4","5","6","7","8","9","10","11","12"};
  char Time_Dig[14];
  struct time t;
  initgraph(&gd,&gm,"c:\\tc\\bgi");
  setcolor(YELLOW);
  circle(300,200,200);
  circle(300,200,180);
  setfillstyle(2,RED);
  floodfill(300,390,YELLOW);
  settextstyle(DEFAULT_FONT,0,2);
//----------------------Constants----------------------//
    for(i=1;i<13;i++)
    {
       int a=160*cos(arg_hour*i-M_PI_2);
       int b=160*sin(arg_hour*i-M_PI_2);
    outtextxy(a+300,b+200,k[i-1]);
    }
  while(!kbhit())
   {
       settextstyle(7,0,4);
      outtextxy(264,100,"Babu");
      settextstyle(7,0,1);
      outtextxy(264,280,"Manisha");
      setcolor(BLACK);
      line(300,200,x1+300,y1+200);
      line(300,200,x2+300,y2+200);
      line(300,200,x3+300,y3+200);
      gettime(&t);
      if(sec!=t.ti_sec)
    {
       sound(5000);
       delay(1);
       nosound();
    }
      hour=t.ti_hour;
      sec=t.ti_sec;
      min=t.ti_min;
      Time_Dig[0]=hour/10+48;
      Time_Dig[1]=hour%10+48;
      Time_Dig[2]=':';
      Time_Dig[3]=min/10+48;
      Time_Dig[4]=min%10+48;
      Time_Dig[5]=':';
      Time_Dig[6]=sec/10+48;
      Time_Dig[7]=sec%10+48;
      Time_Dig[8]='\0';
      outtextxy(270,250,"ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ");
      x1=150*cos(arg_sec*sec-M_PI_2)*0.98;
      y1=150*sin(arg_sec*sec-M_PI_2)*0.98;
      x2=150*cos(arg_sec*min-M_PI_2)*0.9;
      y2=150*sin(arg_sec*min-M_PI_2)*0.9;
      if(hour>12) hour-=12;
      x3=150*cos(arg_hour*hour-M_PI_2+arg_min*min)*0.6;
      y3=150*sin(arg_hour*hour-M_PI_2+arg_min*min)*0.6;
      setcolor(YELLOW);
      line(300,200,x1+300,y1+200);
      setcolor(CYAN);
      line(300,200,x2+300,y2+200);
      setcolor(WHITE);
      line(300,200,x3+300,y3+200);
      setcolor(YELLOW);
      outtextxy(270,250,Time_Dig);
      delay(50);

   }

  getch();
  closegraph();
  restorecrtmode();
}

Wednesday, April 28, 2010

Optimal Storage on Tapes

import java.io.*;
class Store
{
    public static void main (String[] args)throws Exception
        {
            DataInputStream d=new DataInputStream(System.in);
            System.out.println("Enter no. of Programs:");
            int n=Integer.parseInt(d.readLine());
            int[] p=new int[n];
            int[] a=new int[n];
            int i;
            System.out.println("Enter the programs with length:");
            for(i=0;i<=n-1;i++)
            {
                System.out.print("Program"+(i+1)+":");
               p[i]=Integer.parseInt(d.readLine());
               System.out.println();
               a[i]=i+1;
               
            }
            for(i=n-2;i>=0;i--)
            {
                for(int j=0;j<=i;j++)
                {
                if(p[j]>p[j+1])
                {
                    int temp=p[j];
                    p[j]=p[j+1];
                    p[j+1]=temp;
                    int t=a[j];
                    a[j]=a[j+1];
                    a[j+1]=t;
                }
                }
            }
            System.out.println("The Best Ordering is");
           
            for(i=0;i<=n-1;i++)
            {
                System.out.println("Program"+a[i]+":"+p[i]);
            }       
}
}
/*Output:
Enter no. of Programs:
3
Enter the programs with length:
Program1:5

Program2:10

Program3:3

The Best Ordering is
Program3:3
Program1:5
Program2:10

Process completed.
*/

Single Source shortest path Usin BellmanFord

import java.io.*;

class  Graph

{
 BufferedReader Bobj= new BufferedReader(new InputStreamReader(System.in));

 int g[][],v,e,d[],p[],visited[];

void Creategraph()throws IOException

       {int a,b,w;
   
     System.out.print("ENTER THE NO OF VETEX=");
   
     v=Integer.parseInt(Bobj.readLine());
   
     System.out.print("ENTER THE NO OF EDGES=");
   
     e=Integer.parseInt(Bobj.readLine());
   
     g=new int[v+1][v+1];
   
     for(int i=1;i<=v;i++)
   
        for(int j=1;j<=v;j++)
       
          g[i][j]=0;
       
     for(int i=1;i<=e;i++)
   
     {System.out.println("ENTER THE EDGE INFORMATION\n");
     
      System.out.print("ENTER THE SORCE VERTEX=");
     
      a=Integer.parseInt(Bobj.readLine());
     
      System.out.print("ENTER THE DESTINATION VERTEX=");
     
      b=Integer.parseInt(Bobj.readLine());
     
      System.out.print("ENTER THE WEIGTH OF EDGE =");
     
      w=Integer.parseInt(Bobj.readLine());
     
      g[a][b]=g[b][a]=w;
      }
   
}

void Callbellmanford()throws IOException

{

 d=new int[v+1];

 p=new int[v+1];

 visited=new int[v+1];

 for(int i=1;i<=v;i++)

   {
  
     p[i]=0;
   
     visited[i]=0;
   
     d[i]=32767;
   }

  bellmanford();
  
}

void bellmanford()throws IOException

{

  BufferedReader Bobj= new BufferedReader(new InputStreamReader(System.in));
  
  int dc,current,mincost=0,source,dest,c;

  System.out.print("\n\n\n\n\n\n\nENTER THE SOURCE VERTEX=");
 
  source=Integer.parseInt(Bobj.readLine());
 
  System.out.print("ENTER THE DESTINATION VERTEX=");
 
  dest=Integer.parseInt(Bobj.readLine());
 
 
 current=source;

 visited[current]=1;

 d[current]=0;



 dc=dest;


 while (current!=dest)

 {

   for(int i=1;i<=v;i++)
  
   {
     if(g[current][i]!=0 && visited[i]!=1)
   
      if (d[i]>g[current][i]+d[current])
      
         {  d[i]=g[current][i]+d[current];
         
          p[i]=current;
         }
 
   }

     for(int i=1;i<=v;i++)

    //mincost+=d[i];

System.out.println("MINIMUM COST="+d[i]);System.out.println();




  
   int min=32767;
  
   for(int i=1;i<=v;i++)
  
   { if (visited[i]!=1 && d[i]
   
     {
      min=d[i];
     
      current=i;
     
     }
   }
  
   visited[current]=1;
  
   //c++;
  
  
  
 }

 System.out.println("******** GRAPH *********");


System.out.println("MINIMUM COST="+d[dest]);
 
}

}

class Bellman
{

 public static void main(String args[])throws IOException

 {
  Graph g=new Graph();
 
  g.Creategraph();
 
  g.Callbellmanford();
 
 }

}
/*Output:
ENTER THE NO OF VETEX=7
ENTER THE NO OF EDGES=10
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=1
ENTER THE DESTINATION VERTEX=2
ENTER THE WEIGTH OF EDGE =6
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=1
ENTER THE DESTINATION VERTEX=3
ENTER THE WEIGTH OF EDGE =5
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=1
ENTER THE DESTINATION VERTEX=4
ENTER THE WEIGTH OF EDGE =5
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=4
ENTER THE DESTINATION VERTEX=3
ENTER THE WEIGTH OF EDGE =-2
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=3
ENTER THE DESTINATION VERTEX=2
ENTER THE WEIGTH OF EDGE =-2
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=2
ENTER THE DESTINATION VERTEX=5
ENTER THE WEIGTH OF EDGE =-1
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=3
ENTER THE DESTINATION VERTEX=5
ENTER THE WEIGTH OF EDGE =1
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=4
ENTER THE DESTINATION VERTEX=6
ENTER THE WEIGTH OF EDGE =-1
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=6
ENTER THE DESTINATION VERTEX=7
ENTER THE WEIGTH OF EDGE =3
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=5
ENTER THE DESTINATION VERTEX=7
ENTER THE WEIGTH OF EDGE =3







ENTER THE SOURCE VERTEX=1
ENTER THE DESTINATION VERTEX=7
MINIMUM COST=0
MINIMUM COST=6
MINIMUM COST=5
MINIMUM COST=5
MINIMUM COST=32767
MINIMUM COST=32767
MINIMUM COST=32767

MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=5
MINIMUM COST=3
MINIMUM COST=6
MINIMUM COST=32767
MINIMUM COST=32767

MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=5
MINIMUM COST=3
MINIMUM COST=2
MINIMUM COST=32767
MINIMUM COST=32767

MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=5
MINIMUM COST=3
MINIMUM COST=2
MINIMUM COST=32767
MINIMUM COST=5

MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=5
MINIMUM COST=3
MINIMUM COST=2
MINIMUM COST=2
MINIMUM COST=5

MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=5
MINIMUM COST=3
MINIMUM COST=2
MINIMUM COST=2
MINIMUM COST=5

******** GRAPH *********
MINIMUM COST=5

Process completed.*/

Saturday, April 24, 2010

Fractional Knapsack Using Greedy Algorithm

import java.io.*;
class ksap
{
float p[];
float w[];
int n,m;

void getdata()throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));

System.out.println("how many objects");
n=Integer.parseInt(obj.readLine());
System.out.println("enter the capacity of bag");
m=Integer.parseInt(obj.readLine());

p=new float[n+1];
w=new float[m+1];

for(int i=1;i<=n;i++)
{
System.out.println("enter profit and weight");
p[i]=Float.parseFloat(obj.readLine());
w[i]=Float.parseFloat(obj.readLine());
}

}


void sort()
{
for(int i=1;i<=n-1;i++)
for(int j=1;j<=n-i;j++)
if ((p[j]/w[j])<(p[j+1]/w[j+1]))
{
float temp =p[j];
p[j]=p[j+1];
p[j+1]=temp;

float temp1=w[j];
w[j]=w[j+1];
w[j+1]=temp1;
}
}

float greedk()
{ int i;
float x[]=new float[n+1];

float u;
float pr=0;

for( i=1;i<=n;i++)

{ x[i]=0;
u=m;
for( i=1;i<=n;i++)
{
if(w[i]<=u)
x[i]=1;
else
break;
u=u-(w[i]);
}



if( i<=n)
x[i]=u/w[i];
}
for(i=1;i<=n;i++)
pr=pr+(p[i])*(x[i]);
return pr;
}

}
class knapsackgreedy
{
public static void main(String args[])throws IOException

{
ksap ob=new ksap();
ob.getdata();
ob.sort();

float maxprofit;
maxprofit=ob.greedk();
System.out.println("Maximum profit="+maxprofit);
}
}

/* output
how many objects
3
enter the capacity of bag
26
enter profit and weight
12
15
enter profit and weight
20
9
enter profit and weight
30
11
Maximum profit=54.8
Process Exit...
*/

Kruskal Algorithm

import java.util.*;

class Edge
{
  int  v1,v2,w;
}
class Graph
{
  Edge edge[];
  int v,e;
  int p[];

  void creategraph()
  {
    int a,b,w;

    Scanner kbd = new Scanner(System.in);
    System.out.print("Enter number of vertices ");
    v = kbd.nextInt();
    System.out.print("Enter number of edges ");
    e = kbd.nextInt();
    edge = new Edge[e+1];
    for (int i = 1 ; i <= e ; i++)
      edge[i] = new Edge();
    for (int i = 1; i<= e ; i++)
    {
      System.out.print("Enter edge information ");
      a = kbd.nextInt();
      b = kbd.nextInt();
      System.out.print("Enter weight of this edge ");
      w = kbd.nextInt();
      edge[i].v1 =  a;
      edge[i].v2 =  b;
      edge[i].w =  w;
    }

  } // end creategraph

  void bubble()
  {
    for (int i = e-1 ; i >= 1 ; i--)
     for (int j = 1 ; j <= i ; j++)
      if (edge[j].w > edge[j+1].w)
      {
        Edge t = edge[j];
        edge[j] = edge[j+1];
        edge[j+1] = t;
      }
  }

  int find(int z) // find parent of z
  {
    while (p[z] != -1)
     z = p[z];

    return z;
  }
  void union(int u , int v)
  {
    p[v] = u;
  }
  void kruskal()
  {
   p = new int[v+1];
     for(int i = 1 ; i <= v  ; i++)
      p[i] = -1;
    int  mincost = 0 ;
    int  noofedgesadded = 0;
    for (int i = 1 ; i <= e && noofedgesadded != v-1 ; i++)
    {
      Edge t = edge[i];

      int start = t.v1;
      int end = t.v2;

      int p1 = find(start);
      int p2 = find(end);

      if (p1 != p2)
      {
        mincost += t.w;
        System.out.println("Vertex " + start + " to vertex " + end);
        union(p1,p2);
        noofedgesadded++;
       
      }
    } // for loop

    System.out.println("minimum cost = " + mincost);

  } // end of kruskal

} //end Graph class

public class Kruskal
{
  public static void main(String args[])
  {
    Graph g = new Graph();
    g.creategraph();
    g.bubble();
    g.kruskal();
  }
}
/*
 Output:
 Enter number of vertices 6
Enter number of edges 11
Enter edge information
1
2
Enter weight of this edge 2
Enter edge information 1
3
Enter weight of this edge 2
Enter edge information 1
6
Enter weight of this edge 6
Enter edge information 1
4
Enter weight of this edge 18
Enter edge information 2
4
Enter weight of this edge 7
Enter edge information 4
3
Enter weight of this edge 3
Enter edge information 6
5
Enter weight of this edge 5
Enter edge information 3
6
Enter weight of this edge 8
Enter edge information 3
5
Enter weight of this edge 4
Enter edge information 4
5
Enter weight of this edge 9
Enter edge information 2
3
Enter weight of this edge 1
Vertex 2 to vertex 3
Vertex 1 to vertex 2
Vertex 4 to vertex 3
Vertex 3 to vertex 5
Vertex 6 to vertex 5
minimum cost = 15
Process completed.*/

DIJKSTRA ALGORITHM

//IMPLEMENTATION OF DIJKSTRA ALGORITHM

import java.io.*;
class  Graph
{
 BufferedReader Bobj= new BufferedReader(new InputStreamReader(System.in));
 int g[][],v,e,d[],p[],visited[];
void Creategraph()throws IOException
       {
int a,b,w;   
     System.out.print("ENTER THE NO OF VETEX=");
          v=Integer.parseInt(Bobj.readLine());
          System.out.print("ENTER THE NO OF EDGES=");
          e=Integer.parseInt(Bobj.readLine());
          g=new int[v+1][v+1];
          for(int i=1;i<=v;i++)
             for(int j=1;j<=v;j++)
                  g[i][j]=0;
             for(int i=1;i<=e;i++)
          {System.out.println("ENTER THE EDGE INFORMATION\n");
            System.out.print("ENTER THE SORCE VERTEX=");
            a=Integer.parseInt(Bobj.readLine());
            System.out.print("ENTER THE DESTINATION VERTEX=");
            b=Integer.parseInt(Bobj.readLine());
            System.out.print("ENTER THE WEIGTH OF EDGE =");
            w=Integer.parseInt(Bobj.readLine());
            g[a][b]=g[b][a]=w;
      }
     }
void CallDijktra()throws IOException
{
 d=new int[v+1];
 p=new int[v+1];
 visited=new int[v+1];
 for(int i=1;i<=v;i++)
   {
        p[i]=0;
         visited[i]=0;
         d[i]=32767;
   }
   Dijktra();
   }
void Dijktra()throws IOException
{
  BufferedReader Bobj= new BufferedReader(new InputStreamReader(System.in));
     int dc,current,mincost=0,source,dest,c;
  System.out.print("\n\n\n\n\n\n\nENTER THE SOURCE VERTEX=");
    source=Integer.parseInt(Bobj.readLine());
    System.out.print("ENTER THE DESTINATION VERTEX=");
    dest=Integer.parseInt(Bobj.readLine());
  current=source;
  visited[current]=1;
  d[current]=0;
   dc=dest;
   while (current!=dest)
 {
    for(int i=1;i<=v;i++)
      {
     if(g[current][i]!=0 && visited[i]!=1)
          if (d[i]>g[current][i]+d[current])
                { 
d[i]=g[current][i]+d[current];
                    p[i]=current;
         }
     }
     for(int i=1;i<=v;i++)
 System.out.println("MINIMUM COST="+d[i]);System.out.println();
   int min=32767;
      for(int i=1;i<=v;i++)
      {
if (visited[i]!=1 && d[i]
         {
      min=d[i];
         current=i;
        }
   }
     visited[current]=1;
    }
  System.out.println("******** GRAPH *********");
  System.out.println("MINIMUM COST="+d[dest]);
  }
}
class DijktraDemo
{
 public static void main(String args[])throws IOException
  {
  Graph g=new Graph();
    g.Creategraph();
    g.CallDijktra();
   }
 }
/*Output:

ENTER THE NO OF VETEX=6
ENTER THE NO OF EDGES=10
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=1
ENTER THE DESTINATION VERTEX=2
ENTER THE WEIGTH OF EDGE =3
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=1
ENTER THE DESTINATION VERTEX=4
ENTER THE WEIGTH OF EDGE =2
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=1
ENTER THE DESTINATION VERTEX=6
ENTER THE WEIGTH OF EDGE =20
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=2
ENTER THE DESTINATION VERTEX=4
ENTER THE WEIGTH OF EDGE =8
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=2
ENTER THE DESTINATION VERTEX=3
ENTER THE WEIGTH OF EDGE =7
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=4
ENTER THE DESTINATION VERTEX=5
ENTER THE WEIGTH OF EDGE =3
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=4
ENTER THE DESTINATION VERTEX=6
ENTER THE WEIGTH OF EDGE =22
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=3
ENTER THE DESTINATION VERTEX=5
ENTER THE WEIGTH OF EDGE =6
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=5
ENTER THE DESTINATION VERTEX=6
ENTER THE WEIGTH OF EDGE =4
ENTER THE EDGE INFORMATION

ENTER THE SORCE VERTEX=4
ENTER THE DESTINATION VERTEX=6
ENTER THE WEIGTH OF EDGE =22
ENTER THE SOURCE VERTEX=1
ENTER THE DESTINATION VERTEX=6
MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=32767
MINIMUM COST=2
MINIMUM COST=32767
MINIMUM COST=20
MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=32767
MINIMUM COST=2
MINIMUM COST=5
MINIMUM COST=20

MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=10
MINIMUM COST=2
MINIMUM COST=5
MINIMUM COST=20
MINIMUM COST=0
MINIMUM COST=3
MINIMUM COST=10
MINIMUM COST=2
MINIMUM COST=5
MINIMUM COST=9
******** GRAPH *********
MINIMUM COST=9
Process completed.
*/

INTERNET ALGORITHM USING "KNUTHMORIES"

//IMPLEMENTATION OF INTERNET ALGORITHM USING KNUTHMORIES

import java.io.*;
import java.util.*;
class Knuthmories
{
    public int KMP(String T,String P)
    {
        int n=T.length();
        int m=P.length();
        int f[]=new int[10];
        int i=0;
        int j=0;
        f=failure(P,f);
        while (i
        {
            if (P.charAt(j)==T.charAt(i))
            {
                if(j==m-1)
                    return i-m+1;
                i++;
                j++;
            }
            else
            if(j>0)  
                j=f[j-1];
            else
            i++;
        }
        return -1;
    }
  
    public int[] failure(String P,int f[])
    {
        int i=1;
        int m=P.length();
        int j=0;
        f[0]=0;
        while (i
        {
            if (P.charAt(j)==P.charAt(i))
            {
                f[i]=j+1;
                i++;
                j++;
            }
            else if(j>0)
                j=f[j-1];
            else
            {
            f[i]=0;
            i++;
            }
        }
        return f;
    }
}
  
class Knuthmoriesdemo
{
    public static void main(String args[])throws IOException
    {
        String t,p;
        BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the string of your choice");
        t=obj.readLine();
        System.out.println("Enter the string to be compared");
        p=obj.readLine();
        Knuthmories b=new Knuthmories();
        int pos=b.KMP(t,p);
        System.out.println("The position of the string is :"+pos);
    }
}
              

/*OUTPUT
Enter the string of your choice
university
Enter the string to be compared
sity
The position of the string is :6
Process Exit...
*/

Job sequencing with dead lines by Greedy programming technique.

//This program performs job sequencing with dead lines by Greedy programming technique.

import java.io.*;
import java.util.*;
public class JobSeq
{
    public static InputStreamReader input=new InputStreamReader(System.in);
    public static BufferedReader br =new BufferedReader(input);
    static int job[][], n, maxd = 0;
    public static void main(String[] args)  throws IOException{
        System.out.print("Enter total number of jobs: ");
        n = Integer.parseInt( br.readLine() );
        job = new int[n][3];
        System.out.println("Enter job details: ");
        for(int i =0; i
            System.out.print("job #"+(i+1)+" :-\n\tProfit: ");
            job[i][0]=Integer.parseInt( br.readLine() );
            do

           {
                System.out.print("\tDead Line: ");
                job[i][1]=Integer.parseInt( br.readLine() );
                if(job[i][1] <= 0)
                {
                    System.out.print("\tInvalid Deadline!\nEnter Again\n");
                    continue;
                }
                else if(maxd < job[i][1])
                {
                    maxd = job[i][1];
                }
                break;
            }while(true);
            job[i][2]=i+1;
        }
        bubble_srt();
        JobSeq();
        int profit = 0;
        System.out.print("\nThe optimal solution is J = {" );
        for(int i=1; i<=k; i++)
        {
            System.out.print((job[J[i]-1][2])+", ");
            profit += job[J[i]-1][0];
        }
        System.out.println("\b\b } with a profit of "+ profit + ".");
    }
    public static void bubble_srt(){
        int i, j, t[][];
        t = new int[1][3];
        for(i = 0; i < n; i++)
            for(j = 1; j < (n-i); j++)
                if(job[j-1][0] < job[j][0])
                {
                    t[0] = job[j-1];
                    job[j-1]=job[j];
                    job[j]=t[0];
                }
    }
    static int J[], k;
    static void JobSeq()
{
        J = new int[maxd+1];
        int temjob[] = new int[n+1];
        int r;
        for(int i=1; i<=n; i++)
            temjob[i] = job[i-1][1];
        for(int i=0; i<=maxd; i++)
            J[i]=0;
        temjob[0] = J[0] = 0;
        J[1] = 1;
        k=1;
        for(int i=2; i<=n; i++)
        {
            r=k;
            while((temjob[J[r]] > temjob[i]) && (temjob[J[r]] != r))
                r--;
            if((temjob[J[r]] <= temjob[i]) && (temjob[i] > r) )
            {
                for(int q = k; q>= r+1; q--)
                    J[q+1] = J[q];
                J[r+1] = i; k++;
            }
        }
        return;
    }
}
/*Output:
Enter total number of jobs: 4
Enter job details:
 job #1 :-
    Profit: 100
    Dead Line: 2
job #2 :-
    Profit: 10
    Dead Line: 1
job #3 :-
    Profit: 15
    Dead Line: 2
job #4 :-
    Profit: 27
    Dead Line: 1

The optimal solution is J = {4, 1} with a profit of 127.
Process Exit...*/

Thursday, April 22, 2010

PAGE REPLACEMENT ALGORiTHM(FIFO & LRU)

/*PROGRAM TO IMPLEMENT PAGE REPLACEMENT ALGORiTHM(FIFO & LRU)*/
// Add the header files stdlib.h      stdio.h      conio.h

#include
#include
#include

void fifo(int a[],int n)
{
    int i,m=n,x[3],v=0,y=0,next,j=0;
while(m!=0)
{
next=a[v];
j=0;
while(x[j]!=next&&j<3)
j++;

if(j>2)
{
printf("\nReplace page %d\n",x[y]);
x[y]=a[v];
v++;
y++;
m--;
}
else
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
for(i=0;i<3;i++)
    printf(" %d",x[i]);

if(y==3)
y=0;
}
}
void lru (int b[],int n)
{
int i,m=n,x[3],v=0,y=0,next,j=0,min[3],min1=0,w,min2[3],min3;
for(i=0;i<3;i++)
{
x[i]=0;
min[i]=0;
}
for(i=0;i<3;i++)
{
j=0;
while(x[j]!=b[v]&&j<3)
j++;
if(j<3)
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
else
{
printf("\nReplace page %d",x[y]);
x[y]=b[v];
v++;
m--;
y++;
for(j=0;j<3;j++)
printf(" %d",x[j]);
}
}

while(m!=0)
{
next=b[v];
j=0;
while(x[j]!=next&&j<3)
j++;
if(j<3)
{
printf("\n\nPage hit\n\n");
m--;
v++;
}
else
{
for(i=0;i<3;i++)
{
w=v;
while(b[w]!=x[i]&&w>0)
w--;
if(w>0)
{
min[i]=w;
min2[i]=b[w];
}
else
min[i]=w;
}
}
min3=min[0];
min1=0;
for(i=1;i<3;i++)
{
if(min[i]<=min3)
{
min3=min[i];
min1=i;
}
}
}
printf("\nReplace pager %d\n",x[min1]);
x[min1]=b[v];
v++;
m--;
for(i=0;i<3;i++)
printf(" %d", x[i]);
}

void main()
{
int n,i,a[25],b[25],c[25],ch;
clrscr();
printf("Enter how many pages:\n");
scanf("%d",&n);
printf("Enter sequence of pages\n");
for(i=0;i
{
scanf("%d",&a[i]);
b[i]=a[i];
c[i]=a[i];
}
do
{
printf("\nMENU\n");
printf("1.FIFO\n");
printf("2. LRU\n");
printf("3. EXIT\n");
printf("Enter choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1: fifo(a,n);
break;
case 2: lru(c,n);
break;
case 3: break;
}
}
while(ch!=3);
getch();
}
/*Output:
Enter how many pages:
3                                                                              
Enter sequence of pages                                                        
1                                                                              
2                                                                              
3
                                                                               
MENU                                                                           
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice                                                                   
2                                                                              
                                                                               
Replace page 0 1 0 0                                                           
Replace page 0 1 2 0                                                           
Replace page 0 1 2 3                                                           
Replace pager 1                                                                
 0 2 3                                                                         
MENU
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice
1                                                                              
                                                                               
Replace page 8889                                                              
 1 10289 644                                                                   
Replace page 10289                                                             
 1 2 644                                                                       
Replace page 644                                                               
 1 2 3                                                                         
MENU                                                                           
1.FIFO                                                                         
2. LRU                                                                         
3. EXIT                                                                        
Enter choice
3
*/
                                                                       

Wednesday, April 21, 2010

Brute Force Algorithm(Internet algorithm)

import java.io.*;
class bf_fun
{
    int bf(String t,String p)
    {
        int n=t.length();
        int m=p.length();
        for(int i=0;i<=n-m;i++)
        {
            int j=0;
            while(j                j++;
            if(j==m)
            return i+1;
        }
        return -1;
    }
}

class bf
{
    public static void main(String args[])
    throws IOException
    {
        InputStreamReader input=new InputStreamReader(System.in);
        BufferedReader obj=new BufferedReader(input);
        System.out.println("Enter the string:");
        String s=obj.readLine();
        System.out.print("Enter the substring:");
        String ts=obj.readLine();
        bf_fun f=new bf_fun();
        int n=f.bf(s,ts);
        if (n!=-1)
            System.out.println("Pattern matched at position "+n);
           
        else
            System.out.println("No pattern matching");   
    }
}
/*

OUTPUT
Enter the string:
WHAT IS UR NAME
Enter the substring:IS
Pattern matched at position 6

*/

Selection Sort

import java.io.*;
public class SelectionSort
{
   
   public static void main(String args[])throws IOException
    {
           
            int ch;
            InputStreamReader cin=new InputStreamReader(System.in);
            BufferedReader obj=new BufferedReader (cin);
            System.out.println("Enter the no. element");
            int n=Integer.parseInt(obj.readLine());
            int a[]=new int[n];
            for(int i=0;i<=n-1;i++)
            {
                 System.out.println("Enter the element");
                 a[i]=Integer.parseInt(obj.readLine());
            }
            do
            {
                System.out.println("\nMenu\n1.Selection Sort\n2.exit");
                System.out.println("Enter the choice");
                ch=Integer.parseInt(obj.readLine());
                switch (ch)
                {
                case 1:Selectionsort(a,n);
                       System.out.println("Sorted Array is ");
                       for(int w=0;w<=n-1;w++)
                           System.out.println(a[w]);
                   break;
                case 2:
                  break;
                default:System.out.println("Wrong choice");
                  break;
                }
            }
            while(ch!=2);
    }
    public static void Selectionsort(int a[],int n)
    {
        int min,p,t;
        for(int i=0;i<=n-2;i++)
        {
            min=a[i];
            p=i;
            for(int j=i+1;j<=n-1;j++)
            {
                if (a[j]
                {
                    min=a[j];
                    p=j;
                }
            }
            t=a[p];
            a[p]=a[i];
            a[i]=t;
        }
    }
}
/***************************Output****************************************
Enter the no. element
5
Enter the element
6
Enter the element
10
Enter the element
50
Enter the element
-2
Enter the element
0

Menu
1.Selection Sort
2.exit
Enter the choice
1
Sorted Array is
-2
0
6
10
50

Menu
1.Selection Sort
2.exit
Enter the choice
2
Process Exit...
*/

Tuesday, April 20, 2010

Shell Script

Shell script to simulate a simple calculation:

a=$1
op="$2"
b=$3

if[$ # -It 3]
then
echo "$0 num1 opr num2"
echo "opr can be +,-,/,x"
exit 1
fi
case "$op" in
+)echo$(($a+$b));;
-)echo$(($a-$b));;
/)echo$(($a/$b));;
x)echo$(($a*$b));;
*)echo "error";;
esac

Shell script to find largest among 3 integers:

a=$1
op="$2"
b=$3

if[$ # -It 3]
then
echo"$0 n1 n2 n3"
exit 1
fi
if[$a -gt $b -a $a -gt $c]
then
echo "$a is largest"
elif[$a -gt $b -a $b -gt $c]
then
echo "$b is largest"
elif[$a -gt $b -a $c -gt $c]
echo "$c is largest"
else
echo"sorry cannot guess number"
fi


Shell script to sort numbers in file in ascending order:

file=" "
echo -n "enter file name:"
read file
if[! -f $file]
then
echo "$file not a file"
exit 1
fi
sort -n $file


Copy File:

echo"enter sorce and target file names"
read source target
if CP $ source $ target
then
echo
echo "file copied successfully"
else "error in file copy"
fi

0-10 Number-Guess No.:
echo "enter a number from 0 to 10"
read num
if test $num -eq 5
then
echo"guess successfully"
else
echo"try again"
fi

Display 1-10 Numbers:
i=1
until[$i -gt 10]
do
echo $i
i='expr $i +1'
done
 

PRODUCER CONSUMER WITH SEMAPHORE

//add header files stdio.h & conio.h
#include
#include
#include
#define N 5;
typedef int semaphore;
semaphore mutex=1;
semaphore empty=5;
semaphore full=0;
char buffer[5];
int front=0,rear=0;
int wait (semaphore *s)
{
*s=*s-1;
if(*s<0)
{
printf("\n process is blocked");
*s=0;
return 0;
}
return 1;
}
void signal(semaphore *s)
{
*s=*s+1;
if(*s<=0)
{
printf("Process is unblocked");
}
}
void producer(void)
{
char item;
if(wait("Ø"))
{
if(wait(&mutex))
{
printf("Enter the character data:");
scanf(" %c",&item);
buffer[front]=item;
front=(front+1)%5;
signal(&mutex);
signal(&full);
}
}
}
void consumer(void)
{
char item;
if(wait(&full))
{
if(wait(&mutex))
{
item=buffer[rear];
printf("\n consumed item: %c",item);
rear=(rear+1)%5;
signal(&mutex);
signal("Ø");
}
}
}
void view(void)
{
int i;
printf("\n Buffer data:");
printf("\n+---------------------------------+\n");
for(i=0;i<5;i++)
{
printf("| %c",buffer[i]);
}
printf("\n+---------------------------------+");
}
void main()
{
int i,choice,flag=0;
clrscr();
printf("\n 1. Insert item using Producer");
printf("\n 2. Remove item using Consumer");
printf("\n 3. View buffer data");
printf("\n 4. Exit");
do
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: producer();
break;
case 2: consumer();
break;
case 3: view();
break;
case 4: flag=1;
break;
default:printf("\n Please enter correct choice!");
break;
}
}while(flag==0);
}
/****************************OUTPUT************************************
1. Insert item using Producer
2. Remove item using Consumer
3. View buffer data
4. Exit
Enter your choice:1
Enter the character data:A

Enter your choice:1
Enter the character data:B

Enter your choice:1
Enter the character data:C

Enter your choice:1
Enter the character data:D

Enter your choice:1
Enter the character data:E

Enter your choice:3

Buffer data:
+---------------------------------+
| A| B| C| D| E
+---------------------------------+
Enter your choice:2

consumed item: A
Enter your choice:1
Enter the character data:F

Enter your choice:3

Buffer data:
+---------------------------------+
| F| B| C| D| E
+---------------------------------+
Enter your choice:4
*/

Sunday, April 18, 2010

Prim’s Algorithm

//Program  to  implement  Prim’s  Algorithm
import java.util.*;
import java.io.*;
class Graph
{
                int g[][],v,e;
                void create_graph() throws IOException
                {
                                int a,b,i,j,w;
                                BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                               
                                System.out.println("Enter no of vertices");
                                v=Integer.parseInt(in.readLine());
                                System.out.println("Enter no of edges");
                                e=Integer.parseInt(in.readLine());
                                g=new int [v+1][v+1];
                                for(i=1;i<=v;i++)
                                                for(j=1;j<=v;j++)
                                                                g[i][j]=0;
                                                for(i=1;i<=e;i++)
                                                {
                                                System.out.println("Enter no of information");
                                    a=Integer.parseInt(in.readLine()); 
            b=Integer.parseInt(in.readLine());
                                                System.out.println("Enter weight");
                                                w=Integer.parseInt(in.readLine());
                                                g[a][b]=g[b][a]=w;
                                                }
                }
               
                void prim()
                {
                                int current,d[],p[],visited[];
                                d=new int[v+1];
                                p=new int[v+1];
                                visited=new int[v+1];
                                for(int i=1;i<=v;i++)
                                {
                                                d[i]=32767;
                                                p[i]=0;
                                                visited[i]=0;
                                }
                                current=1;
                                d[current]=0;
                                visited[current]=1;
                                int c=1;
                                while (c!=v)
                                {
                                                for(int i=1;i<=v;i++)
                                                {
                                                                if(g[current][i]!=0 && visited[i]!=1)
                                                                                if (g[current][i]
                                                                                {
                                                                                                d[i]=g[current][i];
                                                                                                p[i]=current;
                                                                                }
                                                }
                                                int min=32767;
                                                for(int i=1;i<=v;i++)
                                                {
                                                                if (visited[i]!=1 && d[i]
                                                                {
                                                                                min=d[i];
                                                                                current=i;
                                                                }
                                                }
                                                visited[current]=1;
                                                c=c+1;
                                }
                                int mincost=0;
                                for(int i=1;i<=v;i++)
                                                mincost=mincost+d[i];
                                System.out.println("Minimum cost ="+mincost);
                }
}

class Graphtest
{
                public static void main(String args[]) throws IOException
                {
                                Graph G1=new Graph();
                                G1.create_graph();
                                G1.prim();
                }
               
}

/*OUTPUT:
Enter no of vertices 5
Enter no of edges 10
Enter no of information 1
2
Enter weight 5
Enter no of information 1
3
Enter weight 50
Enter no of information 2
3
Enter weight 15
Enter no of information 5
1
Enter weight 20
Enter no of information 5
3
Enter weight 25
Enter no of information 4
1
Enter weight 25
Enter no of information 1
3
Enter weight 55
Enter no of information 4
2
Enter weight 56
Enter no of information 2
5
Enter weight 58
Enter no of information 3
5
Enter weight 58
Minimum cost =65 Process Exit...*/

 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase