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
15 #include <boost/graph/adjacency_list.hpp>
16 #include <boost/graph/graph_utility.hpp>
17 #include <boost/property_map/property_map.hpp>
18
19 /*
20 Sample Output
21
22 graph name: foo
23 0 --joe--> 1
24 1 --joe--> 0 --curly--> 2 --dick--> 3
25 2 --curly--> 1 --tom--> 4
26 3 --dick--> 1 --harry--> 4
27 4 --tom--> 2 --harry--> 3
28 (0,1) (1,2) (1,3) (2,4) (3,4)
29
30 removing edge (1,3):
31 0 --joe--> 1
32 1 --joe--> 0 --curly--> 2
33 2 --curly--> 1 --tom--> 4
34 3 --harry--> 4
35 4 --tom--> 2 --harry--> 3
36 (0,1) (1,2) (2,4) (3,4)
37
38 */
39
40 struct EdgeProperties
41 {
EdgePropertiesEdgeProperties42 EdgeProperties(const std::string& n) : name(n) {}
43 std::string name;
44 };
45
46 struct VertexProperties
47 {
48 std::size_t index;
49 boost::default_color_type color;
50 };
51
main(int,char * [])52 int main(int, char*[])
53 {
54 using namespace boost;
55 using namespace std;
56
57 typedef adjacency_list< vecS, listS, undirectedS, VertexProperties,
58 EdgeProperties >
59 Graph;
60
61 const int V = 5;
62 Graph g(V);
63
64 property_map< Graph, std::size_t VertexProperties::* >::type id
65 = get(&VertexProperties::index, g);
66 property_map< Graph, std::string EdgeProperties::* >::type name
67 = get(&EdgeProperties::name, g);
68
69 boost::graph_traits< Graph >::vertex_iterator vi, viend;
70 int vnum = 0;
71
72 for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
73 id[*vi] = vnum++;
74
75 add_edge(vertex(0, g), vertex(1, g), EdgeProperties("joe"), g);
76 add_edge(vertex(1, g), vertex(2, g), EdgeProperties("curly"), g);
77 add_edge(vertex(1, g), vertex(3, g), EdgeProperties("dick"), g);
78 add_edge(vertex(2, g), vertex(4, g), EdgeProperties("tom"), g);
79 add_edge(vertex(3, g), vertex(4, g), EdgeProperties("harry"), g);
80
81 graph_traits< Graph >::vertex_iterator i, end;
82 graph_traits< Graph >::out_edge_iterator ei, edge_end;
83 for (boost::tie(i, end) = vertices(g); i != end; ++i)
84 {
85 cout << id[*i] << " ";
86 for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
87 cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
88 cout << endl;
89 }
90 print_edges(g, id);
91
92 cout << endl << "removing edge (1,3): " << endl;
93 remove_edge(vertex(1, g), vertex(3, g), g);
94
95 ei = out_edges(vertex(1, g), g).first;
96 cout << "removing edge (" << id[source(*ei, g)] << "," << id[target(*ei, g)]
97 << ")" << endl;
98 remove_edge(ei, g);
99
100 for (boost::tie(i, end) = vertices(g); i != end; ++i)
101 {
102 cout << id[*i] << " ";
103 for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
104 cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << " ";
105 cout << endl;
106 }
107
108 print_edges(g, id);
109
110 return 0;
111 }
112