Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/graph.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.9 KB
Line 
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 <algorithm>
14
15#include <boost/graph/adjacency_list.hpp>
16
17using namespace boost;
18using namespace std;
19
20typedef property<vertex_color_t, default_color_type,
21    property<vertex_distance_t,int,
22      property<vertex_degree_t,int,
23        property<vertex_in_degree_t, int,
24          property<vertex_out_degree_t,int> > > > > VertexProperty;
25typedef property<edge_weight_t,int> EdgeProperty;
26typedef adjacency_list<vecS, vecS, bidirectionalS, 
27                       VertexProperty, EdgeProperty> Graph;
28
29template <class Graph>
30void print(Graph& g) {
31  typename Graph::vertex_iterator i, end;
32  typename Graph::out_edge_iterator ei, edge_end;
33  for(boost::tie(i,end) = vertices(g); i != end; ++i) {
34    cout << *i << " --> ";
35    for (boost::tie(ei,edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
36      cout << target(*ei, g) << "  ";
37    cout << endl;
38  }
39}
40
41std::size_t myrand(std::size_t N) {
42  std::size_t ret = rand() % N; 
43  //  cout << "N = " << N << "  rand = " << ret << endl;
44  return ret;
45}
46
47template <class Graph>
48bool check_edge(Graph& g, std::size_t a, std::size_t b) {
49  typedef typename Graph::vertex_descriptor Vertex;
50  typename Graph::adjacency_iterator vi, viend, found;
51  boost::tie(vi, viend) = adjacent_vertices(vertex(a,g), g);
52
53  found = find(vi, viend, vertex(b, g));
54  if ( found == viend )
55    return false;
56
57  return true;
58}
59
60int main(int, char*[])
61{
62  std::size_t N = 5;
63
64  Graph g(N);
65  int i;
66
67  bool is_failed = false;
68
69  for (i=0; i<6; ++i) {
70    std::size_t a = myrand(N), b = myrand(N);
71    while ( a == b ) b = myrand(N);
72    cout << "edge edge (" << a << "," << b <<")" << endl;
73    //add edges
74    add_edge(a, b, g);
75    is_failed =  is_failed || (! check_edge(g, a, b) );
76  }
77 
78  if ( is_failed )
79    cerr << "    Failed."<< endl;
80  else
81    cerr << "           Passed."<< endl;
82 
83  print(g);
84 
85  //remove_edge
86  for (i = 0; i<2; ++i) {
87    std::size_t a = myrand(N), b = myrand(N);
88    while ( a == b ) b = myrand(N);
89    cout << "remove edge (" << a << "," << b <<")" << endl;
90    remove_edge(a, b, g);
91    is_failed = is_failed || check_edge(g, a, b);
92  }
93  if ( is_failed )
94    cerr << "    Failed."<< endl;
95  else
96    cerr << "           Passed."<< endl;
97
98  print(g);
99 
100  //add_vertex
101  is_failed = false;
102  std::size_t old_N = N;
103  std::size_t vid   = add_vertex(g);
104  std::size_t vidp1 = add_vertex(g);
105 
106  N = num_vertices(g);
107  if ( (N - 2) != old_N )
108    cerr << "    Failed."<< endl;
109  else
110    cerr << "           Passed."<< endl;     
111 
112  is_failed = false;
113  for (i=0; i<2; ++i) {
114    std::size_t a = myrand(N), b = myrand(N);
115    while ( a == vid ) a = myrand(N);
116    while ( b == vidp1 ) b = myrand(N);
117    cout << "add edge (" << vid << "," << a <<")" << endl;
118    cout << "add edge (" << vid << "," << vidp1 <<")" << endl;
119    add_edge(vid, a, g);
120    add_edge(b, vidp1, g);
121    is_failed = is_failed || ! check_edge(g, vid, a);
122    is_failed = is_failed || ! check_edge(g, b, vidp1);
123  }
124  if ( is_failed )
125    cerr << "    Failed."<< endl;
126  else
127    cerr << "           Passed."<< endl;
128  print(g);
129 
130  // clear_vertex
131  std::size_t c = myrand(N);
132  is_failed = false;
133  clear_vertex(c, g);
134
135  if ( out_degree(c, g) != 0 )
136    is_failed = true;
137
138  cout << "Removing vertex " << c << endl;
139  remove_vertex(c, g);
140 
141  old_N = N;
142  N = num_vertices(g);
143 
144  if ( (N + 1) != old_N )
145    is_failed = true;
146 
147  if ( is_failed )
148    cerr << "    Failed."<< endl;
149  else
150    cerr << "           Passed."<< endl;     
151 
152  print(g);
153 
154  return 0;
155}
Note: See TracBrowser for help on using the repository browser.