- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为 Java 中的无向、未加权(权重 = 1)图编写 Betweeness Centrality 方法。我采用的方法是找到图中的所有最短路径,然后遍历这些路径并计算顶点在该路径中出现的频率。我使用 Floyd Warshall 算法找到最短路径,并使用另一个数组重建路径,类似于 pseudo code on the Wikipedia.
但是,我的结果不正确,我试图找出问题所在,但我做不到。为了完整起见,我将在这里发布整个代码,但是它很困惑,所以我深表歉意。我会评论我认为会出现问题的位。
public void calculateBetweenessCentrality() {
// Floyd warshall algorithm, storing paths with R
int noPath = Integer.MAX_VALUE / 4;
int[][] adjMatrix = getAdjacencyMatrix();
int distances[][] = new int[numVertices][numVertices];
int[][] R = new int[numVertices][numVertices];
// Initialize the arrays, setting "-5000" as null instead. Possible error here?
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
if (adjMatrix[i][j] == 0) {
distances[i][j] = noPath;
R[i][j] = -5000; // null
}
else {
distances[i][j] = adjMatrix[i][j];
R[i][j] = j;
}
}
}
// Do the algorithm, and save in R, possible error here?
for (int k = 0; k < numVertices; k++) {
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
if (distances[i][j] > distances[i][k] + distances[k][j]) {
distances[i][j] = distances[i][k] + distances[k][j];
R[i][j] = R[i][k];
}
}
}
}
// Go through R and construct the shortest paths, record the frequency for each node (indexs). Possible error here?
HashMap<Integer, Integer> frequencies = new HashMap<>(); // Key = index, Value = frequency
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
ArrayList<Integer> path = findShortestPath(R, i, j);
for (int p : path) {
int freq = frequencies.containsKey(p) ? frequencies.get(p) : 0;
frequencies.put(p, freq + 1);
}
}
}
HashMap<Integer, Integer> temp = new HashMap<Integer, Integer>(); // Instead of printing the vertex's adjacency matrix index value, get the actual value for displaying purposes.
for (Entry<Integer, Integer> freq : frequencies.entrySet()) {
temp.put(verticesIndexValue.get(freq.getKey()), freq.getValue());
}
System.out.println("Top 5 nodes: \nNode - Count");
frequencies.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).limit(5)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new))
.forEach((node, frequency) -> System.out.println(node + " - " + frequency));
}
private ArrayList<Integer> findShortestPath(int[][] R, int u, int v) {
ArrayList<Integer> paths = new ArrayList<>();
if(R[u][v] == -5000)
return paths;
paths.add(u);
while(u != v) {
u = R[u][v];
paths.add(u);
}
return paths;
}
我正在测试的图表来自 this input here ,其中每条线都是一条边。该 pastebin 中的图形创建了两个连接的组件。我得到的第一个组件的输出如下:
Top 5 nodes:
Node - Count
11336782 - 11393
50393960 - 9047
627363 - 4079
849131 - 3799
5676102 - 3351
答案其实是50393960是顶级节点。如果有人可以指导我去哪里出错,我将不胜感激。谢谢 =)
最佳答案
您的代码在计算频率的地方包含错误 - 根据介数中心性的定义,在计算特定顶点 V
时,您应该排除以顶点 V 开始或结束的最短路径
。基本上这意味着当遍历最短路径时,您不应该将开始和结束顶点添加到频率中。试试这个:
HashMap<Integer, Integer> frequencies = new HashMap<>(); // Key = index, Value = frequency
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
ArrayList<Integer> path = findShortestPath(R, i, j);
for (int p : path) {
if (p == i || p == j) {
continue;
}
int freq = frequencies.containsKey(p) ? frequencies.get(p) : 0;
frequencies.put(p, freq + 1);
}
}
}
关于java - 使用 Java 中的 Floyd Warshall 算法保存图中的最短路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50542749/
一 问题描述 求节点0到节点2的最短路径。 二 代码 package graph.floyd; import java.util.Scanner; public class Floyd {
一 背景介绍 如果求解任意两个节点之间的最短路径,则需要以每个节点为源点,重复调用 n 次 DijKstra 算法。其实是完全没有必要这么麻烦,Floyd 算法可用于求解任意两个节点之间的最短距离。F
我已经为一个 100 x 100 的邻接矩阵编写了代码,它表示以下有向图: 我正在尝试使用 Floyd-Warshall 算法为图中所有蓝色节点对找到最短路径。你如何只找到所选节点的所有对最短路径?这
我收到一个关于 Floyd's cycle-finding algorithm 的面试问题: Floyd 的循环查找算法何时会失败? 我的意思是,是否有规则可以找到快指针和慢指针之间的步长? 最佳答案
我正在尝试使用 java.awt.image.BufferedImage 在 Java 中实现 Floyd Steinberg 算法。 我使用了描述的算法 here使用自定义调色板,我希望获得与维基百
我正在尝试实现 Floyd-Warshall 算法(所有对最短路径)。在下面的代码中,当我输入一些数字时,它会给出最后一个数字作为输入。我知道代码不完整。 现在我应该怎么做才能为每个 i 和 j 打印
我试图在 .NET 的 C++ 上找到这个算法,但找不到,我找到了这个: // Best solution function boolean hasLoop(Node startNode){ No
我想在这个图问题中重建从源到目标顶点的路径。 如何存储路径,以及在找到从 s 到 d 的最小成本后如何检索它? 请帮我找到一个简单的答案? 例如在这一点上, adjmat[i][j] = Math.m
是否可以使用邻接表对 Floyd Warshall 进行编码?我必须处理文本文件中的一百万个顶点,因此邻接矩阵不是解决方案。任何实现已经可用?请帮忙。 最佳答案 您不能将 Floyd Warshall
我实现了 Floyd-Warshall 算法。根据他们的矩阵,我可以得到正确的结果,关于两个地方之间的最短路径和他们的距离。我的问题是如何打印从 i 到 j 的最短距离。我做了一些研究,发现了一个类似
这可能是一个糟糕的问题,因为我的代表很低,但我已经研究了几个小时的其他解决方案,我的代码似乎与我遇到的工作解决方案几乎相同。请不要忽略基于低代表的问题。 输出矩阵 d[][] 包含给定顶点对之间最短路
Mr. Rowan plans to make a walking tour of Paris. However, since he is a little lazy, he wants to tak
使用 Floyd 时如何使用我电脑上的数据文件? 运行后: floyd init floyd run --gpu --env tensorflow-1.3 "python model.py" 我得到的
假设我有 9 个顶点。所以我有 9x9 解决方案矩阵和 matrix[6,0] = infinity, matrix[6,9]=1, matrix[9,0]=1 现在算法的工作原理如下: for k
This维基百科页面解释了 Floyd Warshall 算法,用于查找图中节点之间的最短路径。维基百科页面使用图像左侧的图表 作为起始图(在 k = 0 时的第一次迭代之前),然后显示剩余的迭代(k
考虑以下链表: 1->2->3->4->5->6->7->8->9->4->...->9->4..... 上面的列表有一个循环如下: [4->5->6->7->8->9->4] 在白板上绘制链表,我尝
我知道当图中有负权重环时,没有找到最小距离的方法,也就没有最小距离的意义了。我的问题是,如果我们向 Floyd Warshall 算法提供具有负权重循环的图,会发生什么情况?它会在 O(n3) 内无限
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
问题陈述:https://www.hackerrank.com/challenges/floyd-city-of-blinding-lights 代码: import scala.io.StdIn._
我能够理解 Floyd 循环查找算法工作原理的基本原理。我唯一无法理解的是 while 循环条件,如下所示: while(slow && fast && fast->next){ sl
我是一名优秀的程序员,十分优秀!