Give an -time algorithm which merges k sorted lists with a total of n elements into one sorted list. (hint: use a heap to speed up the elementary O(k n)-time algorithm). The elementary algorithm compares the heads of each of the k sorted lists to find the minimum element, puts this in the sorted list and repeats. The total time is O(k n).
Suppose instead that we build a heap on the head elements of each of the k lists, with each element labeled as to which list it is from. The minimum element can be found and deleted in time. Further, we can insert the new head of this list in the heap in time.
An alternate approach would be to merge the lists from as in mergesort, using a binary tree on k leaves (one for each list).
lec11-2.au Combinatorial Search
We have seen how clever algorithms can reduce sorting from to . However, the stakes are even higher for combinatorially explosive problems:
The Traveling Salesman Problem
Given a weighted graph, find the shortest cycle which visits each vertex once.
f202.5in
Applications include minimizing plotter movement, printed-circuit board wiring, transportation problems, etc.
There is no known polynomial time algorithm (ie. for some fixed k) for this problem, so search-based algorithms are the only way to go if you need an optional solution. lec11-3.au But I want to use a Supercomputer
Moving to a faster computer can only buy you a relatively small improvement:
Can Eight Pieces Cover a Chess Board?
Consider the 8 main pieces in chess (king, queen, two rooks, two bishops, two knights). Can they be positioned on a chessboard so every square is threatened?
f214in
Only 63 square are threatened in this configuration. Since 1849, no one had been able to find an arrangement with bishops on different colors to cover all squares.
Of course, this is not an important problem, but we will use it as an example of how to attack a combinatorial search problem. lec11-5.au How many positions to test?
Picking a square for each piece gives us the bound:
Anything much larger than is unreasonable to search on a modest computer in a modest amount of time.
However, we can exploit symmetry to save work. With reflections along horizontal, vertical, and diagonal axis, the queen can go in only 10 non-equivallent positions.
Even better, we can restrict the white bishop to 16 spots and the queen to 16, while being certain that we get all distinct configurations.
f222.5in
lec11-6.au
Backtracking
Backtracking is a systematic way to go through all the possible configurations of a search space.
In the general case, we assume our solution is a vector where each element is selected from a finite ordered set ,
We build from a partial solution of length k and try to extend it by adding another element. After extending it, we will test whether what we have so far is still possible as a partial solution.
If it is still a candidate solution, great. If not, we delete and try the next element from :
Compute , the set of candidate first elements of v.
k = 1
While k > 0 do
\> While do (*advance*)
\> \> = an element in
\> \>
\> \> if ( ) is solution, print!
\> \> k = k + 1
\> \> compute , the candidate kth elements given v.
\> k = k - 1 (*backtrack*)
lec11-7.au
Recursive Backtracking
Recursion can be used for elegant and easy implementation of backtracking.
Backtrack(a, k)
if a is a solution, print(a)
else {
\> k = k +1
\> compute
\> while do
\> \> = an element in
\> \> =
\> \> Backtrack(a, k)
}
Backtracking can easily be used to iterate through all subsets or permutations of a set.
Backtracking ensures correctness by enumerating all possibilities.
For backtracking to be efficient, we must prune the search space. lec11-8.au Constructing all Subsets
How many subsets are there of an n-element set?
To construct all subsets, set up an array/vector of n cells, where the value of is either true or false, signifying whether the ith item is or is not in the subset.
To use the notation of the general backtrack algorithm, , and v is a solution whenever .
What order will this generate the subsets of ?
lec11-9.au Constructing all Permutations
How many permutations are there of an n-element set?
To construct all n! permutations, set up an array/vector of n cells, where the value of is an integer from 1 to n which has not appeared thus far in the vector, corresponding to the ith element of the permutation.
To use the notation of the general backtrack algorithm, , and v is a solution whenever .
lec11-9.au
The n-Queens Problem
The first use of pruning to deal with the combinatorial explosion was by the king who rewarded the fellow who discovered chess!
In the eight Queens, we prune whenever one queen threatens another. lec11-11.au Covering the Chess Board
In covering the chess board, we prune whenever we find there is a square which we cannot cover given the initial configuration!
Specifically, each piece can threaten a certain maximum number of squares (queen 27, king 8, rook 14, etc.) Whenever the number of unthreated squares exceeds the sum of the maximum number of coverage remaining in unplaced squares, we can prune.
As implemented by a graduate student project, this backtrack search eliminates of the search space, when the pieces are ordered by decreasing mobility.
With precomputing the list of possible moves, this program could search 1,000 positions per second. But this is too slow!
Although we might further speed the program by an order of magnitude, we need to prune more nodes!
By using a more clever algorithm, we eventually were able to prove no solution existed, in less than one day's worth of computing.
You too can fight the combinatorial explosion! lec11-12.au The Backtracking Contest: Bandwidth
The bandwidth problem takes as input a graph G, with n vertices and m edges (ie. pairs of vertices). The goal is to find a permutation of the vertices on the line which minimizes the maximum length of any edge.
bandwidth-Lbandwidth-R
The bandwidth problem has a variety of applications, including circuit layout, linear algebra, and optimizing memory usage in hypertext documents.
The problem is NP-complete, meaning that it is exceedingly unlikely that you will be able to find an algorithm with polynomial worst-case running time. It remains NP-complete even for restricted classes of trees.
Since the goal of the problem is to find a permutation, a backtracking program which iterates through all the n! possible permutations and computes the length of the longest edge for each gives an easy algorithm. But the goal of this assignment is to find as practically good an algorithm as possible.
The Backtracking Contest: Set Cover
The set cover problem takes as input a collection of subsets of the universal set . The goal is to find the smallest subset of the subsets T such that .
set-cover-Lset-cover-R
Set cover arises when you try to efficiently acquire or represent items that have been packaged in a fixed set of lots. You want to obtain all the items, while buying as few lots as possible. Finding a cover is easy, because you can always buy one of each lot. However, by finding a small set cover you can do the same job for less money.
Since the goal of the problem is to find a subset, a backtracking program which iterates through all the possible subsets and tests whether it represents a cover gives an easy algorithm. But the goal of this assignment is to find as practically good an algorithm as possible.
lec12-4.au Rules of the Game
Producing Efficient Programs
When in doubt, keep it simple, stupid (KISS).