kruskal-example.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. int
  13. main()
  14. {
  15. using namespace boost;
  16. typedef adjacency_list < vecS, vecS, undirectedS,
  17. no_property, property < edge_weight_t, int > > Graph;
  18. typedef graph_traits < Graph >::edge_descriptor Edge;
  19. typedef std::pair<int, int> E;
  20. const int num_nodes = 5;
  21. E edge_array[] = { E(0, 2), E(1, 3), E(1, 4), E(2, 1), E(2, 3),
  22. E(3, 4), E(4, 0), E(4, 1)
  23. };
  24. int weights[] = { 1, 1, 2, 7, 3, 1, 1, 1 };
  25. std::size_t num_edges = sizeof(edge_array) / sizeof(E);
  26. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  27. Graph g(num_nodes);
  28. property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, g);
  29. for (std::size_t j = 0; j < num_edges; ++j) {
  30. Edge e; bool inserted;
  31. boost::tie(e, inserted) = add_edge(edge_array[j].first, edge_array[j].second, g);
  32. weightmap[e] = weights[j];
  33. }
  34. #else
  35. Graph g(edge_array, edge_array + num_edges, weights, num_nodes);
  36. #endif
  37. property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g);
  38. std::vector < Edge > spanning_tree;
  39. kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree));
  40. std::cout << "Print the edges in the MST:" << std::endl;
  41. for (std::vector < Edge >::iterator ei = spanning_tree.begin();
  42. ei != spanning_tree.end(); ++ei) {
  43. std::cout << source(*ei, g) << " <--> " << target(*ei, g)
  44. << " with weight of " << weight[*ei]
  45. << std::endl;
  46. }
  47. std::ofstream fout("figs/kruskal-eg.dot");
  48. fout << "graph A {\n"
  49. << " rankdir=LR\n"
  50. << " size=\"3,3\"\n"
  51. << " ratio=\"filled\"\n"
  52. << " edge[style=\"bold\"]\n" << " node[shape=\"circle\"]\n";
  53. graph_traits<Graph>::edge_iterator eiter, eiter_end;
  54. for (boost::tie(eiter, eiter_end) = edges(g); eiter != eiter_end; ++eiter) {
  55. fout << source(*eiter, g) << " -- " << target(*eiter, g);
  56. if (std::find(spanning_tree.begin(), spanning_tree.end(), *eiter)
  57. != spanning_tree.end())
  58. fout << "[color=\"black\", label=\"" << get(edge_weight, g, *eiter)
  59. << "\"];\n";
  60. else
  61. fout << "[color=\"gray\", label=\"" << get(edge_weight, g, *eiter)
  62. << "\"];\n";
  63. }
  64. fout << "}\n";
  65. return EXIT_SUCCESS;
  66. }