算法

来源:互联网 发布:js中createelement 编辑:程序博客网 时间:2024/05/29 11:44
      http://blog.csdn.net/qq_26122557/article/details/77104961
http://blog.csdn.net/qq_26122557/article/details/77104961
  1. //消费节点,定义为一个链表的头  
  2. public class consumptionNode {  
  3.     private int consumptionNodeId;//消费节点id,用于确定哪一个消费节点  
  4.     private LinkNode nextLinkNodeId;//链路节点id,用于确定哪一个链路节点  
  5.     private int bandwidthRequirement;//定义一个参数保存贷款需求  
  6.     public consumptionNode(int consumptionNodeId,LinkNode nextLinkNodeId,int bandwidthRequirement){  
  7.         this.consumptionNodeId=consumptionNodeId;  
  8.         this.nextLinkNodeId=nextLinkNodeId;  
  9.         this.bandwidthRequirement=bandwidthRequirement;  
  10.     }  
  11.       
  12.       
  13.       
  14.       
  15.       
  16.       
  17.       
  18.     public int getConsumptionNodeId() {  
  19.         return consumptionNodeId;  
  20.     }  
  21.     public void setConsumptionNodeId(int consumptionNodeId) {  
  22.         this.consumptionNodeId = consumptionNodeId;  
  23.     }  
  24.     public LinkNode getNextLinkNodeId() {  
  25.         return nextLinkNodeId;  
  26.     }  
  27.     public void setNextLinkNodeId(LinkNode nextLinkNodeId) {  
  28.         this.nextLinkNodeId = nextLinkNodeId;  
  29.     }  
  30.     public int getBandwidthRequirement() {  
  31.         return bandwidthRequirement;  
  32.     }  
  33.     public void setBandwidthRequirement(int bandwidthRequirement) {  
  34.         this.bandwidthRequirement = bandwidthRequirement;  
  35.     }  
  36.       
  37.   
  38. }  

[java] view plain copy print?
  1. public class EmptyQueueException extends Exception {  
  2.   
  3.     public EmptyQueueException(String string) {  
  4.         // TODO Auto-generated constructor stub  
  5.     }  
  6.   
  7. }  

[java] view plain copy print?
  1. import java.io.BufferedReader;  
  2. import java.io.BufferedWriter;  
  3. import java.io.Closeable;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.util.LinkedList;  
  9. import java.util.List;  
  10.   
  11. public final class FileUtil//util利用  
  12. {  
  13.     /**  
  14.      * read file and output 
  15.      * @param filePath 
  16.      * @param spec//spec规格  
  17.      * @return 
  18.      * @author 
  19.      * @since 2016-3-1 
  20.      */  
  21.     public static String[] read(final String filePath, final Integer spec)  
  22.     {  
  23.         File file = new File(filePath);//获取文件eg. File file = new File("D:/hust/file.txt");  
  24.         // file check  
  25.         if ((!isFileExists(file)) ||   
  26.                 (!file.canRead()))  
  27.         {  
  28.             System.out.println("file [" + filePath + "] is not exist or cannot read!!!");  
  29.             return null;  
  30.         }  
  31.           
  32.         List<String> lines = new LinkedList<String>();  
  33.         BufferedReader br = null;  
  34.         FileReader fb = null;  
  35.         try  
  36.         {  
  37.             fb = new FileReader(file);//使用带有指定文件的String参数的构造方法。创建该输入流对象。并关联源文件。  
  38.             br = new BufferedReader(fb);  
  39.   
  40.             String str = null;  
  41.             int index = 0;  
  42.             while (((spec ==  null) || index++ < spec) && (str = br.readLine()) != null)  
  43. //readline表示该方法读取一行文本,当遇到换行符"\n",回车符"\r"或者回车符后面紧跟着换行符时,该行结束并返回。  
  44. //没有数据时,将会一直处于等待状态。(这里的一行不是物理的一行,而是以分隔号界定的一行)  
  45.             {  
  46.                 lines.add(str);  
  47.             }  
  48.         }  
  49.         catch (IOException e)  
  50.         {  
  51.             e.printStackTrace();  
  52.         }  
  53.         finally  
  54.         {  
  55.             closeQuietly(br);  
  56.             closeQuietly(fb);  
  57.         }  
  58.   
  59.         return lines.toArray(new String[lines.size()]);  
  60.     }  
  61.     /**  
  62.      * write file 
  63.      * @param filePath  
  64.      * @param content  
  65.      * @param append true or false 
  66.      * @return 
  67.      * @author s00274007 
  68.      * @since 2016-3-1 
  69.      */                       
  70.     public static int write (final String filePath, final String[] contents, final boolean append)  
  71.     {  
  72.         File file = new File(filePath);  
  73.         if (contents == null)  
  74.         {  
  75.             System.out.println("file [" + filePath + "] invalid!!!");//invalid无效  
  76.             return 0;  
  77.         }  
  78.   
  79.         if (isFileExists(file) && (!file.canRead()))  
  80.         {  
  81.             return 0;  
  82.         }  
  83.   
  84.         FileWriter fw = null;  
  85.         BufferedWriter bw = null;  
  86.         try  
  87.         {  
  88.             if (!isFileExists(file))  
  89.             {  
  90.                 file.createNewFile();  
  91.             }  
  92.   
  93.             fw = new FileWriter(file, append);  
  94.             bw = new BufferedWriter(fw);  
  95.             for (String content : contents)//遍历contents  
  96.             {  
  97.                 if (content == null)  
  98.                 {  
  99.                     continue;  
  100.                 }  
  101.                 bw.write(content);  
  102.                 bw.newLine();  
  103.             }  
  104.         }  
  105.         catch (IOException e)  
  106.         {  
  107.             e.printStackTrace();  
  108.             return 0;  
  109.         }  
  110.         finally  
  111.         {  
  112.             closeQuietly(bw);  
  113.             closeQuietly(fw);  
  114.         }  
  115.   
  116.         return 1;  
  117.     }  
  118.   
  119.     private static void closeQuietly(Closeable closeable)  
  120.     {  
  121.         try  
  122.         {  
  123.             if (closeable != null)  
  124.             {  
  125.                 closeable.close();  
  126.             }  
  127.         }  
  128.         catch (IOException e)  
  129.         {  
  130.         }  
  131.     }  
  132.   
  133.     private static boolean isFileExists(final File file)  
  134.     {  
  135.         if (file.exists() && file.isFile())  
  136.         {  
  137.             return true;  
  138.         }  
  139.   
  140.         return false;  
  141.     }  
  142.   
  143. }  

