Professional Documents
Culture Documents
1.merge Sort Using Divide and Conquer
1.merge Sort Using Divide and Conquer
import java.util.*;
int i,j,min=999,num_edges=1,a=1,b=1,minpos_i=1,minpos_j=1;
while(num_edges < n)
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(this.cost[i][j]<min)
if(this.isVisited[i]!=0)
min=this.cost[i][j];
a=minpos_i=i;
b=minpos_j=j;
if(this.isVisited[minpos_i]==0 || this.isVisited[minpos_j]==0)
this.minimum_cost=this.minimum_cost+min;
num_edges=num_edges+1;
this.isVisited[b]=1;
this.cost[a][b]=this.cost[b][a]=999;
}
int nodes,i,j;
nodes = in.nextInt();
for(i=1;i<=nodes;i++)
for(j=1;j<=nodes;j++)
p.cost[i][j]=in.nextInt();
if(p.cost[i][j]==0)
p.cost[i][j]=999;
p.isVisited[1]=1; // Initialization
p.calc(nodes);
}
7.Dijkstra's algorithm
import java.util.Scanner; //Scanner Function to take in the Input Values
public class Dijkstra
{
static Scanner scan; // scan is a Scanner Object
public static void main(String[] args)
{
int[] preD = new int[5];
int min = 999, nextNode = 0; // min holds the minimum value, nextNode holds the value
for the next node.
scan = new Scanner(System.in);
int[] distance = new int[5]; // the distance matrix
int[][] matrix = new int[5][5]; // the actual matrix
int[] visited = new int[5]; // the visited array
System.out.println("Enter the cost matrix");
for (int i = 0; i < distance.length; i++)
{
visited[i] = 0; //initialize visited array to zeros
preD[i] = 0;
for (int j = 0; j < distance.length; j++)
{
matrix[i][j] = scan.nextInt(); //fill the matrix
if (matrix[i][j]==0)
matrix[i][j] = 999; // make the zeros as 999
}
}
distance = matrix[0]; //initialize the distance array
visited[0] = 1; //set the source node as visited
distance[0] = 0; //set the distance from source to source to zero which is the starting
point
for (int counter = 0; counter < 5; counter++)
{
min = 999;
for (int i = 0; i < 5; i++)
{
if (min > distance[i] && visited[i]!=1)
{
min = distance[i];
nextNode = i;
}
}
visited[nextNode] = 1;
for (int i = 0; i < 5; i++)
{
if (visited[i]!=1)
{
if (min+matrix[nextNode][i] < distance[i])
{
distance[i] = min+matrix[nextNode][i];
preD[i] = nextNode;
}
}
}
}
for(int i = 0; i < 5; i++)
System.out.print("|" + distance[i]);
System.out.println("|");
int j;
for (int i = 0; i < 5; i++)
{
if (i!=0)
{
System.out.print("Path = " + i);
j = i;
do
{
j = preD[j];
System.out.print(" <- " + j);
}
while(j != 0);
}
System.out.println();
}
}
}
http://campuscoke.blogspot.com/2015/01/distance-vector-
routing-dvr-algorithm.html
import java.io.*;
public class DVR
{
static int graph[][];
static int via[][];
static int rt[][];
static int v;
static int e;
System.out.print("Please enter the Source Node for the edge whose cost has changed: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Please enter the Destination Node for the edge whose cost has changed:
");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Please enter the new cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
}
output:-
Please enter the Source Node for the edge whose cost has changed: 2
Please enter the Destination Node for the edge whose cost has changed: 4
Please enter the new cost: 10
#include
#include
#define LIMIT 10
#define INFINITY 10000
int m,n,k,i,j,dist[LIMIT][LIMIT],sn,dn,min=0;
}node[LIMIT];
main()
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
dist[i][j]=-1; for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(dist[i][j]==-1)
if(i==j)
dist[i][j]=0;
printf(" 0");
} else
scanf("%d",&m);
dist[i][j]=dist[j][i]=m;
Else
printf(" %d",dist[i][j]);
for(i=1;i<=n;i++) { printf("\n");
node[i].from=0;
for(j=1;j<=n;j++)
printf("\t%d",dist[i][j]);
for(i=1;i<=n;i++)
k=1; for(j=1;j<=n;j++
if(dist[i][j]>0)
node[i].from++;
if(dist[j][i]>0)
node[i].hello[k++]=j;
for(i=1;i<=n;i++)
for(j=1;j<=node[i].from;j++)
printf("\n%d",node[i].hello[j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(dist[i][j]>0)
for(i=1;i<=n;i++)
{
printf("\nLINK STATE PACKET FOR %d",i);
printf("\n|-------------|");
printf("\n| %d |",i);
printf("\n|-------------|");
printf("\n|-------------|");
printf("\n|-------------|");
for(j=1;j<=n;j++)
if(dist[i][j]>0)
printf("\n| %d | %d |",j,dist[i][j]);
printf("\n|-------------|");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(dist[i][j]>0)
printf("%d",j);
for(j=1;j<=node[i].from;j++)
printf("%d is %d",node[i].hello[j],dist[i][node[i].hello[j]]);
scanf("%d%d",&sn,&dn); min=spath(sn,dn,min);
getch();
struct state
enum{permanent,tentative}label;
}state[LIMIT];
int i=1,k;
for(p=&state[i];p<=&state[n];p++)
p->pred=0;
p->len=INFINITY;
p->label=tentative;
state[t].len=0;
state[t].label=permanent;
k=t;
do
for(i=1;i<=n;i++)
if(state[k].len+dist[k][i]
state[i].pred=k; state[i].len=state[k].len+dist[k][i];
}
k=0;
min=INFINITY;
for(i=1;i<=n;i++)
min=state[i].len; k=i;
state[k].label=permanent;
while(k!=s);
return(min);
publicclassDNS
{
publicstaticvoidmain(String[]args)
{
int;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("\n Menu:\n 1. DNS 2. Reverse DNS 3. Exit \n");
System.out.println("\nEnteryourchoice");
n=Integer.parseInt(System.console().readLine());
if(n==1)
{
try
{
System.out.println("\nEnterHostName");
Stringname=in.readLine();
InetAddressaddress;
address=InetAddress.getByName(hname);
System.out.println("HostName“+address.getHostName());
System.out.println("IP:"+address.getHostAddress());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
if(n==2)
{
try
{
System.out.println("\nEnterIPaddress");
Stringipstr=in.readLine();
InetAddressia=InetAddress.getByName(ipstr);
System.out.println("IP:"+ipstr);
System.out.println("HostName:"+ia.getHostName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}while(!(n==3));
}
}