-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtraversal.tex
484 lines (429 loc) · 11.1 KB
/
traversal.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
\documentclass[12pt]{beamer}
\usepackage{listings}
\beamertemplatenavigationsymbolsempty
\AtBeginSection[]
{
\begin{frame}
\frametitle{Table of Contents}
\tableofcontents[currentsection]
\end{frame}
}
\lstset{language=C++, basicstyle=\footnotesize}
\setlength{\tabcolsep}{10pt}
\title{Graph traversal}
\subtitle{DFS, BFS, applications}
\author{beOI Training}
\institute{\includegraphics[height=12em]{../share/beoi-logo}}
\begin{document}
\frame{\titlepage}
\section{DFS and BFS}
\begin{frame}
\frametitle{Depth-first principle}
\begin{itemize}
\item Go in depth, backtrack when stuck
\item Visit everything before switching
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{img/dfs-tree}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{DFS example}
Do not visit a node twice!
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{img/6n-graph}
\end{figure}
\[ \mathbf{1} \to \mathbf{2} \to \mathbf{3} \to \mathbf{4} \to \mathbf{5} \to 4 \to \mathbf{6} \to 4 \to 3 \to 2 \to 1 \]
\end{frame}
\begin{frame}[fragile]
\frametitle{DFS recursive implementation}
\begin{lstlisting}
vector<int> neigh[MAXN];
bitset<MAXN> visited;
void dfs(int u)
{
if (visited[u])
return;
visited[u] = true;
for (int v : neigh[u])
dfs(v);
}
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{DFS visit order}
\begin{itemize}
\item Always travels locally: parent $\to$ child $\to$ parent
\item The last discovered are the first visited $\Rightarrow$ we can also use a \emph{Last In First Out} structure (stack)
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{img/stack}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{DFS stack implementation}
\begin{lstlisting}
stack<int> st;
st.push(start);
while (!st.empty())
{
int u = st.top();
st.pop();
for (int v : neigh[u])
{
if (!visited[v])
{
visited[v] = true;
st.push(v);
}
}
}
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Breadth-first principle}
\begin{itemize}
\item Go layer by layer
\item Visit the closest nodes first
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{img/bfs-tree}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{BFS example}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{img/6n-graph}
\end{figure}
\[ 1 \to 2 \to 5 \to 3 \to 4 \to 6 \]
\end{frame}
\begin{frame}
\frametitle{BFS visit order}
The first discovered are the first visited $\Rightarrow$ we can use a \emph{First In First Out} structure (queue)
\begin{figure}
\centering
\includegraphics[width=.6\linewidth]{img/queue}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{BFS implementation}
\begin{lstlisting}
queue<int> q; // different
q.push(start);
while (!q.empty())
{
int u = q.front(); // different
q.pop();
for (int v : neigh[u])
{
if (!visited[v])
{
visited[v] = true;
q.push(v);
}
}
}
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{BFS for shortest path}
BFS traverses graph with nondecreasing distance!
\begin{itemize}
\item Add two tables \texttt{dist[]} and \texttt{parent[]}
\item When adding to queue:
\begin{itemize}
\item \lstinline[basicstyle=\normalsize]{dist[v] = dist[u] + 1}
\item \lstinline[basicstyle=\normalsize]{parent[v] = u}
\end{itemize}
\item Trace path back using \texttt{parent[]}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Comparison}
Complexity: both $O(V+E)$.
~
How to choose:
\begin{itemize}
\item If distance / shortest path necessary $\Rightarrow$ BFS
\item Otherwise $\Rightarrow$ DFS (shorter)
\end{itemize}
\end{frame}
\section{Connected components}
\begin{frame}
\frametitle{Connected components}
\begin{itemize}
\item On \textbf{undirected graphs}, ``$u$ connected to $v$'' is an equivalence relation
\item So it forms a partition of the vertices
\end{itemize}
\begin{figure}
\centering
\begin{tabular}{cc}
\includegraphics[width=0.4\linewidth]{img/6n-graph}
& \includegraphics[width=0.4\linewidth]{img/6n-3comp} \\
1 connected component & 3 connected components
\end{tabular}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Finding connected components}
One run of DFS/BFS visits a whole component!
\begin{itemize}
\item Go through nodes and check if visited
\item In \texttt{dfs()}, store nodes in a vector
\end{itemize}
\begin{lstlisting}
for (int i = 0; i < n; i++)
{
if (!visited[i]) // new CC
{
vector<int> cc;
dfs(i, &cc); // adds nodes to cc
// process cc
}
}
\end{lstlisting}
\end{frame}
\section{Topological sort}
\begin{frame}
\frametitle{Directed acyclic graphs}
Directed graphs without cycles
\begin{figure}
\centering
\begin{tabular}{cc}
\includegraphics[width=0.4\linewidth]{img/6n-dag}
& \includegraphics[width=0.4\linewidth]{img/6n-not-dag} \\
DAG & not DAG
\end{tabular}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Topological sort}
\begin{itemize}
\item There are no cycles, so we can order the nodes so that edge $u \to v \Rightarrow u$ before $v$
\item Example: course prerequisites, how to take them all in order
\item Not unique: $\{3,5,6,4,1,2\}, \{5,3,1,6,2,4\},\ldots$
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=.5\linewidth]{img/6n-dag}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Toposort with zero in-degree}
If $u$ does not have edges pointing to it, it can go first!
\begin{itemize}
\item Start at nodes with $\mathsf{deg}_\mathsf{in} = 0$
\item Remove edges as we find them
\item Continue until no node is left
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Zero in-degree implementation}
\begin{lstlisting}
int in_degree[MAXN]; // pre-filled
queue<int> zero_in; // pre-filled
vector<int> toposort;
while (!zero_in.empty())
{
int u = zero_in.front();
zero_in.pop();
toposort.push_back(u);
for (int v : neigh[u])
{
in_degree[v]--;
if (in_degree[v] == 0)
zero_in.push(v);
}
}
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Toposort with DFS}
Use DFS to build the toposort backwards
\begin{itemize}
\item Call \texttt{dfs()} on every node
\item Add a node \emph{after} recursing
\end{itemize}
Thus every node is
\begin{itemize}
\item after its children in the \emph{backward} toposort
\item before its children in the \emph{forward} toposort
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Toposort DFS implementation}
\begin{lstlisting}
void dfs(int u, vector<int> *toposort)
{
if (visited[u]) return;
visited[u] = true;
// recurse as usual...
toposort->push_back(u);
}
vector<int> toposort;
for (int i = 0; i < n; i++)
dfs(i, &toposort); // no need to check visited[i]
for (int i = n-1; i >= 0; i--)
// use toposort[i]
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Comparison}
Complexity: both $O(V+E)$
~
Use the zero in-degree method only if needed:
\begin{itemize}
\item Additional conditions on the order
\item Enumerate all possible toposorts
\end{itemize}
\end{frame}
\section{Bipartite check}
\begin{frame}
\frametitle{Bipartite graphs}
\begin{itemize}
\item Can be split in two without internal edges
\item Equivalent: no odd-length cycle
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=0.5\linewidth]{img/bipartite}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Bipartite examples}
\begin{figure}
\centering
\begin{tabular}{cc}
\includegraphics[width=0.4\linewidth]{img/6n-bipartite}
& \includegraphics[width=0.4\linewidth]{img/6n-graph} \\
bipartite & not bipartite
\end{tabular}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Bipartite check with DFS/BFS}
Put an arbitrary node $u$ on the left then traverse the graph:
\begin{itemize}
\item If $v$ is on the left, put all its neighbors on the right
\item If $v$ is on the right, put all its neighbors on the left
\item Continue until conflict or finished
\end{itemize}
Note: $v$ on left $\Leftrightarrow \mathsf{dist}(u,v)$ even
\end{frame}
\begin{frame}
\frametitle{Bipartite check correctness}
$G$ is not bipartite $\Leftrightarrow$ the assignment fails
\begin{itemize}
\item If $G$ is not bipartite the assignment will clearly fail
\item If there is a conflict between $u$ and $v$ adjacent
\begin{itemize}
\item Let $p$ be their common parent in the DFS
\item $\mathsf{dist}(p,u)$ and $\mathsf{dist}(p,v)$ have the same parity
\item Cycle $p-u-v$ has odd length $\mathsf{dist}(p,u) + 1 + \mathsf{dist}(p,v)$
\item Thus $G$ is not bipartite
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Bipartite check implementation}
\begin{lstlisting}
int color[MAXN];
bool dfs(int u)
{
for (int v : neigh[u])
{
if (color[v] == -1) // unassigned
{
color[v] = !color[u];
if (!dfs(v))
return false;
}
else if (color[u] == color[v]) // conflict
return false;
}
return true;
}
\end{lstlisting}
\end{frame}
\section{Kosaraju SCC}
\begin{frame}
\frametitle{Strongly connected components}
Nodes $u,v$ in same SCC $\Leftrightarrow$ path $u \to v$ \textbf{and} path $v \to u$
\begin{figure}
\centering
\begin{tabular}{c}
\includegraphics[width=0.5\linewidth]{img/6n-scc} \\
3 strongly connected components
\end{tabular}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Contracted graph}
\begin{itemize}
\item If we contract the SCCs into one node, a DAG appears
\item This is because all cycles have been contracted
\item Useful for DP (as we will see later)
\end{itemize}
\begin{figure}
\centering
\begin{tabular}{cc}
\includegraphics[width=0.4\linewidth]{img/6n-scc}
& \includegraphics[width=0.4\linewidth]{img/6n-contracted} \\
original & contracted
\end{tabular}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Kosaraju's algorithm}
\begin{itemize}
\item Run the DFS toposort algorithm on $G$
\item Use that order and flood with the transpose of $G$
\end{itemize}
\begin{figure}
\centering
\begin{tabular}{cc}
\includegraphics[width=0.4\linewidth]{img/6n-kosa1}
& \includegraphics[width=0.4\linewidth]{img/6n-kosa2} \\
raw toposort order & flood with decreasing index
\end{tabular}
\end{figure}
\end{frame}
\begin{frame}[fragile]
\frametitle{Kosaraju implementation}
\begin{lstlisting}
void dfs(int u, vector<int> neigh[], vector<int> *st)
{
// ... (use neigh[] to flood)
st->push_back(u);
}
vector<int> toposort; // not valid toposort!
for (int i = 0; i < n; i++)
dfs(i, neigh, &toposort);
visited.reset();
for (int i = n-1; i >= 0; i--)
{
if (!visited[toposort[i]])
{
vector<int> scc;
dfs(toposort[i], neighT, &scc);
}
}
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Source of figures}
\begin{itemize}
\item \url{https://en.wikipedia.org/wiki/File:6n-graf.svg}
\item \url{http://en.wikipedia.org/wiki/File:Depth-first-tree.svg}
\item \url{http://en.wikipedia.org/wiki/File:Data_stack.svg}
\item \url{http://en.wikipedia.org/wiki/File:Breadth-first-tree.svg}
\item \url{http://en.wikipedia.org/wiki/File:Data_Queue.svg}
\item \url{https://commons.wikimedia.org/wiki/File:Simple-bipartite-graph.svg}
\end{itemize}
\end{frame}
\end{document}