[java] view plain copy print?
  1. import java.util.LinkedList;  
  2. import java.util.List;  
  3.   
  4. public class Graphs {  
  5.       
  6.     List<consumptionNode> root=new LinkedList<>();  
  7.     List<rootLinkNode> LinkRoot=new LinkedList<>();  
  8.     private int consumptionNodeId;//消费节点id,用于确定哪一个消费节点  
  9.     private LinkNode nextLinkNodeId;//链路节点id,用于确定哪一个链路节点  
  10.     private int bandwidthRequirement;//定义一个参数保存贷款需求  
  11.   
  12.       
  13.       
  14.     //text测试用例  
  15.     //public void getinfo(){  
  16.     //  int num=5;  
  17.     //  for(int i=0;i<num;i++){  
  18.     //      consumptionNodeId=i;  
  19.     //      nextLinkNodeId=null;  
  20.     //      bandwidthRequirement=i;  
  21.     //      addNode( consumptionNodeId, nextLinkNodeId, bandwidthRequirement);  
  22.     //  }  
  23.     //  for(int i=0;i<num;i++){  
  24.     //      int m=root.get(i).getConsumptionNodeId();  
  25.     //      System.out.println();  
  26.     //        
  27.     //  }  
  28.     //    
  29.     //}  
  30.       
  31.       
  32.       
  33.       
  34.     //text2  
  35.     public void getInfo2(){  
  36.         int size=5;  
  37.         buildLinkRootList(size);  
  38.         for(int i=0;i<size;i++){  
  39.             int preNodeId=i;  
  40.                     setNextLinkNodeId(null);  
  41.                     int localNodeId=i;  
  42.                     int totalBandwidth=i;  
  43.                     addLinkNode( i, i+1,i,i,i);  
  44.         }  
  45.           
  46.         for(int i=0;i<size;i++){  
  47.             System.out.println(LinkRoot.get(i).getLocalNodeId()+"其临接链表是"+LinkRoot.get(i).getNextLinkNode().getLocalNodeId());  
  48.         }  
  49.     }  
  50.       
  51.       
  52.       
  53.       
  54.       
  55.       
  56.     //这个方法是添加根节点的方法  
  57.     public void addNode(int total,int consumptionNodeId,LinkNode nextLinkNodeId,int bandwidthRequirement){  
  58.         int totalConsumptionNode = 0;  
  59.           
  60.         for(int i=0;i<totalConsumptionNode;i++){  
  61.             consumptionNode e=new consumptionNode(consumptionNodeId, nextLinkNodeId, bandwidthRequirement);  
  62.             root.add(e);  
  63.         }  
  64.               
  65.     }  
  66.     public void addNode(int consumptionNodeId,LinkNode nextLinkNodeId,int bandwidthRequirement){  
  67.             consumptionNode e=new consumptionNode(consumptionNodeId, nextLinkNodeId, bandwidthRequirement);  
  68.             root.add(e);  
  69.                   
  70.     }  
  71.     //返回root的制定元素  
  72.     public consumptionNode returnNode(int i){   
  73.         return this.root.get(i);  
  74.           
  75.     }  
  76.       
  77.       
  78.       
  79.     //添加网络节点根节点的方法  
  80.     public void addRootLinkNode(int i){  
  81.         rootLinkNode e=new rootLinkNode(i,null);  
  82.         LinkRoot.add(e);  
  83.     }  
  84.       
  85.   
  86.       
  87.       
  88.       
  89.       
  90.     //这个方法用于创建临接表表头节点  
  91.     public void buildLinkRootList(int size){  
  92.         for(int i=0;i<size;i++){  
  93.             rootLinkNode rln=new rootLinkNode(i,null);  
  94.             LinkRoot.add(rln);  
  95.               
  96.         }  
  97.           
  98.     }  
  99.       
  100.       
  101.       
  102.       
  103.       
  104.       
  105.       
  106.     //这个方法是添加临接点的方法  
  107.     public void addLinkNode(int preNodeId,int localNodeId,int totalBandwidth,int nextNodeId,int unitRentalCost){  
  108.         int addPreId=localNodeId;  
  109.         LinkNode nextLinkNode=new LinkNode(addPreId,nextNodeId,1000,null,1);//  
  110.         LinkNode LN=new LinkNode( preNodeId,localNodeId,totalBandwidth,nextLinkNode,unitRentalCost);  
  111.         LinkRoot.get(preNodeId).setNextLinkNodeForRoot(LN);  
  112.           
  113.     }  
  114.   
  115.   
  116.   
  117.   
  118.   
  119.   
  120.     public int getConsumptionNodeId() {  
  121.         return consumptionNodeId;  
  122.     }  
  123.   
  124.   
  125.   
  126.   
  127.   
  128.   
  129.     public void setConsumptionNodeId(int consumptionNodeId) {  
  130.         this.consumptionNodeId = consumptionNodeId;  
  131.     }  
  132.   
  133.   
  134.   
  135.   
  136.   
  137.   
  138.     public LinkNode getNextLinkNodeId() {  
  139.         return nextLinkNodeId;  
  140.     }  
  141.   
  142.   
  143.   
  144.   
  145.   
  146.   
  147.     public void setNextLinkNodeId(LinkNode nextLinkNodeId) {  
  148.         this.nextLinkNodeId = nextLinkNodeId;  
  149.     }  
  150.   
  151.   
  152.   
  153.   
  154.   
  155.   
  156.     public int getBandwidthRequirement() {  
  157.         return bandwidthRequirement;  
  158.     }  
  159.   
  160.   
  161.   
  162.   
  163.   
  164.   
  165.     public void setBandwidthRequirement(int bandwidthRequirement) {  
  166.         this.bandwidthRequirement = bandwidthRequirement;  
  167.     }  
  168.       
  169.       
  170.       
  171.       
  172.       
  173.     public List<consumptionNode> getroot() {  
  174.         return this.root;  
  175.     }  
  176.   
  177.   
  178.   
  179.   
  180.   
  181.   
  182.       
  183.   
  184.   
  185. }  

[java] view plain copy print?
  1. public class LinkNode {  
  2.     private int localNodeId;  
  3.     private int totalBandwidth;  
  4.     private int upBandwidth;  
  5.     private int downBandwidth;  
  6.     private LinkNode nextLinkNode;  
  7.     private int preNodeId;  
  8.     private int unitRentalCost;  
  9.     //所有链路节点的preNodeId保存为链路根节点  
  10.     public LinkNode(int preNodeId,int localNodeId,int totalBandwidth,LinkNode nextLinkNode,int unitRentalCost){  
  11.         this.localNodeId=localNodeId;  
  12.         this.preNodeId=preNodeId;  
  13.         this.totalBandwidth=totalBandwidth;  
  14.         this.upBandwidth=totalBandwidth;  
  15.         this.downBandwidth=totalBandwidth;  
  16.         this.setNextLinkNode(nextLinkNode);  
  17.         this.unitRentalCost=unitRentalCost;  
  18.           
  19.     }  
  20.       
  21.       
  22.       
  23.       
  24.       
  25.       
  26.     public int getLocalNodeId() {  
  27.         return localNodeId;  
  28.     }  
  29.     public void setLocalNodeId(int localNodeId) {  
  30.         this.localNodeId = localNodeId;  
  31.     }  
  32.     public int getTotalBandwidth() {  
  33.         return totalBandwidth;  
  34.     }  
  35.     public void setTotalBandwidth(int totalBandwidth) {  
  36.         this.totalBandwidth = totalBandwidth;  
  37.     }  
  38.     public int getUpBandwidth() {  
  39.         return upBandwidth;  
  40.     }  
  41.     public void setUpBandwidth(int upBandwidth) {  
  42.         this.upBandwidth = upBandwidth;  
  43.     }  
  44.     public int getDownBandwidth() {  
  45.         return downBandwidth;  
  46.     }  
  47.     public void setDownBandwidth(int downBandwidth) {  
  48.         this.downBandwidth = downBandwidth;  
  49.     }  
  50.     public LinkNode getNextLinkNode() {  
  51.         return nextLinkNode;  
  52.     }  
  53.     public void setNextLinkNode(LinkNode nextLinkNode) {  
  54.         this.nextLinkNode = nextLinkNode;  
  55.     }  
  56.   
  57.   
  58.   
  59.   
  60.   
  61.   
  62.     public int getPreNodeId() {  
  63.         return preNodeId;  
  64.     }  
  65.   
  66.   
  67.   
  68.   
  69.   
  70.   
  71.     public void setPreNodeId(int preNodeId) {  
  72.         this.preNodeId = preNodeId;  
  73.     }  
  74.   
  75. }  

