Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
graph::Graph< T > Class Template Reference
Inheritance diagram for graph::Graph< T >:
[legend]
Collaboration diagram for graph::Graph< T >:
[legend]

Public Member Functions

void add_edge (T u, T v, bool bidir=true)
 
std::map< T, bool > breadth_first_search (T src)
 
 Graph (size_t N, const std::vector< std::pair< int, int > > &undirected_edges)
 Populate the adjacency list for each vertex in the graph. Assumes that evey edge is a pair of valid vertex indices. More...
 
int number_of_vertices () const
 

Public Attributes

std::vector< std::vector< int > > neighbors
 for each vertex it stores a list indicies of its neighbors
 

Private Attributes

std::map< T, std::list< T > > adjacency_list
 

Detailed Description

template<typename T>
class graph::Graph< T >

Class for representing a graph as an adjacency list. Its vertices are indexed 0, 1, ..., N - 1.

Constructor & Destructor Documentation

◆ Graph()

template<typename T >
graph::Graph< T >::Graph ( size_t  N,
const std::vector< std::pair< int, int > > &  undirected_edges 
)
inline

Populate the adjacency list for each vertex in the graph. Assumes that evey edge is a pair of valid vertex indices.

Parameters
Nnumber of vertices in the graph
undirected_edgeslist of graph's undirected edges
62  {
63  neighbors.resize(N);
64  for (auto &edge : undirected_edges) {
65  neighbors[edge.first].push_back(edge.second);
66  neighbors[edge.second].push_back(edge.first);
67  }
68  }
Here is the call graph for this function:

Member Function Documentation

◆ add_edge()

template<typename T >
void graph::Graph< T >::add_edge ( u,
v,
bool  bidir = true 
)
inline

add_edge(u,v,bidir) is used to add an edge between node u and node v by default , bidir is made true , i.e graph is bidirectional . It means if edge(u,v) is added then u-->v and v-->u both edges exist.

to make the graph unidirectional pass the third parameter of add_edge as false which will

72  {
73  /**
74  * add_edge(u,v,bidir) is used to add an edge between node u and node v
75  * by default , bidir is made true , i.e graph is bidirectional .
76  * It means if edge(u,v) is added then u-->v and v-->u both edges exist.
77  *
78  * to make the graph unidirectional pass the third parameter of add_edge as
79  * false which will
80  */
81  adjacency_list[u].push_back(v); // u-->v edge added
82  if(bidir==true){
83  // if graph is bidirectional
84  adjacency_list[v].push_back(u); // v-->u edge added
85  }
86  }

◆ breadth_first_search()

template<typename T >
std::map<T,bool> graph::Graph< T >::breadth_first_search ( src)
inline

this function performs the breadth first search on graph and return a mapping which maps the nodes to a boolean value representing whether the node was traversed or not.

mapping to keep track of all visited nodes

initialise every possible vertex to map to false initially none of the vertices are unvisited

queue to store the nodes which are yet to be traversed

push the source vertex to queue to begin traversing

mark the source vertex as visited

traverse the graph till no connected vertex are left extract a node from queue for further traversal

remove the node from the queue

check every vertex connected to the node which are still unvisited

if the neighbour is unvisited , push it into the queue

mark the neighbour as visited

94  {
95  /// mapping to keep track of all visited nodes
96  std::map<T,bool> visited;
97  /// initialise every possible vertex to map to false
98  /// initially none of the vertices are unvisited
99  for(auto const &adjlist: adjacency_list){
100  visited[adjlist.first]=false;
101  for(auto const &node:adjacency_list[adjlist.first]){
102  visited[node]=false;
103  }
104  }
105 
106  /// queue to store the nodes which are yet to be traversed
107  std::queue<T> tracker;
108 
109  /// push the source vertex to queue to begin traversing
110  tracker.push(src);
111  ///mark the source vertex as visited
112  visited[src]=true;
113  while(!tracker.empty()){
114  /// traverse the graph till no connected vertex are left
115  /// extract a node from queue for further traversal
116  T node = tracker.front();
117  /// remove the node from the queue
118  tracker.pop();
119  for(T const &neighbour : adjacency_list[node]){
120  /// check every vertex connected to the node which are still unvisited
121  if(!visited[neighbour]){
122  /// if the neighbour is unvisited , push it into the queue
123  tracker.push(neighbour);
124  /// mark the neighbour as visited
125  visited[neighbour]=true;
126  }
127  }
128  }
129  return visited;
130  }
Here is the call graph for this function:

◆ number_of_vertices()

template<typename T >
int graph::Graph< T >::number_of_vertices ( ) const
inline

Function to get the number of vertices in the graph

Returns
the number of vertices in the graph.
74 { return neighbors.size(); }
Here is the call graph for this function:

Member Data Documentation

◆ adjacency_list

template<typename T >
std::map<T,std::list<T> > graph::Graph< T >::adjacency_list
private

adjacency_list maps every vertex to the list of its neighbours in the order in which they are added.


The documentation for this class was generated from the following files:
graph::Graph::adjacency_list
std::map< T, std::list< T > > adjacency_list
Definition: breadth_first_search.cpp:69
std::vector::resize
T resize(T... args)
std::vector::size
T size(T... args)
node
Definition: avltree.cpp:13
node
struct list node
std::queue
STL class.
std::queue::front
T front(T... args)
std::vector::push_back
T push_back(T... args)
std::queue::pop
T pop(T... args)
graph::Graph::neighbors
std::vector< std::vector< int > > neighbors
for each vertex it stores a list indicies of its neighbors
Definition: lowest_common_ancestor.cpp:77
std::map
STL class.
std::queue::empty
T empty(T... args)
std::queue::push
T push(T... args)