小編給大家分享一下java如何查找圖中兩點(diǎn)之間所有路徑,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供成都服務(wù)器托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
圖類:
package graph2; import java.util.LinkedList; import graph.Graph.edgeNode; public class Graph { class EdgeNode{ int adjvex; EdgeNode nextEdge; } class VexNode{ int data; EdgeNode firstEdge; boolean isVisted; public boolean isVisted() { return isVisted; } public void setVisted(boolean isVisted) { this.isVisted = isVisted; } } VexNode[] vexsarray ; int[] visited = new int[100]; boolean[] isVisited = new boolean[100]; public void linkLast(EdgeNode target,EdgeNode node) { while (target.nextEdge!=null) { target=target.nextEdge; } target.nextEdge=node; } public int getPosition(int data) { for(int i=0;i算法:
package graph2; import java.util.HashMap; import java.util.Map; import java.util.Stack; import javax.swing.plaf.synth.SynthStyle; import graph2.Graph.EdgeNode; public class FindALlPath { //代表某節(jié)點(diǎn)是否在stack中,避免產(chǎn)生回路 public Mapstates=new HashMap(); //存放放入stack中的節(jié)點(diǎn) public Stack stack=new Stack(); //打印stack中信息,即路徑信息 public void printPath(){ StringBuilder sb=new StringBuilder(); for(Integer i :stack){ sb.append(i+"->"); } sb.delete(sb.length()-2,sb.length()); System.out.println(sb.toString()); } //得到x的鄰接點(diǎn)為y的后一個(gè)鄰接點(diǎn)位置,為-1說(shuō)明沒(méi)有找到 public int getNextNode(Graph graph,int x,int y){ int next_node=-1; EdgeNode edge=graph.vexsarray[x].firstEdge; if(null!=edge&&y==-1){ int n=edge.adjvex; //元素還不在stack中 if(!states.get(n)) return n; return -1; } while(null!=edge){ //節(jié)點(diǎn)未訪問(wèn) if(edge.adjvex==y){ if(null!=edge.nextEdge){ next_node=edge.nextEdge.adjvex; if(!states.get(next_node)) return next_node; } else return -1; } edge=edge.nextEdge; } return -1; } public void visit(Graph graph,int x,int y){ //初始化所有節(jié)點(diǎn)在stack中的情況 for(int i=0;i 測(cè)試類:
package graph2; import java.util.Iterator; import graph2.Graph.VexNode; public class Tset2 { public static void main(String[] args) { int[] vexs = {0,1,2,3,4}; int[][] edges = { {0,1}, {0,3}, {1,0}, {1,2}, {2,1}, {2,3}, {2,4}, {3,0}, {3,2}, {3,4}, {4,2}, {4,3}, }; Graph graph = new Graph(); graph.buildGraph(vexs, edges); graph.printGraph(); FindALlPath findALlPath = new FindALlPath(); findALlPath.visit(graph, 4, 0); } }以上是“java如何查找圖中兩點(diǎn)之間所有路徑”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞標(biāo)題:java如何查找圖中兩點(diǎn)之間所有路徑
當(dāng)前URL:http://weahome.cn/article/ijsihg.html