April 1999 Programmer's Challenge
Shortest Network
Mail solutions to:
progchallenge@mactech.com
Due Date: 11:59pm ET, Thursday, 1 April 1999
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)
extern "C" {
#endif
typedef struct Node { /* node coordinates */
double x;
double y;
} Node;
typedef struct Connection { /* connection between Node[index1] and Node[index2] */
long index1;
long index2;
} Connection;
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-1 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.
Test code for this Challenge is available.
You can get a head start on the Challenge by reading the
Programmer's Challenge mailing list. It will be posted to the list on
or before the 12th of the preceding month. To join, send an email to
listserv@listmail.xplain.com
with the subject "subscribe challenge-A". You can also join the
discussion list by sending a message with the subject "subscribe
challenge-D".
|