TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 53 of file lowest_common_ancestor.cpp.

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

Definition at line 62 of file lowest_common_ancestor.cpp.

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 }
std::vector< std::vector< int > > neighbors
for each vertex it stores a list indicies of its neighbors

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

Definition at line 74 of file breadth_first_search.cpp.

74 {
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

◆ 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

Definition at line 96 of file breadth_first_search.cpp.

96 {
98 std::map<T, bool> visited;
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
109 std::queue<T> tracker;
110
112 tracker.push(src);
114 visited[src] = true;
115 while (!tracker.empty()) {
118 T node = tracker.front();
120 tracker.pop();
121 for (T const &neighbour : adjacency_list[node]) {
124 if (!visited[neighbour]) {
126 tracker.push(neighbour);
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

◆ 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.

Definition at line 74 of file lowest_common_ancestor.cpp.

74{ return neighbors.size(); }

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.

Definition at line 69 of file breadth_first_search.cpp.

◆ neighbors

template<typename T >
std::vector<std::vector<int> > graph::Graph< T >::neighbors

for each vertex it stores a list indicies of its neighbors

Definition at line 77 of file lowest_common_ancestor.cpp.


The documentation for this class was generated from the following files: