The above image represents a map of Königsberg. The green areas are land and the yellow lines are bridges.
The emperor of the kingdom wants to set up a train service that transports people from a landmass to another within the empire using the bridges.
There are people in the red castle who want to go to the blue castle subject to the following conditions:
How many possible train routes could they take?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
It is clear that we could reduce this into a multigraph as follows:
We want to ask, How many walks are there from the red node to the blue node that does not repeat edges?
Suppose ways(bridges, node, target) is the number of ways one can reach the target from the current node using the bridges currently available. Then, at this point, we could use one of the bridges accessible to us right now and try to reach the target from that node.
ways(bridges, node, target) = i ∈ b r i d g e s [ n o d e ] ∑ ways(bridges \ { i } , node adjacent to i, target)
Now, we can run this with:
For a small number of bridges, this problem can be solved easily with recursion. As the number grows, we might need to resort to dynamic programming or memoization techniques.