Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
graph Namespace Reference

Graph algorithms. More...

Functions

void add_directed_edge (std::vector< std::vector< int >> *graph, int u, int v)
 Adds a directed edge from vertex u to vertex v. More...
 
void add_undirected_edge (std::vector< std::vector< int >> *graph, int u, int v)
 Adds an undirected edge from vertex u to vertex v. Essentially adds too directed edges to the adjacency list reprsentation of the graph. More...
 
std::vector< bool > breadth_first_search (const std::vector< std::vector< int >> &graph, int start)
 Function performs the breadth first search algorithm over the graph. More...
 

Detailed Description

Graph algorithms.

Function Documentation

◆ add_directed_edge()

void graph::add_directed_edge ( std::vector< std::vector< int >> *  graph,
int  u,
int  v 
)

Adds a directed edge from vertex u to vertex v.

Parameters
graphAdjacency list representation of graph
ufirst vertex
vsecond vertex
66  {
67  (*graph)[u].push_back(v);
68 }

◆ add_undirected_edge()

void graph::add_undirected_edge ( std::vector< std::vector< int >> *  graph,
int  u,
int  v 
)

Adds an undirected edge from vertex u to vertex v. Essentially adds too directed edges to the adjacency list reprsentation of the graph.

Parameters
graphAdjacency list representation of graph
ufirst vertex
vsecond vertex
81  {
82  add_directed_edge(graph, u, v);
83  add_directed_edge(graph, v, u);
84 }
Here is the call graph for this function:

◆ breadth_first_search()

std::vector<bool> graph::breadth_first_search ( const std::vector< std::vector< int >> &  graph,
int  start 
)

Function performs the breadth first search algorithm over the graph.

Parameters
graphAdjacency list representation of graph
startvertex from where traversing starts
Returns
a binary vector indicating which vertices were visited during the search.

vector to keep track of visited vertices

a queue that stores vertices that need to be further explored

mark the starting vertex as visited

if the vertex is not visited then mark it as visited and push it to the queue

96  {
97  /// vector to keep track of visited vertices
98  std::vector<bool> visited(graph.size(), false);
99  /// a queue that stores vertices that need to be further explored
100  std::queue<int> tracker;
101 
102  /// mark the starting vertex as visited
103  visited[start] = true;
104  tracker.push(start);
105  while (!tracker.empty()) {
106  size_t vertex = tracker.front();
107  tracker.pop();
108  for (auto x : graph[vertex]) {
109  /// if the vertex is not visited then mark it as visited
110  /// and push it to the queue
111  if (!visited[x]) {
112  visited[x] = true;
113  tracker.push(x);
114  }
115  }
116  }
117  return visited;
118 }
Here is the call graph for this function:
std::vector< bool >
graph::add_directed_edge
void add_directed_edge(std::vector< std::vector< int >> *graph, int u, int v)
Adds a directed edge from vertex u to vertex v.
Definition: breadth_first_search.cpp:66
std::queue
STL class.
std::queue::front
T front(T... args)
std::queue::pop
T pop(T... args)
graph
Graph algorithms.
std::queue::empty
T empty(T... args)
std::queue::push
T push(T... args)