Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/graph/example/kruskal-example.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 2.6 KB
Line 
1//=======================================================================
2// Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
3//
4// Distributed under the Boost Software License, Version 1.0. (See
5// accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//=======================================================================
8#include <boost/graph/adjacency_list.hpp>
9#include <boost/graph/kruskal_min_spanning_tree.hpp>
10#include <iostream>
11#include <fstream>
12
13int
14main()
15{
16  using namespace boost;
17  typedef adjacency_list < vecS, vecS, undirectedS,
18    no_property, property < edge_weight_t, int > > Graph;
19  typedef graph_traits < Graph >::edge_descriptor Edge;
20  typedef graph_traits < Graph >::vertex_descriptor Vertex;
21  typedef std::pair<int, int> E;
22
23  const int num_nodes = 5;
24  E edge_array[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
25    E(3, 4), E(4, 0), E(4, 1)
26  };
27  int weights[] = { 1, 1, 2, 7, 3, 1, 1, 1 };
28  std::size_t num_edges = sizeof(edge_array) / sizeof(E);
29#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
30  Graph g(num_nodes);
31  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
32  for (std::size_t j = 0; j < num_edges; ++j) {
33    Edge e; bool inserted;
34    tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
35    weightmap[e] = weights[j];
36  }
37#else
38  Graph g(edge_array, edge_array + num_edges, weights, num_nodes);
39#endif
40  property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g);
41  std::vector < Edge > spanning_tree;
42
43  kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree));
44
45  std::cout << "Print the edges in the MST:" << std::endl;
46  for (std::vector < Edge >::iterator ei = spanning_tree.begin();
47       ei != spanning_tree.end(); ++ei) {
48    std::cout << source(*ei, g) << " <--> " << target(*ei, g)
49      << " with weight of " << weight[*ei]
50      << std::endl;
51  }
52
53  std::ofstream fout("figs/kruskal-eg.dot");
54  fout << "graph A {\n"
55    << " rankdir=LR\n"
56    << " size=\"3,3\"\n"
57    << " ratio=\"filled\"\n"
58    << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
59  graph_traits<Graph>::edge_iterator eiter, eiter_end;
60  for (tie(eiter, eiter_end) = edges(g); eiter != eiter_end; ++eiter) {
61    fout << source(*eiter, g) << " -- " << target(*eiter, g);
62    if (std::find(spanning_tree.begin(), spanning_tree.end(), *eiter)
63        != spanning_tree.end())
64      fout << "[color=\"black\", label=\"" << get(edge_weight, g, *eiter)
65           << "\"];\n";
66    else
67      fout << "[color=\"gray\", label=\"" << get(edge_weight, g, *eiter)
68           << "\"];\n";
69  }
70  fout << "}\n";
71  return EXIT_SUCCESS;
72}
Note: See TracBrowser for help on using the repository browser.