cpsc 490 graphs dp
play

CPSC 490 Graphs+DP Part 4: SCC, 2-SAT; Part 1: Intro Lucca - PowerPoint PPT Presentation

CPSC 490 Graphs+DP Part 4: SCC, 2-SAT; Part 1: Intro Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/21 University of British Columbia Announcements Assignment 1 is due this Saturday !!! In todays lecture, we will cover what


  1. CPSC 490 Graphs+DP Part 4: SCC, 2-SAT; Part 1: Intro Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/21 University of British Columbia

  2. Announcements • Assignment 1 is due this Saturday !!! • In today’s lecture, we will cover what you need for the last couple problems. 1

  3. Warm-Up: Cycle Detection in an Undirected Graph We can just run a normal DFS DFS(s): 1 mark s as visited 2 for each edge e = (s, t) : 3 if t is not visited , call DFS(t): 4 if found cycle: output HAS_CYCLE 5 if t is visited and t is not the parent of s, 6 output HAS_CYCLE 7 output NO_CYCLE 8 2

  4. Warm-Up: Cycle Detection in a Directed Graph Can we just run a normal DFS? Normal DFS finds A to C to D path, marks D as visited, finds A to B to D path and outputs HAS CYCLE . But there is no directed cycle in this graph! 3

  5. Warm-Up: Cycle Detection in a Directed Graph Can we just run a normal DFS? Normal DFS finds A to C to D path, marks D as visited, finds A to B to D path and outputs HAS CYCLE . But there is no directed cycle in this graph! We can fix this by keeping track of the current path from our current node to where we started. 3

  6. Black Grey White DFS initialize the color of all nodes as WHITE 1 DFS(s): 2 mark s as GREY 3 for each edge e = (s,t): 4 if t is WHITE , call DFS(t) 5 if t is GREY , output HAS_CYCLE 6 if t is BLACK , do nothing 7 mark s as BLACK 8 output NO_CYCLE 9 (This is an example of an efficient backtracking algorithm!) 4

  7. Black Grey White DFS 5

  8. Black Grey White DFS 6

  9. Black Grey White DFS 7

  10. Black Grey White DFS 8

  11. Black Grey White DFS 9

  12. Black Grey White DFS 10

  13. Black Grey White DFS 11

  14. Strongly Connected Components: Definition Given a directed graph... • u and v are strongly connected if there are paths u → v , v → u • Graph is strongly connected if every pair of nodes are strongly connected. We can divide up a graph into SCCs in O ( V + E ) time with DFS! Figure 1: A graph divided into its SCC’s (Wikipedia) 12

  15. Strongly Connected Component (SCC) Tarjan’s SCC Algorithm: • Initialize a stack • Pick any unvisited node and run DFS from it • For each newly visited node n in the DFS: • assign n an index in order of discovery • push n onto the stack • keep track of a low value: lowest index on stack that n ’s descendants can reach. Why only from stack? • Why only from the stack? • When do we have to update this? • when n .low == n .index, pop off nodes from the top of the stack to n . They form a strongly connected component. • Repeatedly run DFS until all nodes are visited 13

  16. Strongly Connected Component (SCC) 14

  17. Strongly Connected Component (SCC) 15

  18. Strongly Connected Component (SCC) 16

  19. Strongly Connected Component (SCC) 17

  20. Strongly Connected Component (SCC) 18

  21. Strongly Connected Component (SCC) 19

  22. Strongly Connected Component (SCC) 20

  23. Strongly Connected Component (SCC) 21

  24. Strongly Connected Component (SCC) 22

  25. Strongly Connected Component (SCC) 23

  26. Strongly Connected Component (SCC) 24

  27. Strongly Connected Component (SCC) 25

  28. Strongly Connected Component (SCC) 26

  29. Strongly Connected Component (SCC) 27

  30. Strongly Connected Component (SCC) 28

  31. Strongly Connected Component (SCC) 29

  32. Strongly Connected Component (SCC) 30

  33. Strongly Connected Component (SCC) 31

  34. Strongly Connected Component (SCC) 32

  35. Strongly Connected Component (SCC) 33

  36. Strongly Connected Component (SCC) 34

  37. Strongly Connected Component (SCC) 35

  38. Tarjan’s Algorithm - UBC Code Archive int low[N],vis[N],scomp[N],scompNum,I; 1 vector<int> adj[N]; stack<int> verts; 2 void scc(int u) { 3 low[u] = vis[u] = ++I; verts.push(u); 4 for (int v : adj[u]) { 5 if (!vis[v]) scc(v); 6 if (scomp[v] == -1) low[u] = min(low[u], low[v]); 7 } 8 if (vis[u] <= low[u]) { int v; 9 do { v=verts.top(); verts.pop(); scomp[v]=scompNum; } while (v != u); 10 ++scompNum; 11 } 12 } 13 void get_scc(int n) { memset(vis,0,sizeof vis); memset(scomp,-1,sizeof scomp); 14 scompNum=I=0; for (int i=0; i<n; ++i) if (!vis[i]) scc(i); } 15 36

  39. Boolean Satisfiability Problem (SAT) Two key elements are involved: • Boolean variables • x 0 , x 2 , ... , x N − 1 may be true or false • Boolean clauses • Logical expressions using those variables, or their negations (called a ”literal”) • Examples: ( x 1 ∨ ¬ x 2 ∨ x 3 ), ( ¬ x 4 ∨ x 5 ∨ ¬ x 0 ) 37

  40. Boolean Satisfiability Problem (SAT) Problem Input : Several boolean clauses Problem Output : One truth assignment of the variables that make all boolean clauses true (or say it is not possible) This problem is in NP-Complete 38

  41. 2-SAT 2-SAT is the special case of SAT in which every boolean expression has exactly two variables Problem Input : Several boolean expressions with exactly two variables Problem Output : One truth assignment of the variables that make all boolean expressions true (or say it is not possible) This can be solved in polynomial time. 39

  42. 2-SAT – Building a Graph Let’s build a graph out of the problem 40

  43. 2-SAT – Building a Graph Let’s build a graph out of the problem • The variables will be duplicated • There will be a node for each variable x i and a node for each negation ¬ x i 40

  44. 2-SAT – Building a Graph Let’s build a graph out of the problem • The variables will be duplicated • There will be a node for each variable x i and a node for each negation ¬ x i • For each clause ( a ∨ b ): • Notice that ( a ∨ b ) ≡ ( ¬ a → b ) ∧ ( ¬ b → a ) • Convert each original clause into those two implications 40

  45. 2-SAT – Building a Graph Let’s build a graph out of the problem • The variables will be duplicated • There will be a node for each variable x i and a node for each negation ¬ x i • For each clause ( a ∨ b ): • Notice that ( a ∨ b ) ≡ ( ¬ a → b ) ∧ ( ¬ b → a ) • Convert each original clause into those two implications • For each implication ( u → v ), make a directed edge from u to v : • Idea: “if u is true, v must also be true” 40

  46. 2-SAT – Graph Example Clauses: ( x 0 ∨ x 2 ) ∧ ( x 0 ∨ ¬ x 3 ) ∧ ( x 1 ∨¬ x 3 ) ∧ ( x 1 ∨¬ x 4 ) ∧ ( x 2 ∨¬ x 4 ) ∧ ( x 0 ∨¬ x 5 ) ∧ ( x 1 ∨¬ x 5 ) ∧ ( x 2 ∨¬ x 5 ) ∧ ( x 3 ∨ x 6 ) ∧ ( x 4 ∨ x 6 ) ∧ ( x 5 ∨ x 6 ) (image from https://en.wikipedia.org/wiki/2-satisfiability) 41

  47. 2-SAT – Solution Solution: Simply find the SCCs in this graph. 42

  48. 2-SAT – Solution Solution: Simply find the SCCs in this graph. • There is a solution if, and only if, there is no x i such that x i and ¬ x i are in the same SCC. Why? 42

  49. 2-SAT – Solution Solution: Simply find the SCCs in this graph. • There is a solution if, and only if, there is no x i such that x i and ¬ x i are in the same SCC. Why? • If both x i and ¬ x i are both in the same SCC, then choosing either leads to a contradiction. 42

  50. 2-SAT – Solution Solution: Simply find the SCCs in this graph. • There is a solution if, and only if, there is no x i such that x i and ¬ x i are in the same SCC. Why? • If both x i and ¬ x i are both in the same SCC, then choosing either leads to a contradiction. • If there is a solution, we can find a satisfying assignment by looking at the component graph. • If x i = ⇒ ¬ x i , then set x i to be false. Otherwise, set x i to be true. • x i = ⇒ ¬ x i if and only if x i is in a higher order SCC. 42

  51. 2-SAT Implementation – UBC Code Archive bool truth[N/2]; // N must be at least 2 times the number of variables 1 void add_clause(int a, int b) { 2 adj[a^1].push_back(b); adj[b^1].push_back(a); 3 } 4 bool two_sat(int n) { 5 get_scc(n); 6 for (int i = 0; i < n; i += 2) { 7 if (scomp[i] == scomp[i^1]) return false; 8 truth[i/2] = (scomp[i] < scomp[i^1]); 9 } 10 return true; 11 } 12 // usage: for (int i = 0; i < 2*num_vars; ++i) adj[i].clear(); 13 // add_clause(2*x, 2*y^1); // example for x || !y 14 // if (two_sat(2*num_vars)) // satisfiable, truth[x] = assign. for x 15 43 // else // no satisfying assignment exists 16

  52. Discussion Problem: So Much Hate Jack and Lucca had a falling out over bagels and donuts and want to live as far as possible from each other. The city they live in has the shape of a tree with N ≤ 10 6 , with neighbourhoods being the nodes. Find two neighbourhoods that are as far from each other as possible. 44

  53. Discussion Problem: So Much Hate – Insight • Run a DFS from any random node, and find the node X that is furthest away from it. • Run a DFS from node X and find the node Y that is furthest away from it. • X − Y is the diameter of the tree. 45

  54. End of Graphs This is the end of the unit on Graphs. You should now be able to finish all of A1. 46

  55. Dynamic Programming 46

  56. Remember Fibonacci? Let’s revisit the classic Fibonacci function: • F 1 = 1 • F 2 = 1 • F n = F n − 1 + F n − 2 , ∀ n ≥ 3 47

  57. Remember Fibonacci? Let’s revisit the classic Fibonacci function: • F 1 = 1 • F 2 = 1 • F n = F n − 1 + F n − 2 , ∀ n ≥ 3 How would you compute this? 47

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend