mCRL2
Loading...
Searching...
No Matches
GraphOrdering.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2013 University of Twente
2// Copyright (c) 2009-2013 Michael Weber <michaelw@cs.utwente.nl>
3// Copyright (c) 2009-2013 Maks Verver <maksverver@geocities.com>
4// Copyright (c) 2009-2013 Eindhoven University of Technology
5//
6// Distributed under the Boost Software License, Version 1.0.
7// (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
11
12#include <queue>
13#include <stack>
14
16{
17 edgei res = 0;
18 for (verti v = 0; v < g.V(); ++v)
19 {
21 it != g.succ_end(v); ++it )
22 {
23 res += (dir < 0) ? (*it < v) :
24 (dir > 0) ? (*it > v) : (*it == v);
25 }
26 }
27 return res;
28}
29
30void get_bfs_order(const StaticGraph &graph, std::vector<verti> &perm)
31{
32 perm.assign(graph.V(), NO_VERTEX);
33
34 std::queue<verti> queue;
35 verti new_v = 0;
36 for (verti root = 0; root < graph.V(); ++root)
37 {
38 if (perm[root] != NO_VERTEX) continue;
39 perm[root] = new_v++;
40 queue.push(root);
41 while (!queue.empty())
42 {
43 verti v = queue.front();
44 queue.pop();
46 while (it != graph.succ_end(v))
47 {
48 verti w = *it++;
49 if (perm[w] == NO_VERTEX)
50 {
51 perm[w] = new_v++;
52 queue.push(w);
53 }
54 }
55 }
56 }
57 assert(new_v == graph.V());
58}
59
60void get_dfs_order(const StaticGraph &graph, std::vector<verti> &perm)
61{
62 perm.assign(graph.V(), NO_VERTEX);
63
64 std::stack<std::pair<verti, StaticGraph::const_iterator> > stack;
65 verti new_v = 0;
66 for (verti root = 0; root < graph.V(); ++root)
67 {
68 if (perm[root] != NO_VERTEX) continue;
69 perm[root] = new_v++;
70 stack.push(std::make_pair(root, graph.succ_begin(root)));
71 while (!stack.empty())
72 {
73 verti v = stack.top().first;
74 StaticGraph::const_iterator &it = stack.top().second;
75 if (it == graph.succ_end(v))
76 {
77 stack.pop();
78 }
79 else
80 {
81 verti w = *it++;
82 if (perm[w] == NO_VERTEX)
83 {
84 perm[w] = new_v++;
85 stack.push(std::make_pair(w, graph.succ_begin(w)));
86 }
87 }
88 }
89 }
90 assert(new_v == graph.V());
91}
void get_dfs_order(const StaticGraph &graph, std::vector< verti > &perm)
edgei count_ordered_edges(const StaticGraph &g, int dir)
void get_bfs_order(const StaticGraph &graph, std::vector< verti > &perm)
Functions to analyze vertex order in a static graph.
std::size_t edgei
type used to number edges
Definition Graph.h:25
std::size_t verti
type used to number vertices
Definition Graph.h:24
#define NO_VERTEX
Definition Graph.h:27
const_iterator succ_end(verti v) const
Definition Graph.h:193
const_iterator succ_begin(verti v) const
Definition Graph.h:188
verti V() const
Definition Graph.h:179
const verti * const_iterator
Definition Graph.h:66