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