Overview CS20a: NP problems • Graph theory – Strongly-connected components S T I T U T E O F N T A I E C I H N N Computation, Computers, and Programs Recursive functions R O O I 1 F I L O L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Graph theory • A graph is a set of points (vertices) that are interconnected by a set of lines (edges) v2 v3 v6 e1 e4 v5 e9 v1 e3 e7 e5 v8 e8 e6 e10 e2 v7 v4 This is not a vertex U T E S T T I O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 2 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Formal definition of graphs • A graph G is defined as a pair G = (V, E) where – V is a set of vertices – E is a set of edges (v i , v j ), . . . • n = | V | is the size of the graph • | E | is the number of edges T U T E S T I O F I N E T A C N I H N Computation, Computers, and Programs Recursive functions R O O 3 F L I I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview Directed graphs • A directed graph assigns a direction to the edges T S T I U T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 4 I F O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Paths, cycles A directed cycle An articulation point An undirected path An acyclic graph U T E T S I T O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 5 I F L O L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Connected components • A directed graph G is defined as a pair G = (V, E) where – V is a set of vertices – E is a set of edges (v i → v j ), . . . • Two vertices v i , v j are strongly connected iff there is a directed path from v i to v j , and a directed path from v j to v i . • Two vertices v i , v j are weakly connected iff there is an undirected path from v i to v j • Use these to define strongly-connected and weakly- connected components. T U T E S T I O F I N T E A C I N H N Computation, Computers, and Programs Recursive functions O R O 6 F L I I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview Using DFS to get strongly-connected comp • Variables: – count : the current DFS index – dfs [v] : the DFS number of vertex v – low [v] : the lowest numbered vertex x such that there is a back-edge from some descen- dent of v to x – stack a stack of the collected vertices • if low [v] = dfs [v] , then v is the root of a strongly- connected component S T T I U T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 7 I F O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Strongly-connected components U T E S T T I O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 8 I F L O L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Compute DFS spanning forest DFS Forest 6 1 5 7 8 3 2 4 Backedges T U T E S T I O F I N T E A C I N H N Computation, Computers, and Programs Recursive functions O R O 9 F L I I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview Compute low values low[6] = 6 low[1] = 1 6 1 low[5] = 4 low[2] = 1 5 7 8 3 2 low[7] = 7 low[8] = 6 low[3] = 1 4 low[4] = 3 min { low (w) | w is an immediate descendent of v } x = y = min { z | z is reachable by a back-edge from v } low (v) = min (x, y) T S I T U T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 10 F I O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences Using DFS to get strongly-connected comp • Variables: – count : the current DFS index – dfs [v] : the DFS number of vertex v – low [v] : the lowest numbered vertex x such that there is a back-edge from some descen- dent of v to x – stack a stack of the collected vertices • if low [v] = dfs [v] , then v is the root of a strongly- connected component U T E T S I T O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 11 F I L O L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences DFS algorithm let rec search v = mark v as old ; dfs [v] ← count ; count ← count + 1; low [v] ← dfs [v] ; push v onto stack ; for w ∈ outedges [v] do if w is new then search w ; low [v] ← min ( low [v], low [w]) else if dfs [w] < dfs [v] and w on stack then low [v] ← min ( dfs [w], low [v]) ; T U T E S T I O F I N T E A C I N H N Computation, Computers, and Programs Recursive functions O R O 12 F L I I L O A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Overview 2SAT graph A ¬ A T S T I U T E O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 13 I F O L L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences 2SAT algorithm Theorem A 2SAT formula B is satisfiable iff no pair of complementary literals appear in the same strongly- connected component of G . Proof • If they do, then A ⇔ ¬ A , so B is not satisfiable. • Conversely – Collapse the strong components of G into sin- gle vertices; this new graph is acyclic – If A occurs in a strong component before a strong component for ¬ A , assign A = ⊥ – Otherwise assign A = ⊤ U T E S T T I O F N T I A E C I H N N Computation, Computers, and Programs Recursive functions R O O I 14 F I L O L A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences 2SAT algorithm • Given formula B – Form the directed graph – Find the strongly-connected components – If any strongly-connected component contains a literal and its negation, report “not satisfiable” – Otherwise, build a satisfying assignment • Collapse strongly-connected components to get DAG • If A occurs before not A, then let A = false • If not A occurs before A, then let A = true T U T E T S I O F I N T E A C I N H N Computation, Computers, and Programs Recursive functions O R O 15 F L I L I O A G http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002 C Y If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences
Recommend
More recommend