along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: graph.c,v 1.1.2.3 2001/10/29 13:14:57 guus Exp $
+ $Id: graph.c,v 1.1.2.5 2001/10/31 12:50:24 guus Exp $
*/
/* We need to generate two trees from the graph:
favour Kruskal's, because we make an extra AVL tree of edges sorted on
weights (metric). That tree only has to be updated when an edge is added or
removed, and during the MST algorithm we just have go linearly through that
- tree, adding safe edges until #edges = #nodes - 1.
+ tree, adding safe edges until #edges = #nodes - 1. The implementation here
+ however is not so fast, because I tried to avoid having to make a forest and
+ merge trees.
For the SSSP algorithm Dijkstra's seems to be a nice choice. Currently a
simple breadth-first search is presented here.
void mst_kruskal(void)
{
- avl_node_t *node;
+ avl_node_t *node, *next;
edge_t *e;
node_t *n;
connection_t *c;
int safe_edges = 0;
int skipped;
- syslog(LOG_DEBUG, _("Running Kruskal's algorithm:"));
-
/* Clear visited status on nodes */
for(node = node_tree->head; node; node = node->next)
/* Add safe edges */
- while(safe_edges < nodes - 1)
- for(skipped = 0, node = edge_weight_tree->head; node; node = node->next)
+ for(skipped = 0, node = edge_weight_tree->head; node; node = next)
{
-// Algorithm should work without this:
-// if(safe_edges = nodes - 1)
-// break;
-
+ next = node->next;
e = (edge_t *)node->data;
if(e->from->status.visited == e->to->status.visited)
safe_edges++;
- syslog(LOG_DEBUG, _("Adding safe edge %s - %s weight %d"), e->from->name, e->to->name, e->weight);
-
if(skipped)
- break;
- }
-
- syslog(LOG_DEBUG, _("Done."));
-
- if(safe_edges != nodes - 1)
- {
- syslog(LOG_ERR, _("Implementation of Kruskal's algorithm is screwed: %d nodes, found %d safe edges"), nodes, safe_edges);
+ {
+ next = edge_weight_tree->head;
+ continue;
+ }
}
}
Running time: O(E)
*/
-void sssp_bfs(void)
+void sssp_bfs(int prune)
{
avl_node_t *node, *from, *next, *to;
edge_t *e;
node_t *n, *check;
- int nodes = 0;
- int visited = 0;
avl_tree_t *todo_tree;
- syslog(LOG_DEBUG, _("Running BFS algorithm:"));
-
todo_tree = avl_alloc_tree(NULL, NULL);
/* Clear visited status on nodes */
{
n = (node_t *)node->data;
n->status.visited = 0;
- nodes++;
}
/* Begin with myself */
node = avl_alloc_node();
node->data = myself;
avl_insert_top(todo_tree, node);
- visited++;
/* Loop while todo_tree is filled */
if(!check->status.visited)
{
check->status.visited = 1;
- check->nexthop = (n->nexthop == myself) ? n : n->nexthop;
- check->via = check; /* FIXME: only if !(e->options & INDIRECT), otherwise use n->via */
+ check->nexthop = (n->nexthop == myself) ? check : n->nexthop;
+ check->via = (e->options & OPTION_INDIRECT || n->via != n) ? n->via : check;
node = avl_alloc_node();
node->data = check;
avl_insert_before(todo_tree, from, node);
- visited++;
- syslog(LOG_DEBUG, _("Node %s nexthop %s via %s"), check->name, check->nexthop->name, check->via->name);
}
}
}
}
- syslog(LOG_DEBUG, _("Done."));
-
avl_free_tree(todo_tree);
+
+ /* Nodes we haven't visited are unreachable, prune them. */
- if(visited != nodes)
- {
- syslog(LOG_ERR, _("Implementation of BFS algorithm is screwed: %d nodes, visited %d"), nodes, visited);
- }
+ if(prune)
+ for(node = node_tree->head; node; node = next)
+ {
+ next = node->next;
+ n = (node_t *)node->data;
+
+ if(n->status.visited == 0)
+ node_del(n);
+ }
}