Statistics
| Branch: | Revision:

root / custompackages / hello-boost-graph / src / hello-boost-graph.cpp @ 146281ca

History | View | Annotate | Download (2.22 KB)

1
#include <iostream>                  // for std::cout
2
#include <utility>                   // for std::pair
3
#include <algorithm>                 // for std::for_each
4
#include <boost/graph/graph_traits.hpp>
5
#include <boost/graph/adjacency_list.hpp>
6
#include <boost/graph/dijkstra_shortest_paths.hpp>
7

    
8
using namespace boost;
9

    
10
template <typename VertexDescriptor, typename VertexNameMap>
11
void print_vertex_name(VertexDescriptor v, VertexNameMap name_map)
12
{
13
    std::cout << get(name_map, v);
14
}
15

    
16
int main(int,char*[])
17
{
18
    // create a typedef for the Graph type
19
    typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;
20

    
21
    // Make convenient labels for the vertices
22
    enum { A, B, C, D, E, N };
23
    const int num_vertices = N;
24
    const char* name = "ABCDE";
25

    
26
    // writing out the edges in the graph
27
    typedef std::pair<int, int> Edge;
28
    Edge edge_array[] =
29
    { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
30
    Edge(C,E), Edge(B,D), Edge(D,E) };
31
    const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
32

    
33
    // declare a graph object
34
    Graph g(num_vertices);
35

    
36
    // add the edges to the graph object
37
    for (int i = 0; i < num_edges; ++i)
38
    {
39
        add_edge(edge_array[i].first, edge_array[i].second, g);
40
    }
41

    
42
    // Accessing the Vertex Set
43

    
44
    // get the property map for vertex indices
45
    typedef property_map<Graph, vertex_index_t>::type IndexMap;
46
    IndexMap index = get(vertex_index, g);
47

    
48
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
49
    std::pair<vertex_iter, vertex_iter> vp;
50
    for (vp = vertices(g); vp.first != vp.second; ++vp.first)
51
        std::cout << index[*vp.first] <<  " ";
52
    std::cout << std::endl;
53

    
54
    // Accessing the Edge Set
55
    graph_traits<Graph>::edge_iterator ei, ei_end;
56
    for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
57
    {
58
        std::cout << "(" << index[source(*ei, g)]
59
                  << "," << index[target(*ei, g)] << ")";
60
    }
61
    std::cout << std::endl;
62

    
63
//    graph_traits<Graph>::vertex_iterator vi, vi_end;
64
//    for (tie(vi, vi_end) = vertices(G); vi != vi_end; ++vi)
65
//    {
66
//        std::cout << int(vi) << ' ';
67
//    }
68

    
69
//    for(Edge p = vertices(g); p.first != p.second; ++p.first)
70
//    {
71
//        std::cout << int(p.first) << ' ';
72
//    }
73

    
74
    return 0;
75
}