\documentclass[t,12pt]{beamer}

\mode<presentation>
{
 % \usetheme{Antibes}
  \usetheme{Warsaw}
  \usefonttheme{serif}
}



\newcommand{\degree}{\ensuremath{^\circ}}
\definecolor{gray}{rgb}{0.8,.8,.8}


\title{Graphs} 
\author{Part II: SP and MST}
\date{Laura Toma\\Algorithms (csci2200), Bowdoin College}


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 
%\beamerdefaultoverlayspecification{<+->}


\begin{document}

\begin{frame}
  \titlepage
\end{frame}


%\begin{frame}{Outline}
%  \tableofcontents
  % You might wish to add the option [pausesections]
%\end{frame}


%\section[Motivation]{The problem and motivation}
%\subsection{Undirected graphs}
\begin{frame}
  \frametitle{Topics}
  
  Weighted graphs
  \begin{itemize} 
  \item each edge $(u,v)$ has a weight denoted $w(u,v)$ or $w_{uv}$
  \item stored in the adjacency list or adjacency matrix 
  \end{itemize} 
  
\vfill 
  The weight of a path $p= (v_1, v_2, v_3, ...v_k)$ is the sum of the weights of the edges on the path.
\vfill 
  Problems: 
  \begin{itemize}
  \item shortest paths (SP)
  \item minimum spanning tree (MST)
  \end{itemize}
\end{frame} 




\begin{frame}
  \frametitle{Shortest paths}
  Variants: 
  \begin{itemize} 
  \item P2P SP: given two vertices $u,v$: find SP from $u$ to $v$ 
  \item SSSP: given a vertex $u$, find SP from $u$ to all vertices  in $G$
  \item APSP: find SP between any two vertices $(u,v)$
  \end{itemize} 
\pause  
\vfill
Notes: 
\begin{itemize} 
\item SPs not well-defined when graph has a negative cycle
  \begin{itemize}
  \item might want shortest path that has no cycles $\Leftarrow$  NPC
  \end{itemize} 
  
\item When all edge weights are equal, SP can be computed by BFS.
  \begin{itemize}
  \item computing shortest paths in terms of number of edges on the path is a special case of the SP problem
  \end{itemize} 
  \end{itemize} 
\end{frame} 





\begin{frame}
  \frametitle{Point-to-point SP}

  Problem:   given two vertices $u,v$: find SP from $u$ to $v$ 

\vspace{1cm}
  No algorithm is known for computing SP(u,v) that's better, in the
  worst case, than running SSSP(u).
\end{frame} 



\begin{frame}
  \frametitle{APSP}

  Problem:   For any $u,v$: find SP from $u$ to $v$ 

\vspace{1cm}
Can run SSSP(u) $|V|$ times, once  for each vertex $u$. 
~\\~\\
Better algorithms exist. 
\end{frame} 



\begin{frame}
  \frametitle{SSSP}

  $G$ is a weighted (directed or undirected) graph. \\
  Problem: Given vertex $s$, find SP from $s$ to all $v$ in $G$.  

  \vspace{1cm}
  If $G$ has positive weights: Diskstra's algorithm 

  Otherwise: Bellman-Ford algorithm 
\end{frame}



\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}
SSSP(s)\\
Idea: for each vertex $v$, maintain $d[v]$ as the best known shortest path to $v$ (from $s$)\\
~\\
Initially: $d[s]=0$ and  $d[v] = \infty$ for all $v \neq s$\\
~\\
Idea:  Greedy: Visit first the vertex with smallest $d$.  ~\\
~\\
Implementation: use a priority queue. \\
%With a heap, runs in $O(E \lg V)$
\end{frame}


\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}

\textcolor{blue}{Idea: for each vertex $v$, maintain $d[v]$ as the best known shortest path to $v$ (from $s$)}

\vfill 
\begin{itemize} 
\item  Initialize: $d[s]=0$ and  $d[v] = \infty$ for all $v \neq s$. For every $v \in V$, insert $(v,d[v])$ in PQ. 

\item while PQ not empty 
  \begin{itemize}
  \item $v  = $ deleteMin(PQ) 
  \item for each outgoing edge $(v,u)$: relax $(v,u)$
  \end{itemize} 
\end{itemize} 

\vfill
relax$(v,u)$ tests whether we can improve the SP to $u$ by going through $v$
\begin{itemize} 
\item if $d[u] > d[v]+w_{vu}$ then 
  \begin{itemize}
  \item $d[u] = d[v]+w_{vu}$ 
  \item decreaseKey of $u$ in PQ to $d[u]$
  \end{itemize} 
\end{itemize} 
\pause
\textcolor{red}{$O(|V|+|E|) + |V| \cdot$ PQ-insert + $|V| \cdot $ PQ-delete + $|E| \cdot $ PQ-decreaseKey}
%With a heap, runs in $O(E \lg V)$
\end{frame}



