java查找圖中兩點(diǎn)之間所有路徑
本文實(shí)例為大家分享了java查找圖中兩點(diǎn)之間所有路徑的具體代碼,基于鄰接表,供大家參考,具體內(nèi)容如下
圖類(lèi):
package graph1; 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<vexsarray.length;i++) { if (data==vexsarray[i].data) { return i; } } return -1; } public void buildGraph(int[] vexs,int[][] edges ) { int vLen = vexs.length; int eLen = edges.length; vexsarray = new VexNode[vLen]; for(int i=0;i<vLen;i++) { vexsarray[i] = new VexNode(); vexsarray[i].data = vexs[i]; vexsarray[i].firstEdge = null; } for(int i=0;i<eLen;i++) { int a = edges[i][0]; int b = edges[i][1]; int start = getPosition(a); int end = getPosition(b); EdgeNode edgeNode = new EdgeNode(); edgeNode.adjvex = end; if (vexsarray[start].firstEdge == null) { vexsarray[start].firstEdge = edgeNode; } else { linkLast(vexsarray[start].firstEdge,edgeNode); } } } public void printGraph() { for(int i=0;i<vexsarray.length;i++) { System.out.printf("%d--",vexsarray[i].data); EdgeNode node = vexsarray[i].firstEdge; while (node!=null) { System.out.printf("%d(%d)--",node.adjvex,vexsarray[node.adjvex].data); node = node.nextEdge; } System.out.println("\n"); } }
算法:
package graph1; import java.util.HashMap; import java.util.Map; import java.util.Stack; import javax.swing.plaf.synth.SynthStyle; import graph1.Graph.EdgeNode; public class FindALlPath { //代表某節(jié)點(diǎn)是否在stack中,避免產(chǎn)生回路 public Map<Integer,Boolean> states=new HashMap(); //存放放入stack中的節(jié)點(diǎn) public Stack<Integer> 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<graph.vexsarray.length;i++){ states.put(i,false); } //stack top元素 int top_node; //存放當(dāng)前top元素已經(jīng)訪問(wèn)過(guò)的鄰接點(diǎn),若不存在則置-1,此時(shí)代表訪問(wèn)該top元素的第一個(gè)鄰接點(diǎn) int adjvex_node=-1; int next_node; stack.add(x); states.put(x,true); while(!stack.isEmpty()){ top_node=stack.peek(); //找到需要訪問(wèn)的節(jié)點(diǎn) if(top_node==y){ //打印該路徑 printPath(); adjvex_node=stack.pop(); states.put(adjvex_node,false); } else{ //訪問(wèn)top_node的第advex_node個(gè)鄰接點(diǎn) next_node=getNextNode(graph,top_node,adjvex_node); if(next_node!=-1){ stack.push(next_node); //置當(dāng)前節(jié)點(diǎn)訪問(wèn)狀態(tài)為已在stack中 states.put(next_node,true); //臨接點(diǎn)重置 adjvex_node=-1; } //不存在臨接點(diǎn),將stack top元素退出 else{ //當(dāng)前已經(jīng)訪問(wèn)過(guò)了top_node的第adjvex_node鄰接點(diǎn) adjvex_node=stack.pop(); //不在stack中 states.put(adjvex_node,false); } } } } }
測(cè)試類(lèi):
package graph1; import java.util.Iterator; import graph1.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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java String類(lèi)常用方法梳理總結(jié)
這篇文章主要介紹了Java String類(lèi)常用方法梳理總結(jié),類(lèi) String 中包括用于檢查各個(gè)字符串的方法,比如用于比較字符串,搜索字符串,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-06-06javaweb圖書(shū)商城設(shè)計(jì)之圖書(shū)模塊(4)
這篇文章主要介紹了javaweb圖書(shū)商城設(shè)計(jì)之圖書(shū)模塊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Java實(shí)現(xiàn)經(jīng)典游戲推箱子的示例代碼
《推箱子》推箱子是一個(gè)古老的游戲,目的是在訓(xùn)練你的邏輯思考能力。本文將利用Java實(shí)現(xiàn)這一經(jīng)典的小游戲,并采用了swing技術(shù)進(jìn)行了界面化處理,需要的可以參考一下2022-02-02Session過(guò)期后自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面的實(shí)例代碼
這篇文章主要介紹了Session過(guò)期后自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面實(shí)例代碼,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06解決異常FileNotFoundException:class path resource找不到資源文件的問(wèn)題
今天小編就為大家分享一篇關(guān)于解決異常FileNotFoundException:class path resource找不到資源文件的問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)
這篇文章主要介紹了關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)
大家應(yīng)該都知道請(qǐng)求重定向和請(qǐng)求轉(zhuǎn)發(fā)都是web開(kāi)發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Spring-Security對(duì)HTTP相應(yīng)頭的安全支持方式
這篇文章主要介紹了Spring-Security對(duì)HTTP相應(yīng)頭的安全支持方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10