Apr 99 Challenge
Volume Number: 15
Issue Number: 4
Column Tag: Programmer's Challenge
by Bob Boonstra, Westford, MA
Shortest Network
This month's problem was suggested by Michael Kennedy, who wins two Challenge
points for making the suggestion. The problem is to find the shortest network of line
segments interconnecting a specified set of points. Shortest network algorithms have
obvious practical application in constructing transportation and communications
networks. In a January 1989, Scientific American article, Marshall Bern and Ronald
Graham discussed the shortest network "Steiner" problem as one of a class of NP-hard
problems. While no polynomial-time algorithm is known, the article (which,
unfortunately, I have not been able to find online) discusses practical algorithms that
produce networks slightly longer than the optimal one. Your Challenge for this month
is to produce a near-optimal network in minimum time. Fortunately, we have been
granted unlimited power of eminent domain, so there are no restrictions on where
intermediate nodes may be placed or where connections may be routed.
The prototype for the code you should write is:
#if defined(__cplusplus)
typedef struct Node { /* node coordinates */
double x;
double y;
} Node;
typedef struct Connection {
/* connection between Node[index1] and Node[index2] */
long index1;
long index2;
long /* numConnections */ ShortestNetwork(
long numInitialNodes, /* number of nodes to connect */
long *numIntermediateNodes, /* number of nodes added by
ShortestNetwork */
Node nodes[],
/* Nodes 0..numInitialNodes-1 are initialized on entry. */
/* Nodes numInitialNodes..numInitialNodes+*numIntermediateNodes
are added by ShortestNetwork */
Connection connections[], /* connections between nodes */
long maxNodes, /* number of entries allocated for nodes */
long maxConnections /* number of entries allocated for
connections */
);
#if defined(__cplusplus)
}
#endif
Your ShortestNetwork routine will be given a list of numInitialNodes nodes to connect.
You may add intermediate nodes to help you form a shorter network, and must produce
as output a list of connections between pairs of nodes. The connections must provide a
path between any pair of the initial nodes.
Your solution must return the number of intermediate nodes added to the network in
*numIntermediateNodes, while storing the location of those nodes in
nodes[numInitialNodes+k], k=0..*numIntermediateNodes-1. A connection is specified
by storing the indices of the two nodes being connected into the connection array. Your
ShortestNetwork routine should return the number of connections created.
The maxNodes and maxConnections parameters indicate how much storage has been
allocated for nodes and connections. It is my intention to allocate enough storage for all
the nodes and connections your solution might create, but if it turns out that there is
not enough storage, your solution should return a value of -1 to indicate that storage
was exhausted.
The winner will be the solution that generates the shortest network in the minimum
amount of time. Specifically, your solution will be assigned a cost equal to the sum of
the distances between nodes in your list of connections, plus a penalty of 10% for each
second of execution time. Solutions that do not connect all of the initial nodes will be
penalized with a very large cost. The solution with the lowest total cost over a series of
networking problems will be the winner.
This will be a native PowerPC Challenge, using the latest CodeWarrior environment.
Solutions may be coded in C, C++, or Pascal. Thanks to Michael for suggesting this
Challenge.
Three Months Ago Winner
Congratulations to Tom Saxton for submitting the winning solution to the January
Sphere Packing Challenge. You may recall that this Challenge was to pack a set of
spheres of varying size into a box with minimum volume, and to do so in the shortest
amount of time possible. Tom submitted one of only two solutions received for this
Challenge, and his was the only one that performed correctly.
Tom's approach is to decide on a footprint for the box to contain the spheres, "drop" the
spheres individually into the box until they hit another sphere or the bottom of the
box, while attempting to move the dropped sphere around the obstacle without going
outside the box footprint. The solution then iterates with random movements to try to
converge to a better solution. Tom observed in his submission that the time penalty for
this problem (1% per millisecond of execution time) was very severe, making it
unproductive to let his algorithm iterate very long. Every tenth of a second of
execution time requires a factor of 2 reduction in volume to be productive, a rate of
improvement smaller than what Tom was able to achieve.
I evaluated the solutions using six test cases with between 200 and 2000 spheres per
test case. As one might expect, execution time grew exponentially with the number of
spheres. A test case with 1000 spheres took about 20 times as long to solve as a
200-sphere case, and a 2000-sphere case took about 4 times longer than the
1000-sphere case. Tom's solution generated solutions that, in aggregate, occupied
between 1.3 and 3.9 times the volume of individual cubes containing the individual
spheres, which suggests that better solutions could be achieved with a more relaxed
time penalty.
The table below lists, for each of the solutions submitted, the total volume of the boxes
containing the spheres, the total execution time, and the total score including the time
penalty, as well as the code and data sizes for each entry. As usual, the number in
parentheses after the entrant's name is the total number of Challenge points earned in
all Challenges prior to this one.
Name Volume (x1.0E12) Time (secs) Score (x1.0e12) Code Size Data Size
Tom Saxton (79) 65.3 142.3 10107.2 5796 372
A. D. * * ‹* 820 104
Top Contestants
Listed here are the Top Contestants for the Programmer's Challenge, including
everyone who has accumulated 20 or more points during the past two years. The
numbers below include points awarded over the 24 most recent contests, including
points earned by this month's entrants.
1. Munter, Ernst 200
2. Saxton, Tom 99
3. Boring, Randy 56
4. Mallett, Jeff 50
5. Rieken, Willeke 47
6. Maurer, Sebastian 40
7. Heithcock, JG 37
8. Cooper, Greg 34
9. Murphy, ACC 34
10. Lewis, Peter 31
11. Nicolle, Ludovic 27
12. Brown, Pat 20
13. Day, Mark 20
14. Higgins, Charles 20
15. Hostetter, Mat 20
There are three ways to earn points: (1) scoring in the top 5 of any Challenge, (2)
being the first person to find a bug in a published winning solution or, (3) being the
first person to suggest a Challenge that I use. The points you can win are:
1st place ›20 points
2nd place ›10 points
3rd place ›7 points
4th place ›4 points
5th place ›2 points
finding bug ›2 points
suggesting Challenge ›2 points
Here is Tom's winning Sphere Packing solution:
Spheres.cpp
Copyright © 1999 Tom Saxton
#include "Spheres.h
#include "VecUtil.h
enum {
fFalse = 0,
fTrue = 1
};
typedef unsigned long ulong;
// disable asserts
#define Assert(f)
// hard iteration limit
#define cIterLim 10000
// scoring an accepting solutions
#define _FAccept(volNew, volBest) ((volNew) < (volBest))
#define _Score(vol, dtick) ((vol) * (1.0 + (dtick)*10.0/60.0))
// define this to ignore the time penalty
// #define KEEP_GOING
// time checking parameters
#define dtickSec 60
#define dtickCheckScore (dtickSec/30)
#define dtickFirstCheck (dtickSec/30)
#define dtickLastCheck (10*dtickSec)
static const Position s_normalX = { 1.0, 0.0, 0.0 };
static const Position s_normalY = { 0.0, 1.0, 0.0 };
static const Position s_normalZ = { 0.0, 0.0, 1.0 };
static const Position s_normalXNeg = { -1.0, 0.0, 0.0 };
static const Position s_normalYNeg = { 0.0, -1.0, 0.0 };
static const Position s_normalZNeg = { 0.0, 0.0, -1.0 };
static void _InitStartingPos(
const long csphere,
long aisphere[],
const double aradius[],
double baseMin,
double baseBest,
double baseMax,
double *pbase,
Position aposStart[]);
static void _TweakStartingPos(
const long csphere,
long aisphere[],
const double aradius[],
double baseMin,