Skip to content

bidirectional_bfs#226

Merged
siriak merged 2 commits intoTheAlgorithms:masterfrom
Prathameshk2024:feat-bidirectional_bfs
Oct 19, 2025
Merged

bidirectional_bfs#226
siriak merged 2 commits intoTheAlgorithms:masterfrom
Prathameshk2024:feat-bidirectional_bfs

Conversation

@Prathameshk2024
Copy link
Copy Markdown
Contributor

  1. Purpose:
    Finds the shortest path between a source and target node in an unweighted graph.

  2. Approach:
    Runs two simultaneous BFS searches — one from the source and one from the target — until both meet.

  3. Initialization:
    Sets up two queues, two visited lists, and two parent arrays for tracking paths.

  4. Search Process:
    Expands nodes alternately from both sides and checks for a meeting node (common visited vertex).

  5. Path Reconstruction:
    Once the meeting node is found, it traces back to form the complete shortest path from source to target.

Output:
Returns the path, distance (number of edges), and a flag found = TRUE if a path exists.

Complexity:

Time: O(b^(d/2)) → faster than normal BFS

Space: O(V)

Copilot AI review requested due to automatic review settings October 19, 2025 21:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a bidirectional breadth-first search algorithm for finding shortest paths in unweighted graphs. The algorithm runs two simultaneous BFS searches from source and target nodes until they meet, offering improved time complexity over standard BFS.

  • Implements bidirectional BFS with O(b^(d/2)) time complexity
  • Includes path reconstruction from meeting node to both endpoints
  • Provides comprehensive documentation and example usage

Comment thread graph_algorithms/bidirectional_bfs.r
Copy link
Copy Markdown
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 747bb49 into TheAlgorithms:master Oct 19, 2025
2 checks passed
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.

3 participants