\documentclass[11pt]{article}

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


\begin{document}

\title{{\bf Graph basics}\\
\normalsize{(CLRS B.4-B.5, 22.1)}}


\date{}

\maketitle




\begin{itemize}
\item A graph $G = (V,E)$ consists of a set of {\em vertices} $V$
  and a set of {\em edges} $E$.
\item Vertices are objects, and edges model relationships.  E.g:
  graphs can model a network of people, where edges mean friendships
  (the friendship graph).

\item There are two types of graphs: directed and undirected. 

\item {\em Directed graph}: $E$ is a set of ordered pairs of vertices
  $(u,v)$ where $u,v \in V$ \\
  \centerline{\includegraphics[height=3.5cm]{directed_example.pdf}}
        

\item {\em Undirected graph}: $E$ is a set of unordered pairs of
  vertices $\{u,v\}$ where $u,v \in V$ \\ \\
  \centerline{\includegraphics[height=3cm]{undirected_example.pdf}}

\item Note: Some definitions do not allow self loops (graphs are
  simpler to work with). 

\item Often the edges $(u,v)$ in a graph have weights $w(u,v)$, e.g.
        \begin{itemize}
        \item Road networks (distances)
        \item Cable networks (capacity)
        \end{itemize}

\end{itemize} 

\section*{Example}
\begin{itemize} 
\item Graphs appear all over the place in all kinds of applications.

Example: the friendship graph:  vertices are people, and edges are
friendships. Some relevant questions: 
\begin{itemize} 
\item is it directed or undirected?  (if I am your friend, does it
  mean you are my friend??)
\item Are two people friends? How close? 
\item  Am I linked by some chain of friends to a star? 
\item  How close am I to a star? 
\item Is there a path between any two people in the graph? 
\item Who has the most friends? 
\item What is the largest clique? 
\end{itemize} 

\end{itemize} 

\section*{Terminology}

\begin{itemize} 
\item Edge $(u,v)$ is said to be {\em incident} to $u$ and $v$
\item Undirected graph: {\em Degree} of vertex is the number of edges
incident to it.
\item Directed graph:  {\em In (out) degree} of a vertex is the number of
edges entering (leaving) it.
\item Paths: A {\em path} from $u_1$ to $u_2$ is a sequence of
  vertices $<u_1$=$v_0,v_1,v_2, \cdots , v_k$=$u_2>$ such that
  $(v_i,v_{i+1}) \in E$ (or $\{v_i,v_{i+1}\} \in E$). If $v_0 = v_k$
  then it is a {\em cycle\/}. The length of a path is the number of
  edges on it.

\item Reachability: if there is a path from $u_1$ to $u_2$ we write
  $u_1 \rightsquigarrow u_2$ and we say that $u_2$ is {\em reachable}
  from $u_1$ (directed and undirected graphs).
  
\item An undirected graph is {\em connected\/} if every pair of vertices are
connected by a path
        \begin{itemize}
        \item The {\em connected components\/} are the equivalence classes of
        the vertices under the ``reachability'' relation. (All connected
        pair of vertices are in the same connected component).
        \end{itemize}
\item A directed graph is {\em strongly connected\/} if every pair of
vertices are reachable from each other
        \begin{itemize}
        \item The {\em strongly connected components\/} are the equivalence
classes of the vertices under the ``mutual reachability'' relation. 
        \end{itemize}

      \item A graph is called a \emph{tree} if there is a single path
        between any two vertices in $G$. It can be shown that a tree
        is connected, has no cycles, and the number of edges is
        precisely $|E| = |V|-1$ (actually it can be shown that any two
        of these properties implies teh third one).
\end{itemize}


\subsection*{Graph Size}
We will express algorithm running time (and memory) in terms of $|V|$
and $|E|$, often dropping the $|$'s.  Sometimes $|V| = n$ and $|E| =
m$.

\begin{itemize}
\item the number of edges in a graph can be as liittle as $0$  (not
  a very interesting graph): $|E| \ge 0$
\item the largest number of edges in a graph is $|E| = O(|V|^2)$. The
  exact count depends on whether the graph is directed or not, and if
  self loops are allowed.
\item if $|E| \sim |V|^2$ the graph is said to be dense
\item if $|E| \sim |V|$ the graph is said to be sparse
\end{itemize} 

\section*{Graph representation}
A graph can be repreented as adjacency list or matrix. 
\begin{itemize}
\item {\em Adjacency-list} representation: Array of $\vert V \vert$
  list of edges incident to each   vertex. \\
        
        Examples: \\
        \centerline{\includegraphics[height=4cm]{directed_adjacency.pdf}} \\ \\
        \centerline{\hspace*{-6mm}\includegraphics[height=3.7cm]{undirected_adjacency.pdf}} \\
        \begin{itemize} 
        \item Note: For undirected graphs, every edge is stored twice.
        \item If graph is weighted, a weight is stored with each edge.
        \item How much space is required? 
        \end{itemize}


\item {\em Adjacency-matrix} representation:   $\vert V \vert \times \vert V \vert$ matrix $A$ where 
        \[ a_{ij} = \left\{ \begin{array}{ll}
                        1        & \mbox{ if $(i,j) \in E$} \\
                        0        & \mbox{ otherwise } \\
                        \end{array}
        \right. \]
              Examples: \\
        \centerline{\includegraphics[height=4cm]{directed_matrix.pdf}} \\
        \centerline{\includegraphics[height=3.7cm]{undirected_matrix.pdf}} \\
        
        \begin{itemize}
  \item Note: For undirected graphs, the adjacency matrix is
    symmetric along the main diagonal ($A^T = A$).
        \item If graph is weighted, weights are stored instead of one's.
        \item How much space is required? 
        \end{itemize}


\item Comparison of matrix and list representation: \\
        \begin{itemize}
        \item[]
        \begin{tabular}{l|l}
        Adjacency list & Adjacency matrix \\ \hline \\
        $O(\vert V \vert + \vert E \vert)$ space & $O(\vert V \vert ^2)$
space \\
        Good if graph {\em sparse} $(\vert E \vert << \vert V
        \vert ^2)$ & Good if graph {\em dense} $(\vert E \vert \approx
\vert V \vert ^2)$ \\
        No quick access to $(u,v)$ & $O(1)$ access to $(u,v)$ \\
        \end{tabular}
        \end{itemize}
\item We will use adjacency list representation unless stated otherwise
($O(|V|+|E|)$ space).
\item Interesting: large graphs are sparse.
\end{itemize}



\end{document}