[java] view plain copy print?
  1. public class LLNode {  
  2.     private int data;  
  3.     private LLNode nextNode;  
  4.     public LLNode(int data,LLNode nextNode){  
  5.         this.setDataForLLNode(data);  
  6.         this.setNextNodeForLLNode(nextNode);          
  7.     }  
  8.     public LLNode(int data){  
  9.         this.data=data;  
  10.         this.nextNode=null;  
  11.           
  12.     }  
  13.     public int getDataForLLNode() {  
  14.         return data;  
  15.     }  
  16.     public void setDataForLLNode(int data) {  
  17.         this.data = data;  
  18.     }  
  19.     public LLNode getNextNodeForLLNode() {  
  20.         return nextNode;  
  21.     }  
  22.     public void setNextNodeForLLNode(LLNode nextNode) {  
  23.         this.nextNode = nextNode;  
  24.     }  
  25. }  

[java] view plain copy print?
  1. public class LLQueue {  
  2.     private LLNode frontNode;  
  3.     private LLNode rearNode;  
  4.     public LLQueue(){  
  5.         this.setFrontNode(null);  
  6.         this.setRearNode(null);  
  7.     }  
  8.     private LLQueue(LLNode frontNode,LLNode rearNode){  
  9.         this.setFrontNode(frontNode);  
  10.         this.setRearNode(rearNode);  
  11.           
  12.     }  
  13.     public LLNode getFrontNode() {  
  14.         return frontNode;  
  15.     }  
  16.     public void setFrontNode(LLNode frontNode) {  
  17.         this.frontNode = frontNode;  
  18.     }  
  19.     public LLNode getRearNode() {  
  20.         return rearNode;  
  21.     }  
  22.     public void setRearNode(LLNode rearNode) {  
  23.         this.rearNode = rearNode;  
  24.     }  
  25.     public static LLQueue createQueue(){  
  26.         return new LLQueue();  
  27.     }  
  28.     public boolean isEmpty(){  
  29.         return (frontNode==null);  
  30.         //为空则返回1,不为空返回0  
  31.     }  
  32.     public void enQueue(int data){  
  33.         LLNode newNode=new LLNode(data);  
  34.         if(rearNode!=null){  
  35.             rearNode.setNextNodeForLLNode(newNode);  
  36.         }  
  37.         rearNode=newNode;  
  38.         if(frontNode==null){  
  39.             frontNode=newNode;  
  40.         }  
  41.     }  
  42.     public int deQueue(){  
  43.         int data;//int 类型不能赋值为null  
  44.         if(isEmpty()){  
  45.             System.out.println("这个队列是空的");    
  46.             return -1;  
  47.         }else{  
  48.             data=frontNode.getDataForLLNode();  
  49.             frontNode=frontNode.getNextNodeForLLNode();  
  50.         }  
  51.         return data;  
  52.     }  
  53. }  
[java] view plain copy print?
  1. //public class Miracle2 {  
  2.   
  3. //}  
  4. import java.util.Collection;  
  5. import java.util.LinkedHashMap;  
  6. import java.util.LinkedList;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9. import java.util.Queue;  
  10.   
  11.  /* 
  12.   * 求解无向图的单源最短路径 
  13.   */  
  14.  public class Miracle2 {  
  15.        
  16.        
  17.       
  18.        
  19.        
  20.       
  21.      public class Vertex{//这个方法定义顶点的基本属性  
  22.            
  23.            
  24.            
  25.            
  26.            
  27.            
  28.          private String vertexLabel;//顶点标识  
  29.          private List<Edge> adjEdges;//与该顶点邻接的边(点),每一个Vertex里都有一个链表存储其边  
  30.          private int dist;//顶点距离(该顶点到起始顶点的距离)  
  31.          private Vertex preNode;//顶点的前顶点  
  32.          //构造一个实例  
  33.          public Vertex(String vertexLabel) {  
  34.              this.vertexLabel = vertexLabel;  
  35.              adjEdges = new LinkedList<>();  
  36.             dist = Integer.MAX_VALUE;//为距离设置为最大值  
  37.              preNode = null;  
  38.          }  
  39.      }  
  40.      private class Edge{//边由顶点决定  
  41.          private Vertex endVertex;  
  42.          //构造实例  
  43.          public Edge(Vertex endVertex) {  
  44.              this.endVertex = endVertex;  
  45.          }  
  46.      }  
  47.        
  48.      private Map<String, Vertex> nonDirectedGraph;//保存了图中所有的顶点,边的关系以List形式保存在Vertex类中,通  
  49.     // 过键值对保存未处理顶点  
  50.      private Vertex startVertex;//图的起始顶点  
  51.        
  52.      public Miracle2(String graphContent) {//定义一个构造方法  
  53.          nonDirectedGraph = new LinkedHashMap<>();  
  54.          buildGraph(graphContent);  
  55.      }  
  56.        
  57.      private  void buildGraph(String graphContent){//通过传进来的内容构造图  
  58.          String[] lines = graphContent.split("\n");  
  59.            
  60.          String startNodeLabel, endNodeLabel;  
  61.          Vertex startNode, endNode;  
  62.            
  63.   
  64.                    
  65.          //以上是变量的定义  
  66.          for(int i = 0; i < lines.length; i++){  
  67.              String[] nodesInfo = lines[i].split(",");  
  68.              startNodeLabel = nodesInfo[1];  
  69.              endNodeLabel = nodesInfo[2];  
  70.                
  71.              endNode = nonDirectedGraph.get(endNodeLabel);  
  72.              if(endNode == null){  
  73.                  endNode = new Vertex(endNodeLabel);  
  74.                  nonDirectedGraph.put(endNodeLabel, endNode);  
  75.              }  
  76.                
  77.              startNode = nonDirectedGraph.get(startNodeLabel);//从键值对获得标签  
  78.              if(startNode == null){  
  79.                  startNode = new Vertex(startNodeLabel);  
  80.                  nonDirectedGraph.put(startNodeLabel, startNode);  
  81.              }  
  82.              Edge e = new Edge(endNode);  
  83.              //对于无向图而言,起点和终点都要添加边  
  84.              endNode.adjEdges.add(e);  
  85.              startNode.adjEdges.add(e);  
  86.          }  
  87.          startVertex = nonDirectedGraph.get(lines[0].split(",")[1]);//总是以文件中第一行第二列的那个标识顶点作为源点  
  88.      }  
  89.        
  90.      public void unweightedShortestPath(){  
  91.          unweightedShortestPath(startVertex);  
  92.      }  
  93.       
  94.        
  95.      /* 
  96.       *  
  97.       * 需要一个队列来保存图中的顶点,初始时,源点入队列,然后以广度的形式向外扩散求解其他顶点的最短路径 
  98.       */  
  99.      private void unweightedShortestPath(Vertex s){//该方法计算原点s到无向图各个顶点的最短路径  
  100.          //初始化  
  101.          Queue<Vertex> queue = new LinkedList<>();//用队列存储各个点  
  102.          s.dist = 0;  
  103.          queue.offer(s);//将源点dist设置为0并入队列,offer用于添加元素入数列  
  104.            
  105.          while(!queue.isEmpty()){  
  106.              Vertex v = queue.poll();//检索并删除队列的头  
  107.              for (Edge e : v.adjEdges) {//扫描v的邻接边(点)  
  108.                  if(e.endVertex.dist == Integer.MAX_VALUE){//如果这个顶点(e.endVertex)未被访问(每个顶点只会入队列一次)  
  109.                      e.endVertex.dist = v.dist + 1;//更新该顶点到源点的距离  
  110.                      queue.offer(e.endVertex);//将顶点放入队列,准备进行下一次换原电遍历  
  111.                      e.endVertex.preNode = v;//设置该顶点的前驱顶点  
  112.                  }//end if  
  113.              }//end for           
  114.              }//end while  
  115.      }  
  116.        
  117.      //打印图中所有顶点到源点的距离及路径  
  118.      public  void showDistance(){  
  119.          Collection<Vertex> vertexs = nonDirectedGraph.values();//collection是创建一个存储元素的容器  
  120.          for (Vertex vertex : vertexs) {  
  121.              System.out.print(vertex.vertexLabel + "<--");  
  122.              Vertex tmpPreNode = vertex.preNode;  
  123.              while(tmpPreNode != null){  
  124.                 System.out.print(tmpPreNode.vertexLabel + "<--");  
  125.                  tmpPreNode = tmpPreNode.preNode;  
  126.              }  
  127.              System.out.println("distance=" + vertex.dist);  
  128.          }  
  129.      }  
  130. }  
