Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3160 Design and Analysis of Algorithms Tutorial 4 Chengyu Lin.

Similar presentations


Presentation on theme: "CSCI 3160 Design and Analysis of Algorithms Tutorial 4 Chengyu Lin."— Presentation transcript:

1 CSCI 3160 Design and Analysis of Algorithms Tutorial 4 Chengyu Lin

2 Outline More examples about 2-SAT More randomized algorithms – Verifying Matrix Multiplication – String Equality Test Design Patterns

3 2-SAT Given (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) (x 1, x 2, x 3, x 4, x 5 ) = (T, T, T, T, T) is a satisfying assignment. Suppose you do not know about this solution You do not even know if there exists a solution for this formula How to decide if there is one using randomness?

4 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Start with a random assignment, – say (x 1, x 2, x 3, x 4, x 5 ) = (F, T, F, F, T) 10 3245 Number of x i s that agrees with the solution (i.e. number of i such that x i = T)

5 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 4 ∨ x 3 ) flip one of the value of x 3 and x 4 randomly – If we flip x 3, then we jump from 2 to 3 – If we flip x 4, then we jump from 2 to 3 x1x1 x2x2 x3x3 x4x4 x5x5 currentFTFFT clauses

6 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 1 ∨ ¬x 2 ) flip one of the value of x 1 and x 2 randomly – If we flip x 1, then we jump from 3 to 4 – If we flip x 2, then we jump from 3 to 2 x1x1 x2x2 x3x3 x4x4 x5x5 currentFTTFT clauses

7 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 2 ∨ ¬x 3 ) flip one of the value of x 2 and x 3 randomly – If we flip x 2, then we jump from 2 to 3 – If we flip x 3, then we jump from 2 to 1 x1x1 x2x2 x3x3 x4x4 x5x5 currentFFTFT clauses

8 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 1 ∨ ¬x 2 ) flip one of the value of x 1 and x 2 randomly – If we flip x 1, then we jump from 3 to 4 – If we flip x 2, then we jump from 3 to 2 x1x1 x2x2 x3x3 x4x4 x5x5 currentFTTFT clauses

9 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 4 ∨ ¬x 5 ) flip one of the value of x 4 and x 5 randomly – If we flip x 4, then we jump from 4 to 5 – If we flip x 5, then we jump from 4 to 3 x1x1 x2x2 x3x3 x4x4 x5x5 currentTTTFT clauses

10 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: none We have a satisfying assignment! =) x1x1 x2x2 x3x3 x4x4 x5x5 currentTTTTT clauses

11 Analysis To do the analysis, assume we have a satisfying assignment in mind, call it solution – Of course, we do not know if a satisfying assignment exists when we run the algorithm – we do this for the analysis only We compare the current assignment with the solution And record the number of variables that are assigned to the same T/F value in both (current and solution) assignments

12 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 4 ∨ x 3 ) flip one of the value of x 3 and x 4 randomly – If we flip x 3, then we jump from 2 to 3 – If we flip x 4, then we jump from 2 to 3 10 3245 x1x1 x2x2 x3x3 x4x4 x5x5 SolutionTTTTT currentFTFFT clauses with prob. 1 Number of x i s that agrees with the solution (i.e. x i = T)

13 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 1 ∨ ¬x 2 ) flip one of the value of x 1 and x 2 randomly – If we flip x 1, then we jump from 3 to 4 – If we flip x 2, then we jump from 3 to 2 10 3245 x1x1 x2x2 x3x3 x4x4 x5x5 SolutionTTTTT currentFTTFT clauses with prob. 1/2 Number of x i s that agrees with the solution (i.e. x i = T)

14 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 2 ∨ ¬x 3 ) flip one of the value of x 2 and x 3 randomly – If we flip x 2, then we jump from 2 to 3 – If we flip x 3, then we jump from 2 to 1 10 3245 x1x1 x2x2 x3x3 x4x4 x5x5 SolutionTTTTT currentFFTFT clauses with prob. 1/2 Number of x i s that agrees with the solution (i.e. x i = T)

