Mastering Algorithms With C - Kyle Loudon [198]
* *
*****************************************************************************/
#ifndef GRAPHALG_H
#define GRAPHALG_H
#include "graph.h"
#include "list.h"
/*****************************************************************************
* *
* Define a structure for vertices in minimum spanning trees. *
* *
*****************************************************************************/
typedef struct MstVertex_ {
void *data;
double weight;
VertexColor color;
double key;
struct MstVertex_ *parent;
} MstVertex;
/*****************************************************************************
* *
* Define a structure for vertices in shortest-path problems. *
* *
*****************************************************************************/
typedef struct PathVertex_ {
void *data;
double weight;
VertexColor color;
double d;
struct PathVertex_ *parent;
} PathVertex;
/*****************************************************************************
* *
* Define a structure for vertices in traveling-salesman problems. *
* *
*****************************************************************************/
typedef struct TspVertex_ {
void *data;
double x,
y;
VertexColor color;
} TspVertex;
/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/
int mst(Graph *graph, const MstVertex *start, List *span, int (*match)(const
void *key1, const void *key2));
int shortest(Graph *graph, const PathVertex *start, List *paths, int (*match)
(const void *key1, const void *key2));
int tsp(List *vertices, const TspVertex *start, List *tour, int (*match)
(const void *key1, const void *key2));
#endif
Description of Minimum Spanning Trees
Picture a number of pegs on a board connected by pieces of string. Assuming that every peg is reachable from any other by traveling along one or more strings, imagine a game in which the object is to remove some of the strings until all of the pegs remain connected using the least amount of string. This is the idea behind a minimum spanning tree. Formally stated, given an undirected, weighted graph G = (V, E ), a minimum spanning tree is the set T of edges in E that connect all vertices in V at a minimum cost. The edges in T form a tree because each vertex ends up with exactly one parent that precedes it in the span, with the exception of the first vertex, which is the root of the tree.
Prim's Algorithm
One approach to computing a minimum spanning tree is Prim's algorithm. Prim's algorithm grows a minimum spanning tree by adding edges one at a time based on which looks best at the moment. The fact that Prim's algorithm adds edges using this approach makes it greedy (see Chapter 1). Although greedy algorithms often yield approximations rather than optimal solutions, Prim's algorithm actually provides an optimal result.
Fundamentally, the algorithm works by repeatedly selecting a vertex and exploring the edges incident on it to determine if there is a more effective way to span the vertices explored thus far. The algorithm resembles breadth-first search because it explores all edges incident on a vertex before moving deeper in the graph. To determine the vertex to select at each stage, we maintain a color and a key value with every vertex.
Initially, we set all colors to white and we set all key values to ∞, which represents an arbitrarily large value greater than the weight of any edge in the graph. We set the key value of the vertex at which to start the span to 0. As the algorithm progresses, we assign to all vertices except the start vertex a parent in the minimum spanning tree. A vertex is part of the minimum spanning tree only after it is colored black. Before this time, its parent can fluctuate.
Prim's algorithm proceeds as follows. First, from among all white vertices in the graph, we select the vertex u with the smallest