Mu.c lu.c `au L` o.i n´ oi d ¯ˆ - a.i cu.o.ng vˆ `e d `o thi D ¯ˆ 1.1 1.2 1.3 - i.nh ngh˜ıa v`a c´ac kh´ai niˆe.m D 1.1.1 - `ˆo thi c´o hu.´o.ng D 1.1.2 - `ˆo thi v`a ´anh xa d¯a tri D 10 1.1.3 - `ˆo thi vˆo hu.´o.ng D 10 1.1.4 C´ac d¯i.nh ngh˜ıa ch´ınh 11 Ma trˆa.n biˆe˙’u diˆ˜en d¯`ˆo thi 13 1.2.1 Ma trˆa.n liˆen thuˆo.c d¯ı˙’nh-cung 13 1.2.2 Ma trˆa.n liˆen thuˆo.c d¯ı˙’nh-ca.nh 15 1.2.3 `e hay ma trˆa.n liˆen thuˆo.c d¯ı˙’nh-d¯ı˙’nh Ma trˆa.n kˆ 17 1.2.4 C´ac biˆe˙’u diˆ˜en cu˙’a d¯`oˆ thi 18 T´ınh liˆen thˆong 23 1.3.1 `en v`a chu tr`ınh Dˆay chuyˆ 23 1.3.2 - u.`o.ng d¯i v`a ma.ch D 24 1.3.3 T´ınh liˆen thˆong 24 http://www.ebook.edu.vn 1.4 1.5 1.6 1.3.4 `au, k−liˆen thˆong Cˆ 28 1.3.5 - `ˆo thi liˆen thˆong ma.nh D 31 Pha.m vi v`a liˆen thˆong ma.nh 33 1.4.1 Ma trˆa.n pha.m vi 33 1.4.2 `an liˆen thˆong ma.nh T`ım c´ac th`anh phˆ 36 1.4.3 Co so˙’ 39 - ˇa˙’ng cˆa´u cu˙’a c´ac d¯`ˆo thi D 41 1.5.1 1−d¯ˇa˙’ng cˆa´u 42 1.5.2 2−d¯ˇa˙’ng cˆa´u 43 C´ac d¯`oˆ thi d¯aˇ c biˆe.t 46 1.6.1 - `ˆo thi khˆong c´o ma.ch D 46 1.6.2 - `ˆo thi phˇa˙’ng D 46 `o thi C´ ac sˆ o´ co ba˙’n cu˙’a d ¯ˆ 49 2.1 Chu sˆo´ 49 2.2 Sˇa´c sˆo´ 52 C´ach t`ım sˇa´c sˆo´ 54 2.3 Sˆo´ ˆo˙’n d¯.inh 55 2.4 Sˆo´ ˆo˙’n d¯.inh ngo`ai 61 2.5 Phu˙’ 65 2.6 Nhˆan cu˙’a d¯`ˆo thi 69 2.6.1 `e tˆ `on ta.i v`a nhˆa´t C´ac d¯i.nh l´ y vˆ 69 2.6.2 Tr`o cho.i Nim 72 2.2.1 http://www.ebook.edu.vn `e d C´ ac b` to´ an vˆ ¯u.` o.ng d ¯i 3.1 3.2 3.3 3.4 - u.`o.ng d¯i gi˜ D u.a hai d¯ı˙’nh 75 75 3.1.1 - u.`o.ng d¯i gi˜ D u.a hai d¯ı˙’nh 75 3.1.2 - `ˆo thi liˆen thˆong ma.nh D 76 - u.`o.ng d¯i ngˇa´n nhˆa´t gi˜ D u.a hai d¯ı˙’nh 78 3.2.1 Tru.`o.ng ho p ma trˆa.n tro.ng lu.o ng khˆong ˆam 78 3.2.2 Tru.`o.ng ho p ma trˆa.n tro.ng lu.o ng tu` yy ´ 82 - u.`o.ng d¯i ngˇa´n nhˆa´t gi˜ D u.a tˆa´t ca˙’ c´ac cˇa.p d¯ı˙’nh 87 3.3.1 Thuˆa.t to´an Hedetniemi (tru.`o.ng ho p ma trˆa.n tro.ng lu.o ng khˆong ˆam) 88 3.3.2 Thuˆa.t to´an Floyd (tru.`o.ng ho p ma trˆa.n tro.ng lu.o ng tu` yy ´) 93 Ph´at hiˆe.n ma.ch c´o d¯ˆo d`ai ˆam 96 Ma.ch tˆo´i u.u d¯`ˆo thi c´o hai tro.ng lu.o ng 96 3.4.1 ˆ CAY 99 4.1 Mo˙’ d¯`ˆau 4.2 Cˆay Huffman 101 4.3 99 4.2.1 C´ac bˆo m˜a “tˆo´t” 101 4.2.2 M˜a Huffman 103 Cˆay bao tr` um 105 4.3.1 `eu rˆo.ng x´ac d¯.inh cˆay bao tr` Thuˆa.t to´an t`ım kiˆe´m theo chiˆ um 107 4.3.2 `eu sˆau x´ac d¯i.nh cˆay bao tr` Thuˆa.t to´an t`ım kiˆe´m theo chiˆ um 107 4.3.3 T`ım cˆay bao tr` um du a trˆen hai ma˙’ng tuyˆe´n t´ınh 108 4.3.4 Thuˆa.t to´an t`ım tˆa´t ca˙’ c´ac cˆay bao tr` um 112 4.3.5 Hˆe co so˙’ cu˙’a c´ac chu tr`ınh d¯ˆo.c lˆa.p 112 http://www.ebook.edu.vn 4.4 4.5 Cˆay bao tr` um tˆo´i thiˆe˙’u 114 4.4.1 Thuˆa.t to´an Kruskal 116 4.4.2 Thuˆa.t to´an Prim 119 4.4.3 Thuˆa.t to´an Dijkstra-Kevin-Whitney 121 B`ai to´an Steiner 122 B` to´ an Euler v` a b` to´ an Hamilton 5.1 127 B`ai to´an Euler 127 5.1.1 `en Euler 129 Thuˆa.t to´an t`ım dˆay chuyˆ 5.2 B`ai to´an ngu.`o.i d¯u.a thu Trung Hoa 131 5.3 B`ai to´an Hamilton 135 5.3.1 `eu kiˆe.n cˆ `an d¯ˆe˙’ tˆ `on ta.i chu tr`ınh Hamilton 138 C´ac d¯iˆ 5.3.2 `eu kiˆe.n d¯u˙’ vˆ `e su tˆ `on ta.i chu tr`ınh Hamilton C´ac d¯iˆ 5.3.3 `eu kiˆe.n d¯u˙’ vˆ `e su tˆ `on ta.i ma.ch Hamilton 142 C´ac d¯iˆ 139 - `ˆ D o thi phˇ a˙’ ng 149 6.1 - inh ngh˜ıa v`a c´ac v´ı du 149 D 6.2 C´ac biˆe˙’u diˆ˜en kh´ac cu˙’a mˆo.t d¯`oˆ thi phˇa˙’ng 151 6.3 C´ac t´ınh chˆa´t cu˙’a d¯`ˆo thi phˇa˙’ng 154 6.4 Ph´at hiˆe.n t´ınh phˇa˙’ng 157 6.4.1 Kiˆe˙’m tra t´ınh phˇa˙’ng 161 6.5 - ˆo´i ngˆa˜u h`ınh ho.c 167 D 6.6 - ˆo´i ngˆa˜u tˆo˙’ ho p 170 D Ma.ng vˆ a.n ta˙’ i 173 http://www.ebook.edu.vn 7.1 Mo˙’ d¯`ˆau 173 7.2 `ong l´o.n nhˆa´t 174 B`ai to´an luˆ 7.3 7.4 7.2.1 `ong l´o.n nhˆa´t 180 Thuˆa.t to´an g´an nh˜an d¯ˆe˙’ t`ım luˆ 7.2.2 - `ˆo thi d¯iˆ `eu chı˙’nh luˆ `ong 181 D 7.2.3 `ong 182 Phˆan t´ıch luˆ `ong l´o.n nhˆa´t 183 C´ac ca˙’i biˆen d¯o.n gia˙’n cu˙’a b`ai to´an luˆ 7.3.1 `eu nguˆ `on v`a nhiˆ `eu d¯´ıch 183 C´ac d¯`ˆo thi c´o nhiˆ 7.3.2 C´ac d¯`ˆo thi v´o.i r`ang buˆo.c ta.i c´ac cung v`a d¯ı˙’nh 184 7.3.3 `e luˆ `ong 185 C´ac d¯`ˆo thi c´o cˆa.n trˆen v`a cˆa.n du.´o.i vˆ `ong v´o.i chi ph´ı nho˙’ nhˆa´t 186 Luˆ 7.4.1 7.5 Thuˆa.t to´an Klein, Busacker, Gowen 186 Cˇa.p gh´ep 189 7.5.1 `e cˇa.p gh´ep 189 C´ac b`ai to´an vˆ 7.5.2 `an 192 Cˇa.p gh´ep l´o.n nhˆa´t d¯`ˆo thi hai phˆ 7.5.3 `an 193 Cˇa.p gh´ep ho`an ha˙’o d¯`ˆo thi hai phˆ A Thu viˆ e.n Graph.h 197 T` liˆ e.u tham kha˙’ o 209 http://www.ebook.edu.vn http://www.ebook.edu.vn `au L` o.i n´ oi d ¯ˆ Trong thu c tˆe´ d¯ˆe˙’ miˆeu ta˙’ mˆo.t sˆo´ t`ınh huˆo´ng ngu.`o.i ta thu.`o.ng biˆe˙’u thi bˇ`a ng mˆo.t h`ınh a˙’nh `om c´ac d¯iˆe˙’m (c´ac d¯ı˙’nh)-biˆe˙’u diˆ˜en c´ac thu c thˆe˙’-v`a v˜e c´ac d¯oa.n thˇa˙’ng nˆo´i cˇa.p c´ac d¯ı˙’nh biˆe˙’u gˆ diˆ˜en mˆo´i quan hˆe gi˜ u.a ch´ ung Nh˜ u.ng h`ınh nhu thˆe´ thu.`o.ng go.i l`a c´ac d¯`oˆ thi Mu.c d¯´ıch cu˙’a gi´ao tr`ınh n`ay cung cˆa´p nh˜ u.ng kiˆe´n th´ u.c co ba˙’n d¯ˆe˙’ nghiˆen c´ u.u c´ac d¯`oˆ thi C´ac d¯`ˆo thi xuˆa´t `eu l˜ınh vu c v´o.i c´ac tˆen go.i kh´ac nhau: “cˆa´u tr´ hiˆe.n nhiˆ uc” cˆong tr`ınh xˆay du ng, `en thˆong”, “cˆa´u tr´ “ma.ch” d¯iˆe.n tu˙’ , “lu o c d¯`oˆ quan hˆe.”, “cˆa´u tr´ uc truyˆ uc tˆo˙’ ch´ u.c” x˜a hˆo.i v`a kinh tˆe´, “cˆa´u tr´ uc phˆan tu˙’.” ho´a ho.c, vˆan vˆan `eu l˜ınh vu c, c´o rˆa´t nhiˆ `eu nghiˆen c´ Do nh˜ u.ng u ´.ng du.ng rˆo.ng r˜ai cu˙’a n´o nhiˆ u.u `an d¯aˆy; mˆo.t nhˆan tˆo´ chu˙’ yˆe´u g´op phˆ `an th´ xung quanh l´ y thuyˆe´t d¯`oˆ thi nh˜ u.ng nˇam gˆ uc ˙ ’ ˙ ’ ˙ ’ ´ ` d¯aˆ y su ph´at triˆen d¯o´ l`a xuˆa t hiˆe.n c´ac m´ay t´ınh l´o n c´o thˆe thu c hiˆe.n nhiˆeu ph´ep to´an v´o.i tˆo´c d¯oˆ rˆa´t nhanh Viˆe.c biˆe˙’u diˆ˜en tru c tiˆe´p v`a chi tiˆe´t c´ac hˆe thˆo´ng thu c tˆe´, chˇa˙’ng ha.n c´ac `en thˆong, d¯˜a d¯u.a d¯ˆe´n nh˜ ma.ng truyˆ u.ng d¯`ˆo thi c´o k´ıch thu.´o.c l´o.n v`a viˆe.c phˆan t´ıch th`anh `eu v`ao c´ac thuˆa.t to´an “tˆo´t” c˜ cˆong hˆe thˆo´ng phu thuˆo.c rˆa´t nhiˆ ung nhu kha˙’ nˇang cu˙’a m´ay t´ınh Theo d¯o´, gi´ao tr`ınh n`ay s˜e tˆa.p trung v`ao viˆe.c ph´at triˆe˙’n v`a tr`ınh b`ay c´ac thuˆa.t to´an d¯ˆe˙’ phˆan t´ıch c´ac d¯`oˆ thi C´ac phu.o.ng ph´ap phˆan t´ıch v`a thiˆe´t kˆe´ c´ac thuˆa.t to´an gi´ao tr`ınh cho ph´ep sinh viˆen c´o thˆe˙’ viˆe´t dˆ˜e d`ang c´ac chu.o.ng tr`ınh minh ho.a Gi´ao tr`ınh d¯u.o c biˆen soa.n cho c´ac d¯oˆ´i tu.o ng l`a sinh viˆen To´an-Tin v`a Tin ho.c u C d¯ˆe˙’ minh ho.a, nhiˆen c´o thˆe˙’ dˆ˜e d`ang chuyˆe˙’n d¯ˆo˙’i Gi´ao tr`ınh su˙’ du.ng ngˆon ng˜ `an c´o mˆo.t sˆo´ kiˆe´n th´ `e ngˆon ng˜ sang c´ac ngˆon ng˜ u kh´ac; v`a d¯´o, sinh viˆen cˆ u.c vˆ u C Ngo`ai `au hˆe´t c´ac chu o ng tr`ınh thao t´ac trˆen cˆa´u tr´ ra, hˆ uc d˜ u liˆe.u nhu danh s´ach liˆen kˆe´t, nˆen d¯`oi y nˇang lˆa.p tr`ınh tˆo´t ho˙’i sinh viˆen pha˙’i c´o nh˜ u ng k˜ sau: `om ba˙’y chu.o.ng v`a mˆo.t phˆ `an phu lu.c v´o.i nh˜ Gi´ao tr`ınh bao gˆ u.ng nˆo.i dung ch´ınh nhu `e d¯`oˆ thi • Chu.o.ng th´ u nhˆa´t tr`ınh b`ay nh˜ u.ng kh´ai niˆe.m cˇan ba˙’n vˆ ´ ngh˜ıa thu c tiˆ˜en cu˙’a c´ac sˆo´ n`ay • Chu.o.ng tr`ınh b`ay nh˜ u.ng sˆo´ co ba˙’n cu˙’a d¯`ˆo thi Y http://www.ebook.edu.vn • Chu.o.ng t`ım hiˆe˙’u b`ai to´an t`ım d¯u.`o.ng d¯i ngˇa´n nhˆa´t ´.ng du.ng cu˙’a cˆay Huffman n´en d˜ `e cˆay U • Chu.o.ng d¯`ˆe cˆa.p d¯ˆe´n kh´ai niˆe.m vˆ u liˆe.u Ngo`ai xˆay du ng c´ac thuˆa.t to´an t`ım cˆay bao tr` um nho˙’ nhˆa´t • B`ai to´an Euler v`a b`ai to´an Hamilton v`a nh˜ u.ng mo˙’ rˆo.ng cu˙’a ch´ ung s˜e d¯u.o c n´oi d¯ˆe´n Chu.o.ng • Chu.o.ng nghiˆen c´ u.u c´ac t´ınh chˆa´t phˇa˙’ng cu˙’a d¯`ˆo thi.; v`a cuˆo´i c` ung • Chu.o.ng t`ım hiˆe˙’u c´ac b`ai to´an trˆen ma.ng vˆa.n ta˙’i `an phu lu.c tr`ınh b`ay c´ac cˆa´u tr´ `an thiˆe´t d¯ˆe˙’ Ngo`ai ra, phˆ uc d˜ u liˆe.u v`a nh˜ u.ng thu˙’ tu.c cˆ d¯o n gia˙’n ho´a c´ac d¯oa.n chu o ng tr`ınh minh ho.a c´ac thuˆa.t to´an d¯u o c tr`ınh b`ay `an d¯`aˆu tiˆen nˆen khˆong tr´anh kho˙’i kh´a nhiˆ `eu thiˆe´u s´ot T´ac Gi´ao tr`ınh d¯u.o c biˆen soa.n lˆ gia˙’ mong c´o nh˜ u ng d¯´ong g´op t` u ba.n d¯o.c `eu ngu.`o.i m`a khˆong thˆe˙’ liˆe.t kˆe Tˆoi xin ca˙’m o.n nh˜ u.ng gi´ up d¯o˜ d¯a˜ nhˆa.n d¯u.o c t` u nhiˆ hˆe´t, d¯ˇa.c biˆe.t l`a c´ac ba.n sinh viˆen, qu´a tr`ınh biˆen soa.n gi´ao tr`ınh n`ay - `a La.t, ng`ay th´ang nˇam 2002 D PHA M Tiˆe´n So n http://www.ebook.edu.vn Chu.o.ng - a.i cu.o.ng vˆ `e d `o thi D ¯ˆ - i.nh ngh˜ıa v` D a c´ ac kh´ niˆ e.m 1.1 1.1.1 - `ˆ D o thi c´ o hu.´ o.ng - `ˆo thi c´o hu.´o.ng G = (V, E) gˆ `om mˆo.t tˆa.p V c´ac phˆ `an tu˙’ go.i l`a d¯ı˙’nh (hay n´ D ut) v`a mˆo.t tˆa.p E c´ac cung cho mˆo˜i cung e ∈ E tu o ng u ´ ng v´o i mˆo.t cˇa.p c´ac d¯ı˙’nh d¯u o c sˇa´p th´ u tu Nˆe´u c´o d¯u ´ng mˆo.t cung e tu.o.ng u ´.ng c´ac d¯ı˙’nh d¯u.o c sˇa´p th´ u tu (a, b), ta s˜e viˆe´t e := (a, b) Ch´ ung ta s˜e gia˙’ su˙’ c´ac d¯ı˙’nh d¯u.o c d¯a´nh sˆo´ l`a v1 , v2 , , hay gia˙’n tiˆe.n, 1, 2, , n, d¯o´ n = #V l`a sˆo´ c´ac d¯ı˙’nh cu˙’a d¯`ˆo thi Nˆe´u e l`a mˆo.t cung tu.o.ng u ´.ng cˇa.p c´ac d¯ı˙’nh d¯u.o c sˇa´p th´ u tu vi v`a vj th`ı d¯ı˙’nh vi go.i l`a gˆo´c v`a d¯ı˙’nh vj go.i l`a ngo.n; cung e go.i l`a liˆen thuˆo.c hai d¯ı˙’nh vi v`a vj Ch´ ung ta s˜e thu.`o.ng k´ y hiˆe.u m = #E−sˆo´ ca.nh cu˙’a d¯`ˆo thi G C´ac ca.nh thu.`o.ng d¯u.o c d¯´anh sˆo´ l`a e1 , e2 , , em Mˆo.t c´ach h`ınh ho.c, c´ac d¯ı˙’nh d¯u.o c biˆe˙’u diˆ˜en bo˙’.i c´ac d¯iˆe˙’m, v`a e = (vi , vj ) d¯u.o c biˆe˙’u diˆ˜en bo˙’.i mˆo.t cung nˆo´i c´ac d¯iˆe˙’m vi v`a vj Mˆo.t cung c´o gˆo´c tr` ung v´o.i ngo.n go.i l`a khuyˆen `eu ho.n mˆo.t cung v´o.i gˆo´c ta.i vi v`a ngo.n ta.i vj th`ı G go.i l`a d¯a d¯`oˆ thi v`a c´ac Nˆe´u c´o nhiˆ - o.n d¯`oˆ thi c´o hu.´o.ng l`a d¯`oˆ thi khˆong khuyˆen d¯o´ hai ´.ng go.i l`a song song D cung tu.o.ng u `eu nhˆa´t mˆo.t cung (vi , vj ) Chˇa˙’ng ha.n, d¯`oˆ thi H`ınh 1.1 c´o d¯ı˙’nh bˆa´t k` y vi v`a vj c´o nhiˆ cung e8 l`a khuyˆen; c´ac cung e4 v`a e9 l`a song song c` ung tu.o.ng u ´.ng cˇa.p d¯ı˙’nh v3 v`a v4 http://www.ebook.edu.vn e4 v2 • e3 v3 • •v4 e9 e1 e2 • v1 e6 e5 e7 • v5 e8 H`ınh 1.1: V´ı du cu˙’a 2−d¯`ˆo thi c´o hu.´o.ng 1.1.2 - `ˆ D o thi v` a´ anh xa d ¯a tri y hiˆe.u Γ(x) := {y ∈ V | (x, y) ∈ E} Khi d¯´o ta c´o mˆo.t ´anh xa d¯a tri V´o.i mˆo˜i x ∈ V, k´ Γ: V → 2V , x → Γ(x) K´ y hiˆe.u Γ−1 l`a ´anh xa (d¯a tri.) ngu.o c cu˙’a Γ Nˆe´u G l`a d¯o.n d¯`ˆo thi., th`ı d¯`ˆo thi n`ay ho`an to`an d¯u.o c x´ac d¯.inh bo˙’.i tˆa.p V v`a ´anh xa d¯a tri Γ t` u V v`ao 2V V`ı vˆa.y, d¯`oˆ thi n`ay c`on c´o thˆe˙’ k´ y hiˆe.u l`a G = (V, Γ) Nˆe´u xo´a cung e9 H`ınh 1.1 ta nhˆa.n d¯u.o c d¯o.n d¯`oˆ thi v`a d¯o´ c´o thˆe˙’ biˆe˙’u diˆ˜en bo˙’.i ´anh xa d¯a tri Γ Trong tru.`o.ng ho p n`ay ta c´o Γ(v1 ) = {v2 }, 1.1.3 Γ(v2 ) = {v1 , v3 }, Γ(v3 ) = {v4 , v5 }, Γ(v4 ) = {v5 }, Γ(v5 ) = {v1 , v5 } - `ˆ D o thi vˆ o hu.´ o.ng Khi nghiˆen c´ u.u mˆo.t sˆo´ t´ınh chˆa´t cu˙’a c´ac d¯`oˆ thi., ta thˆa´y rˇ`a ng ch´ ung khˆong phu thuˆo.c v`ao `an phˆan biˆe.t su kh´ac gi˜ u.a c´ac d¯iˆe˙’m bˇa´t d¯`aˆu v`a hu ´o ng cu˙’a c´ac cung, t´ u.c l`a khˆong cˆ - iˆ `eu n`ay d¯o.n gia˙’n l`a mˆo˜i c´o ´ıt nhˆa´t mˆo.t cung gi˜ u.a hai d¯ı˙’nh ta khˆong quan kˆe´t th´ uc D ung tˆam d¯ˆe´n th´ u tu cu˙’a ch´ V´o.i mˆo˜i cung, t´ u.c l`a mˆo˜i cˇa.p c´o th´ u tu (vi , vj ) ta cho tu.o.ng u ´.ng cˇa.p khˆong c´o th´ u tu (vi , vj ) go.i l`a c´ac ca.nh Tu.o.ng d¯u.o.ng, ta n´oi rˇa` ng ca.nh l`a mˆo.t cung m`a hu.´o.ng d¯a˜ bi bo˙’ `e h`ınh ho.c, ca.nh (vi , vj ) d¯u.o c biˆe˙’u diˆ˜en bo˙’.i c´ac d¯oa.n thˇa˙’ng (hoˇa.c cong) v`a khˆong quˆen Vˆ c´o m˜ ui tˆen liˆen thuˆo.c hai d¯iˆe˙’m tu.o.ng u ´.ng hai d¯ı˙’nh vi v`a vj 10 http://www.ebook.edu.vn /****** Phan dinh nghia cac kieu du lieu *****/ typedef unsigned char byte; typedef byte Boolean; typedef char DataType[MAXSTRINGS+1]; // Them mot ma ket thuc chuoi /******************************* Cau truc du lieu: don lien ket *******************************/ typedef struct VertexNode *AdjPointer; struct VertexNode { byte Vertex; int Length; int Flow; AdjPointer Next; }; typedef struct { DataType Data; AdjPointer Next; } HeadNode; typedef HeadNode *HeadPointer; typedef HeadPointer ArrayOfPointer[MAXVERTICES]; typedef struct QueueType *QueueNode; struct QueueType { byte Vertex; QueueNode Next; }; typedef struct { QueueNode Head, Tail; } Queue; typedef byte Path[MAXVERTICES]; 198 http://www.ebook.edu.vn typedef byte SetOfVertices[(MAXVERTICES%8)?((MAXVERTICES/8)+1):(MAXVERTICES/8)]; /*********************************** Danh sach da lien ket cho cac canh ***********************************/ typedef struct EdgeNode *EdgePointer; struct EdgeNode { byte Vertex[2]; EdgePointer Link[2]; }; typedef struct { char Data; EdgePointer Next; } ListEdge; typedef ListEdge *ListEdgePointer; typedef ListEdgePointer ArrayOfEdge[MAXVERTICES]; /***** Phan khai bao prototype ham *****/ void Create(AdjPointer *List); Boolean Empty(AdjPointer List); void Push(AdjPointer *List, byte Item); void Pop(AdjPointer *List, byte *Item); void CreatQueue(Queue *Q); Boolean EmptyQueue(Queue Q); void PushQueue(Queue *Q, byte Item); void PopQueue(Queue *Q, byte *Item); Boolean EmptySet(SetOfVertices S); Boolean InSet(SetOfVertices S, byte Value); void InitSet(SetOfVertices S, byte MaxValue); void AddSet(SetOfVertices S, byte Value); void SubSet(SetOfVertices S, byte Value); void MakeV_out(char *FileName, ArrayOfPointer V_out, byte *NumVertices, 199 http://www.ebook.edu.vn Boolean Weight); void MakeV_in(char *FileName, ArrayOfPointer V_in, ArrayOfPointer V_out, byte *NumVertices); void BuildGraph(char *FileName, ArrayOfEdge E, byte *NumVertices); void DisplayV_out(ArrayOfPointer V_out, byte NumVertices, Boolean Weight); void DisplayV_in(ArrayOfPointer V_in, byte NumVertices); void DFS(ArrayOfEdge E, byte Start, SetOfVertices S); void PathTwoVertex(Path Pred, byte Start, byte Terminal); void PopHeadPtr(byte NumVertices, ArrayOfPointer V_out); /***** Phan cai dat cac ham *****/ void Create(AdjPointer *List) { (*List) = NULL; } Boolean Empty(AdjPointer List) { return((List == NULL) ? TRUE : FALSE); } void Push(AdjPointer *List, byte Item) { AdjPointer TempPtr; TempPtr = (AdjPointer) malloc(sizeof(struct VertexNode)); TempPtr->Vertex = Item; TempPtr->Next = (*List); (*List) = TempPtr; } void Pop(AdjPointer *List, byte *Item) { AdjPointer TempPtr; if (Empty(*List)) { printf(" Thao tac tro khong hop le "); return; 200 http://www.ebook.edu.vn } TempPtr = (*List); (*Item) = TempPtr->Vertex; (*List) = TempPtr->Next; free(TempPtr); } void CreatQueue(Queue *Q) { (*Q).Head = NULL; } Boolean EmptyQueue(Queue Q) { return((Q.Head == NULL) ? TRUE : FALSE); } void PushQueue(Queue *Q, byte Item) { QueueNode TempPtr; TempPtr = (QueueNode) malloc(sizeof(struct QueueType)); TempPtr->Vertex = Item; TempPtr->Next = NULL; if ((*Q).Head == NULL) (*Q).Head = TempPtr; else (*Q).Tail->Next = TempPtr; (*Q).Tail = TempPtr; } void PopQueue(Queue *Q, byte *Item) { QueueNode TempPtr; if (EmptyQueue(*Q)) { printf(" Thao tac tro khong hop le "); return; } TempPtr = (*Q).Head; (*Item) = TempPtr->Vertex; (*Q).Head = TempPtr->Next; free(TempPtr); 201 http://www.ebook.edu.vn } Boolean EmptySet(SetOfVertices S) { int i, k = (MAXVERTICES %8 ) ? ((MAXVERTICES / 8) + 1) : (MAXVERTICES / 8); for (i = 0; i < k; i++) if (S[i] != 0) return(FALSE); return(TRUE); } Boolean InSet(SetOfVertices S, byte Value) { if ((Value < 1) || (Value > MAXVERTICES)) return(FALSE); return((S[(Value - 1) / 8] & (0x80 >> ((Value - 1) % 8))) ? TRUE : FALSE); } void InitSet(SetOfVertices S, byte MaxValue) { int i; if (MaxValue>MAXVERTICES) { printf(" Gia tri khong thuoc tap hop "); return; } for (i = 0; i < MaxValue; i++) S[i/8] |= 0x80 >> (i%8); } void AddSet(SetOfVertices S, byte Value) { int i; if ((Value < 1) || (Value > MAXVERTICES)) { printf(" Gia tri khong thuoc tap hop "); return; } 202 http://www.ebook.edu.vn S[(Value-1)/8] |= 0x80 >> ((Value-1)%8); } void SubSet(SetOfVertices S, byte Value) { if ((Value < 1) || (Value > MAXVERTICES)) { printf(" Gia tri khong thuoc tap hop "); return; } S[(Value-1)/8] &= ~(0x80 >> ((Value-1)%8)); } int feoln(FILE *fp) { char c; if ((c=fgetc(fp))==10) { fseek(fp, -2, 1); return(TRUE); } else if (c==EOF) return(TRUE); else { fseek(fp, -1, 1); return(FALSE); } } void freadln(FILE *fp) { char c; while (((c=fgetc(fp))!=10)&&(c!=EOF)); } void MakeV_out(char *FileName, ArrayOfPointer V_out, byte *NumVertices, Boolean Weight) { 203 http://www.ebook.edu.vn byte NumVert; HeadPointer HeadPtr; AdjPointer VerPtr; FILE *FileData; if ((FileData = fopen(FileName, "rt")) == NULL) { printf(" File khong tim thay "); return; } NumVert = 0; while (!feof(FileData)) { HeadPtr = (HeadPointer) malloc(sizeof(HeadNode)); HeadPtr->Next = NULL; fgets(HeadPtr->Data, MAXSTRINGS+1, FileData); // Ham fgets(char *s, int n, FILE *fp) chi doc n-1 ky tu while (!feoln(FileData)) { VerPtr = (AdjPointer) malloc(sizeof(struct VertexNode)); fscanf(FileData, "%d", &(VerPtr->Vertex)); if (Weight) { fscanf(FileData, "%d", &(VerPtr->Length)); VerPtr->Flow = 0; } VerPtr->Next = HeadPtr->Next; HeadPtr->Next = VerPtr; } freadln(FileData); ++NumVert; V_out[NumVert] = HeadPtr; } (*NumVertices) = NumVert; fclose(FileData); } void MakeV_in(char *FileName, ArrayOfPointer V_in, ArrayOfPointer V_out, byte *NumVertices); { byte NumVert; 204 http://www.ebook.edu.vn int i, j; HeadPointer HeadPtr; AdjPointer CurrPtr, VerPtr; MakeV_out(FileName, V_out, &NumVert, TRUE); (*NumVertices) = NumVert; for (i=1; iData, V_out[i]->Data); HeadPtr->Next = NULL; for (j=1; jNext; while (CurrPtr!=NULL) { if (CurrPtr->Vertex==i) { VerPtr=(AdjPointer)malloc(sizeof(struct VertexNode)); VerPtr->Vertex = j; VerPtr->Length = CurrPtr->Length; VerPtr->Flow = 0; VerPtr->Next = HeadPtr->Next; HeadPtr->Next = VerPtr; } CurrPtr = CurrPtr->Next; } } V_in[i] = HeadPtr; } } void BuildGraph(char *FileName, ArrayOfEdge E, byte *NumVertices) { byte EndPt, NumVert; int i; char ch[2]; EdgePointer EdgePtr; FILE *FileData; if ((FileData = fopen(FileName, "rt")) == NULL) { 205 http://www.ebook.edu.vn printf(" File khong tim thay "); return; } NumVert = 0; while (!feoln(FileData)) { ++NumVert; E[NumVert] = (ListEdgePointer) malloc(sizeof(ListEdge)); fscanf(FileData, "%s", ch); E[NumVert]->Data = ch[0]; E[NumVert]->Next = NULL; } (*NumVertices) = NumVert; for (i=1; iData); printf("\n"); freadln(FileData); while (!feof(FileData)) { EdgePtr = (EdgePointer) malloc(sizeof(struct EdgeNode)); for (i=0; iVertex[i] = EndPt; EdgePtr->Link[i] = E[EndPt]->Next; E[EndPt]->Next = EdgePtr; } printf("\n"); freadln(FileData); } fclose(FileData); } void DFS(ArrayOfEdge E, byte Start, SetOfVertices Unvisited) { EdgePointer Ptr; byte StartEnd, OtherEnd, NewStart; SubSet(Unvisited, Start); 206 http://www.ebook.edu.vn Ptr = E[Start]->Next; while ((!EmptySet(Unvisited))&&(Ptr!=NULL)) { StartEnd = 0; OtherEnd = 1; if (Ptr->Vertex[0]!=Start) { StartEnd = 1; OtherEnd = 0; } NewStart = Ptr->Vertex[OtherEnd]; if (InSet(Unvisited, NewStart)) DFS(E, NewStart, Unvisited); Ptr = Ptr->Link[StartEnd]; } } void DisplayV_out(ArrayOfPointer V_out, byte NumVertices, Boolean Weight) { int i; AdjPointer CurrPtr; for (i=1; iData); CurrPtr = V_out[i]->Next; while (CurrPtr!=NULL) { printf("%d ", CurrPtr->Vertex); if (Weight) printf("%d ", CurrPtr->Length); CurrPtr = CurrPtr->Next; } printf("\n"); } printf("\n"); } void DisplayV_in(ArrayOfPointer V_in, byte NumVertices) { int i; AdjPointer CurrPtr; for (i=1; iData); CurrPtr = V_in[i]->Next; while (CurrPtr!=NULL) { printf(" %d %d", CurrPtr->Vertex, CurrPtr->Length); CurrPtr = CurrPtr->Next; } printf("\n"); } } void PathTwoVertex(Path Pred, byte Start, byte Terminal) { if (Terminal != Start) { PathTwoVertex(Pred, Start, Pred[Terminal]); printf(" -> %2d", Terminal); } } void PopHeadPtr(byte NumVertices, ArrayOfPointer V_out) { byte Item; int i; AdjPointer CurrPtr; for (i=1; iNext; while (CurrPtr != NULL) Pop(&CurrPtr, &Item); free(V_out[i]); } } #endif 208 http://www.ebook.edu.vn T` liˆ e.u tham kha˙’ o [1] Appel K., The proof of the four-colour problem, New Scientist 72, 154-155 (1976) [2] Arlinghaus S., Arlinghaus W., Nystuen J., The Hedetniemi matrix sum: an algorithm for shortest path and shortest distance, Geographical Analysis, Vol 22, No 4, Oct., 351-360 (1990) [3] Bellman R., On a routing problem, Quart of Applied Mathematics, 16, 87 (1958) [4] Berge C., L´y thuyˆe´t d¯`ˆo thi v`a u ´.ng du.ng, NXB Khoa ho.c v`a K˜ y thuˆa.t H`a Nˆo.i, 1971 [5] Berge C., Two theorems in graph theory, Proc Nat Ac Sc., 43, 842 (1957) [6] Berry R C., A constrained shortest path, Paper presented at the 39th National ORSA Metting, Dallas, Texas (1971) [7] Bondy J A., Properties of graphs with constraints on degrees, Studia Sci Math Hung 473-475 (1969) [8] Bondy J A., Chvatal V., A method in graph theory, Discrete Math 15, 111-135 (1976) [9] Brooks R L., On coloring the nodes of a network, Proc Cambridge Phil Soc., Vol 37, 194-197 (1941) [10] Busacker R G., Gowen P J., A procedure for determining a family of minimal-cost network flow patterns, Operations Research Office, Technical paper 15 (1961) [11] Cayley A., Collected papers, Quart Jl of Mathematics, 13 Cambridge, 26 (1897) [12] Chase S M., Analysis of algorithms for finding all spanning trees of a graph, Report No 401, Department of Computer Science, University of Illinois, Urbana, Oct (1970) [13] Chvatal V., On Hamilton’s ideals, J Combinat Theory B 12 163-168 (1972) [14] Christofides N., Graph theory an algorithmic approach, Academic Press INC (1975) 209 http://www.ebook.edu.vn [15] Coxeter H S M., Introduction to geometry, Wiley, New York (1961) [16] Danzig G B., All shortest routes in a graph, in Th´eorie des graphes, Proceedings of the International Symposium, Rome 1966, Dunod, Paris, 91-92 (1967) [17] Dirac G A., Some theorems on abstract graphs, Proc London Math Soc 2, 68-81 (1952) [18] De Freisseix H., Rosenstiehl P., The depth-search theorem for planarity, in Proceedings of the Cambridge Combinatorial Conference, North Holland, Amsterdam (1982) [19] Deo N., Graph theory with applications to engineering and computer science, PrenticeHall Inc (1974) [20] Dijkstra, E W., A note on two problems in connection with graphs, Numerische Mathematik, 1, 269 (1959) [21] Dreyfus S E., Wagner R A., The Steiner problem in graphs, Networks, 1, 195 (1972) [22] Euler L., Solutio problematis ad geometriam situs pertinentis, Commun Acad Sci Imp Petropol 8, Opera Omnia (1), Vol 7, 128-140 (1736) [23] Euler L., Commentationes Arithmeticae collectae, St Petersburg, (1766) [24] Fary I., On straight line representation of planar graphs, Acta Sci Math Szeged, Vol 11, 229-293 (1948) [25] Floyd R W., Algorithm 97-Shortest path, Comm of ACM, 5, 345 (1962) [26] Ford L R Jr., Network flow theory, Rand Corporation Report 923 (1946) [27] Ford L R., Fulkerson D R., Flows in networks, Princeton University Press, Princeton (1962) [28] Gilbert E N., Pollack H O., Steiner minimal trees, Jl of SIAM (Appl Math.), 16 (1968) [29] Gomory R E., Hu T C., Synthesis of a communication network, Jl of SIAM (App Math.), 12 348 (1964) [30] Gondran M., Minoux M., Vajda S., Graphs and algorithms, John Wiley & Sons (1990) [31] Hamming R W., Coding and information theory, Prentice Hall (1980) [32] Hanan M., On Steiner’s problem with rectilinear distance, Jl of SIAM (Appl Math.), 14 255 (1966) 210 http://www.ebook.edu.vn [33] Hopcroft J E., Tarjan R E., Isomorphism of planar graphs, in Complexity of Computer Computations, Plenum, New York (1972) [34] Hopcroft J E., Tarjan R E., Efficient planarity testing, J ACM 21, 549-568 (1974) [35] Hu T C., Integer programming and network flows, Addison-Wesley, Reading, Massachusetts (1969) [36] Kerchenbaum A., Van Slyke R., Computing minimum spanning trees efficiently, Proc of the Ann Conf of ACM, Boston, 518 (1972) [37] Kirchhoff G., in “Annalen der Physik and Chemie” 72, 497 (1847) [38] Klein M., A primal method for minimal cost flows with applications to the assignment and transportation problems, Man Sci., 14, 205 (1967) [39] Kruskal J B Jr., On the shortest spanning subtree of a graph and the traveling salesman problem, Proc American Mathematical Soc., 7, 48 (1956) [40] Kraitchik M., Le probl`eme des Reines, Bruxelles (1926) [41] Kevin V., Whitney M., Algorithm 422-Minimum spanning tree, Comm of ACM, 15, 273 (1972) [42] Las Vergnas M., Probl`emes de couplage et probl`emes hamiltoniens en th´eorie des graphes, Doctoral thesis, Univsit´e de Paris VI (1972) uc d˜ u liˆe.u, Scitec [43] Larry N., Sanford L., Lˆa.p tr`ınh nˆang cao bˇa` ng Pascal v´o.i c´ac cˆa´u tr´ [44] Mei-Ko Kwan, Graphic programming using odd or even points, Chinese Math 1, 273 (1962) [45] Moore E F., The shortest path through a maze, Proc Int Symp on the Theory of Switching, Path II, 285 (1957) [46] Murchland J D., A new method for finding all elementary paths in a complete directed graph, London School of Economics, Report LSE-TNT-22 (1965) [47] Ore O., Note on Hamilton circuits, Amer Math Mothly, 67, 55 (1960) [48] Ore O., The four colour problem, Academic Press, New York (1967) [49] Prim R C., Shortest connection networks and some generalizations, Bell Syst Tech Jl., 36, 1389 (1957) [50] Paton K., An algorithm for finding a fundamental set of cycles of a graph, Comm ACM, Vol 12, No 9, Sept., 514-518 (1969) 211 http://www.ebook.edu.vn [51] P´osa L., A theorem concerning Hamiltonian lines, Magyar Tud Akad Mat Kutat´o Inst Kozl 255-226 (1962) [52] Shirey R W., Implementation and analysis of efficient graph planarity testing algorithms, Ph.D Dissertation, Computer Sciences, University of Wisconsin, Madison, Wisc (1969) [53] Tarjan R., Depth-first search and linear graph algorithms, SIAM J Computer 146-160 (1972) [54] Tutte W T., How to draw graph, Proc London Math Soc., Ser 3, Vol 13, 743-768 (1963) [55] Whitney H., 2−Isomorphic graphs, Am J Math Vol 55 245-254 (1933) 212 http://www.ebook.edu.vn