C ++ Boost Graph Library BADE Priority Optical Algorithm Example

xiaoxiao2021-03-06  20

// (organize by Robinkin from Devonit)

#include

#include

#include

#include

#include

USING NAMESPACE BOOST;

Template Class BFS_TIME_VISITOR: PUBLIC DEFAULT_BFS_Visitor {

TypedEf TypeName Property_Traits :: Value_Type T;

PUBLIC:

BFS_TIME_VISITOR (Timemap TMAP, T & T): M_TIMEMAP (TMAP), M_TIME (T) {}

Template

Void Discover_Vertex (Vertex U, Const Graph & G) Const

{

PUT (M_TIMEMAP, U, M_TIME ); // Access M_TIME plus 1 each time

}

TimeMap m_timemap;

T & M_TIME;

}

int

Main ()

{

USING NAMESPACE BOOST;

// Select the graph type we wish to us

Typedef adjacency_list graph_t;

// set up the vertex ids and names

ENUM {R, S, T, U, V, W, X, Y, N};

Const char * name = "Rstuvwxy";

// Specify the Edges in the graph

Typedef std :: pair e; // side pair

E Edge_Array [] = {E (R, S), E (R, V), E (S, W), E (W, R), E (W, T),

E (w, x), e (x, t), e (t, u), e (x, y), e (u, y)

}

// Create the graph Object

Const int n_edges = sizeof (edge_array) / sizeof (e);

#if Defined (boost_msvc) && boost_msvc <= 1300

// VC HAS Trouble with the Edge Iterator Constructor

GRAPH_T G (N);

For (std :: size_t j = 0; j

Add_edge (Edge_Array [J] .first, Edge_Array [J] .Second, G);

#ELSE

Typedef graph_traits :: vertices_size_type v_size_t;

GRAPH_T G (Edge_Array, Edge_Array n_edges, v_size_t (n)); // Constructive

#ENDIF

// typedefs

Typedef graph_traits :: vertex_descriptor vertex; typedef graph_traits :: vertices_size_type size;

Typedef size * iiter;

// a Vector to Hold The Discover Time Property for Each Vertex

Std :: Vector DTIME (NUM_VERTICES (G)); // Access time

Size Time = 0;

BFS_TIME_VISITOR VIS (& DTIME [0], TIME);

Breadth_First_Search (G, Vertex (S, G), Visitor (VIS));

// use std :: sort to order the vertices by their discover time

Std :: Vector :: vertice_size_type> discover_order (n);

Integer_Range Range (0, n);

Std :: Copy (Range.begin (), Range.end (), Discover_Order.begin ());

Std :: Sort (discover_order.begin (), discover_order.end (),

Indirect_cmp > (& DTIME [0]));

Std :: Cout << "Order of Discovery:";

For (int i = 0; i

Std :: cout << name [discover_order [i]] << "

Std :: cout << std :: endl;

Return EXIT_SUCCESS;

}

// Output:

Order of Discovery: S R w v T x u y

转载请注明原文地址:https://www.9cbs.com/read-53662.html

New Post(0)