Skip to content
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

DFS algorithm #2914

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

DFS algorithm #2914

wants to merge 1 commit into from

Conversation

nikolassc
Copy link

This implementation of the Depth First Search (DFS) algorithm provides a robust and flexible solution for graph traversal. It uses an adjacency list representation of the graph, implemented with std::unordered_map<int, std::vector>, allowing it to efficiently handle sparse and large graphs. The algorithm is implemented recursively, visiting each node and its neighbors while tracking visited nodes using a std::unordered_set to prevent revisiting nodes during the traversal. This ensures it can handle cyclic graphs without entering infinite loops.

Additionally, the implementation includes error handling to address invalid starting nodes. If the provided starting node is not present in the graph, the function throws a std::invalid_argument exception with a clear error message, making it resilient against incorrect inputs. The algorithm has been tested on various graph scenarios, including connected, disconnected, and cyclic graphs, and demonstrates correct and predictable behavior in all cases.

The DFS algorithm is efficient, with a time complexity of O(V + E), where V is the number of vertices and E is the number of edges, and a space complexity of O(V) due to the use of the visited set and recursion stack. This implementation is designed to be clear, flexible, and scalable, suitable for a wide range of graph-related applications.

@nikolassc
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant