EECS 281+376 Algorithms
EECS 281+376 Algorithms
Potential Method
The potential method is a technique used in amortized analysis to analyze the cost of a sequence of operations. It involves assigning a "potential" to each state of the data structure, and then using this potential to bound the total cost of the operations.
- Define a potential function, upper bound, and initial potential.
- Show that the potential function strictly decreases after each operation.
- Use the potential function to bound the amortized cost of each operation.
Analyzing Euclid's Algorithm
Euclid(x, y)
- Unit of time = one recursive call.
- Define potential function .
- Initial potential is .
- After each recursive call, the potential decreases, .
- Claim: the number of recursive calls is bounded by .
Greedy Algorithms
Greedy Core Property
- A problem exhibits the greedy choice property if a globally optimal solution can be arrived at by making a locally optimal choice at each step.
- A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to its subproblems.
Exchange Argument
Core idea: we can transform any optimal solution into a solution given by our greedy algorithm by exchanging elements, without losing optimality.
Master Theorem
Then:
Recursion
| Recurrence | Example | Big-O Solution |
|---|---|---|
| Binary Search | ||
| Linear Search | ||
| Tree Traversal | ||
| Selection/etc. Sorts | ||
| Merge/Quick Sorts |
2D Table Search
Improved Binary Partition Search
- On the middle column, perform a binary search to find the row that contains the target value.
- If the target value is found, return true.
- If the target value is not found, recursively search the bottom-left and top-right submatrices.
Stepwise Linear Search
time complexity
- Start at the top-right corner of the table.
- If the current value is equal to the target, return true.
- If the current value is greater than the target, move left.
- If the current value is less than the target, move down.
Majority Element
A majority element in an array is an element that appears more than times, where is the size of the array.
Boyer-Moore Voting Algorithm
time, space
- Initialize a candidate element and a count.
- Iterate through the array:
- If the count is zero, set the current element as the candidate and set count to 1.
- If the current element is equal to the candidate, increment the count.
- If the current element is not equal to the candidate, decrement the count.
- After the iteration, the candidate is the majority element if it exists. (Iterate again to confirm if the candidate is indeed the majority element.)
int findMajorityElement(vector<int>& nums) {
int candidate = 0, count = 0;
for (int num : nums) {
if (count == 0) candidate = num, count = 1;
else if (num == candidate) count++;
else count--;
}
// Verify if the candidate is indeed the majority element
count = 0;
for (int num : nums)
if (num == candidate)
count++;
return (count > nums.size() / 2) ? candidate : -1; // Return -1 if no majority element exists
}
Divide and Conquer Approach
time, space
- Divide the array into two halves.
- Recursively find the majority element in each half.
- If both halves return the same majority element, that is the majority element for the entire array.
- If they return different elements, count the occurrences of each in the entire array and return the one that appears more than n/2 times, if any.
Misra-Gries Algorithm
To find elements that appear more than times in an array.
time, space
- Initialize a map to store up to k-1 candidate elements and their counts.
- Iterate through the array:
- If the current element is in the map, increment its count.
- If the current element is not in the map and the map has less than k-1 elements, add the current element to the map with a count of 1.
- If the current element is not in the map and the map has k-1 elements, decrement the count of each element in the map. Remove any elements whose count drops to zero.
- After processing the array, the map contains potential candidates. Verify each candidate by counting its occurrences in the original array.
Automata and Finite State Machines
- Alphabet: A finite set of symbols.
- String: A finite sequence of symbols from the alphabet.
- Empty string: .
- : repetitions of symbol .
- : Zero or more repetitions of symbol .
- : One or more repetitions of symbol .
- Language: A set of strings over an alphabet.
- Automaton: A mathematical model of computation that processes input strings and transitions between states.
Decision Problems
A decision problem is a problem that can be answered with a yes or no.
Representation: , .
Deterministic Finite Automaton (DFA)
Definition: A DFA is a 5-tuple where:
- is a finite input alphabet.
- is a finite set of states.
- is the transition function.
- is the initial state.
- is the set of accepting states.
A DFA of a language satisfies the following properties:
- For every string , the DFA ends in an accepting state after processing .
- For every string , the DFA ends in a non-accepting state after processing .
Fact: If there is a DFA that accepts a language , then there is a DFA that accepts the complement of .
Turing Machines
Definition: A Turing machine is a 7-tuple where:
- is a finite input alphabet.
- is a finite tape alphabet, where and is the blank symbol.
- is a finite set of states.
- is the initial state.
- is the accepting state.
- is the rejecting state.
- is the transition function.
The language of a TM is .
A Turing Machine recognizes a language if .
- accepts all strings in
- rejects or loops on all strings not in .
A Turing Machine decides a language if it recognizes and halts on all inputs.
- accepts all strings in
- rejects all strings not in .
A language is decidable if there exists a Turing Machine that decides it. Otherwise, it is undecidable.
Countability and Uncountability
- A set is countable if its elements can be put into a one-to-one correspondence with the natural numbers.
- A set is uncountable if it is not countable.
Diagonalization.
- There are countably many Turing Machines, so there are countably many decidable languages.
- There are uncountably many languages.
- Therefore, there exist undecidable languages.