Tuesday, 27 January 2015

program to find the shortest path between any two routers in a given subnet using Dijkstra’s shortest path routing algorithm.


Write a program to find the shortest path between any two routers in a given subnet using Dijkstra’s shortest path routing algorithm.

import java.util.*;

class Dijkstra
{
            int g[][];
            int v, e;
            int d[], p[], vis[];
           
            void CreateGraph()
            {
                        int a, b, w;
                        Scanner is=new Scanner(System. in);
                        System.out.println("Enter the Number of Nodes:");
                        v=is.nextInt();
                        System.out.println("Enter the Number of Edges:");
                        e=is.nextInt();
                        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 Edges Info:");
                                    a=is.nextInt();
                                    b=is.nextInt();
                                   
                                    System.out.println("Enter the Weight of these Edges:");
                                    w=is.nextInt();
                                    g[a][b]=g[b][a]=w;
                        }
            }
           
            void CallDij()
            {
                        vis=new int[v+1];
                        d=new int[v+1];
                        p=new int[v+1];
                       
                        for(int i=1;i<=v;i++)
                                    p[i]=vis[i]=0;
                        for(int i=1;i<=v;i++)
                                    d[i]=32767;
                        Dij();
            }
           
            void Dij()
            {
                        int c, cur, min, src, dest;
                        System.out.println("Enter the Source and Destination Vertex:");
                        Scanner is=new Scanner(System.in);
                        src=is.nextInt();
                        dest=is.nextInt();
                        cur=src;
                        vis[cur]=1;
                        d[cur]=0;
                       
                        while(cur!=dest)
                        {
                                    int dc=d[cur];
                                    for(int i=1;i<=v;i++)
                                    {
                                                if(g[cur][i]!=0 && vis[i]!=1)
                                                            if(g[cur][i]+dc<d[i])
                                                            {
                                                                        d[i]=g[cur][i]+dc;
                                                                        p[i]=cur;
                                                            }
                                    }
                                    min=32767;
                                    for(int i=1;i<=v;i++)
                                    {
                                                if(vis[i]!=1 && d[i]<min)
                                                {
                                                            min=d[i];
                                                            cur=i;
                                                }
                                    }
                                    vis[cur]=1;
                        }
                        System.out.println("shortest distance ="+d[dest]);
            }
}

public class Exp2Dijkstra
{
            public static void main(String s[])
            {
                        Dijkstra g=new Dijkstra();
                        g.CreateGraph();
                        g.CallDij();
            }
}
/*Output: -
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Sameer Kobal>cd\

C:\>set path="C:\Program Files\Java\jdk1.6.0_17\bin"

C:\>javac Exp2Dijkstra.java

C:\>java Exp2Dijkstra
Enter the Number of Nodes:
5
Enter the Number of Edges:
5
Enter the Edges Info:
1 3
Enter the Weight of these Edges:
10
Enter the Edges Info:
3 5
Enter the Weight of these Edges:
20
Enter the Edges Info:
5 2
Enter the Weight of these Edges:
15
Enter the Edges Info:
2 4
Enter the Weight of these Edges:
9
Enter the Edges Info:
4 1
Enter the Weight of these Edges:
2
Enter the Source and Destination Vertex:
1 5
shortest distance =26

*/

program on flow control by implementing Sliding window protocols.


Write a program on flow control by implementing Sliding window protocols.

//sliding window program-Selective Repeat ARQ
import java.io.*;
import java.math.*;
import java.util.*;

public class ntdd9
{
            public static void main(String s[])
            {
                        Scanner is=new Scanner(System.in);
                        int a[]=new int[20];
                        int b[]=new int[8];
                        Random r=new Random();
                        int winsize, pack;
                        boolean flag=true;
                        int x,y,z,w;
                        int seq=r.nextInt(500);
                        for(int i=0;i<20;i++)
                        {
                                    a[i]=seq;
                                    seq++;
                        }
                        System.out.println("Initially sender ");
                        for(int i=0;i<20;i++)
                        {
                                    System.out.println(a[i]);
                        }
                        System.out.println("Enter initially no of data receiver will receive");
                        int f=is.nextInt();
                        System.out.println("values in receiver ");
                                   
                        for(int j=0;j<f;j++)
                        {
                                    b[j]=a[j];
                                    System.out.println(b[j]);
                        }
                        int ack=b[f-1]+1;
                        winsize=b.length-f;
                        System.out.println("window size ="+winsize);
                        System.out.println("Ack ="+ack);
                        System.out.println("Seq =");
                        for(int i=0;i<winsize;i++)
                        {
                                    System.out.println(ack+" ");
                                    ack++;
                        }
                        System.out.println();
                                   
                        if(winsize==0)
                                    System.out.println("buffer is full, data must be deleted");
                        else
                        {
                                    for(int k=0;k<winsize;k++)
                                    {
                                                b[f]=a[f];
                                                f++;
                                    }
                                    System.out.println("receiver");
                                    for(int l=0;l<f;l++)
                                    {
                                                System.out.println(b[l]+"");
                                    }
                                    int m=f;
                                   
                                    outer:while(true && flag)
                                    {
                                                System.out.println("enter packets of data to be consumed by receiver ");
                                                int n=is.nextInt();
                                                int q=0;
                                                System.out.println("receiver after consumed");
                                                for(int i=n;i<f;i++)
                                                {
                                                            System.out.println(b[i]);
                                                            q++;
                                                }
                                                winsize=f-q;
                                                System.out.println("window size "+winsize);
                                                int ackl=a[m];
                                                System.out.println("ack="+ackl);
                                                for(int i=0;i<winsize;i++)
                                                {
                                                            System.out.println(ackl+" ");
                                                            ackl++;
                                                }
                                                System.out.println();
                                                if(winsize==0)
                                                            System.out.println("buffer is full, data must be deleted");
                                                int h=0;
                                                for(int i=0;i<n;i++)
                                                {
                                                            b[h]=a[m];
                                                            if(m<19)
                                                            {
                                                                        h++;
                                                                        m++;
                                                            }
                                                            else
                                                            {
                                                                        flag=false;
                                                                        break outer;
                                                            }
                                                }
                                                System.out.println("receiver after transfer :");
                                                for(int l=0;l<f;l++)
                                                {
                                                            System.out.println(b[l]);
                                                }
                                    }
                                    System.out.println("all the data is transfered");
                                    System.out.println("data at receiver after all data transfered");
                                    for(int l=0;l<f;l++)
                                    {
                                                System.out.println(b[l]);
                                    }
                        }
            }         
}
                                                           
