Java writes a routing algorithm, and txt input and output

Source: Internet
Author: User

Routing algorithm: 1-9 represents host Name


1. Enter:

Figure 3 topologies the input file is Input.txt , the algorithm is two-way line, only one can be entered back and forth

Input.txt :

Leftnodeid,rightnodeid,bandwidth

1,3,100

1,4,100

2,3,100

2,4,100

3,4,100

3,5,100

3,6,100

4,5,100

4,6,100

5,6,100

5,7,100

5,8,100

6,7,100

6,8,100

;

Srcnodeid,dstnodeid,bandwidth

1,7,90

1,8,90

Where Leftnodeid is left node (field name fixed), Rightnodeid right node (field name fixed), bandwidth bandwidth, Srcnodeid source node (field name fixed), Dstnodeid destination node (fixed field name), according to the algorithm, Field names can be increased as needed.

2. Operation:

c:\users\xwx202247> algorithm . exe Input.txt output.txt

3. Output:

calculation result output file after calculation of the algorithm output.txt

Output.txt:

1,3,6,7

2,4,5,8




The code is as follows:


Import java.io.*;

Import java.util.*;
public class Routedesign {
Final static int maxnum = 100;
Final static int minint = 0;
Final static int maxint = 999999;
static int dist [] = new int [Maxnum]; Minimum bandwidth in the current path
static int mprev[] = new int [Maxnum]; The previous hop node of the current node
static int c[][] = new int [Maxnum][maxnum]; Bandwidth between two nodes
static int hop[] = new int [Maxnum]; Current node-to-source node hop count

public static void Dijkstra (int n,int v,int b,int dist[],int mprev[],int c[][]) {
Boolean s[] = new Boolean[maxnum];

for (int i=1;i<=n;i++) {
Dist[i] = C[v][i]; The current minimum bandwidth equals the bandwidth between v,i
S[i]=false;
if (dist[i]==minint)
Mprev[i] = 0; Bandwidth equals 0, no next hop
else{
Mprev[i] = v; Otherwise the next hop is V, and the hop count is 1
Hop[i]=1;
}

}
DIST[V] = maxint;
S[V] = true;
for (int i=2;i<=n;i++) {
int tmp = b;
int u = v;
for (int j=1;j<=n;j++) {
if (!s[j]&&dist[j]>=tmp) {//If the point is not the source point and the source point to the J-point path is the shortest
U=j;
TMP=DIST[J];
}
}
S[u]=true;
for (int j=1;j<=n;j++) {
int least = Dist[u];
if (C[u][j]<dist[u])
LEAST=C[U][J]; Maximum minimum bandwidth value
if ((!s[j) && (Least>dist[j])) {//If the bandwidth in the path of the current node to the source point is too small, update the current node's minimum bandwidth and path
hop[j]=hop[u]+1;
Mprev[j]=u;
Dist[j]=least;

}
else if (!s[j]&& (least = = Dist[j])) {//If equality compares hops, the lesser of hops becomes the path of the current node
if (hop[j]>hop[u]+1) {
hop[j]=hop[u]+1;
mprev[j]= u;
Dist[j]=least;
}

}

}
}
}

public static void SearchPath (int mprev[],int v,int u,string output) throws filenotfoundexception{
OutputStream out = new FileOutputStream (output,true);

int que[] = new Int[maxnum];
int tot=1;
Que[tot]=u;
tot++;
int tmp = Mprev[u]; Sets the previous hop of the U node of the TMP current node
while (TMP!=V) {
Que[tot] =tmp;
tot++;
TMP=MPREV[TMP];
}
Que[tot]=v;
for (int i = tot;i>=i;i--) {
if (i!=1) {
int num=que[i];
try{
Out.write (string.valueof (num). GetBytes ());
Out.write (",". GetBytes ());
}catch (IOException e) {
E.printstacktrace ();
}
}
else{
try{
Out.write (String.valueof (Que[i]). GetBytes ());
Out.write ("\ r \ n". GetBytes ());
}catch (IOException e) {
E.printstacktrace ();
}
}
}
try{
Out.close ();
}catch (IOException e) {
E.printstacktrace (); Printing data in stacks
}

}
public static void Main (string[] args) throws IOException {
TODO auto-generated Method Stub
String input = args[0];
String output = args[1];
BufferedReader in = new BufferedReader (new InputStreamReader (new FileInputStream (input)));
String str = new string ();
int nodenum=0;
int linenum=0;
Vector<string>vstr = new vector<string> ();
Vector<string>dstr = new vector<string> ();

str = In.readline ();
while (true) {
Str=in.readline ();
if (Str.isempty ()) {
Break
}
Vstr.add (str);
linenum++;
}
In.readline ();
In.readline ();
while (true) {
Str=in.readline ();
if (str==null)
Break
else if (Str.isempty ())
Break
Dstr.add (str); Inserting str into the vector
}

String lastline = (string) vstr.lastelement ();
string[] strdata = lastline.split ("\ \,");
int firststr = Integer.parseint (strdata[0]); String character type data is converted to integer data, Strdata[0] is the first parameter string in the input parameter.
int secondstr = Integer.parseint (strdata[1]);
if (FIRSTSTR&LT;SECONDSTR)
Nodenum = Secondstr;
Else
Nodenum = Firststr;
for (int i=1;i<nodenum;i++) {
for (int j=1;j<nodenum;j++) {
C[i][j]=minint;
}

}

for (int i=1;i<=linenum;i++) {
String readvstr = (string) vstr.get (i-1);
String [] Vstrdata = readvstr.split ("\ \,");
int firstvstr = Integer.parseint (vstrdata[0]);
int secondvstr = Integer.parseint (vstrdata[1]);
int thirdvstr = Integer.parseint (vstrdata[2]);
if (Thirdvstr>c[firstvstr][secondvstr]) {
C[FIRSTVSTR][SECONDVSTR]=THIRDVSTR;
C[SECONDVSTR][FIRSTVSTR]=THIRDVSTR;

}
}
for (int i=1;i<nodenum;i++) {
Dist[i]=minint;
Hop[i]=minint;

}
int SRC,DST,BDW;
OutputStream out = new FileOutputStream (output,false);
Out.write ("". GetBytes ());
Out.close ();
for (int i=1;i<=dstr.size (); i++) {
String readvstr = (string) dstr.get (i-1);
String sdstr[] =readvstr.split (",");
src = integer.parseint (sdstr[0]);
DST = Integer.parseint (sdstr[1]);
BDW = Integer.parseint (sdstr[2]); Sort
Dijkstra (NODENUM,SRC,BDW,DIST,MPREV,C);
SearchPath (Mprev,src,dst,output);

}
}

}

Java writes a routing algorithm, and txt input and output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.