In this lesson, Alvin explores the strategy to solving the following interview problem:
Write a function, has_path, that takes in a dictionary representing the adjacency list of a directed acyclic graph and two nodes (_src_, dst). The function should return a boolean indicating whether or not there exists a directed path between the source and destination nodes.
graph = {
'f': ['g', 'i'],
'g': ['h'],
'h': [],
'i': ['g', 'k'],
'j': ['i'],
'k': []
}
has_path(graph, 'f', 'k') # True
graph = {
'f': ['g', 'i'],
'g': ['h'],
'h': [],
'i': ['g', 'k'],
'j': ['i'],
'k': []
}
has_path(graph, 'f', 'j') # False
If you need additional support taking these DSA skills and actually applying them, take Alvin's complete data structures and algorithms course on Structy. You can try out the concepts yourself in their interactive code editor and learn advanced DSA patterns like stack exhaustive recursion.
Use this link to get 20% off the entire Structy DSA learning experience.
Follow Alvin on LinkedIn: https://www.linkedin.com/in/alvin-zablan-b73a92117/