We are given a directed graph represented by an $ n \times n $ adjacency matrix $ A $, where $ A[i][j] = 1 $ if there is an edge from vertex $ i $ to vertex $ j $, and $ 0 $ otherwise. A sink is a vertex $ i $ such that:
- $ A[i][j] = 0 $ for all $ j \ne i $ (no outgoing edges), and
- $ A[j][i] = 1 $ for all $ j \ne i $ (all other vertices have an edge to $ i $).
The algorithm first identifies a candidate sink using a loop with two expressions $ E_1 $ and $ E_2 $, then verifies whether the candidate satisfies the sink conditions.
Determining $ E_1 $ and $ E_2 $
The outer loop maintains a candidate vertex $ i $. The inner loop scans vertices $ j > i $ to check whether $ i $ has any outgoing edge. If $ A[i][j] = 1 $ for some $ j $, then $ i $ cannot be a sink, and we must move to a new candidate.
The inner loop is:
j = i + 1;
while ((j < n) && E1) j++;
This loop should continue as long as vertex $ i $ has no edge to $ j $. Therefore, $ E_1 $ must be true when $ A[i][j] = 0 $, i.e.,
$ E_1 : \texttt{!A[i][j]} $
If the loop terminates with $ j < n $, it means $ A[i][j] = 1 $, so $ i $ has an outgoing edge and is disqualified. In this case, we set the new candidate to $ j $, because any vertex between the old $ i $ and $ j $ cannot be a sink. Thus,
$ E_2 : \texttt{i = j} $
Example with Adjacency Matrix
Consider $ n = 4 $ and the following adjacency matrix:
$$
\begin{array}{c|cccc}
& 0 & 1 & 2 & 3 \\
\hline
0 & 0 & 0 & 1 & 0 \\
1 & 0 & 0 & 1 & 0 \\
2 & 0 & 0 & 0 & 0 \\
3 & 1 & 1 & 1 & 0 \\
\end{array}
$$
Vertex $ 2 $ has no outgoing edges and receives edges from all others, so it is a sink.
Algorithm steps:
- Start with $ i = 0 $
- $ j = 1 $: $ A[0][1] = 0 $ → $ E_1 $ true → $ j = 2 $
- $ A[0][2] = 1 $ → $ E_1 $ false → exit while
- Since $ j = 2 < 4 $, execute $ E_2 $: set $ i = 2 $
- Next iteration: $ j = 3 $, $ A[2][3] = 0 $ → $ j $ increments to $ 4 $
- Loop ends; candidate is $ i = 2 $
- Verification confirms vertex $ 2 $ is a sink
This confirms the correctness of $ E_1 = \texttt{!A[i][j]} $ and $ E_2 = \texttt{i = j} $.
$$
\boxed{\text{C. } E_1 : \texttt{!A[i][j]} \text{ and } E_2 : \texttt{i = j}}
$$