簡體   English   中英

使用 Networkx 計算圖的二階度

[英]Calculating second order degree of graph using Networkx

給定圖 G 的頂點 v,表示為 d 2(v) 的 v 的二階度被定義為距 v 距離為 2 的頂點數。

我的問題如下 - 如何(以及是否)使用一些內置的 function in.networkx 來計算它,或者您需要使用其他方法? 檢查文檔后沒有發現任何接近的東西。

這里有一些 C++ 代碼使用深度優先搜索來解決這個問題。 我假設你可以將它移植到 python,盡管你會失去顯着的性能

       int cPathFinder::countCloseNodes(
            int v,              // start node
            int maxdepth)       // maximimum distance from start
        {
            // init record of visited nodes
            std::vector<int> visited(nodeCount());

            // init record of distance from start node
            std::vector<int> dist(nodeCount());

            // construct stack for nodes ready to be visited
            std::stack<int> stack;

            // add start node to stack
            stack.push(v);

            // while nodes waiting to be visited
            while (!stack.empty())
            {
                // pull next node from stack
                int v = stack.top();
                stack.pop();

                // mark node visited
                visited[v] = 1;

                // check if we need to go deeper
                if (dist[v] < maxdepth)
                {
                    // iterate over adjacent nodes
                    for (int w : adjacent(v))
                    {
                        // check adjacent not visited
                        if (!visited[w])
                        {
                            // store distance from start node
                            dist[w] = dist[v] + 1;

                            // add to stack of nodes to be visited
                            stack.push(w);
                        }
                    }
                }
            }

            // count nodes within maxdepth of start nodes
            int countClose = 0;
            for (int d : dist)
            {
                if (d > 0 && d <= maxdepth)
                    countClose++;
            }
            return countClose;
        }

對於這張圖

finder.addLink("0", "2");
finder.addLink("0", "1");
finder.addLink("1", "12");
finder.addLink("12", "13");
finder.addLink("13", "14");

output 是

count by 2 is 3

整個應用程序的代碼位於https://github.com/JamesBremner/PathFinder3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM