Skip to content

Add Graph Traversal algorithms (BFS and DFS) in Python#231

Merged
Pradeepsingh61 merged 1 commit intomainfrom
add-python-graph-bfs-dfs
Oct 1, 2025
Merged

Add Graph Traversal algorithms (BFS and DFS) in Python#231
Pradeepsingh61 merged 1 commit intomainfrom
add-python-graph-bfs-dfs

Conversation

@Karanjot786
Copy link
Copy Markdown
Collaborator

📝 Description

This PR adds comprehensive Graph Traversal implementations including BFS (Breadth-First Search) and DFS (Depth-First
Search) with both iterative and recursive approaches.

📂 Files Added

  • Python/data_structures/graphs/graph_traversal.py

✨ Features

  • Graph class: Adjacency list representation
  • Add edge methods: Support for both directed and undirected graphs
  • BFS (Breadth-First Search): Level-order traversal using queue
  • DFS (Depth-First Search): Two implementations:
    • Iterative version using stack
    • Recursive version
  • Connectivity check: Verify if graph is connected
  • Comprehensive test cases: 5 different scenarios including:
    • Simple undirected graph
    • Directed graph
    • Larger complex graph
    • Linear graph
    • Disconnected graph with connectivity check

📊 Complexity Analysis

  • Time Complexity: O(V + E) for both BFS and DFS
    • V = number of vertices
    • E = number of edges
  • Space Complexity: O(V) for visited set and queue/stack

🔑 Key Concepts

  • Graph representation using adjacency list (defaultdict)
  • BFS uses queue (FIFO) - finds shortest path in unweighted graphs
  • DFS uses stack (LIFO) - explores as deep as possible
  • Both iterative and recursive DFS implementations
  • Connectivity checking using DFS

🎯 Use Cases

  • BFS: Shortest path, level-order traversal, peer-to-peer networks
  • DFS: Path finding, cycle detection, topological sorting, maze solving

✅ Testing

All test cases run successfully demonstrating:

  • BFS and DFS on various graph structures
  • Both directed and undirected graphs
  • Iterative vs recursive DFS comparison
  • Connectivity verification
  • Edge cases with linear and disconnected graphs

🎃 Hacktoberfest 2025

This contribution is part of Hacktoberfest 2025. The code follows the repository's structure (Language/Topic/FileName) and
includes proper documentation.

📋 Checklist

  • Code follows repository structure (Python/data_structures/graphs/)
  • Proper documentation and comments added
  • Test cases included
  • Time and space complexity documented
  • No user input required (tests run automatically)
  • Code is well-formatted and readable

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 1, 2025

🎉 Welcome to Hacktoberfest 2025, @Karanjot786! 🎃

Thank you for your first contribution to our DSA repository! Here's what happens next:

🔍 Automatic Checks

  • Code Validation: Passed
  • 🧪 Compilation Tests: Passed

📋 Next Steps

🎯 Great job! Your code compiled successfully. Maintainers @Karanjot786 and @Pradeepsingh61 will review your PR soon.

🎁 What You Get

  • 🏆 Hacktoberfest Credit: This PR counts toward your 6 PR goal for exclusive T-shirt + Tree!
  • 🌟 Hall of Fame: You'll be featured in our contributors list
  • 📚 Learning: Code review feedback from experienced developers

💡 Tips for Success

  • Follow our Contributing Guidelines
  • Add comments explaining your algorithm
  • Include time/space complexity analysis
  • Test your code before submitting

Welcome to the community! 🚀

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 1, 2025

🤖 Automated PR Status

🔍 Code Validation

Passed - File naming and structure look good!

🧪 Compilation Tests

Passed - All code compiles successfully!

📋 Overall Status

🎉 Ready for Review - Your PR has passed all automated checks!
👥 Maintainers have been notified for review.


This comment was generated automatically. Checks will re-run when you push new commits.

@Pradeepsingh61 Pradeepsingh61 merged commit a8ff164 into main Oct 1, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants