国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Dijkstra算法——鄰接矩陣實(shí)現(xiàn)+路徑記錄

這篇具有很好參考價(jià)值的文章主要介紹了Dijkstra算法——鄰接矩陣實(shí)現(xiàn)+路徑記錄。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

本文是在下面這篇文章的基礎(chǔ)上做了一些補(bǔ)充,增加了路徑記錄的功能。具體Dijkstra的實(shí)現(xiàn)過(guò)程可以參考下面的這篇文章。
[jarvan:Dijkstra算法詳解 通俗易懂](Dijkstra算法詳解 通俗易懂 - jarvan的文章 - 知乎
https://zhuanlan.zhihu.com/p/338414118)

創(chuàng)建 GraphAdjMat 類(lèi)
GraphAdjMat 類(lèi)用來(lái)實(shí)現(xiàn)圖的鄰接矩陣,方便后續(xù)的測(cè)試,具體代碼如下:

package algorithm.graph;

import java.util.ArrayList;
import java.util.List;

public class GraphAdjMat {
	
	private List<Integer> vertices;
	private List<List<Integer>> adjMat;
	
	/**
	 * 構(gòu)造函數(shù)
	 * @param vertices 頂點(diǎn)列表
	 * @param edges 邊
	 */
	public GraphAdjMat(int[]vertices, int[][]edges) {
		this.vertices = new ArrayList<>();
		this.adjMat = new ArrayList<>();
		for(int v : vertices) {
			addVertex(v);
		}
		for(int[]e : edges) {
			addEdge(e[0],e[1],e[2]);
		}
		//設(shè)置頂點(diǎn)自己到自己的權(quán)重為0
		for(int i=0; i<vertices.length; i++) {
			this.adjMat.get(i).set(i, 0);
		}
	}
	
	public List<List<Integer>> getAdjMat() {
		return this.adjMat;
	}
	
	/**
	 * 添加頂點(diǎn)
	 */
	public void addVertex(int val) {
		int n = vertices.size();
		vertices.add(val);
		List<Integer> newRow = new ArrayList<>();
		for(int i=0; i<n; i++) {
			newRow.add(-1);
		}
		adjMat.add(newRow);
		for(List<Integer> row : adjMat) {
			row.add(-1);
		}
	}
	
	/**
	 * 移除頂點(diǎn)
	 */
	public void removeVertex(int index) {
		if(index < 0 || index >= vertices.size()) {
			throw new IndexOutOfBoundsException();
		}
		vertices.remove(index);
		adjMat.remove(index);
		for(List<Integer> row : adjMat) {
			row.remove(index);
		}
	}
	
	/**
	 * 添加邊
	 * @param i 頂點(diǎn)1
	 * @param j 頂點(diǎn)2
	 * @param weight 邊權(quán)重
	 */
	public void addEdge(int i, int j, int weight) {
		if(i<0||j<0||i>=vertices.size()||j>=vertices.size()||i==j) {
			throw new IndexOutOfBoundsException();
		}
		adjMat.get(i).set(j, weight);
		adjMat.get(j).set(i, weight);
	}
	
	/**
	 * 移除邊
	 */
	public void removeEdge(int i, int j) {
		if(i<0||j<0||i>=vertices.size()||j>=vertices.size()||i==j) {
			throw new IndexOutOfBoundsException();
		}
		adjMat.get(i).set(j, -1);
		adjMat.get(j).set(i, -1);
	}
	
	public void print() {
		System.out.println("adj mat:");
		for(List<Integer> row : adjMat) {
			for(int v : row) {
				System.out.printf("%3d", v);
			}
			System.out.println();
		}
	}

}

GraphAdjMat 類(lèi)中提供了增加頂點(diǎn)、移除頂點(diǎn)、增加邊和移除邊的操作。
創(chuàng)建 DijkstraAlg 類(lèi)
該類(lèi)用于實(shí)現(xiàn) Dijkstra 算法,并打印指定點(diǎn)到所有點(diǎn)的最短距離和路徑信息

package algorithm.graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Dijkstra 算法
 */
public class DijkstraAlg {

	public static void main(String[]args) {
		char[]vertexNames = {'A','B','C','D'};
		int[]vertices = {1,2,3,4};
		int[][]edges = {{0,1,1},{0,3,6},{1,2,4},{1,3,1},{2,3,1}};
		//構(gòu)建鄰接矩陣
		GraphAdjMat adjMat = new GraphAdjMat(vertices, edges);
		adjMat.print();
		int startVertex = 0;
		List<ShortestPath> result = dijkstra(adjMat.getAdjMat(),startVertex);
		System.out.println("dijkstra result: ");
		for(int i=0; i<vertexNames.length; i++) {
			System.out.printf("%3s -> %s distence : %d ; ",vertexNames[startVertex], vertexNames[i], result.get(i).distence);
			List<Integer> path = result.get(i).path;
			System.out.print("A -> ");
			for(int j=0; j<path.size(); j++) {
				if(j < path.size()-1) {					
					System.out.printf("%s -> ",vertexNames[path.get(j)]);
				}else {
					System.out.printf("%s\n", vertexNames[path.get(j)]);
				}
			}
		}
	}
	
	public static List<ShortestPath> dijkstra(List<List<Integer>> graph, int startVertex) {
		int len = graph.size();
		List<ShortestPath> result = new ArrayList<>(len);
		int[]notFound = new int[len];
		//初始化 result
		for(int i=0; i<len; i++) {
			ShortestPath shortestPath = new ShortestPath();
			shortestPath.distence = -1;
			shortestPath.path = new ArrayList<>();
			result.add(i, shortestPath);
		}
		ShortestPath startVertexPath = new ShortestPath();
		startVertexPath.distence = 0;
		startVertexPath.path = new ArrayList<>(0);
		result.set(startVertex,startVertexPath);
		//初始化 notFound
		for(int i=0; i<len; i++) {
			notFound[i] = graph.get(startVertex).get(i);
		}
		notFound[startVertex] = -1;
		//開(kāi)始 Dijkstra 算法
		Map<Integer,List<Integer>> recordPathMap = new HashMap<>();
		for(int i=1; i<len; i++) {
			int min = Integer.MAX_VALUE;
			int minIndex = 0;
			for(int j=0; j<len; j++) {
				if(notFound[j] > 0 && notFound[j] < min) {
					min = notFound[j];
					minIndex = j;
				}
			}
			result.get(minIndex).distence = min;
			notFound[minIndex] = -1;
			//刷新 notFound
			for(int j=0; j<len; j++) {
				//graph.get(minIndex).get(j) > 0 用來(lái)確保 minIndex 頂點(diǎn)有邊,result[j] == -1 用來(lái)確保 j 點(diǎn)沒(méi)有在結(jié)果集中
				if(graph.get(minIndex).get(j) > 0 && result.get(j).distence == -1) {
					int newDistence = result.get(minIndex).distence + graph.get(minIndex).get(j);
					//計(jì)算合并距離如果小于直接到j(luò)點(diǎn)的距離,或者無(wú)法到達(dá)j點(diǎn)事將新距離刷新到notFound中
					if(newDistence < notFound[j] || notFound[j] == -1) {
						notFound[j] = newDistence;
						if(!recordPathMap.containsKey(j)) {
							List<Integer> tempList = new ArrayList<>(1);
							tempList.add(minIndex);
							recordPathMap.put(j, tempList);
						}else {							
							recordPathMap.get(j).add(minIndex);
						}
					}
				}
			}
		}
		System.out.println(recordPathMap);
		//推到路徑
		for(int i=0; i<len; i++) {
			result.get(i).path.addAll(recordPathMap.getOrDefault(i, new ArrayList<>()));
			result.get(i).path.add(i);
		}
		return result;
	}
	
	public static void printArr(int[]arr, String arrName) {
		System.out.print(arrName);
		for(int i : arr) {
			System.out.printf("%3d", i);
		}
		System.out.println();
	}
	
	static class ShortestPath {
		public int distence;
		public List<Integer> path;
	}
}

代碼在原文代碼的基礎(chǔ)上通過(guò)增加 recordPathMap 來(lái)記錄最短路徑,最終打印最短路徑距離和對(duì)應(yīng)路勁信息。
Dijkstra算法——鄰接矩陣實(shí)現(xiàn)+路徑記錄,隨筆,算法文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-802274.html

到了這里,關(guān)于Dijkstra算法——鄰接矩陣實(shí)現(xiàn)+路徑記錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【路徑規(guī)劃】全局路徑規(guī)劃算法——Dijkstra算法(含python實(shí)現(xiàn) | c++實(shí)現(xiàn))

    【路徑規(guī)劃】全局路徑規(guī)劃算法——Dijkstra算法(含python實(shí)現(xiàn) | c++實(shí)現(xiàn))

    路徑規(guī)劃與軌跡跟蹤系列算法學(xué)習(xí) 最短路徑算法-迪杰斯特拉(Dijkstra)算法 迪杰斯特拉dijkstra算法的python實(shí)現(xiàn) Python實(shí)現(xiàn)迪杰斯特拉算法 迪杰斯特拉算法(Dijkstra)是由荷蘭計(jì)算機(jī)科學(xué)家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法。是從一個(gè)節(jié)點(diǎn)遍歷其余各節(jié)點(diǎn)的最短路

    2024年02月08日
    瀏覽(18)
  • dijkstra迪杰斯特拉算法(鄰接表法)

    dijkstra迪杰斯特拉算法(鄰接表法)

    算法簡(jiǎn)易過(guò)程: 求單源有向圖最短路徑 使用 鄰接表法 來(lái)存儲(chǔ)頂點(diǎn)和邊,錄入 有向圖 。 (當(dāng)然也可以無(wú)向圖,不過(guò)錄入時(shí)要錄入兩次,比如 a b 3? ? ? ? b a 3) ?代碼如下: 測(cè)試如下: ?

    2024年02月07日
    瀏覽(33)
  • 基于Dijkstra算法實(shí)現(xiàn)無(wú)人機(jī)三維路徑規(guī)劃

    基于Dijkstra算法實(shí)現(xiàn)無(wú)人機(jī)三維路徑規(guī)劃 無(wú)人機(jī)在飛行任務(wù)中往往需要尋找一條最優(yōu)路徑以達(dá)到最佳的飛行效果。而在三維空間中,路徑規(guī)劃問(wèn)題變得更加復(fù)雜。本文將介紹如何基于Dijkstra算法來(lái)解決無(wú)人機(jī)三維路徑規(guī)劃問(wèn)題,并且提供相應(yīng)的matlab代碼。 一、Dijkstra算法簡(jiǎn)介

    2024年02月14日
    瀏覽(91)
  • Dijkstra實(shí)現(xiàn)(鄰接表C++版)

    Dijkstra實(shí)現(xiàn)(鄰接表C++版)

    從V0出發(fā),到各個(gè)節(jié)點(diǎn)的最短距離 Dijkstra的過(guò)程,就是維護(hù)并更新一個(gè)表來(lái)實(shí)現(xiàn)的。 其中distance表示是從起始節(jié)點(diǎn),到當(dāng)前節(jié)點(diǎn)的距離。 path表示經(jīng)過(guò)哪個(gè)節(jié)點(diǎn)達(dá)到當(dāng)前節(jié)點(diǎn)。 表初始化為:path全-1,distance全為INT_MAX。 vertices path distance 0 -1 ∞ 1 -1 ∞ 2 -1 ∞ 3 -1 ∞ 4 -1 ∞ 5 -1 ∞

    2023年04月13日
    瀏覽(13)
  • 使用鄰接矩陣實(shí)現(xiàn)最小生成樹(shù)Prim算法 題目編號(hào):1135

    用鄰接矩陣存儲(chǔ)無(wú)向圖,實(shí)現(xiàn)最小生成樹(shù)Prim算法,圖中邊的權(quán)值為整型,頂點(diǎn)個(gè)數(shù)少于10個(gè)。 部分代碼提示: #include using namespace std; const int MaxSize = 10; const int INF = 32767; class MGraph { public: MGraph(char a[], int n, int e); private: char vertex[MaxSize]; int arc[MaxSize][MaxSize]; int vertexNum, arcNum; }

    2024年02月06日
    瀏覽(23)
  • 自動(dòng)駕駛路徑規(guī)劃——Dijkstra算法

    自動(dòng)駕駛路徑規(guī)劃——Dijkstra算法

    這個(gè)學(xué)期學(xué)校開(kāi)設(shè)了相應(yīng)的課程,同時(shí)也在學(xué)習(xí)古月居機(jī)器人學(xué)系列的《基于柵格地圖的機(jī)器人路徑規(guī)劃指南》,為了鞏固知識(shí),方便自己的學(xué)習(xí)與整理,遂以學(xué)習(xí)筆記的形式記錄。 ???? 深度優(yōu)先搜索( Depth First Search , DFS ) :首先從某個(gè)頂點(diǎn)出發(fā),依次從它的各個(gè)未被

    2024年01月22日
    瀏覽(29)
  • 基于Dijkstra算法的機(jī)器人編隊(duì)路徑規(guī)劃問(wèn)題

    基于Dijkstra算法的機(jī)器人編隊(duì)路徑規(guī)劃問(wèn)題 路徑規(guī)劃是機(jī)器人領(lǐng)域中的一個(gè)重要問(wèn)題,它涉及確定從起點(diǎn)到目標(biāo)點(diǎn)的最佳路徑。Dijkstra算法是一種經(jīng)典的圖算法,用于解決最短路徑問(wèn)題。在本文中,我們將介紹如何使用Dijkstra算法來(lái)實(shí)現(xiàn)機(jī)器人編隊(duì)的路徑規(guī)劃,并提供相應(yīng)的

    2024年02月08日
    瀏覽(18)
  • 【數(shù)據(jù)結(jié)構(gòu)與算法】圖——鄰接表與鄰接矩陣

    【數(shù)據(jù)結(jié)構(gòu)與算法】圖——鄰接表與鄰接矩陣

    圖(Graph)是由頂點(diǎn)的有窮非空集合和頂點(diǎn)之間邊的集合組成,通常表示為:G(V,E),其中, G表示一個(gè)圖,V是頂點(diǎn)的集合,E是邊的集合 。 在圖中數(shù)據(jù)元素,我們則稱(chēng)之為頂點(diǎn)(Vertex)。 圖中,任意兩個(gè)頂點(diǎn)之間都可能有關(guān)系,頂點(diǎn)之間的邏輯關(guān)系用邊來(lái)表示,邊集可以是

    2024年02月02日
    瀏覽(24)
  • 一起來(lái)學(xué)算法(鄰接矩陣)

    一起來(lái)學(xué)算法(鄰接矩陣)

    ? ? ? ?鄰接矩陣是數(shù)學(xué)和計(jì)算機(jī)科學(xué)中常用的一種表示方式,用來(lái)表述有向圖或無(wú)向圖,一張圖由一組頂點(diǎn)(或結(jié)點(diǎn))和一組表組成,用鄰接矩陣就能表示這些頂點(diǎn)間存在的邊的關(guān)系 ? ? ? ?對(duì)于圖而言,是數(shù)據(jù)結(jié)構(gòu)中最復(fù)雜的結(jié)構(gòu),而是在做題的過(guò)程中,最大的難點(diǎn)在于

    2024年02月05日
    瀏覽(20)
  • 【路徑規(guī)劃】(1) Dijkstra 算法求解最短路,附python完整代碼

    【路徑規(guī)劃】(1) Dijkstra 算法求解最短路,附python完整代碼

    好久不見(jiàn),我又回來(lái)了, 這段時(shí)間把路徑規(guī)劃的一系列算法整理一下 ,感興趣的點(diǎn)個(gè)關(guān)注。今天介紹一下機(jī)器人路徑規(guī)劃算法中最基礎(chǔ)的 Dijkstra 算法,文末有 python 完整代碼,那我們開(kāi)始吧。 1959 年,荷蘭計(jì)算機(jī)科學(xué)家 ·EdsgerWybe·Dijkstra 發(fā)表了論文《 A note on two problems in c

    2023年04月08日
    瀏覽(22)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包