how to win coding competitions: secrets of champions week ... · how to win coding competitions:...
TRANSCRIPT
-
How to Win Coding Competitions: Secrets of Champions
Week 4: Algorithms on GraphsLecture 6: Eulerian paths and Eulerian tours
Maxim BuzdalovSaint Petersburg 2016
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
Definitions
An Eulerian path is a path in a graphthat contains each edge of the graphexactly once
FBCEDFGCADBAE
A
B
C
D
E
F
G
An Eulerian tour is a Eulerian pathwhich starts and ends on the same vertex
FEABGCEDBCADF
A
B
C
D
E
F G
2 / 6
-
When do they exist?
When does Eulerian path exist?
I Undirected graph:I The graph is connectedI There are at most two vertices with odd degree
I Directed graph:I The graph is connected (when directions are removed)I At most one vertex u has deg+(u)− deg−(u) = +1I At most one vertex v has deg+(v)− deg−(v) = −1I All other vertices have deg+(x) = deg−(x)
When does Eulerian tour exist?I Undirected graph:
I The graph is connectedI All vertex degrees are even
I Directed graph:I The graph is strongly connectedI All vertices have deg+(x) = deg−(x)
3 / 6
-
When do they exist?
When does Eulerian path exist?I Undirected graph:
I The graph is connectedI There are at most two vertices with odd degree
I Directed graph:I The graph is connected (when directions are removed)I At most one vertex u has deg+(u)− deg−(u) = +1I At most one vertex v has deg+(v)− deg−(v) = −1I All other vertices have deg+(x) = deg−(x)
When does Eulerian tour exist?I Undirected graph:
I The graph is connectedI All vertex degrees are even
I Directed graph:I The graph is strongly connectedI All vertices have deg+(x) = deg−(x)
3 / 6
-
When do they exist?
When does Eulerian path exist?I Undirected graph:
I The graph is connectedI There are at most two vertices with odd degree
I Directed graph:I The graph is connected (when directions are removed)I At most one vertex u has deg+(u)− deg−(u) = +1I At most one vertex v has deg+(v)− deg−(v) = −1I All other vertices have deg+(x) = deg−(x)
When does Eulerian tour exist?I Undirected graph:
I The graph is connectedI All vertex degrees are even
I Directed graph:I The graph is strongly connectedI All vertices have deg+(x) = deg−(x)
3 / 6
-
When do they exist?
When does Eulerian path exist?I Undirected graph:
I The graph is connectedI There are at most two vertices with odd degree
I Directed graph:I The graph is connected (when directions are removed)I At most one vertex u has deg+(u)− deg−(u) = +1I At most one vertex v has deg+(v)− deg−(v) = −1I All other vertices have deg+(x) = deg−(x)
When does Eulerian tour exist?
I Undirected graph:I The graph is connectedI All vertex degrees are even
I Directed graph:I The graph is strongly connectedI All vertices have deg+(x) = deg−(x)
3 / 6
-
When do they exist?
When does Eulerian path exist?I Undirected graph:
I The graph is connectedI There are at most two vertices with odd degree
I Directed graph:I The graph is connected (when directions are removed)I At most one vertex u has deg+(u)− deg−(u) = +1I At most one vertex v has deg+(v)− deg−(v) = −1I All other vertices have deg+(x) = deg−(x)
When does Eulerian tour exist?I Undirected graph:
I The graph is connectedI All vertex degrees are even
I Directed graph:I The graph is strongly connectedI All vertices have deg+(x) = deg−(x)
3 / 6
-
When do they exist?
When does Eulerian path exist?I Undirected graph:
I The graph is connectedI There are at most two vertices with odd degree
I Directed graph:I The graph is connected (when directions are removed)I At most one vertex u has deg+(u)− deg−(u) = +1I At most one vertex v has deg+(v)− deg−(v) = −1I All other vertices have deg+(x) = deg−(x)
When does Eulerian tour exist?I Undirected graph:
I The graph is connectedI All vertex degrees are even
I Directed graph:I The graph is strongly connectedI All vertices have deg+(x) = deg−(x)
3 / 6
-
How to prove these existence theorems?
I Induction: assume that your graph has |E | edges,and the theorem was proven for all e < |E | numbers of edges
I Pick a vertexI Random when you don’t have “special” verticesI A “special” vertex if you have one
I The one with deg+(v) > deg−(v) if the graph is directed
I Traverse the graph and remove the traversed edgesI If you cannot do this anymore, what happens?
I Either there are no more edges → path/tour is foundI Some edges remain
I There are (maybe several) connected subgraphsI Find the paths/tours in them (can do this by induction)I Connect them with the path/tour constructed from removed edges
I That’s what Depth First Search can do!
4 / 6
-
How to prove these existence theorems?
I Induction: assume that your graph has |E | edges,and the theorem was proven for all e < |E | numbers of edges
I Pick a vertexI Random when you don’t have “special” verticesI A “special” vertex if you have one
I The one with deg+(v) > deg−(v) if the graph is directed
I Traverse the graph and remove the traversed edgesI If you cannot do this anymore, what happens?
I Either there are no more edges → path/tour is foundI Some edges remain
I There are (maybe several) connected subgraphsI Find the paths/tours in them (can do this by induction)I Connect them with the path/tour constructed from removed edges
I That’s what Depth First Search can do!
4 / 6
-
How to prove these existence theorems?
I Induction: assume that your graph has |E | edges,and the theorem was proven for all e < |E | numbers of edges
I Pick a vertexI Random when you don’t have “special” verticesI A “special” vertex if you have one
I The one with deg+(v) > deg−(v) if the graph is directed
I Traverse the graph and remove the traversed edgesI If you cannot do this anymore, what happens?
I Either there are no more edges → path/tour is foundI Some edges remain
I There are (maybe several) connected subgraphsI Find the paths/tours in them (can do this by induction)I Connect them with the path/tour constructed from removed edges
I That’s what Depth First Search can do!
4 / 6
-
How to prove these existence theorems?
I Induction: assume that your graph has |E | edges,and the theorem was proven for all e < |E | numbers of edges
I Pick a vertexI Random when you don’t have “special” verticesI A “special” vertex if you have one
I The one with deg+(v) > deg−(v) if the graph is directed
I Traverse the graph and remove the traversed edges
I If you cannot do this anymore, what happens?I Either there are no more edges → path/tour is foundI Some edges remain
I There are (maybe several) connected subgraphsI Find the paths/tours in them (can do this by induction)I Connect them with the path/tour constructed from removed edges
I That’s what Depth First Search can do!
4 / 6
-
How to prove these existence theorems?
I Induction: assume that your graph has |E | edges,and the theorem was proven for all e < |E | numbers of edges
I Pick a vertexI Random when you don’t have “special” verticesI A “special” vertex if you have one
I The one with deg+(v) > deg−(v) if the graph is directed
I Traverse the graph and remove the traversed edgesI If you cannot do this anymore, what happens?
I Either there are no more edges → path/tour is foundI Some edges remain
I There are (maybe several) connected subgraphsI Find the paths/tours in them (can do this by induction)I Connect them with the path/tour constructed from removed edges
I That’s what Depth First Search can do!
4 / 6
-
How to prove these existence theorems?
I Induction: assume that your graph has |E | edges,and the theorem was proven for all e < |E | numbers of edges
I Pick a vertexI Random when you don’t have “special” verticesI A “special” vertex if you have one
I The one with deg+(v) > deg−(v) if the graph is directed
I Traverse the graph and remove the traversed edgesI If you cannot do this anymore, what happens?
I Either there are no more edges → path/tour is foundI Some edges remain
I There are (maybe several) connected subgraphsI Find the paths/tours in them (can do this by induction)I Connect them with the path/tour constructed from removed edges
I That’s what Depth First Search can do!
4 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E}R ← [ ]procedure DFS(v)
for u ∈ A(v) doRemove u from A(v)if graph is undirected then
Remove v from A(u)end ifDFS(u)
end forR ← [v ] + R
end procedure
5 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E} . No U set is used: can visit a vertex more than once!R ← [ ]procedure DFS(v)
for u ∈ A(v) doRemove u from A(v)if graph is undirected then
Remove v from A(u)end ifDFS(u)
end forR ← [v ] + R
end procedure
5 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E} . No U set is used: can visit a vertex more than once!R ← [ ] . The result storageprocedure DFS(v)
for u ∈ A(v) doRemove u from A(v)if graph is undirected then
Remove v from A(u)end ifDFS(u)
end forR ← [v ] + R
end procedure
5 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E} . No U set is used: can visit a vertex more than once!R ← [ ] . The result storageprocedure DFS(v) . Should be called on a non-regular vertex, if any
for u ∈ A(v) doRemove u from A(v)if graph is undirected then
Remove v from A(u)end ifDFS(u)
end forR ← [v ] + R
end procedure
5 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E} . No U set is used: can visit a vertex more than once!R ← [ ] . The result storageprocedure DFS(v) . Should be called on a non-regular vertex, if any
for u ∈ A(v) doRemove u from A(v) . Will never follow the (v , u) edge anymoreif graph is undirected then
Remove v from A(u)end ifDFS(u)
end forR ← [v ] + R
end procedure
5 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E} . No U set is used: can visit a vertex more than once!R ← [ ] . The result storageprocedure DFS(v) . Should be called on a non-regular vertex, if any
for u ∈ A(v) doRemove u from A(v) . Will never follow the (v , u) edge anymoreif graph is undirected then
Remove v from A(u) . If undirected, the anti-edge must be removedend ifDFS(u)
end forR ← [v ] + R
end procedure
5 / 6
-
DFS modification to solve Eulerian paths
G = 〈V ,E 〉A(v) = {u | (v , u) ∈ E} . No U set is used: can visit a vertex more than once!R ← [ ] . The result storageprocedure DFS(v) . Should be called on a non-regular vertex, if any
for u ∈ A(v) doRemove u from A(v) . Will never follow the (v , u) edge anymoreif graph is undirected then
Remove v from A(u) . If undirected, the anti-edge must be removedend ifDFS(u)
end forR ← [v ] + R . Prepend v to the answer
end procedure
5 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBC
E
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBC
E
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBC
E
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBC
E
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBC
E
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBC
E
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADB
CE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADB
CE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCAD
BCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCAD
BCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCA
DBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCA
DBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGC
ADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGC
ADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFG
CADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFG
CADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDF
GCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDF
GCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAED
FGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAED
FGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAE
DFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAE
DFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBA
EDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBA
EDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FB
AEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FB
AEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
F
BAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
F
BAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6
-
Example: DFS finds Eulerian path
FBAEDFGCADBCE
A
B
C
D
E
F
G
6 / 6