⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rooterframe1.java

📁 这是一个模拟路由器的距离矢量路由算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    jTextArea2.append("3->1\t" + "9\n");
    jTextArea2.append("3->2\t" + "2\n");
    jTextArea2.append("3->4\t" + "7\n");

    jTextArea3.setText("");
    jTextArea3.append("目的\t下一站\t时间\n");
    add();
    path(3);
  //--------------------------------------------------------------
    for (int j = 0; j < nVerts; j++) { // display contents of sPath[]
      jTextArea3.append(String.valueOf(vertexList[j].label) + "\t");
      char parent = vertexList[sPath[j].parentVert].label;
      jTextArea3.append(String.valueOf(parent) + "\t");
      if (sPath[j].distance == INFINITY)
        jTextArea3.append("inf\t\n");
      else
        jTextArea3.append(String.valueOf(sPath[j].distance) + "\n");
    }
  //--------------------------------------------------------------
    cleartree();
  }

  public void router4_actionPerformed(ActionEvent e) {
    jTextArea2.setText("");
    jTextArea2.append("线路\t" + "延迟\n");
    jTextArea2.append("4->2\t" + "4\n");
    jTextArea2.append("4->1\t" + "5\n");
    jTextArea2.append("4->3\t" + "7\n");

    jTextArea3.setText("");
    jTextArea3.append("目的\t下一站\t时间\n");
    add();
    path(4);
  //--------------------------------------------------------------
    for (int j = 0; j < nVerts; j++) { // display contents of sPath[]
      jTextArea3.append(String.valueOf(vertexList[j].label) + "\t");
      char parent = vertexList[sPath[j].parentVert].label;
      jTextArea3.append(String.valueOf(parent) + "\t");
      if (sPath[j].distance == INFINITY)
        jTextArea3.append("inf\t\n");
      else
        jTextArea3.append(String.valueOf(sPath[j].distance) + "\n");
    }
  //--------------------------------------------------------------
    cleartree();


  }
  //////////////////////////////////////////////////////////
  public void addVertex(char lab)
      {
      vertexList[nVerts++] = new Vertex(lab);
      }
//---------------------------------------------------------------
   public void addEdge(int start, int end, int weight)
      {
      adjMat[start][end] = weight;  // (directed)
      adjMat[end][start] = weight;
      }
// -------------------------------------------------------------
   public void path(int startTree)                // find all shortest paths
      {
     // int startTree = 0;             // start at vertex 0
      vertexList[startTree].isInTree = true;
      nTree = 1;                     // put it in tree

      // transfer row of distances from adjMat to sPath
      for(int j=0; j<nVerts; j++)
         {
         int tempDist = adjMat[startTree][j];
         sPath[j] = new DistPar(startTree, tempDist);
         }

      // until all vertices are in the tree
      while(nTree < nVerts)
         {
         int indexMin = getMin();    // get minimum from sPath
         int minDist = sPath[indexMin].distance;

         if(minDist == INFINITY)     // if all infinite
            {                        // or in tree,
            System.out.println("There are unreachable vertices");
            break;                   // sPath is complete
            }
         else
            {                        // reset currentVert
            currentVert = indexMin;  // to closest vert
            startToCurrent = sPath[indexMin].distance;
            // minimum distance from startTree is
            // to currentVert, and is startToCurrent
            }
         // put current vertex in tree
         vertexList[currentVert].isInTree = true;
         nTree++;
         adjust_sPath();             // update sPath[] array
         }  // end while(nTree<nVerts)
      }
// -------------------------------------------------------------
     public void adjust_sPath()
        {
        // adjust values in shortest-path array sPath
        int column = 0;                // skip starting vertex
        while(column < nVerts)         // go across columns
           {
           // if this column's vertex already in tree, skip it
           if( vertexList[column].isInTree )
              {
              column++;
              continue;
              }
           // calculate distance for one sPath entry
                         // get edge from currentVert to column
           int currentToFringe = adjMat[currentVert][column];
                         // add distance from start
           int startToFringe = startToCurrent + currentToFringe;
                         // get distance of current sPath entry
           int sPathDist = sPath[column].distance;

           // compare distance from start with sPath entry
           if(startToFringe < sPathDist)   // if shorter,
              {                            // update sPath
              sPath[column].parentVert = currentVert;
              sPath[column].distance = startToFringe;
              }
           column++;
           }  // end while(column < nVerts)
     }  // end adjust_sPath()
