Advanced algorithms strongly connected components algorithms Euler trail

  • Slides: 107
Download presentation
Advanced algorithms strongly connected components algorithms, Euler trail, Hamiltonian path Jiří Vyskočil, Radek Mařík

Advanced algorithms strongly connected components algorithms, Euler trail, Hamiltonian path Jiří Vyskočil, Radek Mařík 2013

Connected component n A connected component of graph G =(V, E ) with regard

Connected component n 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 }. n In other words: If a graph is disconnected, then parts from which is composed from and that are themselves connected, are called connected components. b C(a)=C(b)={a, b} c a e Advanced algorithms d C(c) =C(d) =C(e)={c, d, e} 2 / 107

Strongly Connected Components n n A directed graph G =(V, E ) is[silně called

Strongly Connected Components n n A directed graph G =(V, E ) is[silně called souvislé strongly komponenty 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} Advanced algorithms a b c d e f g h 3 / 107

Kosaraju-Sharir Algorithm input: graph G = (V, E ) output: set of strongly connected

Kosaraju-Sharir Algorithm input: graph G = (V, E ) output: set of strongly connected components (sets of vertices) 1. S = empty stack; 2. 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; 3. Reverse the directions of all arcs to obtain the transpose graph; 4. 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; Advanced algorithms 4 / 107

DFS-Walk n input: 1) 2) 3) 4) 5) 6) Graph G. procedure DFS-Walk(Vertex u

DFS-Walk n input: 1) 2) 3) 4) 5) 6) 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 ) { 8) state[u ] = OPEN; d[u ] = ++time; 9) for each Vertex v in succ(u ) 10) if (state[v ] == UNVISITED) then {p[v ] = u; DFS-Walk’(v ); } 11) state[u ] = CLOSED; f[u ] = ++time; push u to S; 12) } 7) n output: array p pointing to predecessor vertex, array d with times of vertex opening and array f with time of vertex closing. Advanced algorithms 5 / 107

DFS-Walk - optimized n input: 1) 2) 3) 4) 5) 6) Graph G. procedure

DFS-Walk - optimized n input: 1) 2) 3) 4) 5) 6) 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 ) { 8) state[u ] = OPEN; 9) for each Vertex v in succ(u ) 10) if (state[v ] == UNVISITED) then DFS-Walk’(v ); 11) state[u ] = CLOSED; push u to S; 12) } 7) Advanced algorithms 6 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 7 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 8 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 9 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 10 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 11 / 107

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms CLOSED UNVISITED 12 / 107

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced algorithms CLOSED UNVISITED 13 / 107

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced algorithms CLOSED UNVISITED 14 / 107

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced algorithms CLOSED UNVISITED 15 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED e f g UNVISITED 16 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED e f g UNVISITED 17 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED e f g UNVISITED 18 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED e f g UNVISITED 19 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED e f g UNVISITED 20 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h h e f g UNVISITED 21 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 22 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED c d h e f g UNVISITED 23 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 24 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED a b c d h e f g UNVISITED 25 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED a b c d h e f g UNVISITED 26 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED a b c d h e f g UNVISITED 27 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 28 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 29 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 30 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 31 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 32 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 33 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED b c d h e f g UNVISITED 34 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED c d h e f g UNVISITED 35 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 36 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 37 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 38 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 39 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 40 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 41 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h e f g UNVISITED 42 / 107

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h

Kosaraju-Sharir Algorithm a e OPEN Advanced algorithms b f CLOSED c g d h h e f g UNVISITED 43 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED e f g UNVISITED 44 / 107

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced

Kosaraju-Sharir Algorithm a b c d e f g h f g OPEN Advanced algorithms CLOSED UNVISITED 45 / 107

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms CLOSED UNVISITED 46 / 107

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms CLOSED UNVISITED 47 / 107

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms CLOSED UNVISITED 48 / 107

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms CLOSED UNVISITED 49 / 107

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms

Kosaraju-Sharir Algorithm a b c d e f g h g OPEN Advanced algorithms CLOSED UNVISITED 50 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 51 / 107

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED

Kosaraju-Sharir Algorithm OPEN Advanced algorithms a b c d e f g h CLOSED UNVISITED 52 / 107

Kosaraju-Sharir Algorithm n Complexity: ¨ The Kosaraju-Sharir algorithm performs two complete traversals of the

Kosaraju-Sharir Algorithm n 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. Advanced algorithms 53 / 107

Tarjan's Algorithm input: output: // // // graph G = (V, E) set of

Tarjan's Algorithm input: output: // // // graph G = (V, E) 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; Advanced algorithms 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++; // 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 if; 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 ); end if; end foreach; 54 / 107

Tarjan's Algorithm S pred Advanced algorithms instack = true instack = false 55 /

Tarjan's Algorithm S pred Advanced algorithms instack = true instack = false 55 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 instack = true instack = false

Tarjan's Algorithm S 1 pred Advanced algorithms 1 instack = true instack = false 56 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 instack = true instack

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 instack = true instack = false 57 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 33 instack = true

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 33 instack = true instack = false 58 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 33 instack = true

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 33 instack = true 44 instack = false 59 / 107

