# GATE | Gate IT 2005 | Question 89

• Last Updated : 28 Jun, 2021
Q84 Part_B
A sink in a directed graph is a vertex i such that there is an edge from every vertex j ≠ i to i and there is no edge from i to any other vertex. A directed graph G with n vertices is represented by its adjacency matrix A, where A[i] [j] = 1 if there is an edge directed from vertex i to j and 0 otherwise. The following algorithm determines whether there is a sink in the graph G.
```i = 0
do {
j = i + 1;
while ((j < n) && E1) j++;
if (j < n) E2;
} while (j < n);

flag = 1;
for (j = 0; j < n; j++)
if ((j! = i) && E3)
flag = 0;

if (flag)
printf("Sink exists");
else
printf("Sink does not exist");
```

Choose the correct expressions for E3

(A) (A[i][j] && !A[j][i])
(B) (!A[i][j] && A[j][i])
(C) (!A[i][j] | |  A[j][i])
(D) (A[i][j] | | !A[j][i])

Explanation: Below explanation is for Previous Part of this question:
For vertex i to be a sink, there should be no edge from i to any other vertex. According the input given in question,

```A[i][j] = 1 means there is an edge from vertex i to j.
A[i][j] = 0 means there is no edge from i to j
```

For a node to i to be sink,

```A[i][j] should be 0 for all j
A[j][i] should be 1 for all j.```

The above pseudo code checks every vertex i for sink, starting from i = 0. It basically checks every vertex j after i for being a sink. The trick in pseudo code is, it doesn’t check for j smaller than i. The i picked by this loop may not be sink. It basically makes sure that we don’t ignore a potential sink. The check whether i is actually a sink or not is done later after do while loop.

Vertex i is a potential sink while A[i][j] is zero
Thus, E1 : !A[i][j]

If the above condition is false, then i is not a sink. All j < i can also not be a sink because there is no edge from i to j.
Now, the next potential sink can be j.
So, E2 : i = j

Explanation for this question
The following pseudo code basically checks if the potential sink picked by the code above is actually a sink or not.

```flag = 1;
for (j = 0; j < n; j++)
if ((j! = i) && E3)
flag = 0;
```

flag equals to 0 means i is not a sink. The code sets the flag 0 as soon as it finds out that i is not a sink.

```A node i is not a sink if either of the following
two conditions become true for any j not equal to i.
A[i][j] is 1 for any j
OR
A[j][i] is 0 for any j```

E3 : (A[i][j] | | !A[j][i])

Therefore option D is correct

Quiz of this Question

My Personal Notes arrow_drop_up