Unit 10 Session 1 Advanced (Click for link to problem statements)
Unit 10 Session 2 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
Input:
celebrities1 = {
""Dev Patel"": [""Meryl Streep"", ""Viola Davis""],
""Meryl Streep"": [""Dev Patel"", ""Viola Davis""],
""Viola Davis"": [""Meryl Streep"", ""Viola Davis""]
}
Output: True
Explanation: Every celebrity can reach every other celebrity directly or indirectly.
EDGE CASE
Input:
celebrities2 = {
""John Cho"": [""Rami Malek"", ""Zoe Saldana"", ""Meryl Streep""],
""Rami Malek"": [""John Cho"", ""Zoe Saldana"", ""Meryl Streep""],
""Zoe Saldana"": [""Rami Malek"", ""John Cho"", ""Meryl Streep""],
""Meryl Streep"": []
}
Output: False
Explanation: ""Meryl Streep"" cannot reach anyone, and no one can reach her, making the network not strongly connected.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Strongly Connected Components problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: The group of celebrities is strongly connected if: 1) We can reach all celebrities starting from any celebrity. 2) All celebrities can reach each other in the reverse direction (i.e., a reversed graph).
We will perform the following steps: 1) Perform a DFS from any celebrity to check if we can visit all celebrities. 2) Create a transposed version of the graph (reverse edges). 3) Perform a DFS on the transposed graph to ensure we can visit all celebrities in the reversed direction.
1) Define a helper function `dfs(graph, start, visited)` to perform DFS from the given starting point.
2) Perform DFS on the original graph starting from any celebrity. Track the visited celebrities.
3) If not all celebrities are visited, return `False`.
4) Create a transposed graph where the edges are reversed.
5) Perform DFS on the transposed graph starting from the same celebrity.
6) If all celebrities are visited in the transposed graph, return `True`, otherwise return `False`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def is_strongly_connected(celebrities):
def dfs(graph, start, visited):
stack = [start]
while stack:
celeb = stack.pop()
if celeb not in visited:
visited.add(celeb)
stack.extend(graph[celeb])
# Step 1: Perform DFS from any celebrity
all_celebs = set(celebrities.keys())
start = next(iter(all_celebs)) # Get any starting celebrity
visited = set()
dfs(celebrities, start, visited)
# Check if we visited all celebrities
if visited != all_celebs:
return False
# Step 2: Create a transposed graph (reverse edges)
transposed_graph = {celeb: [] for celeb in celebrities}
for celeb, likes in celebrities.items():
for liked_celeb in likes:
transposed_graph[liked_celeb].append(celeb)
# Step 3: Perform DFS on the transposed graph
visited = set()
dfs(transposed_graph, start, visited)
# Check if we visited all celebrities in the transposed graph
return visited == all_celebs
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
celebrities1 = {
""Dev Patel"": [""Meryl Streep"", ""Viola Davis""],
""Meryl Streep"": [""Dev Patel"", ""Viola Davis""],
""Viola Davis"": [""Meryl Streep"", ""Viola Davis""]
}
celebrities2 = {
""John Cho"": [""Rami Malek"", ""Zoe Saldana"", ""Meryl Streep""],
""Rami Malek"": [""John Cho"", ""Zoe Saldana"", ""Meryl Streep""],
""Zoe Saldana"": [""Rami Malek"", ""John Cho"", ""Meryl Streep""],
""Meryl Streep"": []
}
print(is_strongly_connected(celebrities1)) # Output: True
print(is_strongly_connected(celebrities2)) # Output: False
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(V + E)
, where V
is the number of vertices (celebrities) and E
is the number of edges (likes). We perform DFS twice, once on the original graph and once on the transposed graph.O(V + E)
for storing the graph, transposed graph, and visited sets.