\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}

\textcolor{blue}{Idea: for each vertex $v$, maintain $d[v]$ as the best known shortest path to $v$ (from $s$)}

\vfill 
\begin{itemize} 
\item  Initialize: $d[s]=0$ and  $d[v] = \infty$ for all $v \neq s$. For every $v \in V$, insert $(v,d[v])$ in PQ. 

\item while PQ not empty 
  \begin{itemize}
  \item $v  = $ deleteMin(PQ) 
  \item for each outgoing edge $(v,u)$: relax $(v,u)$
  \end{itemize} 
\end{itemize} 

\vfill
relax$(v,u)$
\begin{itemize} 
\item if $d[u] > d[v]+w_{vu}$ then 
  \begin{itemize}
  \item $d[u] = d[v]+w_{vu}$ 
  \item decreaseKey of $u$ in PQ to $d[u]$
  \end{itemize} 
\end{itemize} 
\textcolor{red}{Analysis:   With a heap, runs in $O(E \lg V)$}
\end{frame}




\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}
Let $S$ denote the set of vertices that have been deleted from PQ. \\
Correctness: At every iteration of the \texttt{while} loop, the following invariants hold: 
\\
\begin{enumerate} 
\item \textcolor{blue}{(I1) for any $v \in V-S$, $d[v]$ is the length of the shortest
  path from $s$ to $v$ among all paths that go only through vertices
  of $S$.}
\item \textcolor{blue} {(I2) for any $v \in S$, $d[v]$ is the length of the shortest path from $s$ to $v$. }
\end{enumerate}
\pause
Prove by induction on the size of $S$. 
\end{frame} 



\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}
At every iteration of the \texttt{while} loop, the following holds: \\
\textcolor{blue}{(I1) for any $v \in V-S$, $d[v]$ is the length of the shortest
  path from $s$ to $v$ among all paths that go only through vertices
  of $S$.}

\vfill 
Basecase: (I1) is trivially true before the first iteration of the
while loop, when $S$ is empty.
\vfill
Assume (I1) is true \emph{before} an iteration of the while loop.
We'll prove that it's true \emph{after} this iteration.
\vfill 
After adding $v$ to $S$, the only paths that can change are to those
vertices that are adjacent to $v$. The algorithm checks them and
releases them.
\end{frame} 





\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}
At every iteration of the \texttt{while} loop, the following holds: \\
\textcolor{blue} {(I2) for any $v \in S$, $d[v]$ is the length of the shortest path from $s$ to $v$. }
\vfill 
Basecase: (I2) is trivially true before the first iteration of the
while loop, when $S$ is empty.
\vfill
Assume (I2) is true \emph{before} an iteration of the while loop.
We'll prove that it's true \emph{after} this iteration.
\vfill 

As we are adding $v$ to $S$, assume by contradiction that the length
of the shortest path to $v$ is $|\delta(s,v)| < d[v]$.  Let $(x,y)$ be the
first edge on $\delta(s,v)$ leaving $S$ ($x$ last vertex in $S$).

\begin{itemize} 
\item $d[v] > \delta(s,v) = \delta(s,y) + \delta(y,v)$
\item $d[y] = \delta(s,y)$  by (I1)
\item $d[v] < d[y]$ because $v$ comes out of PQ before $y$
\end{itemize}
$\Rightarrow \delta(y,v) < 0$ impossible
\end{frame} 



