Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/graph/example/strong_components.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 2.1 KB
Line 
1//=======================================================================
2// Copyright 1997-2001 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 <vector>
13#include <boost/graph/strong_components.hpp>
14#include <boost/graph/adjacency_list.hpp>
15#include <boost/graph/graphviz.hpp>
16#include <boost/graph/graph_utility.hpp>
17/*
18  Sample output:
19  A directed graph:
20  a --> b f h
21  b --> c a
22  c --> d b
23  d --> e
24  e --> d
25  f --> g
26  g --> f d
27  h --> i
28  i --> h j e c
29  j -->
30
31  Total number of components: 4
32  Vertex a is in component 3
33  Vertex b is in component 3
34  Vertex c is in component 3
35  Vertex d is in component 0
36  Vertex e is in component 0
37  Vertex f is in component 1
38  Vertex g is in component 1
39  Vertex h is in component 3
40  Vertex i is in component 3
41  Vertex j is in component 2
42 */
43
44int main(int, char*[])
45{
46  using namespace boost;
47  const char* name = "abcdefghij";
48
49  GraphvizDigraph G;
50  read_graphviz("scc.dot", G);
51
52  std::cout << "A directed graph:" << std::endl;
53  print_graph(G, name);
54  std::cout << std::endl;
55
56  typedef graph_traits<GraphvizGraph>::vertex_descriptor Vertex;
57   
58  std::vector<int> component(num_vertices(G)), discover_time(num_vertices(G));
59  std::vector<default_color_type> color(num_vertices(G));
60  std::vector<Vertex> root(num_vertices(G));
61  int num = strong_components(G, &component[0], 
62                              root_map(&root[0]).
63                              color_map(&color[0]).
64                              discover_time_map(&discover_time[0]));
65   
66  std::cout << "Total number of components: " << num << std::endl;
67  std::vector<int>::size_type i;
68  for (i = 0; i != component.size(); ++i)
69    std::cout << "Vertex " << name[i]
70         <<" is in component " << component[i] << std::endl;
71   
72  return 0;
73}
Note: See TracBrowser for help on using the repository browser.