Unit 12 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?
What is a province?
How does the input matrix represent connections?
is_connected[i][j] = 1
indicates that city i
is directly connected to city j
.Can the matrix have self-loops?
is_connected[i][i] = 1
indicates that a city is connected to itself.HAPPY CASE
Input: is_connected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
Explanation: The first two cities are connected to each other, forming one province. The third city is isolated, forming another province.
Input: is_connected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
Explanation: Each city is isolated, so there are 3 provinces.
EDGE CASE
Input: is_connected = [[1]]
Output: 1
Explanation: A single city is its own province.
Input: is_connected = [[1,1],[1,1]]
Output: 1
Explanation: Both cities are connected, forming one province.
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 Graph Traversal Problems, we can consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Treat the given matrix as an adjacency matrix representing a graph. Use DFS to explore all connected cities starting from each unvisited city. Every time we start a new DFS, it corresponds to discovering a new province.
1) Initialize a `visited` list to keep track of cities we have explored.
2) Define a DFS helper function:
a) Mark the current city as visited.
b) Explore all other cities that are connected to the current city and not visited.
3) Iterate through all cities. If a city is unvisited, call DFS on it and increment the province count.
4) Return the total number of provinces.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def num_provinces(is_connected):
def dfs(city):
# Mark the city as visited
visited[city] = True
# Explore all other cities connected to the current city
for neighbor in range(n):
if is_connected[city][neighbor] == 1 and not visited[neighbor]:
dfs(neighbor)
n = len(is_connected) # Number of cities
visited = [False] * n # Keep track of visited cities
province_count = 0
# Perform DFS for each unvisited city
for city in range(n):
if not visited[city]:
dfs(city)
province_count += 1 # Each DFS call represents a new province
return province_count
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Input: is_connected = 1,1,0],[1,1,0],[0,0,1
Input: is_connected = 1,0,0],[0,1,0],[0,0,1
Input: is_connected = 1
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
is the number of cities.
O(N^2)
because we potentially visit every element in the adjacency matrix.O(N)
due to the recursion stack in DFS and the visited
list.