-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat: add Depth first search #2815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adi776borate
wants to merge
11
commits into
TheAlgorithms:master
Choose a base branch
from
adi776borate:add/number_of_paths
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
91526d3
feat: add count_paths algorithm with a test case
adi776borate 7aa59ac
Merge branch 'master' into add/number_of_paths
adi776borate 13fff24
fix: updated number_of_paths algorithm, added more test cases, and se…
adi776borate 2cc7ed9
Merge branch 'master' into add/number_of_paths
adi776borate 4f981e8
fix: replaced unsigned int with std::uint32_t for fixed size
adi776borate 12c94c1
fix: Handled empty graph, invalid input and added test cases for the …
adi776borate ee6e56c
Merge branch 'master' into add/number_of_paths
adi776borate 80e27ba
Merge branch 'master' into add/number_of_paths
realstealthninja 65e03d6
clang-format and clang-tidy fixes for 80e27baa
github-actions[bot] 0aee341
Merge branch 'master' into add/number_of_paths
realstealthninja 935fd8d
Merge branch 'master' into add/number_of_paths
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/** | ||
* @file | ||
* @brief Algorithm to count paths between two nodes in a directed graph using DFS | ||
* @details | ||
* This algorithm implements Depth First Search (DFS) to count the number of | ||
* possible paths between two nodes in a directed graph. It is represented using | ||
* an adjacency matrix. The algorithm recursively traverses the graph to find | ||
* all paths from the source node `u` to the destination node `v`. | ||
* | ||
* @author [Aditya Borate](https://github.com/adi776borate) | ||
* @see https://en.wikipedia.org/wiki/Path_(graph_theory) | ||
*/ | ||
|
||
#include <vector> /// for std::vector | ||
#include <iostream> /// for IO operations | ||
#include <cassert> /// for assert | ||
#include <cstdint> /// for fixed-size integer types (e.g., std::uint32_t) | ||
|
||
/** | ||
* @namespace graph | ||
* @brief Graph algorithms | ||
*/ | ||
namespace graph { | ||
|
||
/** | ||
* @brief Helper function to perform DFS and count the number of paths from node `u` to node `v` | ||
* @param A adjacency matrix representing the graph (1: edge exists, 0: no edge) | ||
* @param u the starting node | ||
* @param v the destination node | ||
* @param n the number of nodes in the graph | ||
* @param visited a vector to keep track of visited nodes in the current DFS path | ||
* @returns the number of paths from node `u` to node `v` | ||
*/ | ||
std::uint32_t count_paths_dfs(const std::vector<std::vector<std::uint32_t>>& A, | ||
std::uint32_t u, | ||
std::uint32_t v, | ||
std::uint32_t n, | ||
std::vector<bool>& visited) { | ||
if (u == v) { | ||
return 1; // Base case: Reached the destination node | ||
} | ||
|
||
visited[u] = true; // Mark the current node as visited | ||
std::uint32_t path_count = 0; // Count of all paths from `u` to `v` | ||
|
||
for (std::uint32_t i = 0; i < n; i++) { | ||
if (A[u][i] == 1 && !visited[i]) { // Check if there is an edge and the node is not visited | ||
path_count += count_paths_dfs(A, i, v, n, visited); // Recursively explore paths from `i` to `v` | ||
} | ||
} | ||
|
||
visited[u] = false; // Unmark the current node as visited (backtracking) | ||
return path_count; | ||
} | ||
|
||
|
||
/** | ||
* @brief Counts the number of paths from node `u` to node `v` in a directed graph | ||
* using Depth First Search (DFS) | ||
* | ||
* @param A adjacency matrix representing the graph (1: edge exists, 0: no edge) | ||
* @param u the starting node | ||
* @param v the destination node | ||
* @param n the number of nodes in the graph | ||
* @returns the number of paths from node `u` to node `v` | ||
*/ | ||
std::uint32_t count_paths(const std::vector<std::vector<std::uint32_t>>& A, | ||
std::uint32_t u, | ||
std::uint32_t v, | ||
std::uint32_t n) { | ||
// Check for invalid nodes or empty graph | ||
if (u >= n || v >= n || A.empty() || A[0].empty()) { | ||
return 0; // No valid paths if graph is empty or nodes are out of bounds | ||
} | ||
|
||
std::vector<bool> visited(n, false); // Initialize a visited vector for tracking nodes | ||
return count_paths_dfs(A, u, v, n, visited); // Start DFS | ||
} | ||
|
||
} // namespace graph | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
// Test case 1: Simple directed graph with multiple paths | ||
std::vector<std::vector<std::uint32_t>> graph1 = { | ||
{0, 1, 0, 1, 0}, | ||
{0, 0, 1, 0, 1}, | ||
{0, 0, 0, 0, 1}, | ||
{0, 0, 1, 0, 0}, | ||
{0, 0, 0, 0, 0} | ||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
std::uint32_t n1 = 5, u1 = 0, v1 = 4; | ||
assert(graph::count_paths(graph1, u1, v1, n1) == 3); // There are 3 paths from node 0 to 4 | ||
|
||
// Test case 2: No possible path (disconnected graph) | ||
std::vector<std::vector<std::uint32_t>> graph2 = { | ||
{0, 1, 0, 0, 0}, | ||
{0, 0, 0, 0, 0}, | ||
{0, 0, 0, 0, 1}, | ||
{0, 0, 1, 0, 0}, | ||
{0, 0, 0, 0, 0} | ||
}; | ||
std::uint32_t n2 = 5, u2 = 0, v2 = 4; | ||
assert(graph::count_paths(graph2, u2, v2, n2) == 0); // No path from node 0 to 4 | ||
|
||
// Test case 3: Cyclic graph with multiple paths | ||
std::vector<std::vector<std::uint32_t>> graph3 = { | ||
{0, 1, 0, 0, 0}, | ||
{0, 0, 1, 1, 0}, | ||
{1, 0, 0, 0, 1}, | ||
{0, 0, 1, 0, 1}, | ||
{0, 0, 0, 0, 0} | ||
}; | ||
std::uint32_t n3 = 5, u3 = 0, v3 = 4; | ||
assert(graph::count_paths(graph3, u3, v3, n3) == 3); // There are 3 paths from node 0 to 4 | ||
|
||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Test case 4: Single node graph (self-loop) | ||
std::vector<std::vector<std::uint32_t>> graph4 = { | ||
{0} | ||
}; | ||
std::uint32_t n4 = 1, u4 = 0, v4 = 0; | ||
assert(graph::count_paths(graph4, u4, v4, n4) == 1); // There is self-loop, so 1 path from node 0 to 0 | ||
|
||
// Test case 5: Empty graph (no nodes, no paths) | ||
std::vector<std::vector<std::uint32_t>> graph5 = {{}}; | ||
int n5 = 0, u5 = 0, v5 = 0; | ||
assert(graph::count_paths(graph5, u5, v5, n5) == 0); // There are no paths in an empty graph | ||
|
||
// Test case 6: Invalid nodes (out of bounds) | ||
std::vector<std::vector<std::uint32_t>> graph6 = { | ||
{0, 1, 0}, | ||
{0, 0, 1}, | ||
{0, 0, 0} | ||
}; | ||
int n6 = 3, u6 = 0, v6 = 5; // Node `v` is out of bounds (n = 3, so valid indices are 0, 1, 2) | ||
assert(graph::count_paths(graph6, u6, v6, n6) == 0); // Should return 0 because `v = 5` is invalid | ||
|
||
std::cout << "All tests have successfully passed!\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // Run self-test implementations | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.