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

cpsc 490 graphs dp
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

Announcements

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

1

slide-3
SLIDE 3

Warm-Up: Cycle Detection in an Undirected Graph

We can just run a normal DFS

1

DFS(s):

2

mark s as visited

3

for each edge e = (s, t) :

4

if t is not visited , call DFS(t):

5

if found cycle: output HAS_CYCLE

6

if t is visited and t is not the parent of s,

7

  • utput HAS_CYCLE

8

  • utput NO_CYCLE

2

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 6

Black Grey White DFS

1

initialize the color of all nodes as WHITE

2

DFS(s):

3

mark s as GREY

4

for each edge e = (s,t):

5

if t is WHITE , call DFS(t)

6

if t is GREY , output HAS_CYCLE

7

if t is BLACK , do nothing

8

mark s as BLACK

9

  • utput NO_CYCLE

(This is an example of an efficient backtracking algorithm!)

4

slide-7
SLIDE 7

Black Grey White DFS

5

slide-8
SLIDE 8

Black Grey White DFS

6

slide-9
SLIDE 9

Black Grey White DFS

7

slide-10
SLIDE 10

Black Grey White DFS

8

slide-11
SLIDE 11

Black Grey White DFS

9

slide-12
SLIDE 12

Black Grey White DFS

10

slide-13
SLIDE 13

Black Grey White DFS

11

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 16

Strongly Connected Component (SCC)

14

slide-17
SLIDE 17

Strongly Connected Component (SCC)

15

slide-18
SLIDE 18

Strongly Connected Component (SCC)

16

slide-19
SLIDE 19

Strongly Connected Component (SCC)

17

slide-20
SLIDE 20

Strongly Connected Component (SCC)

18

slide-21
SLIDE 21

Strongly Connected Component (SCC)

19

slide-22
SLIDE 22

Strongly Connected Component (SCC)

20

slide-23
SLIDE 23

Strongly Connected Component (SCC)

21

slide-24
SLIDE 24

Strongly Connected Component (SCC)

22

slide-25
SLIDE 25

Strongly Connected Component (SCC)

23

slide-26
SLIDE 26

Strongly Connected Component (SCC)

24

slide-27
SLIDE 27

Strongly Connected Component (SCC)

25

slide-28
SLIDE 28

Strongly Connected Component (SCC)

26

slide-29
SLIDE 29

Strongly Connected Component (SCC)

27

slide-30
SLIDE 30

Strongly Connected Component (SCC)

28

slide-31
SLIDE 31

Strongly Connected Component (SCC)

29

slide-32
SLIDE 32

Strongly Connected Component (SCC)

30

slide-33
SLIDE 33

Strongly Connected Component (SCC)

31

slide-34
SLIDE 34

Strongly Connected Component (SCC)

32

slide-35
SLIDE 35

Strongly Connected Component (SCC)

33

slide-36
SLIDE 36

Strongly Connected Component (SCC)

34

slide-37
SLIDE 37

Strongly Connected Component (SCC)

35

slide-38
SLIDE 38

Tarjan’s Algorithm - UBC Code Archive

1

int low[N],vis[N],scomp[N],scompNum,I;

2

vector<int> adj[N]; stack<int> verts;

3

void scc(int u) {

4

low[u] = vis[u] = ++I; verts.push(u);

5

for (int v : adj[u]) {

6

if (!vis[v]) scc(v);

7

if (scomp[v] == -1) low[u] = min(low[u], low[v]);

8

}

9

if (vis[u] <= low[u]) { int v;

10

do { v=verts.top(); verts.pop(); scomp[v]=scompNum; } while (v != u);

11

++scompNum;

12

}

13

}

14

void get_scc(int n) { memset(vis,0,sizeof vis); memset(scomp,-1,sizeof scomp);

15

scompNum=I=0; for (int i=0; i<n; ++i) if (!vis[i]) scc(i); }

36

slide-39
SLIDE 39

Boolean Satisfiability Problem (SAT)

Two key elements are involved:

  • Boolean variables
  • x0, x2, ..., xN−1 may be true or false
  • Boolean clauses
  • Logical expressions using those variables, or their negations (called a ”literal”)
  • Examples: (x1 ∨ ¬x2 ∨ x3), (¬x4 ∨ x5 ∨ ¬x0)

37

slide-40
SLIDE 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

slide-41
SLIDE 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

slide-42
SLIDE 42

2-SAT – Building a Graph

Let’s build a graph out of the problem

40

slide-43
SLIDE 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 xi and a node for each negation ¬xi

40

slide-44
SLIDE 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 xi and a node for each negation ¬xi
  • For each clause (a ∨ b):
  • Notice that (a ∨ b) ≡ (¬a → b) ∧ (¬b → a)
  • Convert each original clause into those two implications

40

slide-45
SLIDE 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 xi and a node for each negation ¬xi
  • 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

slide-46
SLIDE 46

2-SAT – Graph Example

