On this festive season, Santa is planning his route to deliver presents across a network of cities. The cities and the roads connecting them are represented as a graph. Your task is to determine whether there is a path from Santa's starting city to his destination city, using any graph traversal method (like Depth First Search or Breadth First Search).
You are given a graph represented as an adjacency list. Each key in the graph is a node (city), and its corresponding value is a list of neighboring nodes that are directly connected by a road.
Write a function findPath(graph, start, destination)
that returns true
if there is at least one valid path from the start
node to the destination
node. Otherwise, return false
.
graph
: A dictionary (or map/object) where keys are node identifiers and values are lists/arrays of adjacent nodes.start
: The node from which Santa begins his route.destination
: The node where Santa aims to deliver presents.true
if a path exists from start
to destination
, false
otherwise.For the graph:
{
'A': ['B', 'C'],
'B': ['D'],
'C': ['E'],
'D': ['F'],
'E': [],
'F': []
}
findPath(graph, 'A', 'F')
should return true
(A -> B -> D -> F).findPath(graph, 'A', 'E')
should return true
(A -> C -> E).findPath(graph, 'E', 'F')
should return false
.You can assume that the graph does not contain any cycles, but your solution should ideally handle them (in case of future enhancements).
Happy coding and have a joyful holiday season!