Java编写一个路由算法,并txt输入输出

24 篇文章 0 订阅

路由算法:1-9均代表主机名称


1. 输入:

3拓扑的输入文件为input.txt,本算法为双向线,来回只需输入一个即可

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

 

其中leftnodeID为左节点(字段名固定),rightnodeID右节点(字段名固定),bandwidth带宽,srcNodeID源节点(字段名固定),dstNodeID目的节点(字段名固定),根据算法不同,字段名可以按需增加。

 

2. 运算:

C:\Users\xwx202247>算法.exe input.txt output.txt

3. 输出:

经算法计算后的计算结果输出文件output.txt

Output.txt:

1,3,6,7

2,4,5,8




代码如下:


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];   //当前路径中的最小带宽
    static int mprev[] = new int [maxnum];   //当前节点的前一跳节点
    static int c[][] = new int [maxnum][maxnum]; // 两个节点之间的带宽
    static int hop[] = new int [maxnum]; //当前节点到源节点的跳数
    
    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];              //当前最小带宽等于v,i之间带宽
            s[i]=false;
            if(dist[i]==minint)              
                mprev[i] = 0;            //带宽等于0,则没有下一跳
            else{
                mprev[i] = v;            //否则下一跳是v,跳数为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){      //如果该点不是源点并且源点到j点路径是最短
                    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]; //最得最小带宽值
                if((!s[j])&&(least>dist[j])){ //如果当前节点到源点的路径中的带宽过小,更新当前节点最小带宽及路径
                    hop[j]=hop[u]+1;
                    mprev[j]=u;
                    dist[j]=least;
                    
                }
                else if(!s[j]&&(least == dist[j])){ //如果相等则比较跳数,跳数小者成为当前节点的路径
                    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];  //设置tmp当前节点的u节点的前一跳
            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();        //打印堆栈中数据
            }
            
        }
    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);                          //将str插入到vector中
       }
        
        String LastLine = (String)vstr.lastElement();
        String[] strdata = LastLine.split("\\,");
        int firststr = Integer.parseInt(strdata[0]);     //String字符类型数据转换为Integer整型数据,strdata[0]就是输入参数中的第一个参数字符串。
        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]);                    //排序
          Dijkstra(NodeNum,src,bdw,dist,mprev,c);
          searchPath(mprev,src,dst,output);                      
          
      }
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值