Clauses: (x0 ∨ x2) ∧ (x0 ∨ ¬x3) ∧ (x1 ∨¬x3)∧(x1 ∨¬x4)∧ (x2 ∨¬x4)∧(x0 ∨¬x5)∧ (x1 ∨¬x5)∧(x2 ∨¬x5)∧ (x3 ∨ x6) ∧ (x4 ∨ x6) ∧ (x5 ∨ x6) (image from https://en.wikipedia.org/wiki/2-satisfiability)

41

slide-47
SLIDE 47

2-SAT – Solution

Solution: Simply find the SCCs in this graph.

42

slide-48
SLIDE 48

2-SAT – Solution

Solution: Simply find the SCCs in this graph.

  • There is a solution if, and only if, there is no xi such that xi and ¬xi are in the same SCC.

Why?

42

slide-49
SLIDE 49

2-SAT – Solution

Solution: Simply find the SCCs in this graph.

  • There is a solution if, and only if, there is no xi such that xi and ¬xi are in the same SCC.

Why?

  • If both xi and ¬xi are both in the same SCC, then choosing either leads to a contradiction.

42

slide-50
SLIDE 50

2-SAT – Solution

Solution: Simply find the SCCs in this graph.

  • There is a solution if, and only if, there is no xi such that xi and ¬xi are in the same SCC.

Why?

  • If both xi and ¬xi 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 xi =

⇒ ¬xi, then set xi to be false. Otherwise, set xi to be true.

  • xi =

⇒ ¬xi if and only if xi is in a higher order SCC.

42

slide-51
SLIDE 51

2-SAT Implementation – UBC Code Archive

1

bool truth[N/2]; // N must be at least 2 times the number of variables

2

void add_clause(int a, int b) {

3

adj[a^1].push_back(b); adj[b^1].push_back(a);

4

}

5

bool two_sat(int n) {

6

get_scc(n);

7

for (int i = 0; i < n; i += 2) {

8

if (scomp[i] == scomp[i^1]) return false;

9

truth[i/2] = (scomp[i] < scomp[i^1]);

10

}

11

return true;

12

}

13

// usage: for (int i = 0; i < 2*num_vars; ++i) adj[i].clear();

14

// add_clause(2*x, 2*y^1); // example for x || !y

15

// if (two_sat(2*num_vars)) // satisfiable, truth[x] = assign. for x

16

// else // no satisfying assignment exists

43

slide-52
SLIDE 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 ≤ 106, with neighbourhoods being the nodes. Find two neighbourhoods that are as far from each other as possible.

44

slide-53
SLIDE 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

slide-54
SLIDE 54

End of Graphs

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

46

slide-55
SLIDE 55

Dynamic Programming

46

slide-56
SLIDE 56

Remember Fibonacci?

Let’s revisit the classic Fibonacci function:

  • F1 = 1
  • F2 = 1
  • Fn = Fn−1 + Fn−2, ∀n ≥ 3

47

slide-57
SLIDE 57

Remember Fibonacci?

Let’s revisit the classic Fibonacci function:

  • F1 = 1
  • F2 = 1
  • Fn = Fn−1 + Fn−2, ∀n ≥ 3

How would you compute this?

47

slide-58
SLIDE 58

Intuitive Implementation

1

fun fib(n):

2

if n == 1:

3

return 1

4

if n == 2:

5

return 1

6

return fib(n-1) + fib(n-2)

48

slide-59
SLIDE 59

How Many Operations?

How many calculations do we make to calculate Fn?

  • F1 and F2 are both immediate, so they take 1 operation
  • For each element after, it will recurse to the two previous elements. The recursion tree will

grow the same way the function does. Roughly, to calculate Fn, we are making O(Fn) operations.

49

slide-60
SLIDE 60

A Computation Tree

F50 F49 F48 F47 F47 F47 F46 F46 F48

50

slide-61
SLIDE 61

Insight

Say we want to calculate F50. Then, F50 will require us to recurse and calculate the values of F48 and F49. Assume we just finished calculating F48. Now, we need to calculate F49. This will recurse and calculate the values of F47 and F48 (again!). Has the value of F48 changed between one iteration and the other?

51

slide-62
SLIDE 62

A Computation Tree (Again)

F50 F49 F48 F47 F47 F47 F46 F46 F48

52

slide-63
SLIDE 63

Memoization Insight

If the value of F48 hasn’t changed, then why did we bother recalculating it? Instead of wasting time recalculating values, we can write all the values we’ve calculated in a memo.

  • After calculating a value for the first time, we add it to the memo.
  • When we need to know a value (e.g. ”how much is F48?”), we simply read from that

memo.

53

slide-64
SLIDE 64

Fibonacci Memoized

1

memo: hashmap or array from X -> fib(X); Initially empty

2 3

fun fib(n):

4

if n == 1:

5

return 1

6

if n == 2:

7

return 1

8

if n in memo:

9

return memo[n]

10 11

memo[n] = fib(n-1) + fib(n-2)

12

return memo[n]

54

slide-65
SLIDE 65

Fibonacci Memoized

1

memo: hashmap or array from X -> fib(X); Initially empty

2 3

fun fib(n):

4

if n == 1:

5

return 1

6

if n == 2:

7

return 1

8

if n in memo:

9

return memo[n]

10 11

memo[n] = fib(n-1) + fib(n-2)

12

return memo[n] Note: For this problem, we could also use an array instead of a hashmap.

54

slide-66
SLIDE 66

How Many Operations?

Each value for Fk will only be calculated once (afterwards, it will be added to the memo). The value of Fn depends on all the elements in the sequence that come before it. Hence, to calculate Fn, we have a runtime of O(N).

55

slide-67
SLIDE 67

Memory x Time Trade-Off

The naive implementation costs constant memory and exponential time. The memoized implementation costs linear memory and linear time. In theory, we gave up some memory in order to save huge amounts of time.

56