Академический Документы
Профессиональный Документы
Культура Документы
2
Example problem
3
Problem solving paradigms
4
Complete search
Complete search
• Simple! Just go through all elements in the set, and for each of
them check if they satisify the constraints
• Of course it’s not going to be very efficient...
• But remember, we always want the simplest solution that runs in
time
• Complete search should be the first problem solving paradigm you
think about when you’re trying to solve a problem
6
Example problem: Closest Sums
• https://open.kattis.com/problems/closestsums
7
Complete search
8
Iterating through permutations
int n = 5;
vector<int> perm(n);
for (int i = 0; i < n; i++) perm[i] = i + 1;
do {
for (int i = 0; i < n; i++) {
printf("%d ", perm[i]);
}
printf("\n");
9
Iterating through permutations
10
Iterating through subsets
int n = 5;
for (int subset = 0; subset < (1 << n); subset++) {
for (int i = 0; i < n; i++) {
if ((subset & (1 << i)) != 0) {
printf("%d ", i+1);
}
}
printf("\n");
}
11
Iterating through subsets
• Similar in Python
12
Backtracking
• Backtracking!
13
Backtracking
• Define states
• We have one initial “empty” state
• Some states are partial
• Some states are complete
• Basic idea:
1. Start with the empty state
2. Use recursion to traverse all states by going through the transitions
3. If the current state is invalid, then stop exploring this branch
4. Process all complete states (these are the states we’re looking for)
14
Backtracking
void generate() {
if (!is_valid(S))
return;
if (is_complete(S))
print(S);
S = empty state;
generate();
15
Generating all subsets
generate(0);
16
Generating all permutations
generate(at + 1);
memset(used, 0, n);
generate(0);
17
n queens
18
n queens
const int n = 8;
bool has_queen[n][n];
int queens_left = n;
// generate function
memset(has_queen, 0, sizeof(has_queen));
generate(0, 0);
19
n queens
generate(x, y+1);
20
Example problem: Lucky Numbers
• https://open.kattis.com/problems/luckynumber
21
Divide and conquer
Divide and conquer
23
Divide and conquer: Time complexity
void solve(int n) {
if (n == 0)
return;
solve(n/2);
solve(n/2);
24
Divide and conquer: Time complexity
• You don’t need to know the Master theorem for this course, but still
recommended as it’s very useful
25
Decrease and conquer
26
Binary search
• Algorithm:
1. Base case: the array is empty, return false
2. Compare x to the element in the middle of the array
3. If it’s equal, then we found x and we return true
4. If it’s less, then x must be in the left half of the array
4.1 Binary search the element (recursively) in the left half
5. If it’s greater, then x must be in the right half of the array
5.1 Binary search the element (recursively) in the right half
27
Binary search
• T (n) = T (n/2) + 1
• O(log n)
28
Binary search - iterative
return false;
}
29
Binary search over integers
• This might be the most well known application of binary search, but
it’s far from being the only application
• More generally, we have a predicate p : {0, . . . , n − 1} → {T , F }
which has the property that if p(i) = T , then p(j) = T for all j > i
• Our goal is to find the smallest index j such that p(j) = T as
quickly as possible
30
Binary search over integers
int lo = 0,
hi = n - 1;
if (p(m)) {
hi = m;
} else {
lo = m + 1;
}
}
31
Binary search over integers
bool p(int i) {
return arr[i] >= x;
}
32
Binary search over reals
• Since we’re working with real numbers (hypothetically), our [lo, hi]
can be halved infinitely many times without ever becoming a single
real number
• Instead it will suffice to find a real number j 0 that is very close to
the correct answer j, say not further than EPS = 2−30 away
33
Binary search over reals
if (p(mid)) {
hi = mid;
} else {
lo = mid;
}
}
printf("%0.10lf\n", lo);
34
Binary search over reals
bool p(double j) {
return j*j >= x;
}
bool p(double x) {
return f(x) >= 0.0;
}
35
Example problem
36
Binary search the answer
37
Other types of divide and conquer
38
Binary exponentiation
return res;
}
39
Binary exponentiation
40
Binary exponentiation
41
Binary exponentiation
41
Binary exponentiation
41
Binary exponentiation
41
Binary exponentiation
42
Binary exponentiation
42
Binary exponentiation
42
Binary exponentiation
43
Fibonacci words
44
Fibonacci words
• Let’s try starting with a pair of strings, and let + denote string
concatenation:
• g1 = A
• g2 = B
• gn = gn−2 + gn−1
45
Fibonacci words
• How long is gn ?
• len(g1 ) = 1
• len(g2 ) = 1
• len(gn ) = len(gn−2 ) + len(gn−1 )
• Looks familiar?
• len(gn ) = fibn
• https://open.kattis.com/problems/batmanacci
47
Fibonacci words
48
Fibonacci words
48
Fibonacci words
48