graph/example/ospf-example.cpp
Jeremy Siek 5215e9b4f2 examples from the book
[SVN r11503]
2001-11-01 16:56:10 +00:00

99 lines
3.9 KiB
C++

//=======================================================================
// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
//
// This file is part of the Boost Graph Library
//
// You should have received a copy of the License Agreement for the
// Boost Graph Library along with the software; see the file LICENSE.
// If not, contact Office of Research, Indiana University,
// Bloomington, IN 47405.
//
// Permission to modify the code and to distribute the code is
// granted, provided the text of this NOTICE is retained, a notice if
// the code was modified is included with the above COPYRIGHT NOTICE
// and with the COPYRIGHT NOTICE in the LICENSE file, and that the
// LICENSE file is distributed with the modified code.
//
// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
// By way of example, but not limitation, Licensor MAKES NO
// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
// OR OTHER RIGHTS.
//=======================================================================
#include <fstream> // for file I/O
#include <boost/graph/graphviz.hpp> // for read/write_graphviz()
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/lexical_cast.hpp>
int
main()
{
using namespace boost;
GraphvizDigraph g_dot;
read_graphviz("figs/ospf-graph.dot", g_dot);
typedef adjacency_list < vecS, vecS, directedS, no_property,
property < edge_weight_t, int >>Graph;
typedef graph_traits < Graph >::vertex_descriptor vertex_descriptor;
Graph g(num_vertices(g_dot));
property_map < GraphvizDigraph, edge_attribute_t >::type
edge_attr_map = get(edge_attribute, g_dot);
graph_traits < GraphvizDigraph >::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g_dot); ei != ei_end; ++ei) {
int weight = lexical_cast < int >(edge_attr_map[*ei]["label"]);
property < edge_weight_t, int >edge_property(weight);
add_edge(source(*ei, g_dot), target(*ei, g_dot), edge_property, g);
}
vertex_descriptor router_six;
property_map < GraphvizDigraph, vertex_attribute_t >::type
vertex_attr_map = get(vertex_attribute, g_dot);
graph_traits < GraphvizDigraph >::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi)
if ("RT6" == vertex_attr_map[*vi]["label"]) {
router_six = *vi;
break;
}
std::vector < vertex_descriptor > parent(num_vertices(g));
// All vertices start out as there own parent
typedef graph_traits < Graph >::vertices_size_type size_type;
for (size_type p = 0; p < num_vertices(g); ++p)
parent[p] = p;
dijkstra_shortest_paths(g, router_six, predecessor_map(&parent[0]));
graph_traits < GraphvizDigraph >::edge_descriptor e;
for (size_type i = 0; i < num_vertices(g); ++i)
if (parent[i] != i) {
e = edge(parent[i], i, g_dot).first;
edge_attr_map[e]["color"] = "black";
}
graph_property < GraphvizDigraph, graph_edge_attribute_t >::type &
graph_edge_attr_map = get_property(g_dot, graph_edge_attribute);
graph_edge_attr_map["color"] = "grey";
write_graphviz("figs/ospf-sptree.dot", g_dot);
std::ofstream rtable("routing-table.dat");
rtable << "Dest Next Hop Total Cost" << std::endl;
for (tie(vi, vi_end) = vertices(g_dot); vi != vi_end; ++vi)
if (parent[*vi] != *vi) {
rtable << vertex_attr_map[*vi]["label"] << " ";
vertex_descriptor v = *vi, child;
int path_cost = 0;
property_map < Graph, edge_weight_t >::type
weight_map = get(edge_weight, g);
do {
path_cost += get(weight_map, edge(parent[v], v, g).first);
child = v;
v = parent[v];
} while (v != parent[v]);
rtable << vertex_attr_map[child]["label"] << " ";
rtable << path_cost << std::endl;
}
return EXIT_SUCCESS;
}