4 sides of notes allowed.
Exam emphases
Topics from previous topic guides plus:
Seeing the data structure and algorithm underneath a problem description.
Dynamic Programming:
Unlike HW, where I often asked you to do all parts, I'm likely to ask you to just get a recursive solution or be given one version of a solution and modify it into another version.
P-NP, Decision problems
Problems
Unlike the party problem, assume “knowing is not symmetric for this celebrity problem: Given n people, a celebrity is one who knows nobody else but everybody else knows the celebrity. To determine if there is a celebrity in a group of n people, you can ask questions only of the form, “Does A know B?” Suppose I say I can come up with an algorithm involving O(n) such questions to determine if and who is a celebrity within the group. Is it possible to do better than O(n)?
For a set of symbols x1, x2, x3, ... xn, you are given a combination of m constraints: some equality constraints of the form xi= xj, and some inequality constraints, xi≠ xj. Clearly explain and give pseudo-code for an O(n+m) algorithm to satisfy all of them with integer values, or state it is impossible. For instance, the constraints: x1= x2, x2= x3, x3= x4, x1≠ x4, cannot be satisfied. The constraints x1= x2, x1= x3, x1≠ x4, can be satisfied with 1, 1, 1, 2. Explain why your algorithm is O(n + m). Note: You are given only the symbols and relations. You are NOT given the values for the symbols.
Hint: You cannot treat the = and ≠ relations in a similar way. Where is the better place to start?
Answers
follow
below
...
Solutions
If you ask fewer than n/2 questions, you get no information about at least one member. That person may or may not know any individual and may or may not be know by any individual, allowing for or disallowing anyone as celebrity. Hence there must be at least O(n) questions. You cannot have a better order than the one proposed.
You can make a finer argument for more than n/2, but the short argument above is sufficient to get O(n).
N distinct objects with relations in pairs - sure looks like a something to state as a graph. How to deal with equalities vs inequalities? The situation is not symmetric: Equalities give an equivalence relation; inequalities do not, so try just using the equalities initially. Because of the equivalence relation with equality, any two vertices connected by a path must be equal, so there cannot be an inequality between them. How to check all this efficiently? Run the component numbering variation of the DFS once. Let all variables then get as value the number of their component (cc[i] below). This is the answer if all the inequalities are satisfied. Otherwise there is no solution. Details in pseudo-code:
Create empty lists neighbors[i] i = 1...n #O(n)
for each equality constraint xi= xj: #O(m), yielding no more than m edges
Add i to neighbors[j] and j to neighbors[i]
Run the component numbering algorithm, generating an array cc,
where cc[i] is the component number for xi. #O(n+m)
for each inequality constraints, xi≠ xj: #O(m)
if cc[i] == cc[j]:
return False
return True #The values in cc are a solution #total time still O(n+m)