Profile picture
Alvin ZablanFounder at Structy and React Formula | ex-Google

Undirected Path Approach

In this lesson, Alvin explores the strategy to solving the following interview problem:

Write a function, undirected_path, that takes in a list of edges for an undirected graph and two nodes (*node_A*, node_B). The function should return a boolean indicating whether or not there exists a path between node_A and node_B.

edges = [
('i', 'j'),
('k', 'i'),
('m', 'k'),
('k', 'l'),
('o', 'n')
]

undirected_path(edges, 'j', 'm') # -> True
edges = [
('i', 'j'),
('k', 'i'),
('m', 'k'),
('k', 'l'),
('o', 'n')
]

undirected_path(edges, 'm', 'j') # -> True
edges = [
('i', 'j'),
('k', 'i'),
('m', 'k'),
('k', 'l'),
('o', 'n')
]

undirected_path(edges, 'l', 'j') # -> True
edges = [
('i', 'j'),
('k', 'i'),
('m', 'k'),
('k', 'l'),
('o', 'n')
]

undirected_path(edges, 'k', 'o') # -> 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/