We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Breadth First Search: Shortest Reach

# Breadth First Search: Shortest Reach

#### Sort by

recency

#### |

#### 692 Discussions

#### |

Please Login in order to post a comment

C# solution

public static List bfs(int n, int m, List> edges, int s) { //Initializing graph var edgeWeight = 6; var graph = new Dictionary>(); for (int i = 1; i <= n; i++) graph[i] = new List();

}

shortest python solution

Here's a PHP solution for the "Breadth First Search: Shortest Reach" problem on HackerRank. This implementation uses a BFS algorithm to find the shortest path in an unweighted graph.

## Explanation

Graph Initialization:Distance Initialization:`distances`

array is initialized to store the shortest distance from the start node`s`

to each node. It is initially set to -1 for all nodes to indicate they are unvisited, and 0 for the start node.BFS Implementation:Result Preparation:Input Handling:This solution reads input directly from

`php://stdin`

and is designed to be run in a competitive programming environment where inputs are provided in a specific format. For testing locally, you might need to modify the input handling part to use hardcoded values or read from a file.