Ch 3 Brute Force and Exhaustive Search Flashcards
What is Brute Force Search? (also provide examples)
A straightforward approach, usually based directly on the problems statement and definitions of the concepts involved.
exs:
- computing n!
- multiplying 2 matrices
What are the 2 important properties you must consider with sorting algorithms?
- stability: the relative order of two equal elements is preserved
ex: - in place: it does not use extra memory in proportion to the input array size
ex:
What is a Loop Invariant?
A Loop Invariant is an assertion that we prove to be true each time a loop iteration starts.
How to prove the correctness of a loop invariant?
There are three steps to prove the correctness of a loop invariant:
- Initialization: The invariant is true prior to the first iteration of the loop
- Maintenance: if an invariant is true before an iteration of the loop, then it remains true before the next iteration.
- When the loop terminates, the loop invariant along with the reason the loop terminated is used to derive a conclusion about the state of your algorithm.
Prove the correctness of Selection sort using loop invariants
Write pseudocode for the brute force string match algorithm. Also, whats the time compleixty?
for (int i = 0; i < n-m; i++) {
j = 0;
while ((j < m) and (P[j] = T[i+j])){
j = j+1;
}
if (j == m)
return i
return -1
write pseudocode to find the two closest points in a set of n points (in
the two-dimensional Cartesian plane. Also, whats the time complexity?
Given n cities with known distances between each pair, find
the shortest tour that passes through all the cities exactly
once before returning to the starting city
Whats the space and time complexity for adjacency lists?
Whats the space and time complexity for adjacency matrices
Give pseudocode for the depth first search algorithm? Whats the time complexity?
Give pseudocode for the breadth first search algorhtm? whats the time complexity?
Whats the time complexity for DFS if the graph is in adjacency list representation? What about for adjacency matrices?
Whats the time complexity for BFS if the graph is in adjacency list representation? What about for adjacency matrices?