INFO 150: A Mathematical Foundation for Informatics
David Mix Barrington
Fall, 2017
Lecture #9, 27 September 2017
Brute-Force Search
In Lecture #2 we studied a variety of sequences of numbers,
which can be thought of as functions that assign a number f(n) to each
natural n. We saw constant, linear, quadratic,
exponential, and factorial sequences.
Such sequences are important in the study of computation because
they can model the time complexity of an algorithm. If we have
a measure of the size of an algorithm's input, we can measure the
number of steps that the algorithm takes on the worst input of that
size. For example, you will later learn sorting algorithms that can
sort a list of n items in about n times log n time. And the truth
table method, on a compound proposition with n variables, needs 2n lines
and thus over 2n steps to determine whether the formula is
a tautology.
On the homework you'll take some values of n and some potential
time functions, and determine the running time of the algorithms on
these input sizes, on the assumption that your computer can perform a
billion steps in one second.
We've seen that in some cases we can show a compound proposition to
be a tautology without checking all 2n possible settings.
But there is no known way to find a proof of this form for an
arbitrary tautology that takes a polynomial number of steps
(nk steps for some constant k). In fact, it is not known
whether such a short proof always exists for an arbitrary
tautology.
But there are cases where we know algorithms that are a great
improvement on brute-force search, and you will see these if you ever
take an algorithms course:
- A brute-force approach to sorting a list would check all
n! possible orderings to see which was right. But the best standard
sorting methods take a number of steps around the logarithm of n!,
proportional to n log n.
- If we have a network of nodes and edges, and we want to find
a path from one node to another (as a GPS system does where the
nodes are street addresses and the edges are streets), brute force
would check all possible paths to see whether one succeeds. But you
know that a GPS can find not only a path, but a shortest path, in a
network with millions of nodes in only seconds. It uses the
A* search algorithm, studied in CS 250.
- If we have a network of pipes carrying stuff, each with a
certain capacity, a network flow algorithm can determine
whether a given quantity of stuff can be routed from one place to
another. Brute force would have to check exponentially many ways to
assign an amount of stuff to each pipe, but this problem can also be
solved in polynomial time.
- Suppose we have n people, each who want to adopt a dog from
a set of n dogs. Each person has a rank order on the dogs, and each
dog has a rank order on the people. Our goal is to match each
person to a dog, such that there is no person-dog pair where
each prefers the other to their given match. There are n!
possible matchings, so checking all of them would be impossible even
for pretty small values of n. But the Stable Marriage
Algorithm uses about n2 steps, and is what is
actually used to match thousands of medical school graduates with
residency programs each year.
However, there are many other searching problems where no method
is known that is much better than brute-force search, at least in
the worst case. Some of these algorithm have a property called
NP-completeness, which means that if they can be solved in
polynomial time, essentially all brute force searches can be
carried out in polynomial time. It seems unlikely that such a
general search algorithm exists, but to prove that it doesn't
(assuming that it really doesn't) is currently the greatest unsolved
problem in theoretical computer science and one of the greatest in
all of mathematics. Here are a few examples of NP-complete
problems:
- Determining whether a given compound proposition is a
tautology.
- Given a network of nodes and edges, determine the shortest
path by which a traveling salesperson can visit all of the
nodes and return to their starting point. (Note that we can find
the best way to go from any point to any other, but there are n!
possible orders in which to visit n points.)
- Given a set of positive integers, and a target number, is
there a subset of the integers that add up exactly to the target?
This Subset Sum problem is NP-complete in general, but can
be solved easily if the numbers are all small.
A working programmer should have an idea of what sort of search
problems have known solutions and which are NP-complete. In
addition, there are practical approaches to some NP-complete
problems, such as restricting to special cases, or looking for an
approximation an optimal solution instead of an exact solution.
Last modified 28 September 2017