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.
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.
<?phpfunctionbfs($n,$m,$edges,$s){// Initialize the graph$graph=array_fill(1,$n,[]);foreach($edgesas$edge){list($u,$v)=$edge;$graph[$u][]=$v;$graph[$v][]=$u;}// Initialize distances$distances=array_fill(1,$n,-1);$distances[$s]=0;// BFS using a queue$queue=newSplQueue();$queue->enqueue($s);while(!$queue->isEmpty()){$node=$queue->dequeue();foreach($graph[$node]as$neighbor){if($distances[$neighbor]==-1){$distances[$neighbor]=$distances[$node]+6;$queue->enqueue($neighbor);}}}// Exclude the start node distance$result=[];foreach($distancesas$key=>$distance){if($key!=$s){$result[]=$distance;}}return$result;}?>
Explanation
Graph Initialization:
The graph is initialized as an adjacency list. Each node points to a list of its neighbors.
Distance Initialization:
A 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:
A queue is used to explore the graph level by level. The start node is enqueued first. For each node dequeued, its unvisited neighbors are enqueued, and their distances are updated.
Result Preparation:
The distances for all nodes except the start node are prepared for output.
Input Handling:
The input is read, and the BFS function is called for each test case. The results are then printed.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →
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 nodes
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.