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.
# 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 decision | Example when yes | Example when no |
|---|---|---|
| A mutual connection | Knows | Links to |
| A weighted edge | A distance between cities | Friendship |
Example 1 — why direction matters
- One page links to a second.
- It does not follow that the second links back.
- So one cannot come back on the same edge.
Example 2 — why weight changes the question
- Without weight one asks how many steps.
- With weight one asks how long.
- And the answers can differ.