- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这段代码用于实现未加权图的 Dijkstra 算法。我应该更改什么才能使用加权图?我的图的边是 double 值,是否有机会在 shortestPath 方法中使用泛型类型?
/**
* Determine the shortest path to all vertices from a vertex using Dijkstra's algorithm
* To be called by public short method
*
* @param graph Graph object
* @param sourceIdx Source vertex
* @param knownVertices previously discovered vertices
* @param verticesIndex index of vertices in the minimum path
* @param minDist minimum distances in the path
*
*/
private static <V> void shortestPath(AdjacencyMatrixGraph<V,Double> graph, int sourceIdx, boolean[] knownVertices, int[] verticesIndex, double [] minDist) {
V vertexOrig = graph.vertices.get(sourceIdx);
Queue<V> qaux = new LinkedList<V>();
for(int i = 0; i < graph.numVertices; i++) {
minDist[i] = 0;
verticesIndex[i] = -1;
}
qaux.add(vertexOrig);
while(!qaux.isEmpty()) {
V vertex = qaux.remove();
for (V vertexAdj: graph.directConnections(vertex)) {
if(minDist[graph.toIndex(vertexAdj)] == 0) {
minDist[graph.toIndex(vertexAdj)] = minDist[graph.toIndex(vertex)]
+ graph.getEdge(vertex, vertexAdj);
verticesIndex[graph.toIndex(vertexAdj)] = graph.toIndex(vertex);
qaux.add(vertexAdj);
}
}
}
}
/**
* Determine the shortest path between two vertices using Dijkstra's algorithm
*
* @param graph Graph object
* @param source Source vertex
* @param dest Destination vertices
* @param path Returns the vertices in the path (empty if no path)
* @return minimum distance, -1 if vertices not in graph or no path
*
*/
public static <V> double shortestPath(AdjacencyMatrixGraph<V, Double> graph, V source, V dest, LinkedList<V> path){
path.clear();
if(!graph.checkVertex(source) || !graph.checkVertex(dest)) return -1;
else if(source.equals(dest)) {
path.add(dest);
return 0;
}
double minDist[] = new double[graph.numVertices];
int verticesIndex[] = new int[graph.numVertices];
shortestPath(graph, graph.toIndex(source), new boolean[graph.numVertices]
, verticesIndex, minDist);
if(verticesIndex[graph.toIndex(source)] == -1 || verticesIndex[graph.toIndex(dest)] == -1) return -1;
recreatePath(graph, graph.toIndex(source), graph.toIndex(dest), verticesIndex, path);
Collections.reverse(path);
System.out.println(path);
System.out.println(minDist[graph.toIndex(dest)]);
return minDist[graph.toIndex(dest)];
}
/**
* Recreates the minimum path between two vertex, from the result of Dikstra's algorithm
*
* @param graph Graph object
* @param sourceIdx Source vertex
* @param destIdx Destination vertices
* @param verticesIndex index of vertices in the minimum path
* @param Queue Vertices in the path (empty if no path)
*/
private static <V> void recreatePath(AdjacencyMatrixGraph<V, Double> graph, int sourceIdx, int destIdx, int[] verticesIndex, LinkedList<V> path){
path.add(graph.vertices.get(destIdx));
if (sourceIdx != destIdx){
destIdx = verticesIndex[destIdx];
recreatePath(graph, sourceIdx, destIdx, verticesIndex, path);
}
}
最佳答案
Dijkstra 算法使用加权图来计算从一个顶点到图中所有其他顶点的最短路径,前提是图中没有负边长。因此无需更改 Dijkstra 的实现即可使其与加权图一起使用。如果它不适用于加权图,那么问题出在 Dijkstra 的实现上。
如果图表未加权,您最好使用以线性时间运行的广度优先搜索来计算节点之间的距离。
Dijkstra 算法是一种贪心算法,它通过跟踪必须按其成本排序的展开顶点来工作。即下一个将被扩展的顶点是具有下一个最小成本的顶点。
这是我们不需要对 BFS 做的事情,因为所有的边权重都是相同的。 Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?显示两者之间的区别
通过您的实现,我看到您正在使用 Queue
来跟踪尚未探索的顶点。这不能确保展开的下一个顶点具有最低成本,因此您的算法将失败。
所以每次从Queue
中选取一个顶点进行扩展时,它应该是代价最小的那个。这可以通过每次遍历 Queue
并以最小成本选取顶点来实现,尽管这可能会将其减少到 O(n^2)
算法或使用堆数据结构,确保下一个选取的顶点始终是权重最小的顶点。
关于java - 如何使 Dijkstra 算法适应加权图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40709947/
我有一个包含三个字段的表:ID、值、计数 ID和Value构成PK。 给定一个 ID,我想选择一个按计数加权的值,然后将计数减一。 如果我有 1 A 2 1 B 3 我应该有 2/5 的机会获
我有一个数据集,其中开始日期和日期没有特定的顺序。我想创建一组新的几个月列和数据的加权平均值。 del 代表否。日期范围内的天数 d 是该时间段的平均值 from datetime import da
我正在开发的一个软件应用程序需要能够根据用户当前拥有的任务数量将任务分配给一组用户,其中任务最少的用户最有可能获得下一个任务。然而,当前的任务负载应该被视为一个权重,而不是一个绝对的顺序定义。 IOW
我正在尝试用字典创建一个光学字符识别系统。 事实上,我还没有实现字典=) 我听说有一些基于 Levenstein 距离的简单指标,这些指标考虑了不同符号之间的不同距离。例如。 'N' 和 'H' 彼此
三个AI新手问题: 为什么 A* 可以采用启发式算法来找到最佳路径? 如果障碍物挡住了道路,系带制动技术有什么用? 什么算法适合在有障碍物的网格上找到路径? (像吃 bean 一样) 第一个问题让我们
我有一个 2396x34 double matrix命名 y其中每一行 (2396) 代表一个单独的情况,由 34 个连续的时间段组成。 我也有 numeric[34]命名 x这代表了 34 个连续时
我有一个如下所示的多维数组,我想做一些奇特的排序,但我不知道如何处理它。我想首先按第 5 个元素对数组元素进行排序,然后是第 4 个元素,然后是第 3 个元素,然后是第 2 个元素,然后是第 1 个元
我想对具有三个变量(列)的数据集(即 Sample_Data)进行 Kmeans 聚类,如下所示: A B C 1 12 10 1 2 8 11 2 3 14 10
我环顾四周,发现了一些与我类似的问题,但它们缺乏解释。 我正在尝试搜索包含多列的表格。我希望匹配列数最多的行位于顶部,匹配列数最少的行位于底部。我见过几种方法。我目前的糟糕方式是使用大量 MySQL
我必须大量使用加权概率分布,并且想使用 violinplots 进行一些可视化。但是,我找不到在任何常见嫌疑人(matplotlib、seaborn、bokeh 等)中使用加权数据创建这些数据的方法。
我会尽量做到彻底: 我有 11 个小组。 我有很多人需要在这些组之间进行划分 每个人都有一个加权偏好列表。通常在该列表上有 3 个有序的组,但一些异常值会有更多或更少的组。 IE:人 1 有进入 A
我有 100 个顶点和一个计算顶点 x 和顶点 y 之间边的权重的函数 f(x,y)。 f 不是特别昂贵,因此如果需要,我可以生成带权重的索引邻接列表。 有哪些有效、易处理的方法可以通过最小化或最大化
谁能给我指出一个关于如何构建(乘法和/或加法)加权 voronoi 图的引用实现,该图最好基于 Fortune 的 voronoi 算法? 我的目标:给定一组点(每个点都有一个权重)和一组边界边(通常
有没有一种方法可以使用标准库进行漂亮而优雅的加权洗牌?有 std::discrete_distribution。我想要的是这样的: std::vector data { N elements }; s
其实不是RANDBETWEEN()。我正在尝试创建一个 UDF 来返回数组中数字的索引,其中数字越大,被选择的可能性就越大。 我知道如何将概率分配给工作表中的随机数(即对概率之和使用 MATCH()
canvas 占据了整个屏幕,从绿色可以看出。canvasFrame 有两行,其中第二行是滚动文本小部件。第二行也被加权但是它没有填满屏幕,因为绿色区域应该是黄色的。如何让第二行填充空白并让滚动的文本
我正在研究 HITS 算法实现的加权版本。 这是 Hits 算法的公式(非加权版本): 其中HITS A为权威评分,HITS H为hub评分,维基百科算法伪代码: G := set of pages
我不确定如何实现这个,但这里是描述: 取一个 0-10 之间的数字作为输入(0 总是返回 false,10 总是返回 true) 将接收到的参数作为输入,并传递给一个函数,在运行时确定所需的 bool
所以我在 Pandas DataFrame 中有两个值列和两个权重列,我想生成第三列,它是这两列的分组依据、加权平均值。 因此: df = pd.DataFrame({'category':['a',
我正在尝试使用 ggridges 包(基于 ggplot2)创建一个 joyplot。一般的想法是 joyplot 创建很好缩放的堆叠密度图。但是,我似乎无法使用加权密度生成其中之一。在创建 joyp
我是一名优秀的程序员,十分优秀!