有向无环图的单源最短路径

来源:互联网 发布:中软博通软件上海 编辑:程序博客网 时间:2024/05/21 10:12
package com.data.struct;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Random;import java.util.Set;public class OrderedGraphicShortestPath {private Node [][]graphic;private Node s;private Set<Node> nextNodes=new HashSet<Node>();private Set<Node> addList=new HashSet<Node>();private Set<Node> removeList=new HashSet<Node>();private int time=0;private List<Node> sortList=new ArrayList<Node>();public OrderedGraphicShortestPath(int v,int e){graphic=new Node[v][v];for(int i=0;i<e;i++){int v1=new Random().nextInt(v);int v2=new Random().nextInt(v);if(v2<v1){continue;}Node node=new Node();node.d=Integer.MAX_VALUE-1000;node.w=new Random().nextInt(e);node.start=v1;node.end=v2;graphic[v1][v2]=node;}for(int i=0;i<v;i++){if(graphic[i][i]==null){Node node=new Node();node.d=Integer.MAX_VALUE-1000;node.w=new Random().nextInt(e);node.start=i;node.end=i;node.color=Node.WHITE;graphic[i][i]=node;}else{graphic[i][i].color=Node.WHITE;}}depthFirstSearch();s=sortList.get(0);s.d=0;}public void relex(Node u,Node v){if(graphic[v.end][v.end].d>u.d+graphic[u.start][v.start].w){graphic[v.end][v.end].d=u.d+graphic[u.start][v.start].w;graphic[v.end][v.end].parent=u;addList.add(graphic[v.end][v.end]);System.out.println(graphic[v.end][v.end].start+"=>"+u.start);}}private void depthFirstSearch(){for(int i=0;i<graphic.length;i++){if(graphic[i][i].color==Node.WHITE){dfsVisit(graphic[i][i]);}}}private void dfsVisit(Node node){time++;node.s=time;node.color=Node.GRAY;for(int i=0;i<graphic.length;i++){if(i!=node.start){Node w=graphic[node.start][i];if(w!=null){if(graphic[i][i].color==Node.WHITE){graphic[i][i].color=Node.GRAY;graphic[i][i].oparent=node;dfsVisit(graphic[i][i]);}}}}node.color=Node.BLACK;time++;node.f=time;sortList.add(0, node);}public void printSort(){System.out.println("sort===========");for(int i=0;i<sortList.size();i++){System.out.print(sortList.get(i).start+"  ");;}System.out.println();}public void shortestPath(){nextNodes.add(s);for(int k=0;k<graphic.length;k++){if(k!=s.start&&graphic[s.start][k]!=null){relex(s,graphic[k][k]);}}nextNodes.addAll(addList);nextNodes.remove(s);while(nextNodes.size()>0){addList.clear();removeList.clear();Iterator<Node> it=nextNodes.iterator();while(it.hasNext()){Node node=it.next();for(int k=0;k<graphic.length;k++){if(k!=node.start&&graphic[node.start][k]!=null){relex(node,graphic[k][k]);}}removeList.add(node);}nextNodes.removeAll(removeList);nextNodes.addAll(addList);}}public void  print(){for(int i=0;i<graphic.length;i++){for(int j=0;j<graphic[i].length;j++){if(graphic[i][j]==null){System.out.print(-1+"|"+-1+"|"+-1+"  ");}else{System.out.print(graphic[i][j].start+"|"+graphic[i][j].end+"|"+graphic[i][j].w+"  ");}}System.out.println();}}public void printPath(){List<Integer> indexList=new ArrayList<Integer>();List<Integer> removeList=new ArrayList<Integer>();List<Integer> addList=new ArrayList<Integer>();indexList.add(sortList.get(0).start);while(indexList.size()>0){removeList.clear();addList.clear();for(int x=0;x<indexList.size();x++){int l=indexList.get(x);removeList.add(l);for(int j=0;j<graphic.length;j++){if(graphic[j][j].parent==graphic[l][l]){graphic[l][l].children.add(graphic[j][j]);addList.add(j);}}}indexList.removeAll(removeList);indexList.addAll(addList);}Node h = this.s;        this.print(0, h,h);            System.out.println();}      private void print(int level,Node parent, Node node){     for (int i = 0; i < level; i++) {         System.out.format(" ");     }     System.out.format("|");     if(parent!=node){     System.out.print("("+graphic[parent.start][node.end].w+")");     for (int i = 0; i < level; i++) {         System.out.format("-");     }     }else{     for (int i = 0; i < level; i++) {         System.out.format("-");     }     }          System.out.format("%d%n", node.start);     List<Node> children = node.children;     for(int i=0;i<children.size();i++){     print(level+1,node,children.get(i));     }                }private class Node{private static final int WHITE=1;private static final int GRAY=2;private static final int BLACK=3;private int color;private int d;private int s;private int f;private int w;private int start;private int end;private Node oparent;private Node parent;private List<Node> children=new ArrayList<Node>();}public static void main(String[] args) {OrderedGraphicShortestPath o=new OrderedGraphicShortestPath(5,20);o.print();//o.depthFirstSearch();o.printSort();o.shortestPath();o.printPath();}}

0 0
原创粉丝点击