2016-05-22 8 views
1

隣接リストにグラフを実装しました。ユーザーがインデックスを提供するときに、頂点の到達可能性を別のものから見積もる方法を教えてください。グラフの到達可能性 - C

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

直接隣人をチェックするのは簡単ですが、私は、全体としてのアルゴリズムを実装することに苦労しています。

+1

1つのノードを使用すると、グラフ上で検索を実行する必要が別から到達可能であるかどうかを計算します。おそらく、コースの初めに学んだことを、例えば幅優先探索や深さ優先探索のいずれかにするなど、適応することが期待されます。 –

+0

隣接行列を二乗すると何が起こるか知っていますか? –

答えて

3

コードから: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); 
} 
+0

ここにリンクbro .http://www.geeksforgeeks.org/transitive-closure-of-a-graph/ – DharamBudh

関連する問題