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...*/

0/1 Knapsack problem using Dynamic Algo

import java.io.*;
class KSdyn
{
   public static void main(String args[])throws IOException
 {
   BufferedReader o=new BufferedReader(new InputStreamReader(System.in));
   int n,i,j;
  
   System.out.println("Enter the no of item");
   n=Integer.parseInt(o.readLine());
  
   int p[]=new int[n+1];
  
   int w[]=new int[n+1];
  
   System.out.println("Enter the profit");
   for(i=1;i<=n;i++)
   {
       p[i]=Integer.parseInt(o.readLine());
   }
   System.out.println("Enter the weight");
   for(j=1;j<=n;j++)
   {
       w[j]=Integer.parseInt(o.readLine());
   }
   System.out.println("enter the capacity of knapsack");
   int m=Integer.parseInt(o.readLine());
   knapsack(n,m,w,p);
 }
 static void knapsack(int n,int m,int w[],int p[])
 {
     int s[][]=new int[n+1][m+1];
    for(int W=0;W<=m;W++)
        s[0][W]=0;
    for(int i=0;i<=n;i++)
        s[i][0]=0;
    for(int i=1;i<=n;i++)
      for(int j=0;j<=m;j++)
        if (w[i]<=j)
         {
           if(p[i]+s[i-1][j-w[i]]>s[i-1][j])
              s[i][j]=p[i]+s[i-1][j-w[i]];
              else
              s[i][j]=s[i-1][j];
         }
         else
           s[i][j]=s[i-1][j];
     for(int i=0;i<=n;i++)
     
      {
      for(int j=0;j<=m;j++)
      {
      System.out.print(s[i][j]+" ");
      }
      System.out.println();
      }
        knapsack_item(s,w,n,m);  
 }
 static void knapsack_item(int s[][],int w[],int n,int m)
 {   int ans[]=new int[n];
     int i=n;
    int k=m;
    while (i>0&&k>0)
    {
        if (s[i][k]!=s[i-1][k])
        {
          ans[i]=1;
          k=k-w[i];
          //System.out.println("Item"+i+"is selected");
        }
        i--;
    }
   
    for(i=0;i
       if (ans[i]==1)
       {
           System.out.println("Item"+i+"is selected");
       }
   
 }
}/*Output:
Enter the no of item
4
Enter the profit
3
4
5
6
Enter the weight
2
3
4
5
enter the capacity of knapsack
5
0 0 0 0 0 0
0 0 3 3 3 3
0 0 3 4 4 7
0 0 3 4 5 7
0 0 3 4 5 7
Item1is selected
Item2is selected
*/             
 
ShareThis

Visitor

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