Template Numerical Library: TNL::Graphs::GraphVertexView< MatrixView, Orientation > Struct Template Reference

Template Numerical Library version\ main:16b9213d5

Loading...
Searching...
No Matches
TNL::Graphs::GraphVertexView< MatrixView, Orientation > Struct Template Reference

View type for accessing individual graph vertices and their edges. More...

## Detailed Description

template<typename MatrixView, typename Orientation>
struct TNL::Graphs::GraphVertexView< MatrixView, Orientation >

View type for accessing individual graph vertices and their edges.

GraphVertexView provides access to a single vertex (node) in a graph, allowing iteration over its edges, accessing edge weights, and modifying edge properties. It is similar to a matrix row view but provides graph-specific interface methods.

The GraphVertexView is typically obtained from a Graph using methods like getVertex() or during parallel traversal with forAllVertices(). It wraps either a dense or sparse matrix row view, depending on the underlying adjacency matrix type.

Template Parameters

  MatrixView | Type of the underlying matrix view (DenseMatrixView or SparseMatrixView).
  Orientation | Graph orientation (DirectedGraph or UndirectedGraph).

Example
  #include <iostream>
  #include <TNL/Graphs/Graph.h>
  #include <TNL/Graphs/traverse.h>
  #include <TNL/Containers/Array.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  #include <TNL/Devices/Hip.h>
  template< typename Device >
  void
  vertexViewExample()
  {
  using GraphType = TNL::Graphs::Graph< float, Device, int, TNL::Graphs::DirectedGraph >;
  /***
  * Create a directed graph
  */
  // clang-format off
  GraphType graph( 5,
  { { 0, 1, 10.0 }, { 0, 2, 20.0 },
  { 1, 2, 30.0 }, { 1, 3, 40.0 }, { 1, 4, 50.0 },
  { 2, 3, 60.0 },
  { 3, 0, 70.0 }, { 3, 4, 80.0 },
  { 4, 0, 90.0 } } );
  // clang-format on
  std::cout << "Graph:\n" << graph << '\n';
  /***
  * Modifying edge weights using forAllVertices
  */
  std::cout << "\nExample 3: Modifying edge weights\n";
  TNL::Graphs::forAllVertices(
  graph,
  [] __cuda_callable__( typename GraphType::VertexView vertex )
  {
  for( int i = 0; i < vertex.getDegree(); i++ ) {
  vertex.setEdgeWeight( i, vertex.getEdgeWeight( i ) + 1.0 );
  }
  } );
  std::cout << "Graph after modifying edge weights:\n" << graph << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Running on host:\n";
  vertexViewExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Running on CUDA device:\n";
  vertexViewExample< TNL::Devices::Cuda >();
  #endif
  #ifdef __HIP__
  std::cout << "Running on HIP device:\n";
  vertexViewExample< TNL::Devices::Hip >();
  #endif
  return EXIT_SUCCESS;
  }
  __cuda_callable__
  #define __cuda_callable__
  Definition Macros.h:49
  std::cout
  TNL::Graphs::forAllVertices
  void forAllVertices(Graph &graph, Function &&function, TNL::Algorithms::Segments::LaunchConfiguration launchConfig=TNL::Algorithms::Segments::LaunchConfiguration())
  Iterates in parallel over all graph vertices and applies the given lambda function to each vertex.
  TNL::Graphs::Graph
  Graph class represents a mathematical graph using an adjacency matrix.
  Definition Graph.h:57

Output
  Running on host:
  Graph:
  Row: 0 -> 1:10 2:20
  Row: 1 -> 2:30 3:40 4:50
  Row: 2 -> 3:60
  Row: 3 -> 0:70 4:80
  Row: 4 -> 0:90
  Example 3: Modifying edge weights
  Graph after modifying edge weights:
  Row: 0 -> 1:11 2:21
  Row: 1 -> 2:31 3:41 4:51
  Row: 2 -> 3:61
  Row: 3 -> 0:71 4:81
  Row: 4 -> 0:91

The documentation for this struct was generated from the following file:

- src/TNL/Graphs/GraphVertexView.h