[java] view plain copy print?
  1. public class rootLinkNode {  
  2.     private int localNodeId;  
  3.     private LinkNode nextLinkNode;  
  4.     public rootLinkNode(int localNodeId,LinkNode nextLinkNode){  
  5.         this.setLocalNodeId(localNodeId);  
  6.         this.setNextLinkNodeForRoot(nextLinkNode);  
  7.           
  8.     }  
  9. //public addRootLinkNode(i){  
  10. //      }  
  11.     public LinkNode getNextLinkNode() {  
  12.         return nextLinkNode;  
  13.     }  
  14.     public void setNextLinkNodeForRoot(LinkNode nextLinkNode4) {  
  15.         //this.nextLinkNode = nextLinkNode;  
  16.         if(this.nextLinkNode==null){  
  17.             this.nextLinkNode = nextLinkNode4;  
  18.         }else{  
  19.         LinkNode temp=this.nextLinkNode;  
  20.         while(temp.getNextLinkNode()!=null){  
  21.             temp=temp.getNextLinkNode();  
  22.               
  23.         }  
  24.         temp.setNextLinkNode(nextLinkNode4);}  
  25.     }  
  26.     public int getLocalNodeId() {  
  27.         return localNodeId;  
  28.     }  
  29.     public void setLocalNodeId(int localNodeId) {  
  30.         this.localNodeId = localNodeId;  
  31.     }  
  32. }  
