Write a routing algorithm in Java, and input and output in txt.

Source: Internet
Author: User

Write a routing algorithm in Java, and input and output in txt.

Routing Algorithm: 1-9 represents the Host Name


1. input:

Figure 3 the input file of the topology is input.txt, and the algorithm is bidirectional. You only need to enter one line back and forth.

Input.txt:

LeftnodeID, rightnodeID, bandwidth

1, 2, 100

1, 4, 100

2, 3, 100

2, 2, 100

3, 4, 100

100

3, 6, 100

100

100

5, 6, 100

100

5, 8, 100

100

100

 

;

SrcNodeID, dstNodeID, bandwidth

1, 7, 90

1, 8, 90

 

Here, leftnodeID is left node (fixed field name), rightnodeID is right node (fixed field name), bandwidth, srcNodeID source node (fixed field name), dstNodeID target node (fixed field name ), based on different algorithms, field names can be added as needed.

 

2. Operation:

C: \ Users \ xwx202247> algorithm .exe input.txt output.txt

3. Output:

Calculation result output file output.txt after calculation

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]; // The minimum bandwidth in the current path
Static int mprev [] = new int [maxnum]; // The first hop node of the current node
Static int c [] [] = new int [maxnum] [maxnum]; // bandwidth between two nodes
Static int hop [] = new int [maxnum]; // Number of hops from the current node to the source node

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 is equal to v, and the bandwidth between I
S [I] = false;
If (dist [I] = minint)
Mprev [I] = 0; // If the bandwidth is equal to 0, no next hop exists.
Else {
Mprev [I] = v; // otherwise, the next hop is v and the number of hops 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 vertex is not the Source Vertex and the path from the Source Vertex to the j vertex 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]; // The minimum bandwidth.
If ((! S [j]) & (least> dist [j]) {// If the bandwidth from the current node to the source node is too small, update the minimum bandwidth and path of the current node.
Hop [j] = hop [u] + 1;
Mprev [j] = u;
Dist [j] = least;

}
Else if (! S [j] & (least = dist [j]) {// compare the number of hops if the number of hops is equal. The Skip node 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 first hop of the u node of the current tmp 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 (); // print data in the stack
}

}
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 (new File (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); // insert str into the vector.
}

String LastLine = (String) vstr. lastElement ();
String [] strdata = LastLine. split ("\\,");
Int firststr = Integer. parseInt (strdata [0]); // convert String data to Integer data. strdata [0] is the first parameter String in the input parameter.
Int secondstr = Integer. parseInt (strdata [1]);
If (firststr <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 );

}
}

}

Related Article

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.