// -------------------------------------------------------------
     //displayPaths();                // display sPath[] contents
     public void cleartree(){
     nTree = 0;                     // clear tree
     for(int j=0; j<nVerts; j++)
        vertexList[j].isInTree = false;
      }  // end path()
// -------------------------------------------------------------

   public int getMin()               // get entry from sPath
      {                              //    with minimum distance
      int minDist = INFINITY;        // assume minimum
      int indexMin = 0;
      for(int j=1; j<nVerts; j++)    // for each vertex,
         {                           // if it's in tree and
         if( !vertexList[j].isInTree &&  // smaller than old one
                               sPath[j].distance < minDist )
            {
            minDist = sPath[j].distance;
            indexMin = j;            // update minimum
            }
         }  // end for
      return indexMin;               // return index of minimum
      }  // end getMin()




  //////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////
  public void add()
  {
    vertexList = new Vertex[MAX_VERTS];
                                   // adjacency matrix
    adjMat = new int[MAX_VERTS][MAX_VERTS];
    nVerts = 0;
    nTree = 0;
    for (int j = 0; j < MAX_VERTS; j++) // 初始化邻接矩阵set 					//	adjacency
      for (int k = 0; k < MAX_VERTS; k++) //     matrix
        adjMat[j][k] = INFINITY; //     to infinity
    sPath = new DistPar[MAX_VERTS]; // shortest paths


    addVertex('0');     // 0  (start)
    addVertex('1'); // 1
    addVertex('2'); // 2
    addVertex('3'); // 3
    addVertex('4'); // 4

    addEdge(0, 1, 5); // AB 5
    addEdge(0, 3, 8); // AD 8
    addEdge(1, 2, 6); // BC 6
    addEdge(1, 3, 9); // BD 9
    addEdge(2, 4, 4); // CE 4
    addEdge(3, 2, 2); // DC 2
    addEdge(3, 4, 7); // DE 7
    addEdge(4, 1, 15); // EB 5

  }

  public void send_actionPerformed(ActionEvent e) {
    char send = sendJTextField.getText().charAt(0);
    for (int i = 0; i <nVerts; i++) {
      if((vertexList[i].label)== send)
      {
        send = vertexList[ sPath[i].parentVert].label;
        i = 0;
        sendMes.append(String.valueOf(send) + "\n");//jTextArea
      }
    }
  }

}

class RooterFrame1_send_actionAdapter
    implements ActionListener {
  private RooterFrame1 adaptee;
  RooterFrame1_send_actionAdapter(RooterFrame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.send_actionPerformed(e);
  }
}

class RooterFrame1_router4_actionAdapter
    implements ActionListener {
  private RooterFrame1 adaptee;
  RooterFrame1_router4_actionAdapter(RooterFrame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.router4_actionPerformed(e);
  }
}

class RooterFrame1_router0_actionAdapter
    implements ActionListener {
  private RooterFrame1 adaptee;
  RooterFrame1_router0_actionAdapter(RooterFrame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.router0_actionPerformed(e);
  }
}

class RooterFrame1_router1_actionAdapter
    implements ActionListener {
  private RooterFrame1 adaptee;
  RooterFrame1_router1_actionAdapter(RooterFrame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.router1_actionPerformed(e);
  }
}

class RooterFrame1_router2_actionAdapter
    implements ActionListener {
  private RooterFrame1 adaptee;
  RooterFrame1_router2_actionAdapter(RooterFrame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.router2_actionPerformed(e);
  }
}

class RooterFrame1_router3_actionAdapter
    implements ActionListener {
  private RooterFrame1 adaptee;
  RooterFrame1_router3_actionAdapter(RooterFrame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.router3_actionPerformed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -