Ludium
Sign In
Introduction to Computational Thinking and Data Science
Optimization
01Optimization Models and the Knapsack Problem02Brute-Force Knapsack and the Exponential Power Set03Greedy Algorithms: One Function, Any Definition of Best04Lambda Keys and Why Greedy Can Miss the OptimumProblem set0/10MIT problem set0/2
01The Knapsack Search Tree: Every Combination, 2ⁿ Nodes02Recursive maxVal: An Exhaustive Search That Beats Greedy03Memoization: Turning Slow Fibonacci into fastFib04Overlapping Subproblems and the Memoized KnapsackProblem set0/10MIT problem set0/2
Graph Models
01Graphs as Models: Nodes, Edges and Euler's Seven Bridges02Coding a Graph in Python: Node, Edge, Digraph and Graph03Depth-First Search for the Shortest Path in a Graph04Breadth-First Search: A Queue of Paths, Fewest Hops FirstProblem set0/10MIT problem set0/4

Graphs as Models: Nodes, Edges and Euler's Seven Bridges

What do the Bridges of Königsberg, a rail map and a social network share, and why does adding a weight to an edge change the question?


Finger exercises

Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.

0 / 5 passed
Exercise 1 of 5

Count the bridges at each land mass

Euler's model of Königsberg has four land masses as nodes and seven bridges as undirected edges, given as pairs in bridges. Count how many bridge ends touch each land mass (an edge adds one to each of its two nodes) and store the counts in a dictionary degree mapping each land-mass name to its count.

1
2
3
4
5
6
7
bridges = [('Central', 'North'), ('Central', 'North'),
           ('Central', 'South'), ('Central', 'South'),
           ('Central', 'East'), ('East', 'North'), ('East', 'South')]
degree = {}
# Count the bridges at each land mass.

Python runs in your browser
⌘/Ctrl + Enter runs
?
is a dict with the four land masses
?
counts both ends of every bridge
?
matches Euler's counts
←Previous Overlapping Subproblems and the Memoized KnapsackNext Coding a Graph in Python: Node, Edge, Digraph and Graph →