Graphs: find a sink in less than O(|V|) - or show it can't be done

前端 未结 7 1153
死守一世寂寞
死守一世寂寞 2021-02-01 10:17

I have a graph with n nodes as an adjacency matrix.

Is it possible to detect a sink in less than O(n) time?

If yes, how? If no

7条回答
  •  忘掉有多难
    2021-02-01 10:30

    I've been working on this problem and I believe this does it:

    int graph::containsUniversalSink() {
    /****************************************************************
     Returns: Universal Sink, or -1 if it doesn't exist
     Paramters: Expects an adjacency-matrix to exist called matrix
    ****************************************************************/
    
    //a universal sink is a Vertex with in-degree |V|-1 and out-degree 0
    //a vertex in a graph represented as an adjacency-matrix is a universal sink
    //if and only if its row is all 0s and its column is all 1s except the [i,i] entry - path to itself (hence out-degree |V|-1)
    //for i=0..|V|-1, j=0..|V|-1
    //if m[i][j]==0 then j is not universal sink (unless i==j) - column is not all 1s
    //if m[i][j]==1 then i is not universal sink - row is not all 0s
    int i=0,j=1;
    while (ii && matrix[i][j]==true) {
            //we found a 1, disqualifying vertex i, and we're not in the last row (j>i) so we move to that row to see if it's all 0s
            i=j;
            if (j

    }

提交回复
热议问题