Tarjan's Algorithm S 1 1 2 2 33 44 55 pred Advanced algorithms instack

Tarjan's Algorithm S 1 1 2 2 33 44 55 pred Advanced algorithms instack = true instack = false 60 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 33 44 66 55

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 2 33 44 66 55 instack = true instack = false 61 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 77 66 55

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 77 66 55 2 instack = true instack = false 62 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55 2 instack = true instack = false 63 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55 2 instack = true instack = false 64 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55 2 instack = true instack = false 65 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 55 2 instack = true instack = false 66 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 54

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 44 76 66 54 2 instack = true instack = false 67 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54 2 instack = true instack = false 68 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54 2 instack = true instack = false 69 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54 2 instack = true instack = false 70 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54 2 instack = true instack = false 71 / 107

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54

Tarjan's Algorithm S 1 pred Advanced algorithms 1 2 33 43 76 66 54 2 instack = true instack = false 72 / 107

Tarjan's Algorithm S 1 1 88 pred Advanced algorithms 2 33 43 76 66

Tarjan's Algorithm S 1 1 88 pred Advanced algorithms 2 33 43 76 66 54 2 instack = true instack = false 73 / 107

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 2 33 43 76 66

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 2 33 43 76 66 54 2 instack = true instack = false 74 / 107

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66 54 2 instack = true instack = false 75 / 107

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66 54 2 instack = true instack = false 76 / 107

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66 54 2 instack = true instack = false 77 / 107

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66 54 2 instack = true instack = false 78 / 107

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66

Tarjan's Algorithm S 1 1 81 pred Advanced algorithms 1 33 43 76 66 54 2 instack = true instack = false 79 / 107

n Complexity: Tarjan's Algorithm ¨ The Tarjan's algorithm performs only one complete traversal of

n Complexity: Tarjan's Algorithm ¨ 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. Advanced algorithms 80 / 107

n Euler Trail [eulerovský tah] Euler Trail Problem: Does a (directed or undirected) graph

n 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? Advanced algorithms 81 / 107

Euler Trail - Properties n Theorem: A graph G has an Euler trail if

Euler Trail - Properties n Theorem: A graph G has an Euler trail if and only if it is connected and has 0 or 2 vertices of odd degree. n We can distinguish two cases: 1. Euler trail starts and ends in the same vertex. (Eulerian Tour) →Every vertex must have even degree. 2. Euler trail starts and ends in the different vertices. →The starting and ending vertex must have odd degree and the others have even degree. Advanced algorithms 82 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 83 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 84 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 85 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 86 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 87 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 88 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 89 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 90 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 91 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 92 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 93 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 94 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 95 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 96 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 97 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 98 / 107

input: graph G = (V, E ) Euler Trail output: trail (as a stack

input: graph G = (V, E ) Euler Trail 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)); } } Advanced algorithms 99 / 107

Euler Trail n Complexity: ¨ The Euler trail algorithm performs only one complete traversal

Euler Trail n 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. Advanced algorithms 100 / 107

Hamiltonian Path n Hamiltonian Path Problem: Does a (directed or undirected) graph G contain

Hamiltonian Path n Hamiltonian Path Problem: Does a (directed or undirected) graph G contain a path that visits every node exactly once? start Advanced algorithms target 101 / 107

Hamiltonian Path n Why is the Hamiltonian Path problem so hard (NPC)? n Reduction

Hamiltonian Path n Why is the Hamiltonian Path problem so hard (NPC)? n 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. ¨ Advanced algorithms 102 / 107

Hamiltonian Path High level structure: n S . . . x 1 c 2

Hamiltonian Path High level structure: n S . . . x 1 c 2 x 2. . . . xn c 3 ck T Advanced algorithms 103 / 107

Hamiltonian Path n Internal structure of variable xi: ¨ A number of occurrences of

Hamiltonian Path n 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. xi Direction we travel along this chain represents whether to set the variable to false. . Direction we travel along this chain represents whether to set the variable to true. Advanced algorithms 104 / 107

Hamiltonian Path n Internal structure of variable xi: ¨ If the clause cj contains

Hamiltonian Path n Internal structure of variable xi: ¨ If the clause cj contains the positive literal: xi cj xi. . . Advanced algorithms 105 / 107

Hamiltonian Path n Internal structure of variable xi: ¨ If the clause cj contains

Hamiltonian Path n Internal structure of variable xi: ¨ If the clause cj contains the negative literal: xi cj xi. . . Advanced algorithms 106 / 107

References n Matoušek, J. ; Nešetřil, J. Kapitoly z diskrétní matematiky. Karolinum. Praha 2002.

References n Matoušek, J. ; Nešetřil, J. Kapitoly z diskrétní matematiky. Karolinum. Praha 2002. ISBN 978 -80 -246 -1411 -3. n Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2001). Introduction to Algorithms (2 nd ed. ). MIT Press and Mc. Graw-Hill. ISBN 0 -262 -53196 -8. n Tarjan, R. E. (1972). Depth-first search and linear graph algorithms, SIAM Journal on Computing 1 (2): 146– 160, doi: 10. 1137/0201010 Advanced algorithms 107 / 107