Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
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.
 
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 {
64 for (auto &edge : undirected_edges) {
65 neighbors[edge.first].push_back(edge.second);
66 neighbors[edge.second].push_back(edge.first);
67 }
68 }
std::vector< std::vector< int > > neighbors
for each vertex it stores a list indicies of its neighbors
Definition lowest_common_ancestor.cpp:77
T push_back(T... args)
T resize(T... args)
Here is the call graph for this function:

Member Function Documentation

◆ add_edge()

template<typename T >
void graph::Graph< T >::add_edge ( T u,
T 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

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

◆ breadth_first_search()

template<typename T >
std::map< T, bool > graph::Graph< T >::breadth_first_search ( T 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

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