Graph Treasure Hunt

Graph Traversal Dfs Bfs

Graph Treasure Hunt

Imagine you are on a festive treasure hunt where each location is represented as a node in a graph. Your goal is to determine if the treasure (located at a specified node) is reachable from your starting point.

Problem Statement

You are given a directed graph represented as an adjacency list. The graph is provided as a mapping from node identifiers to a list of neighboring node identifiers. You are also given two integers: start (the starting node) and treasure (the target node where the treasure is hidden).

Write a function findTreasure(graph, start, treasure) that uses a graph traversal algorithm (either DFS or BFS) to determine if the treasure node is reachable from the starting node. Return true if it is reachable, otherwise return false.

Example

Input:

graph = {
  1: [2, 3],
  2: [4],
  3: [5],
  4: [6],
  5: [],
  6: []
}
start = 1
treasure = 6

Output:

true

Note: This problem has an extra festive twist; think of each node as a clue in a holiday treasure hunt!