Final challenge: flights and courses
After this: Pick the algorithm, work out the cost of a route, order the courses and write the line that relaxes an edge.
This challenge has three steps, and each step is scored on its own. First, you pick the algorithm for a cheapest flight and run it by hand. Second, you decide whether a list of courses can be put in order, before and after a new rule. Third, you write the one line that makes Dijkstra's algorithm relax an edge. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 3Cheapest flight
Cheapest flight · the algorithm
Every flight has a positive price. You want the cheapest way from HOME to ROM, with any number of stops. Which algorithm should you use?
Cheapest flight · the answer
Run the algorithm by hand. What is the cheapest price from HOME to ROM, and what is the route?
flights = { # prices are positive "HOME": [("PAR", 300), ("AMS", 120)], "AMS": [("PAR", 90), ("ROM", 350)], "PAR": [("ROM", 150)], "ROM": [],}Map<String, List<Edge>> flights = new LinkedHashMap<>(); // prices are positiveflights.put("HOME", List.of(new Edge("PAR", 300), new Edge("AMS", 120)));flights.put("AMS", List.of(new Edge("PAR", 90), new Edge("ROM", 350)));flights.put("PAR", List.of(new Edge("ROM", 150)));flights.put("ROM", List.of());Course schedule · the order
Each course lists what must be taken first. Is there an order that includes every course, and what is it? More than one option may be valid, so pick any one of them.
prerequisites = { # course -> the courses that must come first "programming": [], "data-structures": ["programming"], "databases": ["programming"], "algorithms": ["data-structures"], "systems": ["algorithms", "databases"],}Map<String, List<String>> prerequisites = new LinkedHashMap<>(); // course -> the courses that must come firstprerequisites.put("programming", List.of());prerequisites.put("data-structures", List.of("programming"));prerequisites.put("databases", List.of("programming"));prerequisites.put("algorithms", List.of("data-structures"));prerequisites.put("systems", List.of("algorithms", "databases"));Course schedule · a new rule
The department adds a rule: programming now requires systems. What does Kahn's algorithm report?
The line that relaxes an edge
Complete Dijkstra's algorithm. The missing line decides whether the edge from nodee.node() to next_nodeedge.to() gives a shorter way. The three lines under it must run only when it does.
def dijkstra(graph, start): distances = {node: float("inf") for node in graph} distances[start] = 0 parents = {start: None} heap = [(0, start)] while heap: dist, node = heapq.heappop(heap) if dist > distances[node]: continue for next_node, weight in graph[node]: new_dist = dist + weight ______________________________ # ← relax only when this is a shorter way to next_node distances[next_node] = new_dist parents[next_node] = node heapq.heappush(heap, (new_dist, next_node)) return distances, parents static Result run(Map<String, List<Edge>> graph, String start) { Map<String, Integer> distances = new HashMap<>(); for (String node : graph.keySet()) distances.put(node, Integer.MAX_VALUE); distances.put(start, 0); Map<String, String> parents = new HashMap<>(); parents.put(start, null); PriorityQueue<Entry> heap = new PriorityQueue<>(Comparator.comparingInt(Entry::dist).thenComparing(Entry::node)); heap.add(new Entry(0, start)); while (!heap.isEmpty()) { Entry e = heap.poll(); if (e.dist() > distances.get(e.node())) continue; for (Edge edge : graph.get(e.node())) { int newDist = e.dist() + edge.weight(); ______________________________ // ← relax only when this is a shorter way to edge.to distances.put(edge.to(), newDist); parents.put(edge.to(), e.node()); heap.add(new Entry(newDist, edge.to())); } } } return new Result(distances, parents); }- Choose the algorithm for a cheapest flight and work out its cost, decide whether a list of courses can be put in order, and write the test that relaxes an edge.