Weighted Undirected Graph
Question
You are given a list of node edges, (u, v), where u has a path to v. Create an unweighted, undirected graph using an adjacency list.
Input: edges = [(1, 2), (1, 3), (2, 3)]
Output: {1: [2, 3], 2: [1, 3], 3: [1, 2]}
Input: edges = [(1, 2), (2, 3), (2, 4)]
Output: {1: [2], 2: [1, 3, 4], 3: [2], 4: [2]}
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
What graph should be returned, given the following edges? edges = [(1, 2), (1, 4), (2, 3), (4, 2)]
{1: [2, 4], 2: [1, 3, 4], 3: [2], 4: [1, 2]}
{1: [2, 4], 2: [3], 4: [2]}
{1: [2, 4], 2: [1, 3], 3: [2], 4: [1, 2]}
{1: [2, 4], 2: [1, 3, 4], 3: [2]}
Take a moment to understand the problem and think of your approach before you start coding.