4 sides of notes allowed + APPENDIX A.
Exam emphases
Topics from previous topic guides plus:
Intree data structure
Seeing the graph and algorithm underneath a problem description.
Floyd, Warshall, and Bellman-Ford algorithms and when to use them
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 that?
Find an O(n) solution.
For a set of symbols x1, x2, x3, ... xn, you are given some equality constraints of the form xi= xj, and some inequality constraints, xi≠ xj. Is it possible to satisfy all of them with integer values? For instance, the constraints: x1= x2, x2= x3, x3= x4, x1≠ x4, cannot be satisfied. Suppose the number of constraints is m. Clearly explain and give pseudo-code for an O(n + m) algorithm to determine if such a set of constraints can be satisfied. 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. The question is about the ability to find consistent 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).
An O(n) solution is a greedy process initially: Each question eliminates one person: If A knows B, A is eliminated as celebrity. If A does not know B, B is eliminated as a celebrity. If you keep asking questions, and never involve someone already determined not to be the celebrity, then it takes n-1 questions to eliminate all but one candidate. Then if that person knows nobody (max n-1 more questions) and everyone knows that person (max n-1 more questions) then the person is a celebrity. This determination takes no more than 3(n-1) questions, definitely 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. All variables in the same component must have the same value. Then check each inequality to make sure that the variables in the pair are not in the same component. 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)