How to find the universal sink of a directed graph with an adjacency-matrix representation

后端 未结 1 396
抹茶落季
抹茶落季 2021-01-03 09:44

I\'m working on an excercise (self-study): \"Show how to determine whether a directed graph G contains a universal link - a vertex with in-degree (V-1) (V is the number of v

相关标签:
1条回答
  • 2021-01-03 10:42

    You can use the following code:

    int DetectSink(matrix G, int V) {
        int i = 0;
        int j = 0;
        while (i < V && j < V)
            if (G[i][j])
                 i = i + 1;
            else j = j + 1;
        if (i < V && IsSink(G, i)) return i;
        return -1;
    }
    

    If k is an universal sink, then the k-th row of the adjacency-matrix ( G ) will be all 0s, and the k-th column will be all 1s (except G[k][k] = 0).

    OBS: We can conclude that there is at most one sink.

    If an univeral sink k exist in G, then eventually, we get to position (i = k, j) or (i, j = k).

                k
      +---+---+---+---+---+
      |   |   | 1 |   |   |
      +---+---+---+---+---+
      |   |   | 1 |   |   |
      +---+---+---+---+---+
    k | 0 | 0 | 0 | 0 | 0 |
      +---+---+---+---+---+
      |   |   | 1 |   |   |
      +---+---+---+---+---+
      |   |   | 1 |   |   |
      +---+---+---+---+---+
    

    If we reach column k (j=k) before row k (i=k), our algorithm excecutes the then block until (i = k, j = k), then it executes the else block until (i = k, j = V). In other case, if k-th row is reached first than k-th column, then the else block excecutes to the end of while loop until (i = k, j = V).

    At the end we must check if i is an universal sink, because we know if a sink exist it is i, but we have not idea what our algorithm is going to do if an universal sink is not in G.

    The running time is O(V), because in every step we increment i or j, so at most 2V such operations occurrs. The IsSink part is O(V).

    There is a nice solution using Divide & Conquer:

    In this solution we keep a set of candidates to universal sink and in every step we make pairs of vertexs and discard one of two vertexs, in order to analize one half of the initial candidates.

    0 讨论(0)
提交回复
热议问题