Ch 3.1 brute force, exhaustive search, & selection sort Flashcards

1
Q

a straightforward approach to solving a problem, usually
directly based on the problem statement and definitions of the concepts
involved

A

brute force

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

consider the exponentiation problem: compute an for a
nonzero number a and a nonnegative integer n. Although this problem might
seem trivial, it provides a useful vehicle for illustrating several algorithm design
strategies, including the brute force. (Also note that computing an mod m for some
large integers is a principal component of a leading encryption algorithm.) By the
definition of exponentiation,
a^n = a* …* a (n times)

A

This suggests simply computing an by multiplying 1 by a n times

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

the consecutive integer checking algorithm for computing gcd(m, n) and the definition-based algorithm for matrix multiplication are examples of what algorithm?

A

brute force

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

it seems to be the only general approach for which it
is more difficult to point out problems it cannot tackle. Second, for some important problems—e.g., sorting, searching, matrix multiplication, string matching—
the brute-force approach yields reasonable algorithms of at least some practical value with no limitation on instance size. Third, the expense of designing a
more efficient algorithm may be unjustifiable if only a few instances of a problem need to be solved and a brute-force algorithm can solve those instances with
acceptable speed. Fourth, even if too inefficient in general, a brute-force algorithm can still be useful for solving small-size instances of a problem. Finally,
a brute-force algorithm can serve an important theoretical or educational purpose as a yardstick with which to judge more efficient alternatives for solving a
problem.

A

brute force

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

We start selection sort by scanning the entire given list to find its smallest element
and exchange it with the first element, putting the smallest element in its final
position in the sorted list. Then we scan the list, starting with the second element,
to find the smallest among the last n − 1 elements and exchange it with the second
element, putting the second smallest element in its final position

A

on the ith pass through the list, which we number from 0 to n − 2, the algorithm searches
for the smallest item among the last n − i elements and swaps it with Ai

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

Another brute-force application to the sorting problem is to compare adjacent
elements of the list and exchange them if they are out of order. By doing it
repeatedly,

A

we end up “bubbling up” the largest element to the last position on
the list. The next pass bubbles up the second largest element, and so on, until
after n − 1 passes the list is sorted.

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

brute force algorithms

A

Brute force algorithms are straightforward methods of solving a problem that try every possibility rather than advanced techniques to improve efficiency.

Directly do the problem without considering time and space performance

Advantages
Simple and general (?)
Iterating through all possible solutions. Show that no other solutions exist
Usually efficient for small input size

Disadvantages
Often takes to much time

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

Brute force algorithms

A

Sorting
Bubble sort
Selection sort

Searching
Sequential search
Exhaustive search

String matching

Geometric problems
Closest pair, Convex Hull

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

insertion sort

A

insertion_sort(A)
for j = 2 to length[A]
{
key = A[j]
// insert A[j] into the sorted sequence A[1..j-1]
i = j – 1
while i>0 and A[i] > key
{
A[i+1] = A[i]
i = i – 1
}// end while
A[i+1] = key
}//end for

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

bubble sort

A

Swap pairwise elements which are out of order, from beginning to end

In each round, at least one element is in rightful position

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

bubble sort algorithm

A

Data Structure: Array

Input: A[0..n-1]

for i=0 to n-1 do
for j=i to n-1 do
if A[j] > A[j+1]
swap A[j] and A[j+1]
end;
end;

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

bubble sort c++

A

for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
{
if (A[i] > A[j])
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}

20 45 90 8
8 45 90 20
8 20 90 45
8 20 45 90

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

optimized bubble sort c++

A

flag = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n-1; j++)
{
if (A[j] > A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1; // if swapping occurs
}
}
if (flag == 0)
break; // the input list already sorted, no need for the inner loop
}

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

Sequential Search Algorithm

A

Data Structure: Array
Input A[0..n−1]
Target key: K

i ← 0
while A[i]  K do
i = i + 1
end;
if (i<n)
“Found” at i
else
“not found“;

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

brute force string matching

A

Given a string of n characters called the text and a string of m characters (m  n) called the pattern, find a substring of the text that matches the pattern.

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

Algorithm BruteForceStringMatch(T[0..n-1], P[0..m-1])

A

