Data Structures in Java
Lecture 19: Applications of DFS
11/30/2015 Daniel Bauer
1
Data Structures in Java Lecture 19: Applications of DFS 11/30/2015 - - PowerPoint PPT Presentation
Data Structures in Java Lecture 19: Applications of DFS 11/30/2015 Daniel Bauer 1 Contents Applications of DFS Euler Circuits Biconnectivity in Undirected Graphs. Finding Strongly Connected Components for Directed Graphs. 2
Lecture 19: Applications of DFS
11/30/2015 Daniel Bauer
1
Directed Graphs.
2
Directed Graphs.
3
4
graph that visits every edge exactly once.
5
graph that visits every edge exactly once.
6
graph that visits every edge exactly once.
7
graph that visits every edge exactly once.
8
graph that visits every edge exactly once.
9
graph that visits every edge exactly once.
10
graph that visits every edge exactly once.
11
graph that visits every edge exactly once.
12
graph that visits every edge exactly once.
13
graph that visits every edge exactly once. This graph does not have an Euler Path.
14
This graph does NOT have an Euler Circuit
ends at the same node.
15
ends at the same node.
16
ends at the same node.
17
ends at the same node.
18
ends at the same node.
19
ends at the same node.
20
ends at the same node.
21
ends at the same node.
22
ends at the same node.
23
ends at the same node.
24
ends at the same node.
25
ends at the same node.
26
ends at the same node. This graph does not have an Euler Path.
27
we will be stuck! 3
28
we will be stuck! 2
29
we will be stuck! 1
30
we will be stuck!
need to have even degree (even number of edges).
31
3
vertices must have odd degree.
3
32
3
vertices must have odd degree.
3
33
need to have even degree (even number of edges).
vertices need to have odd degree.
(i.e. every graph that contains only vertices of even degree has an Euler circuit). (Hierholzer, 1873 )
34
Leonhard Euler, 1735 Source: Wikipedia The city of Königsberg in Prussia (now Kaliningrad, Russia) was set on both sides of the Pregel River, and included two large islands which were connected to each other and the mainland by seven bridges. Euler’s Problem: Find a walk through the city that crosses every bridge exactly once!
35
Leonhard Euler, 1735
36
Leonhard Euler, 1735
3 5 3 3
There is no Euler Path in this graph.
37
and ending in s. Mark all edges on the circuit as visited.
to not get stuck before we arrive at s again.
visited:
edges.
circuit
38
8 5 6 9
10
7 2 4
1
3
4 2 2 2 2 4 4 4 4 4
39
8 5 6 9
10
7 2 4
1
3
3 1 2 2 2 4 4 4 4 4
40
8 5 6 9
10
7 2 4
1
3
2 1 2 2 2 4 4 3 4 4
41
8 5 6 9
10
7 2 4
1
3
2 1 2 2 2 4 4 2 3 4
42
8 5 6 9
10
7 2 4
1
3
2 1 2 2 2 4 4 2 2 3
43
8 5 6 9
10
7 2 4
1
3
2 2 2 2 4 4 2 2 2
1 2 4 6 5 1
44
1
8 5 6 9
10
7
2
4 3
2 2 2 2 4 4 2 2 2
1 2 4 6 5 1
45
1
8 5 6 9
10
7
2
4 3
2 2 2 2 2 2
1 2 4 6 5 1 2 7 6 10 3 2
46
1
8 5 6 9
10
7
2
4 3
2 2 2 2 2 2
2 7 6 10 3 2 1 4 6 5 1
47
1
8 5 6 9
10
7
4 3
1 2 7 6 10 3 2 4 6 5 1
2
7 4 5 8 9 10 7
48
1
8 5 6 9
10
7
4 3 2
7 4 5 8 9 10 7 1 2 6 10 3 2 4 6 5 1
49
graph that visits every vertex exactly once (except that the first and last vertex may be the same).
starts and ends in the same node. No hamiltonian path.
50
linear time.
Hamiltonian Path/Cycle is much harder!
No hamiltonian path.
51
Directed Graphs.
52
path from every vertex to every other vertex.
unconnected graph
53
v, such that removing v will disconnect the remaining graph.
A B C D E A B C D F E G
biconnected not biconnected
54
v, such that removing v will disconnect the remaining graph.
A B C D E A B F E G
biconnected not biconnected
D
55
v, such that removing v will disconnect the remaining graph.
A B C D E A B C D F E G
biconnected not biconnected
56
v, such that removing v will disconnect the remaining graph.
A B C D E A B F E G
biconnected not biconnected
C
57
v, such that removing v will disconnect the remaining graph.
A B C D E A B
C D
F E G
biconnected not biconnected C and D are articulation points.
58
|V|·O(|V|+|E|) = O(|V|2 + |V|·|E|)
connected.
59
A B C D E
spanning tree.
60
A
B C D E A
spanning tree.
61
A B
C D E A B
spanning tree.
62
A B C
D E A B C
spanning tree.
63
A B C D
E A B C D
spanning tree.
64
A B C D
E A B C D
spanning tree.
65
A B C D
E A B C D
spanning tree.
66
A B C D E
A B C D E
spanning tree.
67
A B C D E
A B C D E
spanning tree.
68
A B C D E
tree edges, the root is an articulation point.
A B C D F E G F G
C is an articulation point.
69
A B D E
tree edges, the root is an articulation point.
A B C D F E G F C G
A is not an articulation point.
70
A B
D E
back-edge from the subtree below w to any ancestor of v.
A B C
D F E
G
F
C G
C is an articulation point because
71
A B D E
back-edge from the subtree below w to any ancestor of v.
A B C D F E
G
F C
G
D is an articulation point because
72
are visited by DFS.
v Num(v) A 1 B 2 C 3 D 4 E 5 F 6 G 7
A B D E F C G
1 2 3 4 5 6 7
73
reachable by following a path that contains at most one back edge.
v Num(v) Low(v) A 1 1 B 2 C 3 D 4 E 5 F 6 G 7
A B D E F C G
1 2 3 4 5 6 7
74
reachable by following a path that contains at most one back edge.
v Num(v) Low(v) A 1 1 B 2 C 3 D 4 E 5 F 6 G 7
A B D E F C G
1 1 1 1 2 3 4 5 6 7
75
reachable by following a path that contains at most one back edge.
v Num(v) Low(v) A 1 1 B 2 C 3 D 4 E 5 F 6 G 7
A B D E F C G
1 1 1 4 4 7 1 2 3 4 5 6 7
76
v Num(v) Low(v) A 1 1 B 2 C 3 D 4 E 5 F 6 G 7
A B D E F C G
1 1 1 4 4 7 1 2 3 4 5 6 7
back-edge from the subtree below w to any ancestor of v.
77
v Num(v) Low(v) A 1 1 B 2 C 3 D 4 E 5 F 6 G 7
A B
D E
F
C G
1 1 1 4 4 7 1 2 3 4 5 6 7
78
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
79
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1
80
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2
81
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 3
82
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 3 1 7
83
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 3 1 5 7
84
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 3 1 5 4 7
85
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 3 1 4 4 7
86
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 3 1 4 4 7
87
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 2 1 1 4 4 7
88
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 1 1 1 4 4 7
89
A B D E F C G
1 2 3 4 5 6 7
Low(v) = Num(v) for all back edges (v,u) { if ( Num(u) < Low(v) ) Low(v) = Num(u); } for all tree edges (v,u) { compute_low(u); if ( Low(u) < Low(v) ) Low(v) = Low(u); } compute_low(v) { }
1 1 1 1 4 4 7
Time to compute low numbers: O(|V|+|E|) Time to compute preorder numbers: O(|V| +|E|) Time to check for articulation points: O(|V|+|E|) Total: O(|V|+|E|)
90
Directed Graphs.
91
an undirected path from every vertex to every other vertex.
weakly connected graph
92
a path from every vertex to every other vertex.
v
Weakly connected, but not strongly connected (no other vertex can be reached from v).
93
some start node s.
s
94
some start node s.
s
95
some start node s.
s
96
some start node s.
s
97
some start node s.
s
98
some start node s.
s
99
some start node s.
s
100
some start node s.
s
101
some start node s.
s
102
some start node s.
s
103
some start node s.
s
104
some start node s.
s
105
each partition is strongly connected.
106
each partition is strongly connected.
A D E B C F G H J I
107
each partition is strongly connected.
A D E B C F
G H J I
108
each partition is strongly connected.
A D E B C F G H J I
109
unvisited node.
A
D B C F G H J I E
110
unvisited node.
A D
B C F G H J I
E
E
111
unvisited node.
A D
B C F G H J I
E
E D
112
unvisited node.
A D B
C F G H J I
E
E D A
113
unvisited node.
A D B C
F G H J I
E
E D A C
114
unvisited node.
A D B C F
G H J I
E
E D A C B F
115
unvisited node.
A D B C F
G
H
J I
E
E D A C B F
116
unvisited node.
A D B C F
G
H J I E
E D A C B F J I
117
unvisited node.
A D B C F
G
H J I E
E D A C B F J I H
118
unvisited node.
A D B C F G H J I E
E D A C B F J I H G
119
A D E B C F G H J I
E D A C B F J I H G
120
nodes are a strong component. Remove this component from the graph and stack.
A D E B C F G H J I
E D A C B F J I H G
121
nodes are a strong component. Remove this component from the graph and stack.
A D E B C F
G
H J I
E D A C B F J I H
122
nodes are a strong component. Remove this component from the graph and stack.
A D E B C F
G
H J I
E D A C B F J I H
123
nodes are a strong component. Remove this component from the graph and stack.
A D E B C F
G H J I
E D A C B F J I
124
nodes are a strong component. Remove this component from the graph and stack.
A D E B C F
G H J I
E D A C B F
125
nodes are a strong component. Remove this component from the graph and stack.
A
D E
B C F G H J I
E D A C B
126
nodes are a strong component. Remove this component from the graph and stack.
A
D E
B C F G H J I
E D
127
nodes are a strong component. Remove this component from the graph and stack.
A D
E
B C F G H J I
E
128