Ch 3 Brute Force and Exhaustive Search Flashcards

1
Q

What is Brute Force Search? (also provide examples)

A

A straightforward approach, usually based directly on the problems statement and definitions of the concepts involved.

exs:
- computing n!
- multiplying 2 matrices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 2 important properties you must consider with sorting algorithms?

A
  • 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:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Loop Invariant?

A

A Loop Invariant is an assertion that we prove to be true each time a loop iteration starts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to prove the correctness of a loop invariant?

A

There are three steps to prove the correctness of a loop invariant:

  1. Initialization: The invariant is true prior to the first iteration of the loop
  2. Maintenance: if an invariant is true before an iteration of the loop, then it remains true before the next iteration.
  3. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Prove the correctness of Selection sort using loop invariants

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write pseudocode for the brute force string match algorithm. Also, whats the time compleixty?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Whats the space and time complexity for adjacency lists?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Whats the space and time complexity for adjacency matrices

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give pseudocode for the depth first search algorithm? Whats the time complexity?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give pseudocode for the breadth first search algorhtm? whats the time complexity?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Whats the time complexity for DFS if the graph is in adjacency list representation? What about for adjacency matrices?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Whats the time complexity for BFS if the graph is in adjacency list representation? What about for adjacency matrices?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly