java最短路徑,求最短路徑的方法

本文目錄一覽:

JAVA求10個景點間各個景點的最短路徑 圖隨便話 距離隨便 求代碼

最有效,切不複雜的方法使用Breadth First Search (BFS). 基本代碼如下(偽代碼)。因為BFS不用遞歸,所以可能會有點難理解。

public Stack findPath(Vertex 起始景點, Vertex 目標景點){

Queue Vertex q = new QueueVertex();

s.enqueue(起始景點);

Vertex 當前位置;

while(!s.isEmpty()){

當前位置 = s.dequeue();

if (當前位置 == 目標景點) break;

for (每一個相鄰於 當前位置 的景點 Vertex v){

if (!v.visited){

v.parent = 當前位置;

// 不是規定,不過可以節省一點時間

if (v == 目標景點){

current = v;

break;

}

s.enqueue(Vertex v);

v.visited = true;

}

}

}

Stack Vertex solution = new Stack Vertex();

Vertex parent = current;

while (parent != 起始景點){

solution.push(parent);

parent = current.parent;

}

for (graph中的每一個vertex) vertex.visited = false;

return solution(); // 其實這裡建議用一個 Path 的inner class 來裝所獲得的路線

}

然後再 main 求每兩個景點之間的距離即可

public static void main(String[] argv){

PathFinder pf = new PathFinder();

Stack[][] 路徑 = new Stack[10][10];

for(int i=0; ipf.vertices.length; i++){

for(int j=i+1; jpf.vertices.length; j++){

Stack s = pf.findPath(pf.vertices[i], pf.vertices[j]);

路徑[i][j] = s; 路徑[j][i] = s; // 假設你的graph是一個undirected graph

}

}

// 這麼一來就大功告成了!對於每兩個景點n 與 m之間的最短路徑就是在 stack[n][m] 中

}

還有一種方法就是用Depth First Search遞歸式的尋找路徑,不過這樣比較慢,而且我的代碼可能會造成stack overflow

public Stack dfs(Vertex 當前景點,Vertex 目標景點){

if(當前景點 == 目標景點) return;

Stack solution = new Stack();

Stack temp;

for (相鄰於 點錢景點 的每一個 Vertex v){

if (!v.visited){

v.visited = true;

temp = dfs(v, 目標景點);

// 抱歉,不記得是stack.size()還是stack.length()

if (solution.size() == 0) solution = temp;

else if(temp.size() solution.size()) solution = temp;

v.visited = false; 復原

}

}

return solution;

}

然後再在上述的Main中叫dfs…

參考:

用java語言求最短路徑

最短路徑就是敲代碼。 這個東西行業公認,沒有比敲代碼學語言更加快的路了。

如果是單純感興趣可以買兩本書自學 什麼thinkinjava之類的,開始肯定看不懂的,誰開始都看不懂,摸索著來,時間長了就理解了。如果有其它語言基礎學起來就快多了,因為語言這種東西,除了語法不一樣,邏輯都是一樣的。

如果是工作需要什麼的,可以找個培訓啥的。當然前提你有錢。

最後順帶吐個槽,捷徑好找的話,程序員這工作就不值錢了。

Java最短路徑應用程序

package Test3;

import java.util.LinkedList;

import java.util.List;

/**

* 單源最短路徑問題

*

* @author Sailor

*

*/

