Question 4
Consider the following algorithm:
FIND-SOURCE(G)
• Pick an arbitrary vertex $v$ in $G$
• For $i = 1$ to $n$
— If $v$ is a source, return $v$
— Find an incoming edge $(u, v)$ into $v$
— Set $v \leftarrow u$
• Output: \"No source found\".
# otherwise, $v$ contains an incoming edge
a) Give an example of a graph on which the algorithm fails to find a source.
b) Prove that if $G$ is a DAG then the algorithms always find a source. In particular, conclude that
every DAG has a source.
Hint: Do a proof by contradiction and apply the Pigeonhole Principle.
c) A naïve implementation of the FIND-SOURCE algorithm will be costly since finding an incoming
edge could take $O(m)$ steps. This, in turn, will make the total runtime $O(nm)$. Suggest
a modification and/or a different implementation of the algorithm that will result the total
runtime of $O(n + m)$. Explain your solution!