// Implements brute-force string matching
// Input: An array T[0..n-1] of n characters representing a text and
// an array P[0..m-1] of m characters representing a pattern
// Output: The index of the first character in the text that
// starts a matching substring or -1 if the search
// is unsuccessful
for i = 0 to n-m do
j=0
while j<m and P[j] = T[i+j] do
j = j+1
if j = m return i
return -1;

16
Q

closest=pair and convex hull problems by brute force

A

In computational geometry, two well-known problems are to find the closest pair of points and the convex hull of a set of points.

17
Q

The closest-pair problem, in 2D space, is to find the closest pair of points given a set of n points. Given a list P of n points, P1=(x1,y1), …, Pn=(xn,yn), find distance between two closest points in the plane

A

BruteForceClosest(P)
min = ∞;
for i = 1 to n-1
for j = i+1 to n do
d ← distance(Pi ,Pj) // Use sqrt(distances squared)
if d < min then
min = d;
return min;

18
Q

Convex-Hall problem

A

A set of points (finite or infinite) in the plane is called convex if for any two points p and q in the set, the entire line segment with the endpoints at p and q belongs to the set.

It is a rubber band wrapped around the “outside” points.

The convex hull of a set S of points is the smallest convex set containing S. (The “smallest” requirement means that the convex hull of S must be a subset of any convex set containing S.)

19
Q

Convex-Hall Problem Theorem

A

The convex hull of any set S of n > 2 points not all on the same line is a convex polygon with the vertices at some of the points of S. (If all the points do lie on the same line, the polygon degenerates to a line segment but still with the endpoints at two points of S.)

20
Q

How could you write a brute-force algorithm to find the convex hull?

A

Compute a line segment (P1, P2) where P1 and P2 are on the convex hull’s boundary.
Check if and only if all the other points in the set lie on the same side of the line segment. For all points above the line, ax + by > c, while for all points below the line, ax + by < c. Using these formulas, we can determine if two points are on the boundary to the convex hull.

21
Q

graph coloring problem

A

Given a graphGand a number of colors, find an assignment of colors to the vertices ofGsuch that two vertices that are connected by an edge are never assigned the same color

22
Q

vertex coloring

A

Given m colors, find a way of coloring the vertices of a graph such that no two adjacent vertices are colored using same color. The other graph coloring problems likeEdge Coloring(No vertex is incident to two edges of same color) andFace Coloring(Geographical Map Coloring) can be transformed into vertex coloring.

23
Q

chromatic number

A

The smallest number of colors needed to color the vertices of a graph G so that no two adjacent vertices share the same color is called its chromatic number.

24
Q

The graph coloring problem has huge number of applications.

A

Scheduling or Time Tabling
Frequency Assignment
Sudoku
Register Allocation
Bipartite Graphs
Map Coloring

25
Q

Color the map so that no two neighboring regions are colored the same

A

All possible solutions

m-Coloring Decision problem
# of colors given, is it feasible?
Is it feasible m coloring decision problem

m-Coloring optimization problem
Colors not given, what is minimum number of colors

26
Q

combinatorial problems

A

involve finding a grouping, ordering, or assignment of a discrete, finite set of objects that satisfies given conditions.

27
Q

Combinatorial problems arise in many areas of computer science and application domains:

A

finding shortest/cheapest round trips (TSP)

finding satisfying truth assignments for a given propositional formula (SAT)

planning, scheduling, time tabling

internet data packet routing

protein structure prediction

combinatorial auctions winner determination

28
Q

Knapsack search

A

The exhaustive search approach to solving the knapsack problem is based on generating all subsets of a given set of items.

Since the number of subsets of an n-element set is 2^n, the exhaustive search leads us to a O(2^n) algorithm.

The knapsack problem is a NP-Hard problem. No polynomial time algorithm solution for the problem. Moreover, most computer scientists believe that such algorithms do not exist.

29
Q

Depth-first and breadth-first traversal algorithms are very useful in

A

applications involving graphs in artificial intelligence and operations research.

30
Q

Depth-First Search

A

DFS(G, u)
{
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
}

init()
{
for each u ∈ G
u.visited = false
for each u ∈ G
DFS(G, u)
}

31
Q

The depth-first search (DFS) algorithm sticks with one path, following that path down a graph structure until it ends.

A

The breadth-first search (BFS) approach, however, evaluates all the possible paths from a given node equally, checking all potential vertices from one node together, and comparing them simultaneously.

DFS checks if a path exists. BFS is good to find a shortest path.