Esempio n. 1
0
        // 计算各个联通子图的平均最短路径
        public static List <double> getGraphShorestPath(List <List <string> > data)
        {
            List <List <int> > allSubgraph = GraphUtils.getAllSubgraph(data);
            List <double>      shorestPath = new List <double>();

            foreach (List <int> subgraph in allSubgraph)
            {
                int    length      = 0;
                int    numOfEdge   = 0;
                double averagePath = 0;
                foreach (int nodeIndex in subgraph)
                {
                    List <int> nodeShorestPath = GraphUtils.getAllShortestPathOfNode(data, nodeIndex);
                    foreach (int friendNode in subgraph)
                    {
                        if (nodeShorestPath[friendNode - 1] != 0)
                        {
                            length    += nodeShorestPath[friendNode - 1];
                            numOfEdge += 1;
                        }
                    }
                }
                // 判断子图是否是单个节点
                if (numOfEdge == 0)
                {
                    shorestPath.Add(averagePath);
                }
                else
                {
                    averagePath = 1.0 * length / numOfEdge;
                    shorestPath.Add(averagePath);
                }
            }
            return(shorestPath);
        }
Esempio n. 2
0
        // 计算某两个点之间的最短路径
        public static int getNodeToNodeShortestPath(List <List <string> > data, int startIndex, int endIndex)
        {
            List <int> shortestPath = GraphUtils.getAllShortestPathOfNode(data, startIndex);

            return(shortestPath[endIndex - 1]);
        }