\documentclass[11pt]{article}

\usepackage{fullpage}
\usepackage{graphicx,epsfig}
\usepackage{amssymb}

\begin{document}

\title{{\bf  Strongly Connected Components (SCC's)}\\
\normalsize{(CLRS 22.5)}}


\date{}

\maketitle

\section*{Basics}
\begin{itemize}

\item 
\fbox{\parbox{15cm}{
    A strongly connected component (SCC) of a directed graph
    $G=(V,E)$ is a maximal set of vertices such that any two vertices in
    the set are mutually reachable.
}}
\item Example: All vertices along a directed cycle are in the same SCC. 

\item Intuitively, we think of a SCC as a cycle.

\item Given a directed graph, some questions that one may be
  interested in are:

  (1) Given a vertex $v$, compute it's strongly connected component
  (SCC).

  (2) Is $G$ strongly connected (SC)? 

  (3) Compute the SCCs of $G$.

\end{itemize} 

  
\paragraph{Practice:}
\begin{itemize} 

\item Draw a graph with  3 vertices and (a) 1SCC; (b) 2 SCCs; (c) 3 SCCs. 

\item What happens to SCC's as you add an edge to the graph? 

\item Show an example when adding an edge does not change the SCC's,
  and show one where it does.


\end{itemize} 


\section{Computing the SCC of a given vertex $v$}


All vertices in the same SCC as $v$ must be able to reach $v$ and $v$
must be able to reach them. This suggests the following approach,
which runs in linear time $O(V+E)$:

\begin{itemize} 

\item  Perform DFS(v) (or BFS(v)) to find all vertices reachable from $v$. 

\item Perform DFS(v) using the \emph{incomign} edges to find the
  vertices that reach $v$ (a different way to say this is that we
  perform DFS in the transpose graph $G^T$).

\item Find the vertices that are reached by both DFS's.  These are the
  vertices that are in the same SCC.  Why? Let $u,u'$ be two vertices
  that are both reachable from $v$ and that reach $v$. Then it follows
  that $u \rightsquigarrow v$ and $v \rightsquigarrow u'$ and thus $u
  \rightsquigarrow u'$. Similarly, $u' \rightsquigarrow v$ and $v
  \rightsquigarrow u$ and thus $u' \rightsquigarrow u$.
\end{itemize}



\section{Finding whether a graph $G$ is strongly connected}


For a graph to be SC, every vertex needs to be able to reach every
other vertex.  Take an arbitrary vertex $v$ and run DFS(v). If this
does not reach all vertices, $G$ is not SC. If DFS(v) reaches all
vertices, we continue with DFS(v) using the \emph{incoming} edges (in
other words, DFS(v) on $G^T$): if this does not reach all vertices,
then clearly $G$ is not SC. Otherwise, we know that $v$ reaches all
vertices, and all vertices reach $v$.  Does that mean that $G$ is
strongly connected?  Yes. We have the following approach which runs in
linear time $O(V+E)$:


\begin{enumerate}
\item Pick an arbitrary vertex $v$ and perform DFS($v$). 
\item Perform DFS($v$) in the transpose graph $G^T$.
\item If both searches reach \emph{all} vertices, $G$ is SC. Otherwise
  it's not.
\end{enumerate} 

Proof: Let $u,u'$ arbitrary vertices in $G$.  We know that $u
\rightsquigarrow v$ (because of (2)) and $v \rightsquigarrow u'$
(because of (1)) and thus $u \rightsquigarrow u'$. Similarly $u'
\rightsquigarrow u$. Thus any $u,u'$ are mutually reachable.



\section{Find the SCCs in $G$}


\begin{itemize} 

\item {\bf Naive: } Ok, so we want to compute the SCC's in $G$. We
  always start a problem by coming up with the most straightfirward
  solution we can possibly think of. For e.g.  we can start with an
  arbitrary vertex $v$ and compute its strongly connected component;
  then we delete $v$ and all vertices in its SCC from the graph, and
  repeat.  This finds the components one at a time; it can be shown
  this runs in the worst case $|V|$ searches, for a total of
  $O(V(V+E)$.

\item We ask the usual question: can we do better?  

\item Yes!  There is a very clever and simple algorithm by
  Kosaraju-Sharir that finds the SCCs in linear time!  It's very
  short, but it's quite hard to understand.

\item It's key ingredient, and a concept that's useful when thinking
  of SCC is the \emph{component graph}: this is a graph with one
  vertex per strongly connected components, and with an edge $C
  \rightarrow C'$ if there exists an edge $(u,v)$ in $G$ with $u$ in
  $C$ and $v$ in $C'$.

\item Exercise: There's one important property that the component
  graph has: What is it?

  Answer:  The compoent graph cannot have cycles. 

  Proof: Consider two SCC's, C and C', and assume there is a cycle
  between them: this means than an arbitrary vertex in $C$ can reach
  an arbitrary vertex in C', and the otehr way around, via this
  cycle. This means that C, C' would be a single strongly component,
  which violates our assumption that they are separate, maximal SCC's.

\item Exercise: If we perform DFS on a graph with several SCC's, is it
  possible that one SCC be broken across two difefrent DFS trees?

  Answer: No. Once DFS reaches a vertex of a SCC, it will reach the
  entire component, eventually. That is to say, a DFS tree will visit
  the whole SCC, or not visit it at all.

\item Exercise: Is it possibel that a DFS tree contains more than one SCC? 

  Answer: Yes.

\item Kosaraju-Sharir idea is to call DFS on the vertices in $G$ in
  such an order so that a DFS tree identifies precisely \emph{one}
  SCC.

\item Exercise: Can you think of a vertex $v$ to call DFS(v) so that
  all verties reached are in \emph{only one} SCC?

  Answer: $v$ can be any of the vertices in the ``last'' SCC of $G$ in
  topological order. 

\item As it turns out, there is no good way to identify the components
  in reverse topological order; but it is possibel to identify them in
  topological order. We'll also get one SCC per DFS tree if we call
  DFS on any $v$ in the ``first'' SCC in topological order----for this
  we would need to do DFS in $G^T$ (i.e. on incoming edges).

\item Remember that tpological order on a DAG is given by reverse
  order of finish time. This gives us the following algorithm:


  \fbox{\parbox{11.5cm}{
      {\bf SCC Algorithm (Kosaraju-Sharir)}
      
      \begin{enumerate} 
        
      \item Perform DFS(G) to compute finish times for all vertices.
      \item Compute $G^T$. 
      \item For each vertex $u$ in $G$ in reverse order of finish time: run
        $DFS(u)$ and label all vertices in its DFS tree as a separate SCC.
\end{enumerate} 
}}


\item Why does this work? By now we have the intuition. Traversing the
  vertices in reverse finish time gives us topological order in the
  component graph. We formalize with the following claim:

\item Notation: For a SCC C, denote $f(C)$ to be the largest finish
  time of a vertex in $C$. Denote $d(C)$ to be the smallest
  start/discover time of a vertex in $C$.

\item Claim: Let C, C' be two SCC's in $G$.  If there is an edge
  $(u,v)$ from a vertex $u \in C$ to a vertex $v \in C'$, then $C$
  must be finished \emph{after} C': $f(C) > f(C')$.

  Proof: 
  \begin{itemize} 
  \item Case 1: Component C is discovered before C' by DFS(G), $d(C) <
    d(C')$: Let $x$ be the first vertex in $C$ discovered by DFS. From
    $x$, DFS will reach all vertices in C, and, through edge $(u,v)$,
    it will reach component C'. It's not possible to have an edge from
    C' back to C (it would cause a cycle), and so the entire C' is
    finished before vertex $u$, and thus before $x$. Thus we have
    $f(x) > f(C')$ and thus $f(C) > f(C')$.


  \item Case 2: Component C' is discovered first: $d(C') < d(C)$: Let
    $y$ be the first vertex discovered in C'. From $y$ DFS will reach
    all vertices in $C'$. There cannot be an edge from a vertex in C'
    to a vertex in C (because it would cause a cycle), hence no vertex
    in C is reachable from $y$.  This means that when DFS finishes,
    all vertices in C are still white.  This means C is discovered and
    finished after $f(C')$.

  \end{itemize} 
\end{itemize} 


\end{document}
