1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class graph_demo {
public static void main(String[] args) { // TODO 自动生成的方法存根 Graph theGraph = new Graph(); theGraph.addVertex('A'); //数组元素0 theGraph.addVertex('B'); //数组元素1 theGraph.addVertex('C'); //数组元素2 theGraph.addVertex('D'); //数组元素3 theGraph.addVertex('E'); //数组元素4 // theGraph.addEdge(0, 1); //AB // theGraph.addEdge(1, 2); //BC // theGraph.addEdge(0, 3); //AD // theGraph.addEdge(3, 4); //DE // System.out.println("dfs访问的顺序:"); // theGraph.dfs(); // System.out.println(); // // System.out.println("bfs访问的顺序:"); // theGraph.bfs(); // theGraph.addEdge(0, 1); //AB // theGraph.addEdge(0, 2); //AC // theGraph.addEdge(0, 3); //AD // theGraph.addEdge(0, 4); //AE // theGraph.addEdge(1, 2); //BC // theGraph.addEdge(1, 3); //BD // theGraph.addEdge(1, 4); //BE // //theGraph.addEdge(2, 3); //CD // //theGraph.addEdge(2, 4); //CE // theGraph.addEdge(3, 4); //DE // System.out.println("最小生成树:"); // theGraph.mst(); theGraph.addVertex('F'); //数组元素5 theGraph.addVertex('G'); //数组元素6 theGraph.addVertex('H'); //数组元素6 theGraph.addEdge(0, 3); //AD theGraph.addEdge(0, 4); //AE theGraph.addEdge(1, 4); //BE theGraph.addEdge(2, 5); //CF theGraph.addEdge(3, 6); //DG theGraph.addEdge(4, 6); //EG theGraph.addEdge(5, 7); //FH theGraph.addEdge(6, 7); //GH theGraph.topo(); }
}
|