Return to the Lecture Notes Index

15-111 Lecture 17 (Friday, June 11, 2002)
Prim's algorithm

Yesterday, we learned about spanning trees. You have n nodes, must add n-1 edges. You have to have exactly n-1 edges, and no more, or else you'd get a cycle. We introduced Krushkal's algorithm to create a minimum spanning trees.

   a --- b        a-b = 2  d-e = 7
  / \   / \       a-c = 4  d-f = 8
 /   \ /   \      a-d = 1  d-g = 4
c --- d --- e     b-d = 3  e-g = 6
 \   / \   /      b-e = 7  f-g = 1
  \ /   \ /       c-d = 2
   f --- g        c-f = 5

Another algorithm for creating a spanning tree is called Prim's algorithm. It uses a greedy algorithm of a slightly different design. First we have a table for each of our entries.

   known   v12  length
a   yes     a      0
b           a      2
c           a      4
d           a      1
e
f
g

We need a root for the tree to create. We know how to get there, because you start there. In your table, reflect this on that node, and then put the length of all the other nodes directly from a inside.

   known   v12  length
a   yes     a      0
b           a      2
c           a      4
d           a      1
e
f
g

Pick the smallest not known length and make it known. Once you do that, just try to add all of its connecting nodes.

   known   v12  length
a   yes     a      0
b           a      2
c           d      2
d   yes     a      1
e           d      7
f           d      8
g           d      4

Take the smallest node again, since there's a tie we'll just pick b, but it doens't matter. Looking at the connecting edges, there are no more to add that aren't already added, so we don't need to change anything else.

   known   v12  length
a   yes     a      0
b   yes     a      2
c           d      2
d   yes     a      1
e           d      7
f           d      8
g           d      4

The next smallest length is from c, so make that known and make any smaller lengths changed.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      2
d   yes     a      1
e           d      7
f           c      5
g           d      4

The next smallest is g, make that known and try to add its connecting nodes to the table.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      2
d   yes     a      1
e           d      7
f           g      1
g   yes     d      4

f is now the smallest, so make that known.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      2
d   yes     a      1
e   yes     d      7
f   yes     g      1
g   yes     d      4

Now there's only one left, so just make that known and you're done.

We've just built a tree that looks as follows.

   a --- b        a-b = 2  d-e = 7
    \             a-c = 4  d-f = 8
     \            a-d = 1  d-g = 4
c --- d --- e     b-d = 3  e-g = 6
       \          b-e = 7  f-g = 1
        \         c-d = 2
   f --- g        c-f = 5
Djikstra's algorithm

There's another type of problem, called the shortest path problem. A motorist doesn't care about how much asphault is necessary to create the rodes, but instead how long it takes for him to get to one point to another. Start out with the exact same table. The root is going to represent the starting point. Where do we start? For the example, we'll start at 1. Trivially, from 1, we can get to 1 with a length of 0. We can also get to other places, so add their costs.

   known   v12  length
a   yes     a      0
b           a      2
c           a      4
d           a      1
e
f
g

So far, we've done exactly the same as Prim's. Just like Prim's algorithm, again just remove the minimum cost, which at this point is d. Now we have to see if there's anything new to add that makes the table better. We can now get to g from d, taking 4 hours, plus the 1 hour to get to d. For the others, it's the same way. This is the only difference from Prim's algorithm, that you have a cumulative cost function.

   known   v12  length
a   yes     a      0
b   yes     a      2
c           d      3
d   yes     a      1
e           d      8
f           d      9
g           d      5

The next smallest length is 2, so make b known and try to update the table. Nothing else changes, so just take the next smallest value again.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      3
d   yes     a      1
e           d      8
f           d      9
g           d      5

Followed by that is a length of 5, so see if that makes any difference. We can see that we can now get to f from g with a cost of 1 extra from going to g.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      3
d   yes     a      1
e           d      8
f           g      6
g   yes     d      5

We then pop the next lowest, with the length of 6 and see that by knowing that path, no new quicker paths are found.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      3
d   yes     a      1
e           d      8
f   yes     g      6
g   yes     d      5

After that we pop our final value, and now we've found the shortest paths to all of them.

   known   v12  length
a   yes     a      0
b   yes     a      2
c   yes     d      3
d   yes     a      1
e   yes     d      8
f   yes     g      6
g   yes     d      5

With this algorithm we've created the following decision tree, which tells us the shortest path for all nodes starting from a.

   a --- b        a-b = 2  d-e = 7
    \             a-c = 4  d-f = 8
     \            a-d = 1  d-g = 4
c --- d --- e     b-d = 3  e-g = 6
       \          b-e = 7  f-g = 1
        \         c-d = 2
   f --- g        c-f = 5