0

I found this algorithm for digraphs that checks whether a total sink exists in a graph. https://www.geeksforgeeks.org/determine-whether-universal-sink-exists-directed-graph/

My question is : Is this valid for a non - dag digraph?
Because if a cycle between v1, v2 exists then we may miss to identify this 1 and mistakenly think of v2 as a source sink (or am I missing something here)?
enter image description here

enter image description here

tonythestark
  • 519
  • 4
  • 15

1 Answers1

0

The algorithm will work correctly. You are missing the additional is_sink method.

def is_sink(self, i):
    for j in range(self.vertices):

        # if any element in the row i is 1, it means
        # that there is an edge emanating from the
        # vertex, which means it cannot be a sink
        if self.adjacency_matrix[i][j] == 1:
            return False

        # if any element other than i in the column
        # i is 0, it means that there is no edge from
        # that vertex to the vertex we are testing
        # and hence it cannot be a sink
        if self.adjacency_matrix[j][i] == 0 and j != i:
            return False

    # if none of the checks fails, return true
    return True

After j == vertices, we make this check to ensure that there is indeed no cycle present in the graph which contains the vertex i.

AKSingh
  • 1,535
  • 1
  • 8
  • 17