/*output:
Initially sender
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
Enter initially no of data receiver will receive
5
values in receiver
319
320
321
322
323
window size =3
Ack =324
Seq =
324
325
326

receiver
319
320
321
322
323
324
325
326
enter packets of data to be consumed by receiver
6
receiver after consumed
325
326
window size 6
ack=327
327
328
329
330
331
332

receiver after transfer :
327
328
329
330
331
332
325
326
enter packets of data to be consumed by receiver
4
receiver after consumed
331
332
325
326
window size 4
ack=333
333
334
335
336

receiver after transfer :
333
334
335
336
331
332
325
326
enter packets of data to be consumed by receiver
5
receiver after consumed
332
325
326
window size 5
ack=337
337
338
339
340
341

all the data is transfered
data at receiver after all data transfered
337
338
335
336
331
332
325
326
Press any key to continue...

*/

Simulation of ISO-OSI reference Model


Simulation of ISO-OSI reference Model

import java.io.*;

class osinew
{
            public static void main(String args[])throws IOException
            {
                        int stream[]=new int[40];
                        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("enter the data stream(10 bits)");
                        for(int i=22;i<32;i++)
                        {
                                    stream[i]=Integer.parseInt(br.readLine());
                        }
                       
                        System.out.println("the header added in application layer is 000");
                        stream[21]=0;
                        stream[20]=0;
                        stream[19]=0;
                       
                        for(int i=19;i<32;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the header added in presentation layer is 001");
                        stream[18]=1;
                        stream[17]=0;
                        stream[16]=0;
                        //System.out.println();
                        for(int i=16;i<32;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the header added in session layer is 010");
                        stream[15]=0;
                        stream[14]=1;
                        stream[13]=0;
                        //System.out.println();
                        for(int i=13;i<32;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the header added in transport layer is 011");
                        stream[12]=1;
                        stream[11]=1;
                        stream[10]=0;
                        //System.out.println();
                        for(int i=10;i<32;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the header added in network layer is 100");
                        stream[9]=0;
                        stream[8]=0;
                        stream[7]=1;
                        //System.out.println();
                        for(int i=7;i<32;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the header added in datalink layer is 101");//header of dll
                        stream[6]=1;
                        stream[5]=0;
                        stream[4]=1;
                        //System.out.println();
                        for(int i=4;i<32;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }System.out.println();
                        System.out.println();
                       
                        System.out.println("the trailer added in datalink layer is 110");//trailer of dll
                        stream[34]=0;
                        stream[33]=1;
                        stream[32]=1;
                       
                        //System.out.println();
                        for(int i=4;i<35;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the header added in physical layer is 000");//header of physical layer
                        stream[3]=0;
                        stream[2]=0;
                        stream[1]=0;
                        //System.out.println();
                        for(int i=1;i<35;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
                        System.out.println();
                        System.out.println("the whole stream received at the receiver side is:");
                        for(int i=1;i<35;i++)
                        {
                                    System.out.print(""+stream[i]);
                        }
                        System.out.println();
           
            }
}
/* OUTPUT
solution 1
enter the data stream(10 bits)
1
1
0
0
1
0
0
1
0
1
the header added in application layer is 000

0001100100101
the header added in presentation layer is 001

0010001100100101
the header added in session layer is 010

0100010001100100101
the header added in transport layer is 011

0110100010001100100101
the header added in network layer is 100

1000110100010001100100101
the header added in datalink layer is 101

1011000110100010001100100101
the trailer added in datalink layer is 110

1011000110100010001100100101110the header added in physical layer is 000
the whole stream received at the receiver side is:
1111011000110100010001100100101110Press any key to continue...

solution 2:
enter the data stream(10 bits)
1
1
1
1
1
0
0
0
0
0
the header added in application layer is 000
0001111100000

the header added in presentation layer is 001
0010001111100000

the header added in session layer is 010
0100010001111100000

the header added in transport layer is 011
0110100010001111100000

the header added in network layer is 100
1000110100010001111100000

the header added in datalink layer is 101
1011000110100010001111100000

the trailer added in datalink layer is 110
1011000110100010001111100000110

the header added in physical layer is 000
0001011000110100010001111100000110

the whole stream received at the receiver side is:
0001011000110100010001111100000110
Press any key to continue...

 */