adjacency_list.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <boost/config.hpp>
  10. #include <iostream>
  11. #include <vector>
  12. #include <utility>
  13. #include <string>
  14. #include <boost/graph/adjacency_list.hpp>
  15. #include <boost/graph/graph_utility.hpp>
  16. #include <boost/property_map/property_map.hpp>
  17. /*
  18. Sample Output
  19. graph name: foo
  20. 0 --joe--> 1
  21. 1 --joe--> 0 --curly--> 2 --dick--> 3
  22. 2 --curly--> 1 --tom--> 4
  23. 3 --dick--> 1 --harry--> 4
  24. 4 --tom--> 2 --harry--> 3
  25. (0,1) (1,2) (1,3) (2,4) (3,4)
  26. removing edge (1,3):
  27. 0 --joe--> 1
  28. 1 --joe--> 0 --curly--> 2
  29. 2 --curly--> 1 --tom--> 4
  30. 3 --harry--> 4
  31. 4 --tom--> 2 --harry--> 3
  32. (0,1) (1,2) (2,4) (3,4)
  33. */
  34. struct EdgeProperties {
  35. EdgeProperties(const std::string& n) : name(n) { }
  36. std::string name;
  37. };
  38. struct VertexProperties {
  39. std::size_t index;
  40. boost::default_color_type color;
  41. };
  42. int main(int , char* [])
  43. {
  44. using namespace boost;
  45. using namespace std;
  46. typedef adjacency_list<vecS, listS, undirectedS,
  47. VertexProperties, EdgeProperties> Graph;
  48. const int V = 5;
  49. Graph g(V);
  50. property_map<Graph, std::size_t VertexProperties::*>::type
  51. id = get(&VertexProperties::index, g);
  52. property_map<Graph, std::string EdgeProperties::*>::type
  53. name = get(&EdgeProperties::name, g);
  54. boost::graph_traits<Graph>::vertex_iterator vi, viend;
  55. int vnum = 0;
  56. for (boost::tie(vi,viend) = vertices(g); vi != viend; ++vi)
  57. id[*vi] = vnum++;
  58. add_edge(vertex(0, g), vertex(1, g), EdgeProperties("joe"), g);
  59. add_edge(vertex(1, g), vertex(2, g), EdgeProperties("curly"), g);
  60. add_edge(vertex(1, g), vertex(3, g), EdgeProperties("dick"), g);
  61. add_edge(vertex(2, g), vertex(4, g), EdgeProperties("tom"), g);
  62. add_edge(vertex(3, g), vertex(4, g), EdgeProperties("harry"), g);
  63. graph_traits<Graph>::vertex_iterator i, end;
  64. graph_traits<Graph>::out_edge_iterator ei, edge_end;
  65. for (boost::tie(i,end) = vertices(g); i != end; ++i) {
  66. cout << id[*i] << " ";
  67. for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
  68. cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
  69. cout << endl;
  70. }
  71. print_edges(g, id);
  72. cout << endl << "removing edge (1,3): " << endl;
  73. remove_edge(vertex(1, g), vertex(3, g), g);
  74. ei = out_edges(vertex(1, g), g).first;
  75. cout << "removing edge (" << id[source(*ei, g)]
  76. << "," << id[target(*ei, g)] << ")" << endl;
  77. remove_edge(ei, g);
  78. for(boost::tie(i,end) = vertices(g); i != end; ++i) {
  79. cout << id[*i] << " ";
  80. for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
  81. cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
  82. cout << endl;
  83. }
  84. print_edges(g, id);
  85. return 0;
  86. }