Unit 10 Session 2 Standard (Click for link to problem statements)
Unit 10 Session 2 Advanced (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?
flights
dictionary represent?
flights
is a starting location, and each value is a list of tuples where the first item is a destination and the second is the cost of the flight to that destination.start
and dest
?
-1
.HAPPY CASE
Input:
flights = {
'LAX': [('SFO', 50)],
'SFO': [('LAX', 50), ('ORD', 100), ('ERW', 210)],
'ERW': [('SFO', 210), ('ORD', 100)],
'ORD': [('ERW', 300), ('SFO', 100), ('MIA', 400)],
'MIA': [('ORD', 400)]
}
start = 'LAX'
dest = 'MIA'
Output:
550
Explanation: One possible path is LAX -> SFO -> ORD -> MIA with total cost 50 + 100 + 400 = 550.
EDGE CASE
Input:
flights = {
'LAX': [('SFO', 50)],
'SFO': [('LAX', 50)],
'MIA': []
}
start = 'LAX'
dest = 'MIA'
Output:
-1
Explanation: There is no possible path from 'LAX' to 'MIA'.
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 Weighted Graph problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use DFS to explore all possible paths from start
to dest
. At each step, keep track of the total cost. If a valid path to the destination is found, return the total cost. If no valid path exists, return -1
.
1) Initialize a `visited` set to track locations we have already visited to avoid cycles.
2) Define a recursive DFS function:
a) If the current location is `dest`, return the total cost.
b) Mark the current location as visited.
c) For each neighbor (destination) of the current location, recursively explore the path with the updated total cost.
d) If a valid path is found, return the total cost.
e) Backtrack by unmarking the current location as visited.
3) Start the DFS from the `start` location.
4) If no valid path is found, return `-1`.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def calculate_cost(flights, start, dest):
visited = set()
# Helper function for DFS with backtracking
def dfs(current, total_cost):
# If we reach the destination, return the cost
if current == dest:
return total_cost
# Mark the current node as visited
visited.add(current)
# Explore all possible flights from the current location
for neighbor, cost in flights.get(current, []):
if neighbor not in visited:
# Recursively calculate the cost of going to the neighbor
result = dfs(neighbor, total_cost + cost)
if result != -1: # If a valid path is found, return it immediately
return result
# Unmark the current node as visited (backtracking)
visited.remove(current)
# If no valid path is found, return -1
return -1
# Call DFS starting from the 'start' location
return dfs(start, 0)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
flights = {
'LAX': [('SFO', 50)],
'SFO': [('LAX', 50), ('ORD', 100), ('ERW', 210)],
'ERW': [('SFO', 210), ('ORD', 100)],
'ORD': [('ERW', 300), ('SFO', 100), ('MIA', 400)],
'MIA': [('ORD', 400)]
}
print(calculate_cost(flights, 'LAX', 'MIA')) # Expected output: 550 or 960
print(calculate_cost(flights, 'LAX', 'MIA')) # Another valid path should return 960
print(calculate_cost(flights, 'LAX', 'MIA')) # Should return -1 when no path is found
550 # LAX -> SFO -> ORD -> MIA
960 # LAX -> SFO -> ERW -> ORD -> MIA
-1 # No path found
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(V + E)
, where V
is the number of locations and E
is the number of flights. This accounts for exploring all flights in the worst case.O(V)
for the visited
set and recursion stack used in DFS.