\documentclass[10pt]{article}

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


\begin{document}

\title{{\bf Shortest Paths} \\
\normalsize{(CLRS 24.0, 24.3)}}
\date{}
\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\vspace{-1cm}
\begin{itemize}
\item We discussed that BFS finds shortest paths if the length of a
    path is defined to be the number of edges on it
\item In general we have weights on edges and we are interested in
    shortest paths with respect to the sum of the weights of edges on
    a path \\
    
    Example: Finding shortest driving distance between two addresses (lots
    of www-sites with this functionality). Note that weight on an edge
    (road) can be more than just distance (weight can e.g. be a function of
    distance, road condition, congestion probability, etc).

\item Formal definition of shortest path: 

  $G = (V,E)$ weighted graph, directed or undirected. 

Weight of path $P = <v_0,v_1,v_2, \cdots, v_k>$ is $w(P) =
  \sum_{i=1}^k w(v_{i-1},v_i)$. 

Shortest path $\delta(u,v)$ from $u$ to $v$ has weight
  
  \[ \delta(u,v) = \left\{ \begin{array}{ll}
                \min\{w(P): \mbox{$P$ is path from $u$ to $v$}\} & \mbox{If
                  path exists} \\
                \infty                                     & \mbox{Otherwise} \\
              \end{array} \right. \]
            
            Example: Shortest path from $a$ to $e$ (of length 21) \\

            \centerline{\includegraphics[height=3cm]{SSSP_example.pdf}}
            
          
\item Properties of shortest paths:
            
\begin{itemize}
\item Subpaths of shortest paths are shortest paths: If $P =
  <u=v_0,v_1,v_2, \cdots, v_k=v>$ is shortest path from $u$ to $v$
  then for all $i<k$ $P'= <u=v_0,v_1,v_2, \cdots, v_i>$ is shortest
  path from $u$ to $v_i$
  
  
\item No (unique) shortest path exists if graph has cycle with
  negative weight \\
  
  Example: If we change weight of edge $(h,i)$ to $-8$, we have a
  cycle (i,h,g) with negative weight ($-1$). Using this we can make the 
  weight of path between $a$ and $e$ arbitrarily low by going through
  the cycle several times \\
  
  \centerline{\includegraphics[height=3cm]{SSSP_negative.pdf}}
  
  On the other hand, the problem is well defined if we let edge
  $(h,i)$ have weight $-7$ (no negative cycles)

\item {\bf We will {\em only} consider graphs with non-negative
weights.} In general it is possible to find shortest paths if the
graph has negative edge weights (but no negative cycles), but the
algorithms that are known for this more general case are slower. 
\end{itemize}



\item Different variants of shortest path problem:
  \begin{itemize}
  \item {\em Single pair shortest path}: Find shortest path from $u$
    to $v$
  \item {\em Single source shortest path (SSSP)}: Find shortest path
    from source $s$ to all vertices $v \in V$
  \item {\em All pair shortest path (APSP)}: Find shortest path from
    $u$ to $v$ for all $u,v \in V$
  \end{itemize}
\item Note:
  \begin{itemize}
  \item No algorithm is known for computing a single pair shortest path
    better than solving the (``bigger'') SSSP problem
  \item APSP can be solved by running SSSP $\vert V \vert$ times \\
    $\Downarrow$ \\
    We concentrate on the SSSP problem.
  \end{itemize}
\end{itemize}


\section*{SSSP for  non-negative weights---Dijkstra's algorithm}
\begin{itemize}
\item Dijkstra's algorithm for SSSP is a greedy algorithm: 
\item Idea: Grow set (tree) $S$ of vertices we know the shortest path to;
  repeatedly add new vertex $v$ that can be reached from $S$ using
  one edge. $v$ is chosen as the vertex with the minimal path weight
  among paths $<s=v_0,v_1, \cdots v_i,v>$ with $v_j \in S$ for all $j
  \leq i$
\item Implemented using priority queue on vertices in $V\setminus
  S$.
  
\item Algorithm computes {\em shortest path tree} (stored using
  parent[v]) which can be used to find actual shortest paths
  
\item Algorithm works for directed graphs as well

  
  \fbox{
    \parbox{12cm} {
      Dijkstra(s) 
      \begin{itemize}
      \item[] FOR each $v \in V$ DO
        \begin{itemize}
        \item[] $d[v] = \infty$
        \item[] {\sc Insert}($Q,v,\infty$)
        \end{itemize}
      \item[] $S = \emptyset$
      \item[] $d[s] = 0$
      \item[] {\sc Change}($Q,s,0$)
      \item[] WHILE $Q$ not empty DO
        \begin{itemize}
        \item[] $u =$ {\sc Deletemin}($Q$)
        \item[] $S=S \cup \{ u \}$
        \item[] FOR each $e=(u,v) \in E$ with $v \in V \setminus S$ DO
          \begin{itemize}
          \item[] IF $d[v] > d[u] + w(u,v)$ THEN
          \item[] ~~~~$d[v] = d[u] + w(u,v)$
          \item[] ~~~~{\sc Change}($Q,v,d[v]$)
          \item[] ~~~~parent[$v] = u$
           \end{itemize}
         \end{itemize}
       \end{itemize}
  }}
  

\item Example: \\ \\
\centerline{\includegraphics[width=13cm]{dijk_1.pdf}} \\ \\
\centerline{\includegraphics[width=13cm]{dijk_2.pdf}} \\ \\

\item Analysis: 
\begin{itemize}
\item We perform  $\vert V \vert$ {\sc Insert}'s
\item We perform $\vert V \vert$ {\sc Deletemin}'s
\item We perform at most one {\sc Change} for each of the $\vert E
  \vert$ edges \\ 
%
  $\Downarrow$ \\ 
%
  Assuming edge-list representation and the priority queue implemented
  with a heap $O((\vert V \vert + \vert E \vert) \log \vert V
  \vert)=O(\vert E \vert \log \vert V \vert)$ running time.
\end{itemize}   

\item Correctness:
  \begin{itemize}
  \item We prove correctness by induction on size of $S$
  \item We will prove that after each iteration of the while-loop the
    following {\em invariants\/} hold:
    \begin{itemize}
    \item[(I1)] $v\notin S \Rightarrow d[v]$ is length of shortest
      path from $s$ to $v$ among all paths from $s$ to $v$ that
      contain only vertices from $S$.
    \item(I2)] $v\in S \Rightarrow d[v]=\delta(s,v)$
    
    \end{itemize}
    $\Downarrow$ \\
    When algorithm terminates ($S=V$) we have solved SSSP
  \item Proof:
    
    Invariant trivially holds initially ($S = \emptyset$). To prove
    that invariant holds after one iteration of while-loop, given that
    it holds before the iteration, we need to prove that after adding
    $u$ to $S$:
    \begin{itemize}
    \item[(I1)] $d[v]$ correct for all $(u,v)\in E$ where $v
      \notin S$
      \begin{itemize}
      \item Easily seen to be true since $d[v]$ explicitly
        updated by algorithm (all the new paths to $v$ of
        the special type go through $u$)
      \end{itemize}
    \item[(I2)] $d[u]=\delta(s,u)$
      \begin{itemize}
      \item Assume by contradiction that $d[u]>\delta(s,u)$, that is, the found
        path is not the shortest.
      \item Consider shortest path to $u$ and edge
        $(x,y)$ on this path where $x\in S$ and $y\notin S$
        (such an edge must exist since $s\in S$ and
        $u\notin S$)\\
	
        \hfill\includegraphics[width=8cm]{sssp_proof.pdf}
        \vspace*{3mm}
        
      \item  We know that $\delta(s,u) = \delta(s,y) + \delta(y, u) = \delta(s,y) + w$
      \item We know that $d[y] = \delta(s,y)$ by (I1)
      \item Therefore $\delta(s,u) = d[y] + w$ 
      \item We chose $u$ such that
	$d[u]$ was minimized $\Rightarrow d[y]>d[u]$
      \item Therefore $d[u] > \delta(s,u) = d[y] + w > d[u] + w$
	$\Rightarrow w$ must be $<0 \Rightarrow$ contradiction since
	all weights are non-negative
      \end{itemize}
    \end{itemize}
  \end{itemize}
\end{itemize}


%% \newpage
%% \section{All pairs shortest path (APSP)---non-negative weights}

%% \begin{itemize}
%% \item In the APSP problem, we want to compute the shortest path between any
%% two vertices $u,v\in V$
%%         \begin{itemize}
%%         \item Note that the output is of size $O(|V|^2)$ so we cannot hope
%%         to design a better than $O(|V|^2)$ time algorithm
%%         \end{itemize}
%% \item We can solve the problem simply by running Dijkstra's algorithm $|V|$
%% times $\Rightarrow$ \\ $O(|V|\cdot |E|\log |V|)$ algorithm
%%         \begin{itemize}
%%         \item In the worst case (dense graph) this is $O(|V|^3\log |V|)$
%%         \end{itemize}

%% \item We can obtain a much simpler $O(|V|^3)$ algorithm by working on
%% adjacency matrix $A$:

%% \fbox{
%% \parbox{8cm} {
%% FOR $k=1$ to $|V|$ do 
%%         \begin{itemize}
%%         \item[] FOR $i=1$ to $|V|$ DO
%%                 \begin{itemize}
%%                 \item[] FOR $j=1$ to $|V|$ DO
%%                 \item[] ~~~~IF $A[i,j]>A[i,k]+A[k,j]$ THEN
%%                 \item[] ~~~~~~~~$A[i,j]=A[i,k]+A[k,j]$
%%                 \item[] ~~~~FI
%%                 \item[] OD
%%                 \end{itemize}
%%         \item[] OD
%%         \end{itemize}
%% OD
%% }}
%% \vspace*{3mm}

%% \item Correctness:
%%         \begin{itemize}
%%         \item We prove correctness by induction
%%         \item We will prove that after each iteration of the $k$-loop the
%%         following \emph{invariant} holds:

%%         After the $k$'th (out of $|V|$) iterations, $A[i,j]$ contains the
%%         length of shortest path from $v_i$ to $v_j$ that (apart from $v_i$
%%         and $v_j$) only contains vertices of index at most $k$

%%         $\Downarrow$

%%         When algorithm terminates we have solved APSP


%%         \item Proof: 
%%                 \begin{itemize}
%%                 \item Invariant holds initially (we start with adjacency
%%                 matrix $A$).
%%                 \item When ``adding'' vertex with index $k$ we explicitly
%%                 check all new paths between $v_i$ and $v_j$ through $v_k$
%%                 for all $|V|^2$ pairs.
%%                 \end{itemize}
%%         \end{itemize}
%% \item Note:
%%         \begin{itemize}
%%         \item We can easily produce adjacency-matrix from adjacency list in
%%         $O(|V^2|)$ time
%%         \item Algorithm runs in $O(|V|^3)$ time, even if the graph is
%%         sparse. Using algorithm based on Dijkstra's algorithm we will get
%%         much better performance for sparse graphs.
%%         \item Using more efficient heap, algorithm based on
%%         Dijkstra's algorithm can be improved to $O(|V|^2\log |V|+|V|\cdot
%%         |E|)=O(|V|^3)$
%%         \end{itemize}
%% \end{itemize}

\end{document} 


