Yufi Tao ITEE Univrsity of Qunslan
In tis lctur, w will stuy anotr classic prolm: finin a minimum spannin tr of an unirct wit rap. Intrstinly, vn tou t prolm appars ratr iffrnt from SSSP (sinl sourc sortst pat), it can solv y an aloritm tat is rminiscnt of Dijkstra s aloritm.
Unirct Wit Graps Lt G = (V, E) an unirct rap. Lt w a function tat maps ac of G to a positiv intr valu. Spcifically, for ac, w() is a positiv intr valu, wic w call t wit of. A unirct wit rap is fin as t pair (G, w). W will not an twn vrtics u an v in G as {u, v} insta of (u, v) to mpasiz tat t orrin of u, v os not mattr. W consir tat G is connct, namly, tr is a pat twn any two vrtics in V.
Exampl a 7 f 8 0 c 6 T intr on ac inicats its wit. For xampl, t wit of {, } is 9, an tat of {, } is.
Spannin Trs Rmmr tat a tr is fin as a connct unirct rap wit no cycls. Givn a connct unirct wit rap (G, w) wit G = (V, E), a spannin tr T is a tr satisfyin t followin conitions: T vrtx st of T is V. Evry of T is an in G. T cost of T is fin as t sum of t wits of all t s in T (not tat T must av V s).
Exampl a 7 f 8 0 c 6 a f a f a 8 7 f c 6 c 6 c T scon row sows tr spannin trs (of t rap in t first row). T cost of t first two trs is 7, an tat of t rit tr is 48.
T Minimum Spannin Tr Prolm Givn a connct unirct wit rap (G, w) wit G = (V, E), t oal of t minimum spannin tr (MST) prolm is to fin a spannin tr of t smallst cost. Suc a tr is call an MST of (G, w).
Exampl a 7 f 8 0 c 6 a f a f c 6 c 6 Bot trs in t scon row ar MSTs. Tis mans tat MSTs may not uniqu.
Prim s Aloritm Nxt, w will iscuss an aloritm call Prim s aloritm for solvin t MST prolm. W assum tat G is stor in t ajacncy list format. Rcall tat an {u, v} is rprsnt twic: onc y placin u in t ajacncy list of of v, an anotr tim y placin v in t ajacncy list of u. T wit of {u, v} is stor in ot placs.
Prim s Aloritm T aloritm rows a tr T mst y incluin on vrtx at a tim. At any momnt, it ivis t vrtx st V into two parts: T st S of vrtics tat ar alray in T mst. T st of otr vrtics: V \ S. At t n of t aloritm, S = V. If an conncts a vrtx in V an a vrtx in V \ S, w call it an xtnsion. At all tims, t aloritm nforcs t followin litst xtnsion principl: For vry vrtx v V \ S, it rmmrs wic xtnsion of v as t smallst wit rfrr to as t litst xtnsion of v, an not as st-xt(v).
Prim s Aloritm. Lt {u, v} an wit t smallst wit amon all s.. St S = {u, v}. Initializ a tr T mst wit only on {u, v}.. Enforc t litst xtnsion principl: For vry vrtx z of V \ S If z is a nior of u, ut not of v st-xt(z) = {z, u} If z is a nior of v, ut not of u st-xt(z) = {z, v} Otrwis st-xt(z) = t litr twn {z, u} an {z, v}
Prim s Aloritm 4. Rpat t followin until S = V : 5. Gt an xtnsion {u, v} wit t smallst wit /* Witout loss of nrality, suppos u S an v / S */ 6. A v into S, an a {u, v} into T mst /* Nxt, w rstor t litst xtnsion principl. */ 7. for vry {v, z} of v: If z / S tn If st-xt(z) is avir tan {v, z} tn St st-xt(z) = {v, z}
Exampl E {a, } is t litst of all. So, at t innin S = {a, }. T MST w ar rowin now as on {a, }. a 7 f 8 0 c 6 vrtx v st-xt(v) an wit a c {c, a}, nil, {, }, 0 f {a, f }, 7 {, }, {a, }, 8 Not: Es {c, a} an {c, } av t sam wit. Eitr of tm can st-xt(c).
Exampl E {c, a} is t litst xtnsion. So, w a c to S, wic is now S = {a,, c}. A {c, a} into t MST. a 7 f 8 0 c 6 vrtx v st-xt(v) an wit a c nil, {, }, 0 f {c, f }, 5 {, }, {c, }, 6
Exampl E {c, f } is t litst xtnsion. So, w a f to S, wic is now S = {a,, c, f }. A {c, f } into t MST. a 7 f 8 0 c 6 vrtx v st-xt(v) an wit a c nil, {, f }, f {, }, {c, }, 6
Exampl E {, f } is t litst xtnsion. So, w a to S, wic is now S = {a,, c, f, }. A {, f } into t MST. a 7 f 8 0 c 6 vrtx v st-xt(v) an wit a c {, }, f {, }, {c, }, 6
Exampl E {c, } is t litst xtnsion. So, w a to S, wic is now S = {a,, c, f,, }. A {c, } into t MST. a 7 f 8 0 c 6 vrtx v st-xt(v) an wit a c {, }, f {, }, 9
Exampl E {, } is t litst xtnsion. So, w a to S, wic is now S = {a,, c, f,, }. A {, } into t MST. a 7 f 8 0 c 6 vrtx v st-xt(v) an wit a c {, }, f
Exampl Finally, {, } is t litst xtnsion. So, w a to S, wic is now S = {a,, c, f,,,, }. A {, } into t MST. a 7 f 8 0 c 6 vrtx v a c f st-xt(v) an wit W av otain our final MST.
Nxt w will prov tat t aloritm is corrct, namly, t tr output is in an MST. W will o so y inuction on t squnc of t s a to t tr. Spcifically, t claim to provn is: Claim: For any i [, V ], tr must an MST containin all t first i s cosn y our aloritm. Tn t aloritm s corrctnss follows from t aov claim at i = V.
Corrctnss Proof Lt us first rcall a funamntal proprty of unirct raps: Lmma: Lt T a tr of n vrtics. Ain an aritrary twn two vrtics in T introucs a cycl. Proof: Suppos tat t is a twn u an v. Bfor t was a, tr is alray a pat allowin us to o from u to v in T. Trfor, t {u, v} allows us to mov from v ack to u, tus witnssin a cycl.
Corrctnss Proof Now w proc to prov t claim on Sli 0. Bas Cas: i =. Lt {u, v} an wit t smallst wit in t rap. W will prov tat t must xist in som MST. Tak any MST T tat os not contain {u, v}. A t to T, wic crats a cycl. Rmov an aritrary in T suc tat {u, v}, wic ivs a nw tr T. Sinc {u, v} as t smallst wit, t cost of T is smallr tan or qual to tat of T. Tis mans tat T is also an MST. Hnc, t claim ols for i =.
Corrctnss Proof Inuctiv Cas: Assumin tat t claim ols for i k (k ), nxt w prov tat it also ols for i = k. Lt {u, v} t k-t a y our aloritm, an S t st of vrtics alray in t aloritm s tr for t aition of {u, v}. Witout loss of nrality, suppos tat u S, an v / S. By t inuctiv assumption, w know tat tr is som MST T tat inclus all t first k s. If T also inclus {u, v}, tn t claim alray ols. Nxt, w consir t cas wr T os not av {u, v}.
Corrctnss Proof W a {u, v} to T, wic crats a cycl. Lt us walk on tis cycl startin from v, cross {u, v} into S, kp walkin witin S until travlin out of S for t first tim. Lt t tat rout us out of S {u, v }. S u v V \ S u v Not tat ot {u, v} an {u, v } ar xtnsion s rit for t momnt our aloritm picks t k-t. Sinc {u, v} as t smallst wit amon all t xtnsion s, w know tat t wit of {u, v} is smallr tan or qual to tat of {u, v }.
Corrctnss Proof Now, rmov {u, v } from T, wic ivs anotr tr T. T cost of T cannot mor tan tat of T. Tis mans tat T must also an MST. W tus av prov tat t claim ols for i = k as wll.
Runnin Tim It will lft as an xrcis for you to apply t ata structurs you av larn to implmnt Prim s aloritm in O(( V + E ) lo V ) tim. Rmark: Usin aain an avanc ata structur (call t Fionacci Hap) tat will not covr in tis cours, w can improv t runnin tim to O( V lo V + E ).