Download presentation
Presentation is loading. Please wait.
1
Jiří Vyskočil, Radek Mařík 2013
Advanced algorithms strongly connected components algorithms, Euler trail, Hamiltonian path Jiří Vyskočil, Radek Mařík 2013
2
Connected component A connected component of graph G =(V,E ) with regard to vertex v is a set C(v ) = {u ∈ V | there exists a path in G from u to v }. In other words: If a graph is disconnected, then parts from which is composed from and that are themselves connected, are called connected components. a b d c e C(a)=C(b)={a,b} C(c) =C(d) =C(e)={c,d,e}
3
Strongly Connected Components [silně souvislé komponenty]
A directed graph G =(V,E ) is called strongly connected if there is a path in each direction between every couple of vertices in the graph. The strongly connected components of a directed graph G are its maximal strongly connected subgraphs. SCC(v ) = {u ∈ V | there exists a path in G from u to v and a path in G from v to u} a b c d e f g h
4
Kosaraju-Sharir Algorithm
input: graph G = (V, E ) output: set of strongly connected components (sets of vertices) S = empty stack; while S does not contain all vertices do Choose an arbitrary vertex v not in S; DFS-Walk’(v ) and each time that DFS finishes expanding a vertex u, push u onto S; Reverse the directions of all arcs to obtain the transpose graph; while S is nonempty do v = pop(S); if v is UNVISITED then DFS-Walk(v ); The set of visited vertices will give the strongly connected component containing v;
5
DFS-Walk input: Graph G.
procedure DFS-Walk(Vertex u ) { state[u ] = OPEN; d[u ] = ++time; for each Vertex v in succ(u ) if (state[v ] == UNVISITED) then {p[v ] = u; DFS-Walk(v ); } state[u ] = CLOSED; f[u ] = ++time; } procedure DFS-Walk’(Vertex u ) { if (state[v ] == UNVISITED) then {p[v ] = u; DFS-Walk’(v ); } state[u ] = CLOSED; f[u ] = ++time; push u to S; output: array p pointing to predecessor vertex, array d with times of vertex opening and array f with time of vertex closing.
6
DFS-Walk - optimized input: Graph G. procedure DFS-Walk(Vertex u ) {
state[u ] = OPEN; for each Vertex v in succ(u ) if (state[v ] == UNVISITED) then DFS-Walk(v ); state[u ] = CLOSED; } procedure DFS-Walk’(Vertex u ) { if (state[v ] == UNVISITED) then DFS-Walk’(v ); state[u ] = CLOSED; push u to S;
7
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
8
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
9
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
10
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
11
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
12
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
13
Kosaraju-Sharir Algorithm
f g a b c d e f g h OPEN CLOSED UNVISITED
14
Kosaraju-Sharir Algorithm
f g a b c d e f g h OPEN CLOSED UNVISITED
15
Kosaraju-Sharir Algorithm
f g a b c d e f g h OPEN CLOSED UNVISITED
16
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
17
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
18
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
19
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
20
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
21
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
22
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
23
Kosaraju-Sharir Algorithm
c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
24
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
25
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
26
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
27
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
28
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
29
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
30
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
31
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
32
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
33
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
34
Kosaraju-Sharir Algorithm
b c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
35
Kosaraju-Sharir Algorithm
c d h e f g a b c d e f g h OPEN CLOSED UNVISITED
36
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
37
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
38
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
39
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
40
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
41
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
42
Kosaraju-Sharir Algorithm
d h e f g a b c d e f g h OPEN CLOSED UNVISITED
43
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
44
Kosaraju-Sharir Algorithm
e f g a b c d e f g h OPEN CLOSED UNVISITED
45
Kosaraju-Sharir Algorithm
f g a b c d e f g h OPEN CLOSED UNVISITED
46
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
47
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
48
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
49
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
50
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
51
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
52
Kosaraju-Sharir Algorithm
b c d e f g h OPEN CLOSED UNVISITED
53
Kosaraju-Sharir Algorithm
Complexity: The Kosaraju-Sharir algorithm performs two complete traversals of the graph. If the graph is represented as an adjacency list then the algorithm runs in Θ(|V|+|E|) time (linear time). If the graph is represented as an adjacency matrix then the algorithm runs in O(|V|2) time.
54
Tarjan's Algorithm input: graph G = (V, E)
output: set of strongly connected components // every node has following fields: // index: a unique number to ID node // lowlink: ties node to others in SCC // pred: pointer to stack predecessor // instack: true if node is in stack procedure push( v ) // stack may be null v.pred = S; v.instack = true; S = v; end push; function pop( v ) // val param v is stack copy S = v.pred; v.pred = null; v.instack = false; return v; end pop; procedure find_scc( v ) v.index = v.lowlink = ++index; push( v ); foreach node w in succ( v ) do if w.index = 0 then // not yet visited find_scc( w ); v.lowlink = min( v.lowlink, w.lowlink ); elsif w.instack then v.lowlink = min( v.lowlink, w.index ); end if; end foreach; if v.lowlink = v.index then // v: head of SCC SCC++; // track how many SCCs found repeat x = pop( S ); add x to current strongly connected component; until x = v; output the current strongly connected component; end find_scc; index = 0; // unique node number > 0 S = null; // pointer to node stack SCC = 0; // number of SCCs in G foreach node v in V do if v.index = 0 then // yet unvisited find_scc( v );
55
Tarjan's Algorithm S pred instack = true instack = false
56
Tarjan's Algorithm S 1 1 pred instack = true instack = false
57
Tarjan's Algorithm S 1 1 2 2 pred instack = true instack = false
58
Tarjan's Algorithm S 1 1 2 2 3 3 pred instack = true instack = false
59
Tarjan's Algorithm S 1 2 3 4 pred instack = true instack = false 1 2 3
60
Tarjan's Algorithm S 1 2 3 4 5 pred instack = true instack = false 1 2
61
Tarjan's Algorithm S 1 2 3 4 6 5 pred instack = true instack = false 1
62
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
63
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
64
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
65
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
66
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
67
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
68
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
69
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
70
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
71
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
72
Tarjan's Algorithm S 1 2 3 4 7 6 5 pred instack = true instack = false
73
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
74
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
75
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
76
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
77
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
78
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
79
Tarjan's Algorithm S 1 2 3 4 8 7 6 5 pred instack = true
instack = false
80
Tarjan's Algorithm Complexity: The Tarjan's algorithm performs only one complete traversal of the graph. If the graph is represented as an adjacency list then the algorithm runs in Θ(|V|+|E|) time (linear time). If the graph is represented as an adjacency matrix then the algorithm runs in O(|V|2) time. The Tarjan's algorithm runs faster than the Kosaraju-Sharir algorithm.
81
Euler Trail [eulerovský tah]
Euler Trail Problem: Does a (directed or undirected) graph G contain a trail (trail is similar to path but vertices can repeat and edges cannot repeat) that visits every edge exactly once?
82
Euler Trail - Properties
Theorem: A graph G has an Euler trail if and only if it is connected and has 0 or 2 vertices of odd degree. We can distinguish two cases: Euler trail starts and ends in the same vertex. (Eulerian Tour) Every vertex must have even degree. Euler trail starts and ends in the different vertices. The starting and ending vertex must have odd degree and the others have even degree.
83
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
84
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
85
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
86
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
87
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
88
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
89
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
90
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
91
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
92
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
93
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
94
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
95
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
96
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
97
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
98
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
99
Euler Trail input: graph G = (V, E )
output: trail (as a stack with edges) procedure euler-trail(vertex v); { foreach vertex u in succ(v) do { remove edge(v,u) from graph; euler-trail(u); push(edge(v,u)); }
100
Euler Trail Complexity: The Euler trail algorithm performs only one complete traversal of the graph. If the graph is represented as an adjacency list then the algorithm runs in Θ(|V|+|E|) time (linear time). If the graph is represented as an adjacency matrix then the algorithm runs in O(|V|2) time.
101
Hamiltonian Path Hamiltonian Path Problem:
Does a (directed or undirected) graph G contain a path that visits every node exactly once? start target
102
Hamiltonian Path Why is the Hamiltonian Path problem so hard (NPC)?
Reduction Idea: Suppose we have a black box to solve Hamiltonian Path. We already know that SAT is hard – NP-Complete (Cook 1971). If we can do a polynomial time transformation of an arbitrary input SAT instance to some instance for our black box in such a way, that our black box solution will directly represent SAT solution for the input, then If we solve our black box in polynomial time then we can solve even SAT in polynomial time.
103
Hamiltonian Path . . . x1 c1 c2 x2 c3 . . . . . xn ck . . . . . .
High level structure: S . . . x1 x2 . xn c1 c2 c3 . ck . . . . . . . . . T
104
Hamiltonian Path xi . . . Internal structure of variable xi:
A number of occurrences of variable xi in the whole SAT exactly corresponds to the number of pairs in yellow ovals. Direction we travel along this chain represents whether to set the variable to false. xi . . . Direction we travel along this chain represents whether to set the variable to true.
105
Hamiltonian Path cj xi . . . Internal structure of variable xi:
If the clause cj contains the positive literal: xi cj xi . . .
106
Hamiltonian Path cj xi . . . Internal structure of variable xi:
If the clause cj contains the negative literal: xi cj xi . . .
107
References Matoušek, J.; Nešetřil, J. Kapitoly z diskrétní matematiky. Karolinum. Praha ISBN Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). Introduction to Algorithms (2nd ed.). MIT Press and McGraw-Hill. ISBN Tarjan, R. E. (1972). Depth-first search and linear graph algorithms, SIAM Journal on Computing 1 (2): 146–160, doi: /
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.