15 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 1 ∨ ¬x 2 ) flip one of the value of x 1 and x 2 randomly – If we flip x 1, then we jump from 3 to 4 – If we flip x 2, then we jump from 3 to 2 10 3245 x1x1 x2x2 x3x3 x4x4 x5x5 SolutionTTTTT currentFTTFT clauses with prob. 1/2 Number of x i s that agrees with the solution (i.e. x i = T)

16 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: (x 4 ∨ ¬x 5 ) flip one of the value of x 4 and x 5 randomly – If we flip x 4, then we jump from 4 to 5 – If we flip x 5, then we jump from 4 to 3 10 3245 x1x1 x2x2 x3x3 x4x4 x5x5 SolutionTTTTT currentTTTFT clauses with prob. 1/2 Number of x i s that agrees with the solution (i.e. x i = T)

17 2-SAT (x 1 ∨ ¬x 2 ) ∧ (x 2 ∨ ¬x 3 ) ∧ (x 4 ∨ x 3 ) ∧ (x 5 ∨ x 1 ) ∧ (x 4 ∨ ¬x 5 ) Find an unsatisfied clause: none We have a satisfying assignment! =) 10 3245 Number of x i s that agrees with the solution (i.e. x i = T) x1x1 x2x2 x3x3 x4x4 x5x5 SolutionTTTTT currentTTTTT clauses

18 2-SAT We always move to the right with probability at least 1/2 10 3245 with prob. 1 10 3245 with prob. 1/2 10 3245 10 3245 10 3245 10 3245

19 2-SAT We always move forward with probably at least 1/2 – a satisfying assignment must satisfy every clause When we find an unsatisfied clause, it must be bad The values of the literals in a satisfying assignment must be one of the good ones Hence, can always flip one to move to the right e.g. (x 1 ∨ ¬x 2 ) x1x1 x2x2 goodTF TT FF badFT

20 2-SAT We always move forward with probably at least 1/2 When we reach n (i.e. 5 in the example), we have a satisfying assignment. – Sufficient condition, not necessary. – There may be other satisfying assignments – But that will only improve the running time This is random walk on line and so we expect O(n 2 ) jumps to hit n (refer to lecture notes)

21 2-SAT Randomized algorithm: Repeat O(n 2 ) times, – if we hit 5 in the middle of these O(n 2 ) flips, return there is an assignment – otherwise, return there is NO satisfying assignment

22 Verifying Matrix Multiplication Given 3 n x n integer matrices A, B and C, verify if AB = C Naïve algorithm – Compute AB, check if it is equal to C – Runs in time O(n 3 ), dominated by computing AB How to do faster with high probability?

23 Verifying Matrix Multiplication Given 3 n x n integer matrices A, B and C, verify if AB = C Consider AB – C Check if AB – C is a zero matrix Lecture: how to check if two polynomials are identical? – Evaluate the polynomials at different points

24 Verifying Matrix Multiplication Given 3 n x n integer matrices A, B and C, verify if AB = C Now, multiply AB – C by different random vectors To avoid computing AB, we compute A(Bx) – Cx We have Working over modulo 2, If AB ≠ C, then Pr[(AB - C)x ≠ 0] ≥ 1/2

25 String equality Alice has an n-bit string (a 1, a 2, …, a n ) Bob has an n-bit string (b 1, b 2, …, b n ) Alice can communicate with Bob How to send as few bits as possible so that they know the two strings are equal?

26 String equality Alice has an n-bit string (a 1, a 2, …, a n ) Bob has an n-bit string (b 1, b 2, …, b n ) Represent – (a 1, a 2, …, a n ) by a 1 + a 2 x + … + a n x n-1 – (b 1, b 2, …, b n ) by b 1 + b 2 x + … + b n x n-1 Polynomial Identity Testing! This idea can be extended to pattern matching

27 Design Patterns Las Vegas algorithm – Always produces the correct answer – Usually only expected running time is guaranteed – Example: randomized quick sort

28 Design Patterns Monte Carlo algorithm – Running time is deterministic – Small probability of error – Usually repeats a simple procedure to get a large success probability – Example: min-cut

29 End Questions?


Download ppt "CSCI 3160 Design and Analysis of Algorithms Tutorial 4 Chengyu Lin."

Similar presentations


Ads by Google