c - 图中的可达性 - C

标签 c graph graph-algorithm

我已经在邻接表 中实现了我的图表。当用户提供索引时,如何估计一个顶点与另一个顶点的可达性?

int isReachable(int nodes, int graph[nodes][nodes], int src, int dest) 

检查直接邻居很容易,但我很难实现整个算法。

最佳答案

代码来自:http://www.geeksforgeeks.org/transitive-closure-of-a-graph/

int reach[V][V], i, j, k;

    /* Initialize the solution matrix same as input graph matrix. Or
       we can say the initial values of shortest distances are based
       on shortest paths considering no intermediate vertex. */
    for (i = 0; i < V; i++)
        for (j = 0; j < V; j++)
            reach[i][j] = graph[i][j];

    /* Add all vertices one by one to the set of intermediate vertices.
      ---> Before start of a iteration, we have reachability values for
           all pairs of vertices such that the reachability values 
           consider only the vertices in set {0, 1, 2, .. k-1} as 
           intermediate vertices.
      ----> After the end of a iteration, vertex no. k is added to the 
            set of intermediate vertices and the set becomes {0, 1, .. k} */
    for (k = 0; k < V; k++)
    {
        // Pick all vertices as source one by one
        for (i = 0; i < V; i++)
        {
            // Pick all vertices as destination for the
            // above picked source
            for (j = 0; j < V; j++)
            {
                // If vertex k is on a path from i to j,
                // then make sure that the value of reach[i][j] is 1
                reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);
            }
        }
    }

    // Print the shortest distance matrix
    printSolution(reach);
}

关于c - 图中的可达性 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37370674/

相关文章:

r - R绘图图中的第二个Y轴

algorithm - 图与 BFS 和 DFS 树的等价性

c++ - 如何使用邻接表表示具有虚拟顶点的图?

c - 将 C 转换为 IA-32 汇编 - 比较 w/2 条件

C 一次读取4个字节

algorithm - 拓扑排序,但具有某种分组

mysql - 数据库查询是否比在一台服务器上查找 LinkedIn 类型二级连接的算法更快?

graph - 物流应用的图算法和理论?

c++ - 在 C 中使用指针时出现段错误

c - parent 去世后,不会将标准输入控制权交给 child