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

Similar documents
Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Lecture 10: Strongly Connected Components, Biconnected Graphs

Outline. Graphs. Divide and Conquer.

Data Structures and Algorithms. Chapter 7. Graphs

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

Let G = (V, E) be a graph. If u, v V, then u is adjacent to v if {u, v} E. We also use the notation u v to denote that u is adjacent to v.

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Chapter 9 Graph Algorithms

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

NP-complete Reductions

A Simplified Correctness Proof for a Well-Known Algorithm Computing Strongly Connected Components

Chapter 9 Graph Algorithms

Design and Analysis of Algorithms

Homework Assignment #3 Graph

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Best known solution time is Ω(V!) Check every permutation of vertices to see if there is a graph edge between adjacent vertices

P and NP CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Algorithms and Data Structures

GRAPHS, GRAPH MODELS, GRAPH TERMINOLOGY, AND SPECIAL TYPES OF GRAPHS

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Traveling Salesman Problem (TSP) Input: undirected graph G=(V,E), c: E R + Goal: find a tour (Hamiltonian cycle) of minimum cost

NP-Completeness. Algorithms

P and NP CISC5835, Algorithms for Big Data CIS, Fordham Univ. Instructor: X. Zhang

(Re)Introduction to Graphs and Some Algorithms

Lecture 7 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson)

Coping with NP-Completeness

Graph Algorithms Using Depth First Search

2 Approximation Algorithms for Metric TSP

Varying Applications (examples)

Lecture 5: Graphs. Rajat Mittal. IIT Kanpur

Advanced algorithms. binary heap, d-ary heap, binomial heap, amortized analysis, Fibonacci heap. Jiří Vyskočil, Radek Mařík 2013

Assignment 4 Solutions of graph problems

Data Structures and Algorithms. Werner Nutt

Eulerian Cycle (2A) Walk : vertices may repeat, edges may repeat (closed or open) Trail: vertices may repeat, edges cannot repeat (open)

CSCE 350: Chin-Tser Huang. University of South Carolina

Lecture 22 Tuesday, April 10

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Graphs and Algorithms

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Outline. Introduction. Representations of Graphs Graph Traversals. Applications. Definitions and Basic Terminologies

Graph Representation

Questions? You are given the complete graph of Facebook. What questions would you ask? (What questions could we hope to answer?)

Algorithmic Aspects of Communication Networks

Unweighted Graphs & Algorithms

Adjacent: Two distinct vertices u, v are adjacent if there is an edge with ends u, v. In this case we let uv denote such an edge.

Unit 2: Algorithmic Graph Theory

Graph Algorithms: Chapters Part 1: Introductory graph concepts

CS521 \ Notes for the Final Exam

Fundamental Properties of Graphs

Maximum flows & Maximum Matchings

Reference Sheet for CO142.2 Discrete Mathematics II

Minimal Equation Sets for Output Computation in Object-Oriented Models

Graph Algorithms. Definition

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

LECTURE 17 GRAPH TRAVERSALS

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

DFS & STRONGLY CONNECTED COMPONENTS

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Unit 8: Coping with NP-Completeness. Complexity classes Reducibility and NP-completeness proofs Coping with NP-complete problems. Y.-W.

Algorithms and Data Structures

Graph Data Processing with MapReduce

1 Digraphs. Definition 1

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

Outline. 1 The matching problem. 2 The Chinese Postman Problem

Chapter 2 Graphs. 2.1 Definition of Graphs

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Konigsberg Bridge Problem

Randomized Graph Algorithms

Fundamental Graph Algorithms Part Four

P -vs- NP. NP Problems. P = polynomial time. NP = non-deterministic polynomial time

Lecture 6 Basic Graph Algorithms

Module 6 NP-Complete Problems and Heuristics

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Solutions to relevant spring 2000 exam problems

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Formally-Proven Kosaraju s algorithm

Topic 12: Elementary Graph Algorithms

8 NP-complete problem Hard problems: demo

CSCE 750, Fall 2002 Notes 6 Page Graph Problems ffl explore all nodes (breadth first and depth first) ffl find the shortest path from a given s

Graph Theory. Connectivity, Coloring, Matching. Arjun Suresh 1. 1 GATE Overflow

Design and Analysis of Algorithms - - Assessment

Module 6 NP-Complete Problems and Heuristics

Strongly Connected Components

