简体   繁体   English

无权无向图中的平均最短路径

[英]average shortest path in unweighted undirected graph

can somebody help me? 有人可以帮我吗? I'm a bit lost. 我有点迷路了。 Well, i'm not an expert in Java for sure. 好吧,我肯定不是Java方面的专家。

I need to write java code algorithm to calculate an average shortest path in unweighted undirected graph (network). 我需要编写Java代码算法来计算未加权无向图(网络)中的平均最短路径。 This graph is a grid of 100 nodes (10 x 10) and would like to search all shortest paths between all pairs (nodes) in network and then divide by number of shortest paths to get an average shotrest path. 此图是一个由100个节点(10 x 10)组成的网格,并且想要搜索网络中所有对(节点)之间的所有最短路径,然后除以最短路径数以获得平均Shotrest路径。 Is this posible by modifying Dijstra's algorithm? 通过修改Dijstra的算法可以做到吗? Can someone show me how, please? 有人可以告诉我如何吗?

dijkstra's alghoritm 迪克斯特拉的算法

public static void dijkstra(int s, int[][] A, int N, int[] d) {
    int mini;  int[] visit = new int[N];
    for (int i=0;i<N;i++) {
        d[i] = N*N;    
        visit[i] = 0;  
    }
    d[s] = 0;           
    for (int k=0;k<N;k++) {
        mini = -1;
        for (int i=0;i<N;i++)
            if ((visit[i]==0) && ((mini == -1) || (d[i] < d[mini])))
                mini = i;
        visit[mini] = 1;
        for (int i=0;i<N;i++)
            if (A[mini][i]==1)
                if (d[mini] + A[mini][i] < d[i]) 
                    d[i] = d[mini] + A[mini][i];
    }
}

Dijkstra's algorithm will give you the shortest path from a given node to all other nodes in the connected graph. Dijkstra的算法将为您提供从给定节点到连接图中所有其他节点的最短路径。 One way to get your average is to iterate through each node of the graph, running Dijkstra's algorithm to get the shortest distance from that node to each of the others, and taking the average of paths starting from that node. 一种获取平均值的方法是遍历图的每个节点,运行Dijkstra的算法以获取该节点到其他每个节点的最短距离,并取从该节点开始的平均路径。 Accumulate the "average of paths starting from current node" as you iterate. 迭代时,累积“从当前节点开始的平均路径”。 Divide by number of nodes when you finish iterating. 完成迭代后,请除以节点数。

This is a brute force approach, and will calculate each distance twice, but it should give you the correct average. 这是一种蛮力方法,将两次计算每个距离,但它应该为您提供正确的平均值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM