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/config.hpp>
9 #include <iostream>
10 #include <boost/graph/adjacency_list.hpp>
11 using namespace boost;
12 
undirected_graph_demo1()13 template < typename UndirectedGraph > void undirected_graph_demo1()
14 {
15     const int V = 3;
16     UndirectedGraph undigraph(V);
17     typename graph_traits< UndirectedGraph >::vertex_descriptor zero, one, two;
18     typename graph_traits< UndirectedGraph >::out_edge_iterator out, out_end;
19     typename graph_traits< UndirectedGraph >::in_edge_iterator in, in_end;
20 
21     zero = vertex(0, undigraph);
22     one = vertex(1, undigraph);
23     two = vertex(2, undigraph);
24     add_edge(zero, one, undigraph);
25     add_edge(zero, two, undigraph);
26     add_edge(one, two, undigraph);
27 
28     std::cout << "out_edges(0):";
29     for (boost::tie(out, out_end) = out_edges(zero, undigraph); out != out_end;
30          ++out)
31         std::cout << ' ' << *out;
32     std::cout << std::endl << "in_edges(0):";
33     for (boost::tie(in, in_end) = in_edges(zero, undigraph); in != in_end; ++in)
34         std::cout << ' ' << *in;
35     std::cout << std::endl;
36 }
37 
directed_graph_demo()38 template < typename DirectedGraph > void directed_graph_demo()
39 {
40     const int V = 2;
41     DirectedGraph digraph(V);
42     typename graph_traits< DirectedGraph >::vertex_descriptor u, v;
43     typedef typename DirectedGraph::edge_property_type Weight;
44     typename property_map< DirectedGraph, edge_weight_t >::type weight
45         = get(edge_weight, digraph);
46     typename graph_traits< DirectedGraph >::edge_descriptor e1, e2;
47     bool found;
48 
49     u = vertex(0, digraph);
50     v = vertex(1, digraph);
51     add_edge(u, v, Weight(1.2), digraph);
52     add_edge(v, u, Weight(2.4), digraph);
53     boost::tie(e1, found) = edge(u, v, digraph);
54     boost::tie(e2, found) = edge(v, u, digraph);
55     std::cout << "in a directed graph is ";
56 #ifdef __GNUC__
57     // no boolalpha
58     std::cout << "(u,v) == (v,u) ? " << (e1 == e2) << std::endl;
59 #else
60     std::cout << "(u,v) == (v,u) ? " << std::boolalpha << (e1 == e2)
61               << std::endl;
62 #endif
63     std::cout << "weight[(u,v)] = " << get(weight, e1) << std::endl;
64     std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl;
65 }
66 
undirected_graph_demo2()67 template < typename UndirectedGraph > void undirected_graph_demo2()
68 {
69     const int V = 2;
70     UndirectedGraph undigraph(V);
71     typename graph_traits< UndirectedGraph >::vertex_descriptor u, v;
72     typedef typename UndirectedGraph::edge_property_type Weight;
73     typename property_map< UndirectedGraph, edge_weight_t >::type weight
74         = get(edge_weight, undigraph);
75     typename graph_traits< UndirectedGraph >::edge_descriptor e1, e2;
76     bool found;
77 
78     u = vertex(0, undigraph);
79     v = vertex(1, undigraph);
80     add_edge(u, v, Weight(3.1), undigraph);
81     boost::tie(e1, found) = edge(u, v, undigraph);
82     boost::tie(e2, found) = edge(v, u, undigraph);
83     std::cout << "in an undirected graph is ";
84 #ifdef __GNUC__
85     std::cout << "(u,v) == (v,u) ? " << (e1 == e2) << std::endl;
86 #else
87     std::cout << "(u,v) == (v,u) ? " << std::boolalpha << (e1 == e2)
88               << std::endl;
89 #endif
90     std::cout << "weight[(u,v)] = " << get(weight, e1) << std::endl;
91     std::cout << "weight[(v,u)] = " << get(weight, e2) << std::endl;
92 
93     std::cout << "the edges incident to v: ";
94     typename boost::graph_traits< UndirectedGraph >::out_edge_iterator e, e_end;
95     typename boost::graph_traits< UndirectedGraph >::vertex_descriptor s
96         = vertex(0, undigraph);
97     for (boost::tie(e, e_end) = out_edges(s, undigraph); e != e_end; ++e)
98         std::cout << "(" << source(*e, undigraph) << ","
99                   << target(*e, undigraph) << ")" << std::endl;
100 }
101 
main()102 int main()
103 {
104     typedef property< edge_weight_t, double > Weight;
105     typedef adjacency_list< vecS, vecS, undirectedS, no_property, Weight >
106         UndirectedGraph;
107     typedef adjacency_list< vecS, vecS, directedS, no_property, Weight >
108         DirectedGraph;
109     undirected_graph_demo1< UndirectedGraph >();
110     directed_graph_demo< DirectedGraph >();
111     undirected_graph_demo2< UndirectedGraph >();
112     return 0;
113 }
114