Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Graph.cpp
1 #include "Teuchos_Graph.hpp"
2 
3 #include <iostream>
4 
5 #include "Teuchos_vector.hpp"
6 
7 namespace Teuchos {
8 
9 Graph make_graph_with_nnodes(int nnodes) {
10  return Graph(std::size_t(nnodes));
11 }
12 
13 int get_nnodes(Graph const& g) {
14  return size(g);
15 }
16 
17 void add_edge(Graph& g, int i, int j) {
18  at(g, i).push_back(j);
19 }
20 
21 NodeEdges const& get_edges(Graph const& g, int i) {
22  return at(g, i);
23 }
24 
25 NodeEdges& get_edges(Graph& g, int i) {
26  return at(g, i);
27 }
28 
29 int count_edges(const Graph& g, int i) {
30  return size(at(g, i));
31 }
32 
33 Graph make_transpose(Graph const& g) {
34  int nnodes = get_nnodes(g);
35  Graph transpose = make_graph_with_nnodes(nnodes);
36  for (int i = 0; i < nnodes; ++i) {
37  const NodeEdges& edges = get_edges(g, i);
38  for (NodeEdges::const_iterator it = edges.begin(); it != edges.end(); ++it) {
39  int j = *it;
40  add_edge(transpose, j, i);
41  }
42  }
43  return transpose;
44 }
45 
46 int at(Graph const& g, int i, int j) {
47  return at(at(g, i), j);
48 }
49 
50 std::ostream& operator<<(std::ostream& os, Graph const& g) {
51  for (int i = 0; i < get_nnodes(g); ++i) {
52  os << i << ":";
53  const NodeEdges& edges = get_edges(g, i);
54  for (NodeEdges::const_iterator it = edges.begin(); it != edges.end(); ++it) {
55  int j = *it;
56  os << " " << j;
57  }
58  os << '\n';
59  }
60  return os;
61 }
62 
63 }