topo-sort-with-leda.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <vector>
  9. #include <string>
  10. #include <boost/graph/topological_sort.hpp>
  11. #include <boost/graph/leda_graph.hpp>
  12. // Undefine macros from LEDA that conflict with the C++ Standard Library.
  13. #undef string
  14. #undef vector
  15. int
  16. main()
  17. {
  18. using namespace boost;
  19. typedef GRAPH < std::string, char >graph_t;
  20. graph_t leda_g;
  21. typedef graph_traits < graph_t >::vertex_descriptor vertex_t;
  22. std::vector < vertex_t > vert(7);
  23. vert[0] = add_vertex(std::string("pick up kids from school"), leda_g);
  24. vert[1] = add_vertex(std::string("buy groceries (and snacks)"), leda_g);
  25. vert[2] = add_vertex(std::string("get cash at ATM"), leda_g);
  26. vert[3] =
  27. add_vertex(std::string("drop off kids at soccer practice"), leda_g);
  28. vert[4] = add_vertex(std::string("cook dinner"), leda_g);
  29. vert[5] = add_vertex(std::string("pick up kids from soccer"), leda_g);
  30. vert[6] = add_vertex(std::string("eat dinner"), leda_g);
  31. add_edge(vert[0], vert[3], leda_g);
  32. add_edge(vert[1], vert[3], leda_g);
  33. add_edge(vert[1], vert[4], leda_g);
  34. add_edge(vert[2], vert[1], leda_g);
  35. add_edge(vert[3], vert[5], leda_g);
  36. add_edge(vert[4], vert[6], leda_g);
  37. add_edge(vert[5], vert[6], leda_g);
  38. std::vector < vertex_t > topo_order;
  39. node_array < default_color_type > color_array(leda_g);
  40. topological_sort(leda_g, std::back_inserter(topo_order),
  41. color_map(make_leda_node_property_map(color_array)));
  42. std::reverse(topo_order.begin(), topo_order.end());
  43. int n = 1;
  44. for (std::vector < vertex_t >::iterator i = topo_order.begin();
  45. i != topo_order.end(); ++i, ++n)
  46. std::cout << n << ": " << leda_g[*i] << std::endl;
  47. return EXIT_SUCCESS;
  48. }