Jiří Vyskočil, Radek Mařík 2013

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Combinatorial Algorithms
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Approximation Algorithms
PSUCS322 HM 1 Languages and Compiler Design II Strongly Connected Components Herbert G. Mayer PSU Spring 2010 rev.: 5/28/2010.
1 Data Structures DFS, Topological Sort Dana Shapira.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Complexity ©D.Moshkovitz 1 Paths On the Reasonability of Finding Paths in Graphs.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Graph Theory By: Maciej Kicinski Chiu Ming Luk. Extract Triple words.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
COSC 3101A - Design and Analysis of Algorithms 14 NP-Completeness.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CMSC 341 Graphs. 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a set of edges, E. Each edge is a pair (v,w)
Introduction to Algorithms
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
An Introduction to Graph Theory
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
More NP-Complete and NP-hard Problems
IOI/ACM ICPC Training 4 June 2005.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures Graphs - Terminology
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Chapter 3. Decompositions of Graphs
CSC 172 DATA STRUCTURES.
Jiří Vyskočil, Radek Mařík 2013
Richard Anderson Lecture 26 NP-Completeness
Richard Anderson Lecture 26 NP-Completeness
Csc 2720 Instructor: Zhuojun Duan
Hamiltonian Graphs Graphs Hubert Chan (Chapter 9.5)
Depth-First Search.
CS120 Graphs.
Graph Algorithms Using Depth First Search
Data Structures and Algorithms for Information Processing
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Algorithm.
CSC 172 DATA STRUCTURES.
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Graph Theory.
Graphs Chapter 13.
Advanced Algorithms Analysis and Design
Applications of DFS Topological sort (for directed acyclic graph)
Search Related Algorithms
Richard Anderson Lecture 28 NP-Completeness
Graphs.
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
Jiří Vyskočil, Radek Mařík 2012
Richard Anderson Autumn 2016 Lecture 5
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Lecture 6 Graph Traversal
Depth-First Search CSE 2011 Winter April 2019.
A vertex u is reachable from vertex v iff there is a path from v to u.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Elementary Graph Algorithms
Lecture 24 Classical NP-hard Problems
Richard Anderson Winter 2019 Lecture 5
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Lecture 23 NP-Hard Problems
GRAPH TRAVERSAL.
Presentation transcript:

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

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}

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

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;

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.

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;

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

Kosaraju-Sharir Algorithm b c d e f g h OPEN CLOSED UNVISITED

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.

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 );

Tarjan's Algorithm S pred instack = true instack = false

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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?

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.

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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)); }

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.

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

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.

Hamiltonian Path . . . x1 c1 c2 x2 c3 . . . . . xn ck . . . . . . High level structure: S . . . x1 x2 . xn c1 c2 c3 . ck . . . . . . . . . T

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.

Hamiltonian Path cj xi . . . Internal structure of variable xi: If the clause cj contains the positive literal: xi cj xi . . .

Hamiltonian Path cj xi . . . Internal structure of variable xi: If the clause cj contains the negative literal: xi cj xi . . .

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