Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/graph/example/gerdemann.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: 4.6 KB
Line 
1// -*- c++ -*-
2//=======================================================================
3// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
4// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5//
6// Distributed under the Boost Software License, Version 1.0. (See
7// accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9//=======================================================================
10#include <boost/config.hpp>
11#include <iostream>
12
13#include <boost/graph/adjacency_list.hpp>
14
15/*
16  Thanks to Dale Gerdemann for this example, which inspired some
17  changes to adjacency_list to make this work properly.
18 */
19
20/*
21  Sample output:
22
23  0  --c--> 1   --j--> 1   --c--> 2   --x--> 2 
24  1  --c--> 2   --d--> 3 
25  2  --t--> 4 
26  3  --h--> 4 
27  4
28
29  merging vertex 1 into vertex 0
30
31  0  --c--> 0   --j--> 0   --c--> 1   --x--> 1   --d--> 2 
32  1  --t--> 3 
33  2  --h--> 3 
34  3
35 */
36
37// merge_vertex(u,v,g):
38// incoming/outgoing edges for v become incoming/outgoing edges for u
39// v is deleted
40template <class Graph, class GetEdgeProperties>
41void merge_vertex
42  (typename boost::graph_traits<Graph>::vertex_descriptor u,
43   typename boost::graph_traits<Graph>::vertex_descriptor v,
44   Graph& g, GetEdgeProperties getp)
45{
46  typedef boost::graph_traits<Graph> Traits;
47  typename Traits::edge_descriptor e;
48  typename Traits::out_edge_iterator out_i, out_end;
49  for (tie(out_i, out_end) = out_edges(v, g); out_i != out_end; ++out_i) {
50    e = *out_i;
51    typename Traits::vertex_descriptor targ = target(e, g);
52    add_edge(u, targ, getp(e), g);
53  }
54  typename Traits::in_edge_iterator in_i, in_end;
55  for (tie(in_i, in_end) = in_edges(v, g); in_i != in_end; ++in_i) {
56    e = *in_i;
57    typename Traits::vertex_descriptor src = source(e, g);
58    add_edge(src, u, getp(e), g);
59  }
60  clear_vertex(v, g);
61  remove_vertex(v, g);
62}
63
64template <class StoredEdge>
65struct order_by_name
66  : public std::binary_function<StoredEdge,StoredEdge,bool> 
67{
68  bool operator()(const StoredEdge& e1, const StoredEdge& e2) const {
69    // Using std::pair operator< as an easy way to get lexicographical
70    // compare over tuples.
71    return std::make_pair(e1.get_target(), boost::get(boost::edge_name, e1))
72      < std::make_pair(e2.get_target(), boost::get(boost::edge_name, e2));
73  }
74};
75struct ordered_set_by_nameS { };
76
77#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
78namespace boost {
79  template <class ValueType>
80  struct container_gen<ordered_set_by_nameS, ValueType> {
81    typedef std::set<ValueType, order_by_name<ValueType> > type;
82  };
83  template <>
84  struct parallel_edge_traits<ordered_set_by_nameS> { 
85    typedef allow_parallel_edge_tag type;
86  };
87}
88#endif
89
90template <class Graph>
91struct get_edge_name {
92  get_edge_name(const Graph& g_) : g(g_) { }
93
94  template <class Edge>
95  boost::property<boost::edge_name_t, char> operator()(Edge e) const {
96    return boost::property<boost::edge_name_t, char>(boost::get(boost::edge_name, g, e));
97  }
98  const Graph& g;
99};
100
101int
102main()
103{
104#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
105  std::cout << "This program requires partial specialization." << std::endl;
106#else
107  using namespace boost;
108  typedef property<edge_name_t, char> EdgeProperty;
109  typedef adjacency_list<ordered_set_by_nameS, vecS, bidirectionalS,
110    no_property, EdgeProperty> graph_type;
111
112  graph_type g;
113
114  add_edge(0, 1, EdgeProperty('j'), g);
115  add_edge(0, 2, EdgeProperty('c'), g);
116  add_edge(0, 2, EdgeProperty('x'), g);
117  add_edge(1, 3, EdgeProperty('d'), g);
118  add_edge(1, 2, EdgeProperty('c'), g);
119  add_edge(1, 3, EdgeProperty('d'), g);
120  add_edge(2, 4, EdgeProperty('t'), g);
121  add_edge(3, 4, EdgeProperty('h'), g);
122  add_edge(0, 1, EdgeProperty('c'), g);
123 
124  property_map<graph_type, vertex_index_t>::type id = get(vertex_index, g);
125  property_map<graph_type, edge_name_t>::type name = get(edge_name, g);
126
127  graph_traits<graph_type>::vertex_iterator i, end;
128  graph_traits<graph_type>::out_edge_iterator ei, edge_end;
129
130  for (boost::tie(i, end) = vertices(g); i != end; ++i) {
131    std::cout << id[*i] << " ";
132    for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
133      std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << "  ";
134    std::cout << std::endl;
135  }
136  std::cout << std::endl;
137
138  std::cout << "merging vertex 1 into vertex 0" << std::endl << std::endl;
139  merge_vertex(0, 1, g, get_edge_name<graph_type>(g));
140 
141  for (boost::tie(i, end) = vertices(g); i != end; ++i) {
142    std::cout << id[*i] << " ";
143    for (boost::tie(ei, edge_end) = out_edges(*i, g); ei != edge_end; ++ei)
144      std::cout << " --" << name[*ei] << "--> " << id[target(*ei, g)] << "  ";
145    std::cout << std::endl;
146  }
147  std::cout << std::endl;
148#endif 
149  return 0;
150}
Note: See TracBrowser for help on using the repository browser.