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 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_Graph.hpp"
11 
12 #include <iostream>
13 
14 #include "Teuchos_vector.hpp"
15 
16 namespace Teuchos {
17 
18 Graph make_graph_with_nnodes(int nnodes) {
19  return Graph(std::size_t(nnodes));
20 }
21 
22 int get_nnodes(Graph const& g) {
23  return Teuchos::size(g);
24 }
25 
26 void add_edge(Graph& g, int i, int j) {
27  at(g, i).push_back(j);
28 }
29 
30 NodeEdges const& get_edges(Graph const& g, int i) {
31  return at(g, i);
32 }
33 
34 NodeEdges& get_edges(Graph& g, int i) {
35  return at(g, i);
36 }
37 
38 int count_edges(const Graph& g, int i) {
39  return Teuchos::size(at(g, i));
40 }
41 
42 Graph make_transpose(Graph const& g) {
43  int nnodes = get_nnodes(g);
44  Graph transpose = make_graph_with_nnodes(nnodes);
45  for (int i = 0; i < nnodes; ++i) {
46  const NodeEdges& edges = get_edges(g, i);
47  for (NodeEdges::const_iterator it = edges.begin(); it != edges.end(); ++it) {
48  int j = *it;
49  add_edge(transpose, j, i);
50  }
51  }
52  return transpose;
53 }
54 
55 int at(Graph const& g, int i, int j) {
56  return at(at(g, i), j);
57 }
58 
59 std::ostream& operator<<(std::ostream& os, Graph const& g) {
60  for (int i = 0; i < get_nnodes(g); ++i) {
61  os << i << ":";
62  const NodeEdges& edges = get_edges(g, i);
63  for (NodeEdges::const_iterator it = edges.begin(); it != edges.end(); ++it) {
64  int j = *it;
65  os << " " << j;
66  }
67  os << '\n';
68  }
69  return os;
70 }
71 
72 }