TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Depth First Search Algorithm using Stack (Depth First Search Algorithm) More...
#include <iostream>
#include <stack>
#include <vector>
#include <cassert>
#include <limits>
Go to the source code of this file.
Namespaces | |
namespace | graph |
Graph Algorithms. | |
namespace | depth_first_search |
Functions for Depth First Search algorithm. | |
Functions | |
void | graph::depth_first_search::addEdge (std::vector< std::vector< size_t > > *adj, size_t u, size_t v) |
Adds and edge between two vertices of graph say u and v in this case. | |
std::vector< size_t > | graph::depth_first_search::dfs (const std::vector< std::vector< size_t > > &graph, size_t start) |
Explores the given vertex, exploring a vertex means traversing over all the vertices which are connected to the vertex that is currently being explored and push it onto the stack. | |
static void | tests () |
int | main () |
Main function. | |
Variables | |
constexpr int | WHITE = 0 |
constexpr int | GREY = 1 |
indicates the node hasn't been explored | |
constexpr int | BLACK = 2 |
indicates node is in stack waiting to be explored | |
constexpr int64_t | INF = std::numeric_limits<int16_t>::max() |
indicates node has already been explored | |
Depth First Search Algorithm using Stack (Depth First Search Algorithm)
Depth First Search also quoted as DFS is a Graph Traversal Algorithm. Time Complexity O(|V| + |E|) where V is number of vertices and E is number of edges in graph.
Application of Depth First Search are
Definition in file depth_first_search_with_stack.cpp.
void graph::depth_first_search::addEdge | ( | std::vector< std::vector< size_t > > * | adj, |
size_t | u, | ||
size_t | v ) |
Adds and edge between two vertices of graph say u and v in this case.
adj | Adjacency list representation of graph |
u | first vertex |
v | second vertex |
Definition at line 64 of file depth_first_search_with_stack.cpp.
std::vector< size_t > graph::depth_first_search::dfs | ( | const std::vector< std::vector< size_t > > & | graph, |
size_t | start ) |
Explores the given vertex, exploring a vertex means traversing over all the vertices which are connected to the vertex that is currently being explored and push it onto the stack.
adj | graph |
start | starting vertex for DFS |
checked[i] stores the status of each node
while stack is not empty we keep exploring the node on top of stack
push the node to the final result vector
exploring the neighbours of the current node
Node has been explored
Definition at line 87 of file depth_first_search_with_stack.cpp.
int main | ( | void | ) |
Main function.
creating a graph
taking input for the edges
taking input for the starting position
Printing the order of traversal
Definition at line 173 of file depth_first_search_with_stack.cpp.
|
static |
Self-test implementations
Test 1
for the above sample data, this is the expected output
Test 2
for the above sample data, this is the expected output
Test 3
for the above sample data, this is the expected output
Definition at line 123 of file depth_first_search_with_stack.cpp.
|
constexpr |
indicates node is in stack waiting to be explored
Definition at line 40 of file depth_first_search_with_stack.cpp.
|
constexpr |
indicates the node hasn't been explored
Definition at line 39 of file depth_first_search_with_stack.cpp.
|
constexpr |
indicates node has already been explored
Definition at line 41 of file depth_first_search_with_stack.cpp.
|
constexpr |
for IO operations header for std::stack header for std::vector header for preprocessor macro assert() header for limits of integral types
Definition at line 38 of file depth_first_search_with_stack.cpp.