public class ShortestPath {

private static String showPath[] = { “”, “”, “”, “”, “”, “” };

// 返回圖的最短路徑

public static int[] getShortPath(int path[][]) {

LinkedListInteger savePath = new LinkedListInteger();// 用於保存已添加進來的節點

int mark = 1;

int shortestPath[] = new int[path.length];

for (int i = 0; i shortestPath.length; i++) {

shortestPath[i] = -1;

}

savePath.add(new Integer(0));

if (savePath.size() == 1) {

int num = savePath.getLast().intValue();

int minIndex = 0;

for (int j = 0; j shortestPath.length; j++) {

shortestPath[j] = path[num][j];

if (shortestPath[j] = 0) {

showPath[j] = “1–” + (j + 1);

} else {

showPath[j] = “無通路”;

}

}

minIndex = getAddIndex(savePath, shortestPath);

savePath.add(minIndex);

}

if (savePath.size() 1) {

while (mark 6) {// savePath.size()6 當有不可到達的點是將要出現死循環

int num = savePath.getLast().intValue();

int minIndex = 0;

for (int j = 0; j path.length; j++) {

if (path[num][j] = 0) {

if (shortestPath[j] 0) {

shortestPath[j] = path[num][j] + shortestPath[num];

showPath[j] = showPath[num] + “–” + (j + 1);

} else {

if (shortestPath[num] + path[num][j] shortestPath[j]) {

shortestPath[j] = shortestPath[num]

+ path[num][j];

showPath[j] = showPath[num] + “–” + (j + 1);

}

}

}

}

minIndex = getAddIndex(savePath, shortestPath);

if (minIndex 0)

savePath.add(minIndex);

mark++;

}

}

return shortestPath;

}

// 獲得加入到保存路徑的節點

public static int getAddIndex(List list, int num[]) {

int index = 0;

for (int i = 0; i num.length; i++) {

if (!list.contains(new Integer(i))) {

if (num[i] 0 index == 0) {

index = i;

}

if (num[i] 0 index 0) {

if (num[i] num[index])

index = i;

}

}

}

return index;

}

public static void main(String[] args) {

int path[][] = { { 0, -1, 15, -1, -1, -1 }, { 2, 0, -1, -1, 10, 30 },

{ -1, 4, 0, -1, -1, 10 }, { -1, -1, -1, 0, -1, -1 },

{ -1, -1, -1, 15, 0, -1 }, { -1, -1, -1, 4, 10, 0 } };

int shortestPaht[] = getShortPath(path);

for (int i = 0; i shortestPaht.length; i++) {

System.out.print(“節點 1 到節點 ” + (i + 1) + ” 的最短距離是”

+ shortestPaht[i] + “\t”);

System.out.println(“路徑為:” + showPath[i]);

}

}

}

這是最短路徑的演算法

你再加個畫圖功能就行咧

JAVA中最短路徑演算法

給你個算graph上最短路徑的比較流行的方法

Algorithm Dijkstra(V, E, cost, s)

T ;

Cost(V[s]) 0

Prev(V[s]) none

for i 0 to length[V] – 1 do

if (i 6= s) then

Cost(V[i]) +1

Prev(V[i]) none

Build heap NotInTree from V

for i 1 to length[V] do

u DeleteMin(NotInTree)

add (u, Prev(u)) to T

for each neighbor v of u do

if (Cost(v) Cost(u) + cost(u,v)) then

Cost(v) Cost(u) + cost(u,v)

Prev(v) u

return T

Java/Android:許多點構成許多路徑,從中找出兩點間的最短路徑,怎麼實現?

可以通過循環查找,然後比較兩點的距離,建立一個局部變數來獲取這個比較後的短的距離,最終得到的就是這許多路徑的最短路徑

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/276886.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-19 13:20
下一篇 2024-12-19 13:20

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • 解決.net 6.0運行閃退的方法

    如果你正在使用.net 6.0開發應用程序,可能會遇到程序閃退的情況。這篇文章將從多個方面為你解決這個問題。 一、代碼問題 代碼問題是導致.net 6.0程序閃退的主要原因之一。首…

    編程 2025-04-29
  • Python中init方法的作用及使用方法

    Python中的init方法是一個類的構造函數,在創建對象時被調用。在本篇文章中,我們將從多個方面詳細討論init方法的作用,使用方法以及注意點。 一、定義init方法 在Pyth…

    編程 2025-04-29
  • Python創建分配內存的方法

    在python中,我們常常需要創建並分配內存來存儲數據。不同的類型和數據結構可能需要不同的方法來分配內存。本文將從多個方面介紹Python創建分配內存的方法,包括列表、元組、字典、…

    編程 2025-04-29

發表回復

登錄後才能評論