[java] view plain copy print?
  1. import java.util.HashMap;  
  2. import java.util.LinkedList;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. public class run {  
  7.     public static void main(String[] args){  
  8.         FileUtil fu=new FileUtil();  
  9.         //String filePath="D:\\info.txt";  
  10.     //  final int spec=4;  
  11.           
  12.         //String[] infos=fu.read(filePath, spec);  
  13.           
  14.         //int size=infos.length;  
  15.         //String info = null;  
  16.         //String infoOut=null;  
  17.         //for(int i=0;i<size;i++){  
  18.         //   info=infos[i];  
  19.         //  System.out.println(info);  
  20.         //    infoOut=infoOut+info+"\r";  
  21.         //    
  22.     //  }  
  23.           
  24.         //Miracle2 mi =new Miracle2(infoOut);  
  25.         //mi.showDistance();  
  26.           
  27.           
  28.         //String info3="789\n123";  
  29.         //System.out.println(info3);  
  30.           
  31.           
  32.           
  33.           
  34.         int serverDeploymentCost;//服务器部署成本  
  35.         int numOfConsumptionNode;//消费节点数量  
  36.         int numOfNetworkLink;//网络链路数量  
  37.         int numOfLinkNode;//链路节点数量  
  38.           
  39.         Graphs g=new Graphs();  
  40.         //g.getInfo2();  
  41.         String filePath2="D:\\case0.txt";  
  42.         int spec =3;  
  43.         String[] infos=fu.read(filePath2, spec);  
  44. //      for(int i=0;i<spec;i++){  
  45. //          System.out.println(infos[i]);  
  46. //      }  
  47.         String temp=infos[0];  
  48.         System.out.println(temp);  
  49.           
  50.           
  51.           
  52.           
  53.         //text3测试按空格分开是否有误  
  54.         String[] splitTemp=temp.split("\\s");  
  55. //      for(int i=0;i<3;i++){//当i=3时是0,1,2行被选中  
  56. //          System.out.println(splitTemp[i]);  
  57. //      }  
  58.         numOfConsumptionNode=Integer.parseInt(splitTemp[2]);  
  59.         numOfNetworkLink=Integer.parseInt(splitTemp[1]);  
  60.         numOfLinkNode=Integer.parseInt(splitTemp[0]);  
  61.         serverDeploymentCost=Integer.parseInt(infos[2]);  
  62.         //28个网络节点,45条链路,12个消费节点  
  63.           
  64.           
  65.           
  66.           
  67.         //System.out.println(numOfConsumptionNode);  
  68.         //System.out.println(numOfNetworkLink);  
  69.         //System.out.println(numOfLinkNode);  
  70.           
  71.         //计算需要的总共需要的spec  
  72.         spec=5+numOfNetworkLink+numOfConsumptionNode;  
  73.         String[] finalinfos=fu.read(filePath2, spec);  
  74.         //计算读取链路数需要的开始和结束行  
  75.         //第几行-1就是所需行号  
  76.         int startNumOfLink=4;  
  77.         int endNumOfLink=4+numOfNetworkLink-1;  
  78.         //计算读取消费节点数需要的开始和结束行  
  79.         int startNumOfConsumptionNode=endNumOfLink+2;  
  80.         int endNumOfConsumptionNode=startNumOfConsumptionNode+numOfConsumptionNode-1;  
  81.           
  82.           
  83.         System.out.println(endNumOfConsumptionNode);  
  84.           
  85.         //先存储消费节点,下面是为消费节点赋值并创建的代码  
  86.           
  87.         for(int i=startNumOfConsumptionNode;i<endNumOfConsumptionNode+1;i++){  
  88.             String temp2=finalinfos[i];  
  89.         String[]    consumptiontemp=temp2.split("\\s");  
  90.         //int i=startNumOfConsumptionNode;    
  91.         //consumptiontemp=finalinfos[i].split("\\s");  
  92.               
  93.               
  94.               
  95.             int consumptionNodeId=Integer.parseInt(consumptiontemp[0]);//消费节点id,用于确定哪一个消费节点  
  96.             LinkNode nextLinkNodeId=new LinkNode(i, Integer.parseInt(consumptiontemp[1]), 1000null,0);//链路节点id,用于确定哪一个链路节点  
  97.             //Integer.parseInt(consumptiontemp[1])  
  98.             int bandwidthRequirement=Integer.parseInt(consumptiontemp[2]);//定义一个参数保存贷款需求  
  99.               
  100.               
  101.               
  102.             //消费节点0,相连网络节点ID为8,视频带宽消耗需求为40   
  103.               
  104.               
  105.               
  106.               
  107.             //原打算在这里新建添加消费节点的方法,但是该方  
  108.             //法应存在与图中,应直接从图调用  
  109.             //List<consumptionNode> runroot=g.getroot();  
  110.             //consumptionNode e=new consumptionNode(consumptionNodeId,nextLinkNodeId,bandwidthRequirement);  
  111.             //runroot.add<>;  
  112.               
  113.               
  114.               
  115.             //从上图获得的参数构建消费节点链表  
  116.             g.addNode(consumptionNodeId, nextLinkNodeId, bandwidthRequirement);  
  117.         }  
  118.           
  119.         //test  
  120. //      for(int i=0;i<numOfConsumptionNode;i++){  
  121. //            
  122. //          System.out.println(g.returnNode(i).getConsumptionNodeId());  
  123. //          System.out.println(g.returnNode(i).getNextLinkNodeId().getLocalNodeId());  
  124. //          System.out.println(g.returnNode(i).getBandwidthRequirement());  
  125. //            
  126. //      }  
  127.           
  128.           
  129.           
  130.           
  131.           
  132.           
  133.         //添加链路头结点的方法  
  134.         //numOfLinkNode网络链路节点数量  
  135.         int localNodeId;  
  136.         LinkNode nextLinkNode;  
  137.         for(int i=0;i<numOfLinkNode;i++){  
  138.             //rootLinkNode(i,null);  
  139.             g.addRootLinkNode(i);  
  140.         }  
  141.          //System.out.println("下面是测试链路头结点程序");  
  142.         //System.out.println(numOfLinkNode);  
  143.         //for(int i=0;i<50;i++){  
  144.         //  System.out.println(g.LinkRoot.get(i).getLocalNodeId());  
  145.         //}  
  146.           
  147.           
  148.           
  149.         //添加链路节点的方法  
  150.           
  151.          //for(int i=startNumOfLink;i<endNumOfConsumptionNode+1;i++){  
  152.         for(int i=startNumOfLink;i<endNumOfLink;i++){  
  153.             //定义链路节点基本元素  
  154.             int lnlocalNodeId;  
  155.             int lntotalBandwidth;  
  156.             LinkNode lnnextLinkNode;  
  157.             int lnpreNodeId;  
  158.             int lnunitRentalCost;  
  159.             String temp3=finalinfos[i];  
  160.             String[] consumptiontemp=temp3.split("\\s");  
  161.             lnpreNodeId=Integer.parseInt(consumptiontemp[0]);  
  162.             lnlocalNodeId=Integer.parseInt(consumptiontemp[1]);  
  163.             lntotalBandwidth=Integer.parseInt(consumptiontemp[2]);  
  164.             lnunitRentalCost=Integer.parseInt(consumptiontemp[3]);  
  165.             //新建节点之后加进去  
  166.             lnnextLinkNode=new LinkNode(lnpreNodeId, lnlocalNodeId, lntotalBandwidth, null, lnunitRentalCost);  
  167.             LinkNode reverseLnnextLinkNode;  
  168.             reverseLnnextLinkNode=new LinkNode(lnlocalNodeId, lnpreNodeId, lntotalBandwidth, null, lnunitRentalCost);  
  169.             if(g.LinkRoot.get(lnpreNodeId).getNextLinkNode()==null){  
  170.                 g.LinkRoot.get(lnpreNodeId).setNextLinkNodeForRoot(lnnextLinkNode);  
  171.             }else{  
  172.                  LinkNode temp4=g.LinkRoot.get(lnpreNodeId).getNextLinkNode();  
  173.                  LinkNode temp5=g.LinkRoot.get(lnpreNodeId).getNextLinkNode();  
  174.                     while(temp4!=null&&temp4.getLocalNodeId()!=lnlocalNodeId){  
  175.                         temp5=temp4;  
  176.                         temp4=temp4.getNextLinkNode();  
  177.                       
  178.                     }  
  179.                     //if(temp4==null){if(temp4.getLocalNodeId()!=lnlocalNodeId)  
  180.                     if(temp4==null){  
  181.                         temp5.setNextLinkNode(lnnextLinkNode);  
  182.                         //g.LinkRoot.get(lnpreNodeId).setNextLinkNodeForRoot(lnnextLinkNode);  
  183.                     }  
  184.                       
  185.             }  
  186.               
  187.               
  188.             //反向代码  
  189.             if(g.LinkRoot.get(lnlocalNodeId).getNextLinkNode()==null){  
  190.                 g.LinkRoot.get(lnlocalNodeId).setNextLinkNodeForRoot(reverseLnnextLinkNode);  
  191.             }else{  
  192.                  LinkNode temp4=g.LinkRoot.get(lnlocalNodeId).getNextLinkNode();  
  193.                  LinkNode temp5=g.LinkRoot.get(lnlocalNodeId).getNextLinkNode();  
  194.                     while(temp4!=null&&temp4.getLocalNodeId()!=lnpreNodeId){  
  195.                         temp5=temp4;  
  196.                         temp4=temp4.getNextLinkNode();  
  197.                       
  198.                     }  
  199.                     if(temp4==null){  
  200.                         g.LinkRoot.get(lnlocalNodeId).setNextLinkNodeForRoot(reverseLnnextLinkNode);  
  201.                     }  
  202.                       
  203.             }  
  204.              
  205.             //g.LinkRoot.get(lnpreNodeId).setNextLinkNodeForRoot(lnnextLinkNode);  
  206.             //LinkNode temp4=g.LinkRoot.get(lnpreNodeId).getNextLinkNode();  
  207.            // while(){  
  208.                   
  209.           //  }  
  210.           
  211.         }  
  212.           
  213.         //这里起始节点命名为preNodeId  
  214.         //链路起始节点为0,链路终止节点为16 ,总带宽为8,单位网络租用费为2  
  215.           
  216. //      System.out.println("下面是测试数据");  
  217. //      for(int i =0;i<numOfLinkNode;i++){  
  218. //          LinkNode temp6=g.LinkRoot.get(i).getNextLinkNode();  
  219. //          System.out.println(temp6.getLocalNodeId());  
  220. //      }  
  221. //      System.out.println(g.LinkRoot.get(1).getNextLinkNode().getLocalNodeId());  
  222.           
  223.           
  224.         //总测试用例  
  225.         //我们已经得到  
  226.         //28个网络节点,45条链路,12个消费节点  
  227.         //服务器部署成本为100  
  228.         //链路节点信息  
  229.         //链路起始节点为0,链路终止节点为16 ,总带宽为8,单位网络租用费为2  
  230.         //消费节点信息  
  231.         //消费节点0,相连网络节点ID为8,视频带宽消耗需求为40   
  232.         System.out.printf("有%d个网络节点,%d个链路,%d个消费节点", numOfLinkNode,numOfNetworkLink,numOfConsumptionNode);  
  233.           
  234.       
  235.           
  236.         //从消费结点开始遍历,存储在队列中  
  237.         //任取一消费节点,检查其是否满足链路带宽等于其需求,满足则开始配对  
  238.     int rootSize=g.root.size();  
  239.     //System.out.println(rootSize);  
  240.     //定义一个集合保存满足单路的结点  
  241.     //list<int> singleWayconsumption=new LinkList<>;  
  242.       
  243.     int destination;  
  244.     int countOfSingleWayConsumption=0;  
  245.     int[] nextNodeForConsumptionNode=new int[rootSize];  
  246.     for(int i=0;i<rootSize;i++){  
  247.         //第一个消费节点的第一个链路节点提供的带宽  
  248.         int idFornextNodeofConsumptionNode=g.root.get(i).getNextLinkNodeId().getLocalNodeId();  
  249.         //System.out.println(idFornextNodeofConsumptionNode);  
  250.         nextNodeForConsumptionNode[i]=idFornextNodeofConsumptionNode;  
  251.         int firstConsumptionLinkNodeBandWith=g.LinkRoot.get(idFornextNodeofConsumptionNode).getNextLinkNode().getTotalBandwidth();  
  252.         //System.out.println(idFornextNodeofConsumptionNode);  
  253.     //  int firstConsumptionLinkNodeBandWith=g.root.get(i).getNextLinkNodeId().getNextLinkNode().getTotalBandwidth();  
  254.           
  255.         if(g.root.get(i).getBandwidthRequirement()<=firstConsumptionLinkNodeBandWith){  
  256.             //singleWayConsumption[countOfSingleWayConsumption]=g.root.get(i).getConsumptionNodeId();  
  257.               
  258.             countOfSingleWayConsumption++;  
  259.         }  
  260.           
  261.           
  262. //      //text  
  263. //      for(int d=0;d<countOfSingleWayConsumption;d++){  
  264. //          System.out.println(singleWayConsumption[d]);  
  265. //          }  
  266.         }  
  267.     int[] singleWayConsumption=new int[countOfSingleWayConsumption] ;  
  268.     int countFor2=0;  
  269. //  System.out.println(countOfSingleWayConsumption);  
  270.       
  271.     for(int i=0;i<rootSize;i++){  
  272.         //第一个消费节点的第一个链路节点提供的带宽  
  273.         //int firstConsumptionLinkNodeBandWith2=g.root.get(c).getNextLinkNodeId().getNextLinkNode().getTotalBandwidth();  
  274.         int idFornextNodeofConsumptionNode2=g.root.get(i).getNextLinkNodeId().getLocalNodeId();  
  275.         int firstConsumptionLinkNodeBandWith2=g.LinkRoot .get(idFornextNodeofConsumptionNode2).getNextLinkNode().getTotalBandwidth();  
  276.           
  277.           
  278.         if(g.root.get(i).getBandwidthRequirement()<=firstConsumptionLinkNodeBandWith2){  
  279.             singleWayConsumption[countFor2]=g.root.get(i).getConsumptionNodeId();  
  280.             countFor2++;  
  281.             //countOfSingleWayConsumption++;  
  282.         }  
  283.       
  284. }  
  285.       
  286.     //map.put(1, 5);  
  287.     //System.out.println(map.get(1));  
  288.     System.out.println("检验这个数是多少");  
  289.     System.out.println(countOfSingleWayConsumption);      
  290.     searchMatrix matrixForRun=new searchMatrix();  
  291.     int[][] a=matrixForRun.createMatrix(numOfLinkNode,numOfLinkNode);  
  292.     matrixForRun.assignment(a, numOfLinkNode, numOfLinkNode);  
  293.     //定义一个数组记录链路可使用的最大带宽    //开始遍历并保存路径  
  294.     //获得所有消费节点的临界点,保存在数组中,进行比较  //int[][] route=null;  
  295.     //int[][] route=new int[1000][10];  
  296.     //当一个索引=-1时,表示该行遍历结束    //nextNodeForConsumptionNode  
  297.     for(int i=0;i<numOfLinkNode;i++){  
  298.         a[i][i]=1;  
  299.     }  
  300.     LLQueue llqueue=new LLQueue();  
  301.     //建立一个hashmap存储邻接点为键值,消费节点为value  
  302.             Map map=new HashMap<>();  
  303.             for(int i=0;i<rootSize;i++){  
  304.                 map.put(g.root.get(i).getNextLinkNodeId().getLocalNodeId(), g.root.get(i).getConsumptionNodeId());        
  305.             }  
  306.           
  307.     for(int i=0;i<countOfSingleWayConsumption;i++){  
  308.         int thisConsumptionNode=singleWayConsumption[i];  
  309.         System.out.println("这是一次遍历");  
  310.           
  311.         //route[i][0]=thisConsumptionNode;  
  312.         llqueue.enQueue(thisConsumptionNode);  
  313.         //route[i][0]=1;        //route[i][1]=nextNodeForConsumptionNode[thisConsumptionNode];  
  314.         //llqueue.enQueue(nextNodeForConsumptionNode[thisConsumptionNode]);  
  315.         llqueue.enQueue(g.root.get(thisConsumptionNode).getNextLinkNodeId().getLocalNodeId());  
  316.         //int temp11=nextNodeForConsumptionNode[thisConsumptionNode];  
  317.         int temp11=g.root.get(thisConsumptionNode).getNextLinkNodeId().getLocalNodeId();  
  318.         System.out.println(thisConsumptionNode);  
  319.         System.out.println(temp11);  
  320.         //while(g.LinkRoot.get(temp11).getNextLinkNode().getLocalNodeId()!=null){  
  321.         levelSearch(temp11,g,nextNodeForConsumptionNode,llqueue,i,a,map);     
  322.         //}     //g.LinkRoot.get(temp11).getNextLinkNode()!=null//判断一个节点下一个指向点不为空的语句        //g.LinkRoot.get(temp11).getNextLinkNode().getLocalNodeId()  
  323.         //获得下一个节点的id        //compare(g. inkRoot.get(temp11).getNextLinkNode().getLocalNodeId(),nextNodeForConsumptionNode);        //比较其临界点是否有链路直连的临接点的方法  
  324.     }  
  325.     //int[] route = null;//route[0]=0;//route[1]=1;//route[2]=2;//  System.out.println("测试链表是否为空");//   rootLinkNode temp13=g.LinkRoot.get(1);  
  326. //  LinkNode temp14=temp13.getNextLinkNode();// while(temp14!=null){//      System.out.println(temp14.getLocalNodeId());//      temp14=temp14.getNextLinkNode();  
  327. //  }// System.out.println("测试结束");   
  328.     }  
  329.   
  330.     public static void levelSearch(int numOfNode,Graphs g,int[] aggregate,LLQueue llqueue,int i,int[][]iswalk,Map map){  
  331.         //每个链路节点的第一个节点是rootlinknode属性  
  332.         rootLinkNode temp=g.LinkRoot.get(numOfNode);  
  333.         LinkNode temp1=null;  
  334.         int maxBandWidth=10000;  
  335. //      while(temp.getNextLinkNode()!=null&&compare(temp.getLocalNodeId(),aggregate)){//            temp1=temp.getNextLinkNode();//         j++;//          route[i][j]=temp1.getLocalNodeId();  
  336. //          if(maxBandWidth>=temp1.getUpBandwidth()){//              maxBandWidth=temp1.getUpBandwidth();//          }//         //      }  
  337.         levelSearchForNode(numOfNode,g,aggregate,llqueue,i,temp,temp1,maxBandWidth,iswalk,map);  
  338.     }  
  339.     //这个方法遍历子节点  
  340.     public static void levelSearchForNode(int numOfNode,Graphs g,int[] aggregate,LLQueue llqueue,int i,rootLinkNode temp,LinkNode temp1,int maxBandWidth,int[][]iswalk,Map map){  
  341.         //findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);  
  342.         //尝试引入临接矩阵,标签已索引过的边  
  343.         //LinkNode temp=g.LinkRoo  
  344.             temp=g.LinkRoot.get(numOfNode);  
  345. //          System.out.println(numOfNode);t.get(numOfNode);  
  346.          //levelSearchForNode(numOfNode,g,aggregate,route,i,j,temp,temp1,maxBandWidth);  
  347.         while(temp.getNextLinkNode()!=null&&compare(temp.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBoolean(iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()])){  
  348.             iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()]=1;  
  349.             iswalk[temp.getNextLinkNode().getLocalNodeId()][temp.getLocalNodeId()]=1;  
  350.             temp1=temp.getNextLinkNode();  
  351.             //j++;  
  352.             //route[i][j]=temp1.getLocalNodeId();  
  353.             llqueue.enQueue(temp1.getLocalNodeId());  
  354.             //          System.out.println("下面是135135");//6         System.out.println(temp.getNextLinkNode().getLocalNodeId());//          System.out.println("下面是j");//           System.out.println(j);  
  355.             if(maxBandWidth>=temp1.getUpBandwidth()){  
  356.                 maxBandWidth=temp1.getUpBandwidth();  
  357.             }  
  358.             numOfNode=temp1.getLocalNodeId();  
  359. //          System.out.println(iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()]);  
  360. //          System.out.println(conversionBoolean(iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()]));  
  361.             levelSearchForNode(numOfNode,g,aggregate,llqueue,i,temp,temp1,maxBandWidth,iswalk,map);  
  362.         }  
  363.         if(temp.getNextLinkNode()==null){  
  364.           
  365.           
  366.         LLNode tempForIf=null;  
  367.         tempForIf=llqueue.getFrontNode();  
  368.         while(tempForIf.getNextNodeForLLNode()!=llqueue.getRearNode()){  
  369.             tempForIf=tempForIf.getNextNodeForLLNode();  
  370.         }   
  371.         llqueue.setRearNode(tempForIf);  
  372.           
  373.         numOfNode=tempForIf.getNextNodeForLLNode().getDataForLLNode();  
  374.         rootLinkNode temp111=g.LinkRoot.get(numOfNode);  
  375.         LinkNode temp112=null;  
  376.         //由根节点转为一般节点  
  377.         if(temp111.getNextLinkNode()!=null){  
  378.             temp112=temp111.getNextLinkNode();  
  379.         }  
  380.           
  381.         findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);}  
  382.         else{  
  383.             rootLinkNode temp111=null;  
  384.             LinkNode temp112=temp.getNextLinkNode();  
  385.             System.out.println( temp112.getLocalNodeId());  
  386.             System.out.println( temp112.getNextLinkNode().getLocalNodeId());  
  387.         findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);  
  388.         }  
  389. //      if(temp.getNextLinkNode()==null){  
  390.               
  391.               
  392. //          LLNode tempForIf=null;  
  393. //          tempForIf=llqueue.getFrontNode();  
  394. //          while(tempForIf.getNextNodeForLLNode()!=llqueue.getRearNode()){  
  395. //              tempForIf=tempForIf.getNextNodeForLLNode();  
  396. //          }   
  397. //          llqueue.setRearNode(tempForIf);  
  398.               
  399. //          numOfNode=tempForIf.getNextNodeForLLNode().getDataForLLNode();  
  400. //          rootLinkNode temp111=g.LinkRoot.get(numOfNode);  
  401. //          LinkNode temp112=null;  
  402.             //由根节点转为一般节点  
  403. //          if(temp111.getNextLinkNode()!=null){  
  404. //              temp112=temp111.getNextLinkNode();  
  405. //          }  
  406.             //if(temp112.getNextLinkNode()!=null&&compare(temp112.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBoolean(iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()])){  
  407.                   
  408.             //}  
  409.             //获得未访问的节点    
  410.             //如果下一个节点又为空了,只能递归调用此函数  
  411.  //           while(temp112.getNextLinkNode()!=null&&compare(temp112.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBooleanReverse(iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()])){  
  412. //              if(temp112.getNextLinkNode()==null){  
  413.                       
  414. //              }  
  415.  //             temp112=temp112.getNextLinkNode();  
  416. //          }  
  417.             //while(temp111.getNextLinkNode()!=null&&compare(temp.getLocalNodeId(),aggregate)&&conversionBoolean(iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()])){            
  418.             //}  
  419. //            iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()]=1;  
  420. //          iswalk[temp112.getNextLinkNode().getLocalNodeId()][temp111.getLocalNodeId()]=1;  
  421. //          numOfNode=temp112.getLocalNodeId();  
  422. //          temp=g.LinkRoot.get(numOfNode);  
  423. //          llqueue.enQueue(numOfNode);  
  424. //          temp1=null;  
  425. //          levelSearchForNode(numOfNode,g,aggregate,llqueue,i,temp,temp1,maxBandWidth,iswalk,map);  
  426. //      }  
  427.         //if(compareReverse(temp.getNextLinkNode().getLocalNodeId(),aggregate)){  
  428.         //获取未访问的下个节点恰好为消费节点邻接点的点,输入消费节点  
  429. //          if(temp.getNextLinkNode()!=null&&compareReverse(temp.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBoolean(iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()])){   
  430.             //打印路径  
  431. //          route[i][j]=-1;  
  432. //          llqueue.enQueue(temp.getNextLinkNode().getLocalNodeId());  
  433. //          iswalk[temp.getLocalNodeId()][temp.getNextLinkNode().getLocalNodeId()]=1;  
  434. //          iswalk[temp.getNextLinkNode().getLocalNodeId()][temp.getLocalNodeId()]=1;  
  435. //          llqueue.enQueue(Integer.parseInt(String.valueOf(map.get(temp.getNextLinkNode().getLocalNodeId()))));  
  436. //            
  437. //          System.out.println("下面是打印数据");  
  438. //          for(int d=0;d<route[i].length;d++){  
  439. //              System.out.println(route[i][d]);  
  440. //          }  
  441. //          LLNode tempforQueue=llqueue.getFrontNode();  
  442. //          while(tempforQueue.getNextNodeForLLNode()!=llqueue.getRearNode()){  
  443. //              int data=tempforQueue.getDataForLLNode();  
  444. //              System.out.println(data);  
  445. //              tempforQueue=tempforQueue.getNextNodeForLLNode();  
  446. //          }  
  447. //          System.out.println(llqueue.getRearNode().getDataForLLNode());  
  448. //          llqueue.setRearNode(tempforQueue);  
  449. //          while(llqueue.getFrontNode()!=llqueue.getRearNode()){  
  450. //              int data=llqueue.deQueue();  
  451. //              System.out.println(data);  
  452. //          }  
  453. //          System.out.println(llqueue.getFrontNode().getDataForLLNode());  
  454. //          llqueue.setFrontNode(null);  
  455. //          llqueue.setRearNode(null);  
  456. //          System.out.println("结束了");    
  457. //          numOfNode=tempforQueue.getDataForLLNode();  
  458. //          temp1=g.LinkRoot.get(numOfNode).getNextLinkNode();  
  459. //           while(temp1.getNextLinkNode()!=null&&conversionBoolean(iswalk[numOfNode][temp1.getLocalNodeId()])){  
  460. //               temp1=temp1.getNextLinkNode();  
  461. //           }  
  462. //           if(conversionBooleanReverse(iswalk[numOfNode][temp1.getLocalNodeId()])){  
  463. //               numOfNode=temp1.getLocalNodeId();  
  464. //               temp=g.LinkRoot.get(numOfNode);  
  465. ///                 temp1=null;  
  466. //           }else{  
  467. //               LLNode tempForElse=llqueue.getFrontNode();  
  468. //              while(tempForElse.getNextNodeForLLNode()!=llqueue.getRearNode()){  
  469. //                  tempForElse=tempForElse.getNextNodeForLLNode();  
  470. //              }  
  471. //              llqueue.setRearNode(tempForElse);  
  472. //              numOfNode=tempForElse.getDataForLLNode();  
  473. //              temp=g.LinkRoot.get(numOfNode);  
  474. //           }  
  475.               
  476. //          levelSearchForNode(numOfNode,g,aggregate,llqueue,i,temp,temp1,maxBandWidth,iswalk,map);  
  477. //      }  
  478.     }  
  479.       
  480.     //最后返回正确,说明该节点成立  
  481.     private static boolean compare(int element,int[] aggregate) {  
  482.         // TODO Auto-generated method stub  
  483.         int a=aggregate.length;  
  484.         //int a=8;      //for(int i=0;i<a;i++){                  //}  
  485.         int c=0;  
  486.         //while(element!=aggregate[c]&&c<a){  
  487.             while(c<a&&element!=aggregate[c]){     
  488.             c++;  
  489.         }  
  490.         if(c==a){  
  491.             return true;  
  492.         }else{  
  493.             return false;  
  494.         }  
  495.     }  
  496.     //没访问过返回1,正向转化值输入-1返回true  
  497.     public static boolean conversionBoolean(int a){  
  498.         if(a==-1){  
  499.             return true;  
  500.         }else{  
  501.             return false;  
  502.         }  
  503.     }  
  504.     //反向的转化值,未访问由TRUE转为false,输入1返回true  
  505.     public static boolean conversionBooleanReverse(int a){  
  506.         if(a==-1){  
  507.             return false;  
  508.         }else{  
  509.             return true;  
  510.         }  
  511.     }  
  512.     //aggregate集合//输入待比较的元素和比较元素的集合//比较没有返回1,比较有返回0      
  513.       
  514.         //定义反向的比较有返回true,没有返回false  
  515.     private static boolean compareReverse(int element,int[] aggregate) {  
  516.         // TODO Auto-generated method stub  
  517.         int a=aggregate.length;  
  518.         //int a=8;      //for(int i=0;i<a;i++){                  //}  
  519.         int c=0;  
  520.         //while(element!=aggregate[c]&&c<a){  
  521.             while(c<a&&element!=aggregate[c]){     
  522.             c++;  
  523.         }  
  524.         if(c==a){  
  525.             return false;  
  526.         }else{  
  527.             return true;  
  528.         }  
  529.     }  
  530.     public void replaceRootNode(int numOfNode,Graphs g,LinkNode temp1,rootLinkNode temp,int[][] iswalk,LLNode rearNode){  
  531.         temp1=g.LinkRoot.get(numOfNode).getNextLinkNode();  
  532.          while(temp1.getNextLinkNode()!=null&&conversionBoolean(iswalk[numOfNode][temp1.getLocalNodeId()])){  
  533.              temp1=temp1.getNextLinkNode();  
  534.          }  
  535.          if(conversionBoolean(iswalk[numOfNode][temp1.getLocalNodeId()])){  
  536.              numOfNode=temp1.getLocalNodeId();  
  537.              temp=g.LinkRoot.get(numOfNode);  
  538.                 temp1=null;  
  539.          }else{  
  540.              if(rearNode.getDataForLLNode()!=g.LinkRoot.get(numOfNode).getLocalNodeId()){                  
  541.              }  
  542.          }  
  543.           
  544.     }  
  545.     public static void findNewRootNode(LinkNode temp112,rootLinkNode temp111,int[] aggregate,int[][] iswalk,LLQueue llqueue,int numOfNode,Graphs g,Map map){  
  546.           while(temp112.getNextLinkNode()!=null&&compare(temp112.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBooleanReverse(iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()])){  
  547. //              if(temp112.getNextLinkNode()==null){                      
  548. //              }  
  549.             temp112=temp112.getNextLinkNode();  
  550.             }  
  551.           if(temp112.getNextLinkNode()==null){  
  552.                 LLNode tempForIf=null;  
  553.                 tempForIf=llqueue.getFrontNode();  
  554.                 while(tempForIf.getNextNodeForLLNode()!=llqueue.getRearNode()){  
  555.                     tempForIf=tempForIf.getNextNodeForLLNode();  
  556.                 }   
  557.                 llqueue.setRearNode(tempForIf);  
  558.                   
  559.                 numOfNode=tempForIf.getNextNodeForLLNode().getDataForLLNode();  
  560.                  temp111=g.LinkRoot.get(numOfNode);  
  561.                  temp112=null;  
  562.                 //由根节点转为一般节点  
  563.                 if(temp111.getNextLinkNode()!=null){  
  564.                     temp112=temp111.getNextLinkNode();  
  565.                     findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);  
  566.                 }  
  567.                 
  568.           }  
  569.           else{  
  570.               if(temp112.getNextLinkNode()!=null&&conversionBooleanReverse(iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()])){  
  571.                   temp112=temp112.getNextLinkNode();  
  572.                   findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);  
  573.               }else{  
  574.                   if(temp112.getNextLinkNode()!=null&&compare(temp112.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBoolean(iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()])){  
  575.                       llqueue.enQueue(temp112.getNextLinkNode().getLocalNodeId());  
  576.                         iswalk[temp112.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()]=1;  
  577.                         iswalk[temp112.getNextLinkNode().getLocalNodeId()][temp112.getLocalNodeId()]=1;  
  578.                         llqueue.enQueue(Integer.parseInt(String.valueOf(map.get(temp112.getNextLinkNode().getLocalNodeId()))));  
  579.                           
  580.                         System.out.println("下面是打印数据");  
  581. //                      for(int d=0;d<route[i].length;d++){  
  582. //                          System.out.println(route[i][d]);  
  583. //                      }  
  584.                         LLNode tempforQueue=llqueue.getFrontNode();  
  585.                         while(tempforQueue.getNextNodeForLLNode()!=llqueue.getRearNode()){  
  586.                             int data=tempforQueue.getDataForLLNode();  
  587.                             System.out.println(data);  
  588.                             tempforQueue=tempforQueue.getNextNodeForLLNode();  
  589.                         }  
  590.                         System.out.println(llqueue.getRearNode().getDataForLLNode());   
  591.                         llqueue.setRearNode(tempforQueue);  
  592.                         temp112=temp112.getNextLinkNode();  
  593.                           findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);  
  594.                   }else{if(compare(temp112.getNextLinkNode().getLocalNodeId(),aggregate)&&conversionBooleanReverse(iswalk[temp111.getLocalNodeId()][temp112.getNextLinkNode().getLocalNodeId()])){  
  595.                       temp112=temp112.getNextLinkNode();  
  596.                       findNewRootNode(temp112,temp111,aggregate,iswalk,llqueue,numOfNode,g,map);  
  597.                   }}  
  598.               }  
  599.           }  
  600.           
  601.     }  
  602.     //从消费结点开始遍历,存储在队列中  //任取一消费节点,检查其是否满足链路带宽等于其需求,满足则开始配对  
  603.     //private static void shorestPath() {   // TODO Auto-generated method stub  //  g   //}  
  604.     //思路    //建立一个临接矩阵表示边是否访问过  //要利用主代码新建的变量,就要在方法中输入变量    //依次输入要使用的变量  
  605.     //g.LinkRoot.get(temp11).getNextLinkNode()!=null    //判断一个节点下一个指向点不为空的语句    //g.LinkRoot.get(temp11).getNextLinkNode().getLocalNodeId() //获得下一个节点的id  
  606. //compare(g. inkRoot.get(temp11).getNextLinkNode().getLocalNodeId(),nextNodeForConsumptionNode);    //比较其临界点是否有链路直连的临接点的方法  //这个方法只遍历头结点  
  607.     //parameter要开始遍历的链路头结点,已建立缓存的图,保存消费点临结点的集合,保存路径的数组,第几个消费节点的下标  
  608.       
[java] view plain copy print?
http://blog.csdn.net/qq_26122557/article/details/77104961

  1. public class searchMatrix {  
  2.     private int[][] matrix;  
  3.   
  4.     public int[][] createMatrix(int x,int y) {  
  5.         matrix=new int[x][y];  
  6.         return matrix;  
  7.     }  
  8.     public void assignment(int[][] matrix,int x,int y){  
  9.         //分配  
  10.         for(int i=0;i<x;i++){  
  11.             for(int j=0;j<y;j++){  
  12.                 matrix[i][j]=-1;  
  13.             }  
  14.         }  
  15.     }  
  16.     //当值为-1时表示未访问,为1表示访问过  
  17.     public void setMatrix(int[][] matrix) {  
  18.         this.matrix = matrix;  
  19.     }  
  20.     public int getValue(int x,int y){  
  21.         return matrix[x][y];  
  22.     }  
  23.     public void setValue(int x,int y,int value){  
  24.         matrix[x][y]=value;  
  25.     }  
  26.     public int[][] getMatrix() {  
  27.         return matrix;  
  28.     }  
  29. }