\begin{frame}
  \frametitle{SSSP: Dijkstra'a algorithm}

What happens if we run Dijkstra's algorithm on a graph with negative weights? 

\vfill
\pause
Find an example of a graph where Dijkstra does not compute the SP correctly.

\vfill

\end{frame} 


\begin{frame}
  \frametitle{SSSP with negative weights}
Note: If $G$ is undirected and has negative weights, that immediately means a negative cycle. 
  \end{frame}


\begin{frame}
  \frametitle{SSSP with negative weights}
  $G$ {\bf directed} graph. \\
  \pause If $G$ has no negative cycles, then there exists a SP from
  $s$ to $v$ that is \emph{simple} and hence has $|V|-1$ edges.
  \vfill \pause
  Let $\delta(u,v)$ denote the shortest path from $u$ to $v$. \\
  Start with $d[v] = \infty$ and progresively refine it, until $d[v] = |\delta(s,v)|$\\
  Similar to Dijkstra:  Dijkstra relaxes edges in greedy order of increasing $d[]$; that does
  not work for negative edges
 \end{frame} 


\begin{frame}
  \frametitle{SSSP with negative weights}
  $G$ {\bf directed} graph. \\
\alert{
  Bellman-Ford algorithm ($s$): \\
  \begin{itemize} 
  \item Initialize: $d[s]=0$ and $d[v] = \infty$ for all $v \neq
    s$. 
  \item for $i=1$ to $|V|-1$ do: 
    \begin{itemize}
    \item for every edge $(v,u)$ in $G$: relax$(v,u)$
    \end{itemize} 
  \end{itemize} 
}
  \vfill
  relax$(v,u)$
  \begin{itemize} 
  \item if $d[u] > d[v]+w_{vu}$ then 
    \begin{itemize}
    \item $d[u] = d[v]+w_{vu}$ 
    \end{itemize} 
  \end{itemize} 
\end{frame} 



\begin{frame}
  \frametitle{Bellman-Ford}
WHY does this work?\\
Intuition:  look at the number of edges along a shortest path (SP) from $s$
\pause 
\begin{itemize} 
\item initially, only $d[s]$ is correct. Put differently, all SP that consist of  $0$ edges are correctly computed.
\pause 
\item after round 1: all SP from $s$ that consist precisely of $1$ edge are correctly computed. 
\pause 
\item after round 2: all SP from $s$ that consist precisely of $2$ edges are correctly computed. 
\pause
\item ...
\item after round $i$: all SP from $s$ that consist precisely of $i$ edges are correctly computed. 
\end{itemize} 
\end{frame} 



\begin{frame}
  \frametitle{Bellman-Ford}
WHY does this work?\\
Intuition:  look at the number of edges along a shortest path (SP) from $s$

\textcolor{gray}{Let $\delta(u,v)$ denote the shortest path from $u$ to $v$.}\\
\textcolor{blue}{Let $OPT(v,i)$ denote the length of the shortest path from $s$ to $v$ among all paths containing $\le i$ edges.} \\
\pause We have:
\begin{itemize} 
\pause
\item $OPT(s,0) = 0$
\pause
\item $OPT(v,0) = \infty$ for any $v \neq s$
\pause
\item $OPT(v, |V|-1) = |\delta(s,v)|$
\pause
\item $OPT(v,i) = min \{OPT(v, i-1), min_{u | (u,v)} \{ OPT(u,i-1) + w_{uv}\} \}$
\end{itemize} 
\vfill
\textcolor{blue}{\pause Claim: After round $i$ in Bellman-Ford we have $d[v] = OPT(v,i)$}
\end{frame} 




\begin{frame}
  \frametitle{Bellman-Ford}

Running time:   $O(V\cdot E)$\\

After $V-1$ rounds, $d[v] = OPT(v, |V|-1) = |\delta(s,v)|$
\vfill
\pause
What happens if we do more rounds? (beyond $|V|-1$)
\pause
\begin{itemize} 
\item the values $d[v]$ will not decrease any further.... unless.....there's a negative cycle
\end{itemize} 
\pause
\vfill 
Negative cycles:   What happens if $G$ negative cycles? 
\pause
\begin{itemize} 
\item $d[v]$ are not SP (there are no SP)
\item some values $d[v]$ will keep decreasing 
\end{itemize}
\pause
\vfill
$\rightarrow$ Bellman-Ford can be used to test for the existence of negative cycles in the graph:
\end{frame} 




\begin{frame}
  \frametitle{Bellman-Ford}
  $G$ {\bf directed} graph. \\

  Bellman-Ford algorithm ($s$): \\
  \begin{itemize} 
  \item Initialize: $d[s]=0$ and $d[v] = \infty$ for all $v \neq
    s$. 
  \item for $i=1$ to $|V|-1$ do: 
    \begin{itemize}
    \item for every edge $(v,u)$ in $G$: relax$(v,u)$
    \end{itemize} 
  \item \textcolor{blue}{for each edge $(v,u)$ in $G$: if $d[v]+ w_{vu} < d[u] \Rightarrow$ NEG CYCLE  }
  \end{itemize} 
\vfill
\pause
Note: detects negative cycle \emph{reachable from $s$}. Can be extended to detect if $G$ has any negative cycle. 
\end{frame} 



\begin{frame}
  \frametitle{SSSP}
  Summary of known algorithms: 
\textcolor{blue}  {
  \begin{itemize} 
  \item $G$ unweighted
    \begin{itemize} 
    \item BFS in $O(V+E)$
    \end{itemize} 
  \item $G$ DAG
    \begin{itemize} 
    \item   dynamic programming in $O(V+E)$
    \end{itemize} 
    \item $G$ directed, no negative weights
      \begin{itemize} 
      \item Dijkstra's algorithm in $O(E \lg V)$
      \end{itemize} 
    \item $G$ directed, no negative cycles
      \begin{itemize} 
        \item Bellman-Ford algorithm in $O(V\cdot E)$
        \end{itemize}
    \end{itemize} 
} 
\end{frame}


\end{document}