Lecture 3: Graphs and flows

Math 443/543 Graph Theory Notes 2: Transportation problems

Final. Name: TA: Section Time: Course Login: Person on Left: Person on Right: U.C. Berkeley CS170 : Algorithms, Fall 2013

An On-Line Satisfiability Algorithm for Conjunctive Normal Form Expressions with Two Literals

A Deterministic Polynomial-time Algorithm for the Clique Problem and the Equality of P and NP Complexity Classes

NP-Complete Reductions 2

11.2 Eulerian Trails

Detecting negative cycles with Tarjan s breadth-first scanning algorithm

1 The Traveling Salesperson Problem (TSP)

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

CSE 548: Analysis of Algorithms. Lecture 13 ( Approximation Algorithms )

Transcription:

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

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. c b a C(a)=C(b)={a,b} e d C(c) =C(d) =C(e)={c,d,e} 2 / 107

Strongly Connected Components 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 3 / 107

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; 4 / 107

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

input: Graph G. 1) procedure DFS-Walk(Vertex u ) { 2) state[u ] = OPEN; 3) for each Vertex v in succ(u ) 4) if (state[v ] == UNVISITED) then DFS-Walk(v ); 5) state[u ] = CLOSED; 6) } DFS-Walk - optimized 7) 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) } 6 / 107

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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. 53 / 107

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 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 instack = true instack = false 55 / 107

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

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

Tarjan's Algorithm S 1 1 2 2 3 3 pred instack = true instack = false 58 / 107

Tarjan's Algorithm S 1 1 2 2 3 3 4 4 pred instack = true instack = false 59 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 5 5 pred instack = true instack = false 60 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 6 6 5 5 pred instack = true instack = false 61 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 7 7 6 5 6 5 pred instack = true instack = false 62 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 6 7 6 5 6 5 pred instack = true instack = false 63 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 6 7 6 5 6 5 pred instack = true instack = false 64 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 6 7 6 5 6 5 pred instack = true instack = false 65 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 6 7 6 5 6 5 pred instack = true instack = false 66 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 4 6 7 6 5 6 4 pred instack = true instack = false 67 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 6 7 6 5 6 4 pred instack = true instack = false 68 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 6 7 6 5 6 4 pred instack = true instack = false 69 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 6 7 6 5 6 4 pred instack = true instack = false 70 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 6 7 6 5 6 4 pred instack = true instack = false 71 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 6 7 6 5 6 4 pred instack = true instack = false 72 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 8 6 8 7 6 5 6 4 pred instack = true instack = false 73 / 107

Tarjan's Algorithm S 1 1 3 4 2 2 3 3 1 6 8 7 6 5 6 4 pred instack = true instack = false 74 / 107

Tarjan's Algorithm S 1 1 3 4 2 1 3 3 1 6 8 7 6 5 6 4 pred instack = true instack = false 75 / 107

Tarjan's Algorithm S 1 1 3 4 2 1 3 3 1 6 8 7 6 5 6 4 pred instack = true instack = false 76 / 107

Tarjan's Algorithm S 1 1 3 4 2 1 3 3 1 6 8 7 6 5 6 4 pred instack = true instack = false 77 / 107

Tarjan's Algorithm S 1 1 3 4 2 1 3 3 1 6 8 7 6 5 6 4 pred instack = true instack = false 78 / 107

Tarjan's Algorithm S 1 1 3 4 2 1 3 3 1 6 8 7 6 5 6 4 pred instack = true instack = false 79 / 107

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. 80 / 107

Euler Trail Problem: Euler Trail 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? 81 / 107

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: 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. 82 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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)); } } 83 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

input: graph G = (V, E ) output: trail (as a stack with edges) Euler Trail 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 / 107

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. 100 / 107

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

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. 102 / 107

Hamiltonian Path High level structure: x 1 S... c 1 x 2... c 2 c 3..... x n... c k T 103 / 107

Internal structure of variable x i : Hamiltonian Path A number of occurrences of variable x i in the whole SAT exactly corresponds to the number of pairs in yellow ovals. x i 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. 104 / 107

Internal structure of variable x i : If the clause c j contains the positive literal: x i Hamiltonian Path c j x i... 105 / 107

Internal structure of variable x i : If the clause c j contains the negative literal: x i Hamiltonian Path c j x i... 106 / 107

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