| 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 | |
|---|
| 10 | #include <boost/config.hpp> |
|---|
| 11 | #include <iostream> |
|---|
| 12 | #include <algorithm> |
|---|
| 13 | #include <boost/graph/adjacency_list.hpp> |
|---|
| 14 | |
|---|
| 15 | using namespace std; |
|---|
| 16 | using namespace boost; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | /* |
|---|
| 20 | Edge Basics |
|---|
| 21 | |
|---|
| 22 | This example demonstrates the GGCL Edge interface |
|---|
| 23 | |
|---|
| 24 | There is not much to the Edge interface. Basically just two |
|---|
| 25 | functions to access the source and target vertex: |
|---|
| 26 | |
|---|
| 27 | source(e) |
|---|
| 28 | target(e) |
|---|
| 29 | |
|---|
| 30 | and one associated type for the vertex type: |
|---|
| 31 | |
|---|
| 32 | edge_traits<Edge>::vertex_type |
|---|
| 33 | |
|---|
| 34 | Sample output: |
|---|
| 35 | |
|---|
| 36 | (0,1) (0,2) (0,3) (0,4) (2,0) (2,4) (3,0) (3,1) |
|---|
| 37 | |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | template <class Graph> |
|---|
| 43 | struct exercise_edge { |
|---|
| 44 | exercise_edge(Graph& g) : G(g) {} |
|---|
| 45 | |
|---|
| 46 | typedef typename boost::graph_traits<Graph>::edge_descriptor Edge; |
|---|
| 47 | typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; |
|---|
| 48 | void operator()(Edge e) const |
|---|
| 49 | { |
|---|
| 50 | //begin |
|---|
| 51 | // Get the associated vertex type out of the edge using the |
|---|
| 52 | // edge_traits class |
|---|
| 53 | // Use the source() and target() functions to access the vertices |
|---|
| 54 | // that belong to Edge e |
|---|
| 55 | Vertex src = source(e, G); |
|---|
| 56 | Vertex targ = target(e, G); |
|---|
| 57 | |
|---|
| 58 | // print out the vertex id's just because |
|---|
| 59 | cout << "(" << src << "," << targ << ") "; |
|---|
| 60 | //end |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | Graph& G; |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | int |
|---|
| 68 | main() |
|---|
| 69 | { |
|---|
| 70 | typedef adjacency_list<> MyGraph; |
|---|
| 71 | |
|---|
| 72 | typedef pair<int,int> Pair; |
|---|
| 73 | Pair edge_array[8] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4), |
|---|
| 74 | Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1) }; |
|---|
| 75 | |
|---|
| 76 | // Construct a graph using the edge_array (passing in pointers |
|---|
| 77 | // (iterators) to the beginning and end of the array), and |
|---|
| 78 | // specifying the number of vertices as 5 |
|---|
| 79 | MyGraph G(5); |
|---|
| 80 | for (int i=0; i<8; ++i) |
|---|
| 81 | add_edge(edge_array[i].first, edge_array[i].second, G); |
|---|
| 82 | |
|---|
| 83 | // Use the STL for_each algorithm to "exercise" all of the edges in |
|---|
| 84 | // the graph |
|---|
| 85 | for_each(edges(G).first, edges(G).second, exercise_edge<MyGraph>(G)); |
|---|
| 86 | cout << endl; |
|---|
| 87 | return 0; |
|---|
| 88 | } |
|---|