dijkstra

Dijkstra和Floyd求最短路径

拥有回忆 提交于 2019-12-10 06:38:25
记录一下这两个算法 Dijkstra # include <stdlib.h> # include <stdio.h> # include <iostream> using namespace std ; /*邻接矩阵法*/ # define MAXV 50 # define INF 32767 typedef struct { int edges [ MAXV ] [ MAXV ] ; int n , e ; } MatGraph ; void CreateGraph ( MatGraph & G ) //创建的是带权有向图 { int n , e ; int x , y , w ; printf ( "输入顶点和边的个数:\n" ) ; cin >> n >> e ; G . e = e ; G . n = n ; for ( int i = 0 ; i < G . n ; i ++ ) for ( int j = 0 ; j < G . n ; j ++ ) { if ( i == j ) G . edges [ i ] [ j ] = 0 ; else G . edges [ i ] [ j ] = INF ; } for ( int i = 0 ; i < G . e ; i ++ ) { printf ( "输入第%d条边的信息\n" , i + 1 ) ; cin >

Dijkstra's Algorithm for Negative Weights

≯℡__Kan透↙ 提交于 2019-12-10 04:17:20
问题 Okay, first of all I know Dijkstra does not work for negative weights and we can use Bellman-ford instead of it. But in a problem I was given it states that all the edges have weights from 0 to 1 (0 and 1 are not included). And the cost of the path is actually the product. So what I was thinking is just take the log. Now all the edges are negative. Now I know Dijkstra won't work for negative weights but in this case all the edges are negative so can't we do something so that Dijkstra would

最短路径算法

陌路散爱 提交于 2019-12-09 18:09:23
单源最短路径问题 问题描述:给你一个顶点做源点,你想要知道,如何从源点到达其他所有点的最短路径。 OK,这个问题看起来没什么用。我们一般想知道的是A点到B点的最短路径,这个单源最短路径问题告诉我们A点到所有点的最短路径,会不会计算过多了呢? 有趣的是,解决A点到B点的最短路径算法不会比单源最短路径问题简单,我们所知的求A点到B点的最短路径算法就是求A点到任何点的最短路径。我们除了这样做,好像也没什么好办法了。 Dijkstra算法 基本原理: 每次新扩展一个距离最短的点,更新与其相邻的点的距离。当所有边权都为正时,由于不会存在一个距离更短的没扩展过的点,所以这个点的距离永远不会再被改变,因而保证了算法的正确性。不过根据这个原理,用Dijkstra求最短路的图 不能有负权边 ,因为扩展到负权边的时候会产生更短的距离,有可能就破坏了已经更新的点距离不会改变的性质。 适用条件与限制: 有向图 和 无向图 都可以使用本算法,无向图中的每条边可以看成相反的两条边。 用来求最短路的图中不能存在负权边。(可以利用拓扑排序检测) 算法流程: 在以下说明中,s为源,w[u,v]为点u和v之间的边的长度,结果保存在dist[] 初始化:源的距离dist[s]设为0,其他的点距离设为 正无穷大 ,同时把所有的点的状态设为没有扩展过。 循环n-1次: 在没有扩展过的点中取距离最小的点u

How do I define a custom distance in Boost Dijkstra?

混江龙づ霸主 提交于 2019-12-09 17:48:28
问题 I'm currently looking at the documentation of Boost Dijkstra - http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/dijkstra_shortest_paths.html; my objective is to modify the distance combining to get a "max" instead of a "plus" when computing my distances. The doc says this: IN: distance_combine(CombineFunction cmb) This function is used to combine distances to compute the distance of a path. The CombineFunction type must be a model of Binary Function. The first argument typ of the binary

Does java have an indexed minimum priority queue?

你离开我真会死。 提交于 2019-12-09 10:05:09
问题 I need it for an implementation of Dijkstra's algorithm, and I do have my own implementation but documenting my code would be easier with java's own classes. 回答1: No, Java standard library has no such data structure. I think most people use this: http://algs4.cs.princeton.edu/24pq/IndexMinPQ.java.html 回答2: What do you mean 'indexed'? Priority queue doesn't support indexing, unless it won't be queue any more. Java supports standard Priority Queue like C++ STL. It can be found in java.util

Use Dijkstra's to find a Minimum Spanning Tree?

牧云@^-^@ 提交于 2019-12-09 02:36:35
问题 Dijkstra's is typically used to find the shortest distance between two nodes in a graph. Can it be used to find a minimum spanning tree? If so, how? Edit: This isn't homework, but I am trying to understand a question on an old practice exam. 回答1: Strictly, the answer is no. Dijkstra's algorithm finds the shortest path between 2 vertices on a graph. However, a very small change to the algorithm produces another algorithm which does efficiently produce an MST. The Algorithm Design Manual is the

1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

…衆ロ難τιáo~ 提交于 2019-12-08 15:26:33
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique. Input Specification: Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N ( ≤) is the number of cities (and hence the cities are numbered from 0 to

Dijkstra shortest path for an undirected graph

前提是你 提交于 2019-12-08 12:36:03
问题 My following code is working perfectly fine for directed graphs and when given an undirected graph, it will not return the shortest path. public void Djikstra(int s){ boolean[] marked = new boolean[V]; dist = new double[V]; for(int i = 0; i<V; i++){ # initializing array dist[i] = Double.POSITIVE_INFINITY; } dist[s] = 0.0; Queue<Integer> pqs = new PriorityQueue<Integer>(); pqs.add(s); while(!pqs.isEmpty()){ int v = pqs.poll(); if(marked[v]) continue; marked[v] = true; for(Edge e : get_list(v))

Dijkstra's algorthm modification

ぐ巨炮叔叔 提交于 2019-12-08 08:53:57
问题 I am aware of the Dijkstra's shortest path algorithm. However, if I were to modify it so that instead of finding the shortest path it would find the longest path using a greedy algorithm. What would I have to do to the code below: Here is what Im using: as a compare function to select the correct node in the shortest path version: if (Cost(potential_node) > Cost(current_node) + cost(source , current_node)) then cost (potential_node) = cost(current_node) + cost (source, current_node) However,

Dijkstra’s Algorithm in python

╄→尐↘猪︶ㄣ 提交于 2019-12-08 06:05:34
问题 I am trying to implement Dijkstra’s algorithm on my python code but I can't really get the algorithm right. The algorithm I am using is from this youtube link: https://www.youtube.com/watch?v=pVfj6mxhdMw So basically my class has these 3 variables: self.nodes = [] #a,b,c self.neighbours = {} # a:[b,c], b:[c], c:[a] self.weights = {} #[a,b] = 2, [a,c] = 5 Here is how I partially implemented my shortest path function using the algorithm provided in the video: def dijkstra(self, start, end):