Grade 10 Computer Science · Graphs and paths

When a connection is not a hierarchy

What happens when the one-parent rule is removed.

A link that does not come back the same way

“Knows” and “links to” sound like the same kind of connection. But if Dana knows Omar, Omar knows Dana too, and if one page links to another, it is far from certain the second links back. If you pick an algorithm before deciding which connection you have, which question will it answer?

A tree described a structure with an up and a down. Many things in reality are not like that: cities connected by roads, people who know one another, pages that link to one another, tasks that depend on one another. In all of these there is no root and no parent — there are only connections. When the one-parent rule is removed, one gets a graph.

In a graph the same node can be reached by more than one route.
# mutual, unweighted: who knows whom
friends = {
    "dana": {"omar", "lea"},
    "omar": {"dana"},
    "lea":  {"dana"},
}

# one-way, weighted: how long each road takes
roads = {"a": {"b": 5, "c": 2}, "b": {"c": 1}, "c": {}}

Why a tree is a particular case

A tree is exactly a graph with two properties: it is connected, and it has no cycles. From that combination it follows that there is one and only one path between any two nodes. The moment the condition is removed, the number of paths can be large, and even infinite if repeating nodes is allowed. All the difficulty added in this unit comes from that single point.

The decisionExample when yesExample when no
A mutual connectionKnowsLinks to
A weighted edgeA distance between citiesFriendship

Example 1 — why direction matters

  1. One page links to a second.
  2. It does not follow that the second links back.
  3. So one cannot come back on the same edge.

Example 2 — why weight changes the question

  1. Without weight one asks how many steps.
  2. With weight one asks how long.
  3. And the answers can differ.
One line before the code: what the connection is, whether it is mutual, and whether it has a weight.