next up previous contents
Next: Transitive Closure: Introduction Up: Graphs: Introduction Previous: Shortest Path: Introduction   Contents

Minimum Spanning Tree: Intro [185]




\begin{picture}(748,193)(6,626)
\thicklines\put(100,800){\circle{28}}
\put(340,7...
...ut(255,725){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 7}}}
\end{picture}

Note that G is undirected.

What is the tree that connects G at the lowest cost?

How is this different from Shortest Path?



MST: MST Property [186]




\begin{picture}(668,273)(6,566)
\thicklines\put( 20,780){\circle{28}}
\put( 60,8...
...ut(630,600){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm 6}}}
\end{picture}



1. Put the ROOT (1) into the set S

2. Find minimum edge from set S to set V-S

3. Add edge to set S

4. If S=V then Stop else Repeat 2.



MST: Pseudocode [187]

Prim's algorithms is same as Dijkstra's except D is not cumulative.

#define ROOT 1
NODE v, w;
SET V, S;             /* both V and S are SETS          */
int Dist[MAX][MAX];   /* distance from node v to w      */
int D[MAX];           /* current distance from the root */
int P[MAX];           /* backward path towards root     */

S = {ROOT}; 
for (all other nodes w in V-S) {
  D[w] = Dist[ROOT][w];
  P[w] = ROOT;
}
while (S is not yet V) {            /* how many times will this iterate? */
  choose node w in V-S such that D[w] is minimum;
  S = S union {w};
  for (each node v in V-S)          /* how big can this be?              */
    if (D[v] > Dist[w][v]) {
      D[v] = Dist[w][v];
      P[v] = w;
    }
}
ANALYSIS: O(      )



MST: Example Execution [188]

init S={ROOT}
v S D P
1 1 0 0
2 0 2 1
3 0 4 1
4 0 1 1
5 0 infty 1
6 0 infty 1
7 0 infty 1
add w=4 to S
v S D P
1 1 0 0
2 0 2 1
3 0 2 4
4 1 1 1
5 0 7 4
6 0 8 4
7 0 4 4
add w=2 to S
v S D P
1 1 0 0
2 1 2 1
3 0 2 4
4 1 1 1
5 0 7 4
6 0 8 4
7 0 4 4
add w=3 to S
v S D P
1 1 0 0
2 1 2 1
3 1 2 4
4 1 1 1
5 0 7 4
6 0 5 3
7 0 4 4

add w=7 to S
v S D P
1 1 0 0
2 1 2 1
3 1 2 4
4 1 1 1
5 0 6 4
6 0 1 7
7 1 4 4
add w=6 to S
v S D P
1 1 0 0
2 1 2 1
3 1 2 4
4 1 1 1
5 0 6 4
6 1 1 7
7 1 4 4
add w=5 to S
v S D P
1 1 0 0
2 1 2 1
3 1 2 4
4 1 1 1
5 1 6 4
6 1 1 7
7 1 4 4



MST: mst [189]

% gcc -o mst mst.c                                     > tcc mst.c

Data File with Test Case: mst.dat

7
99  2  4  1 99 99 99
 2 99 99  3 10 99 99
 4 99 99  2 99  5 99
 1  3  2 99  7  8  4  
99 10 99  7 99 99  6
99 99  5  8 99 99  1
99 99 99  4  6  1 99

% mst mst.dat
n: 7 steps: 42  total distance of spanning tree: 16

% mst mst.dat graph
  0  2  0  1  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  2  0  0  0  4
  0  0  0  0  0  0  0
  0  0  0  0  0  0  0
  0  0  0  0  6  1  0



MST: mst [190]

% mst mst.dat step 
 u  v step  dist   u  v step  dist   u  v step  dist 
---------------------------------------------------- 
 1  2    2 +       4  3   10 -       4  7   21 -      
 1  3    3 +       4  5   12 +       4  3   21 +   5      
 1  3    3 -       4  5   12 -       4  5   26 +      
 1  2    4 -       4  6   13 +       4  5   27 -      
 1  4    4 +       4  6   13 -       3  6   27 +      
 1  5    5 +       4  7   14 +       3  6   28 -      
 1  5    5 -       4  7   14 -       4  7   28 +      
 1  6    6 +       1  2   14 +   3   4  7   28 +   9      
 1  6    6 -       4  3   17 +       7  5   33 +      
 1  7    7 +       4  5   19 +       7  5   34 -      
 1  7    7 -       4  5   19 -       7  6   34 +      
 1  4    7 +   1   4  6   20 +       7  6   35 +  10      
 1  2    9 +       4  6   20 -       7  5   40 +      
 4  3   10 +       4  7   21 +       7  5   42 +  16



MST: mst.c [191]

#define ROOT 1
int A[MAX][MAX], B[MAX][MAX];
void MST() {
  int i, w, v, min, S[MAX], P[MAX], D[MAX];
  
  S[ROOT] = 1;   D[ROOT] = 0;   P[ROOT] = 0;
  for (v=2; v<=n; v++) { S[v] = 0; D[v] = A[ROOT][v]; P[v] = ROOT; }
  for (i=2; i<=n; i++) {
    min = INFINITY;
    w = 0;
    for (v=1; v<=n; v++) 
      if ((S[v] == 0) && (D[v] < min)) {
        min = D[v]; 
        w = v;
      }
    S[w] = 1; 
    B[P[w]][w] = A[P[w]][w]; 
    for (v=1; v<=n; v++) 
      if ((S[v] == 0) && (D[v] > A[w][v])) {
        D[v] = A[w][v]; 
        P[v]=w;
      }
  }
}


next up previous contents
Next: Transitive Closure: Introduction Up: Graphs: Introduction Previous: Shortest Path: Introduction   Contents
Ted Billard 2001-10-25