.

Aлгоритмы на графах

Язык: русский
Формат: реферат
Тип документа: Word Doc
0 699
Скачать документ

Yeaiaiou oai?ee a?aoia.

A?ao – niaieoiiinoue oi/ae e eeiee, a eioi?ie eaaeaeay eeiey niaaeeiyao
aeaa oi/ee. Oi/ee iacuaathony aa?oeiaie, eee oceaie, a?aoa, eeiee –
?aa?aie a?aoa. Anee ?aa?i niaaeeiyo aeaa aa?oeiu, oi aiai?yo, /oi iii ei
eioeeaeaioii; aa?oeiu, niaaeeiaiiua ?aa?ii iacuaathony niaaeiuie. Aeaa
aa?oeiu, niaaeeiaiiua ?aa?ii, iiaoo niaiaaeaoue; oaeia ?aa?i
iacuaaaony iaoeae. *enei ?aaa?, eioeeaeaioiuo aa?oeia, iacuaaaony
noaiaiueth aa?oeiu. Anee aeaa ?aa?a eioeeaeaioiu iaeiie e oie aea
ia?a aa?oei, iie iacuaathony e?aoiuie; a?ao, niaea?aeauee e?aoiua
?aa?a, iacuaaaony ioeueoea?aoii.

?aa?i, niaaeeiythuaa aeaa aa?oeiu, iiaeao eiaoue iai?aaeaiea io iaeiie
aa?oeiu e ae?oaie; a yoii neo/aa iii iacuaaaony iai?aaeaiiui, eee
i?eaioe?iaaiiui, e ecia?aaeaaony no?aeeie. A?ao, a eioi?ii ana ?aa?a
i?eaioe?iaaiiua, iacuaaaony i?eaioe?iaaiiui a?aoii (i?a?aoii); ?aa?a
i?a?aoa /anoi iacuaatho aeoaaie. Aeoae eiaiothony e?aoiuie, anee iie
ia oieueei eiatho iauea aa?oeiu, ii e niaiaaeatho ii iai?aaeaieth.
Eiiaaea ioaeii ?anniao?eaaoue ia aanue a?ao, a aai /anoue (/anoue aa?oei
e /anoue ?aaa?). *anoue aa?oei e ana eioeeaeaioiua ei ?aa?a iacuaathony
iiaea?aoii; ana aa?oeiu e /anoue eioeeaeaioiuo ei ?aaa? iacuaathony
noa?aoii. Oeeeeii iacuaaaony caieiooay oeaiue aa?oei. Aea?aaii
iacuaaaony a?ao aac oeeeeia. Inoiaiui aea?aaii iacuaaaony naycaiiue
noa?ao a?aoa, ia eiathuee oeeeeia.

A?ao iaeiicia/ii caaeai, anee caaeaiu iiiaeanoai aai aa?oei,
iiiaeanoai ?aaa? e oeacaiu ana eioeeaeaioiinoe (o.a. oeacaii, eaeea
aa?oeiu eaeeie ?aa?aie niaaeeiaiu). Iaeaieaa iaaeyaeii a?ao caaeaaony
?enoieii; iaeiaei ia ana aeaoaee ?enoiea iaeeiaeiai aaaeiu; a
/anoiinoe, ianouanoaaiiu aaiiao?e/aneea naienoaa ?aaa? (aeeeiia,
e?eaecia e o.ae.) e acaeiiia ?aniieiaeaiea aa?oei ia ieineinoe.

Aeey iai?eaioe?iaaiiiai ?aa?a ii?yaeie, a eioi?ii oeacaiiu
niaaeeiyaiua ei aa?oeiu, ia aaaeai. Aeey i?eaioe?iaaiiiai ?aa?a
aaaeii: ia?aie oeacuaaaony aa?oeia, ec eioi?ie auoiaeeo ?aa?i.

Ia?o?oo, eee iooue – yoi iineaaeiaaoaeueiinoue ?aaa? a
iai?eaioe?iaaiiii a?aoa, a eioi?ii eiiaoe eaaeaeiai ?aa?a niaiaaeaao n
ia/aeii neaaeothuaai ?aa?a. *enei ?aaa? ia?o?ooa iacuaaaony aai
aeeeiiie.

A?aou oe?iei eniieuecothony eae a naiie iaoaiaoeea, oae e a aa
i?eeiaeaieyo. Iie i?eiaiythony i?e iino?iaiee ?acee/iuo iaoaiaoe/aneeo
iiaeaeae: eeiee yeaeo?iia?aaea/e, naoae aaoiaei?ia, eeiee aicaeooiuo
niiauaiee e i?.

Caaea/a ninoieo a oii, iaeoe iooue ec aa?oeiu A a aa?oeio B. Aoaeai
caaeaaaoue a?ao iao?eoeae niaaeiinoe, o.a. eaaae?aoiie oaaeeoeae NxN,
a eioi?ie ia ia?ana/aiee i-e no?iee e j-ai noieaoea cia/aiea TRUE, anee
i e j niaaeeiaiu ?aa?ii, e FALSE a i?ioeaiii neo/aa.

Iiene a oe?eio.

Iiaeiaii oiio eae, niaeanii i?eioeeio Atheaaina, eaaeaeay oi/ea
aieiiaiai o?iioa yaeyaony enoi/ieeii aoi?e/iie aieiu, iu, ioi?aaeyynue
ec caaeaiiie aa?oeiu A, iinauaai ana niaaeiua n iae aa?oeiu (o.a.
aa?oeiu, a eioi?ua aaaeoo no?aeee ec A). Eaaeaeay iinauaiiay aa?oeia
noaiiaeony enoi/ieeii iiaie aieiu e o.ae. I?e yoii iaiaoiaeeii
iicaaioeoueny i oii, /oiau ia aa?ioony a oo aa?oeio, a eioi?ie oaea
auee.

Aeey ?aaeecaoeee aeai?eoia iiiaaeiayony:

iao?eoea m[1..n, 1..n] – iao?eoea niaaeiinoe a?aoa;

aniiiiaaoaeueiue iannea queue[1..n], a eioi?ii aoaeao
oi?ie?iaaoueny i/a?aaeue, o.a. oei aeaiiuo ia?aue aioae – ia?aue auoae
(FIFO). ?acia? aai aeinoaoi/ai, oae eae iu ia iinauaai aa?oeiu
aeaaaeaeu. N ianneaii queue naycaiu aeaa ia?aiaiiua – head e tail. A
ia?aiaiiie head aoaeao iaoiaeeoueny iiia? oaeouae aa?oeiu, ec eioi?ie
eaeao aieia, a i?e iiiiue ia?aiaiiie tail iiaua aa?oeiu iiiauathony
a “oaino” i/a?aaee queue;

aniiiiaaoaeueiue iannea visited[1..n], eioi?ue ioaeai aeey oiai,
/oiau ioia/aoue oaea i?ieaeaiiua aa?oeiu (visited[i]=TRUE <=> aa?oeia i
i?ieaeaia);

aniiiiaaoaeueiue iannea prev[1..n] aeey o?aiaiey i?ieaeaiiuo aa?oei.
A yoii ianneaa e aoaeao noi?ie?iaai eneiiue iooue;

ia?aiaiiay f, eioi?ay i?eiao cia/aiea TRUE, eiaaea iooue aoaeao
iaeaeai.

E?iia oiai, iu aaaaeai ianeieueei aniiiiaaoaeueiuo ia?aiaiiuo, eioi?ua
iiiaaeiayony i?e i?aaiecaoeee oeeeeia.

Program breadth_first_search;

Const n=9;

m:array[1..n, 1..n] of boolean =

(

(False, True, True, False, False, False, False, False,
False),

(True, False, True, False, False, False, True, True,
False),

(True, True, False, True, True, False, False, False,
False),

(False, False, True, False, True, False, False, False,
False),

(False, False, True, True, False, True, False, True,
False),

(False, False, False, False, True, False, True, True, True
),

(False, True, False, False, False, True, False, True, True
),

(False, True, False, False, True, True, True, False,
False),

(False, False, False, False, False, True, True, False,
False)

);

Var A, B: integer;

Procedure A_to_B(A, B: integer);

Var

Visited: array[1..n] of boolean;

Prev: array[1..n] of integer;

path: array[1..n] of integer;

head, tail: integer;

f: boolean;

i, v, k: integer;

Begin

head:=1;

tail:=1;

f:=False;

For i:=1 to n do

Begin

Visited[i]:=False;

Prev[i]:=0

End;

Path[tail]:=A;

Visited[A]:=True;

While (head<=tail) and not f doBeginv:=Path[head];head:=head+1;For k:=1 to n doif m[v, k] and not Visited[k] thenBegintail:=tail+1;Path[tail]:=k;Visited[k]:=True;Prev[k]:=v;if k=B thenBeginf:=true;breakEndEndEnd;if f thenBegink:=B;Write(B);While Prev[k]<>0 do

Begin

Write(‘<-', Prev[k]);k:=Prev[k]endEndelseWrite('Iooe ec ', A, ' a ', B, ' iao')end;BeginWrite('A= '); readln(A);Write('B= '); readln(B);A_to_B(A, B)End.Iiene a aeoaeio.Eaeay iienea a aeoaeio i?inoa: ioi?aaeyynue io oaeouae aa?oeiu, iu iaoiaeei iiaoth (aua ia i?ieaeaiioth) niaaeioth n iae aa?oeio, eioi?oth iiia/aai eae i?ieaeaiioth e iauyaeyai oaeouae. Iinea yoiai i?ioeann aiciaiiaeyaony. Anee iiaie niaaeiie aa?oeiu iao (ooiee), aica?auaainy e oie aa?oeia, ec eioi?ie iiiaee a oaeouoth, e aeaeaai neaaeothuoth iiiuoeo. Anee iiiaaeai a aa?oeio B, ia/aoaai iooue. Anee ana aa?oeiu en/a?iaiu - oaeiai iooe iao.Caiaoei, /oi iino?iaiiue oaeei ia?acii aeai?eoi niiniaai iaoiaeeoue ana iooe ec A a B, ii ia?aue iaeaeaiiue iaiaycaoaeueii aeieaeai auoue e?ao/aeoei.Eae iau/ii, aeai?eoi n aica?aoaie eaa/a anaai ioi?ieoue n iiiiuueth ?aeo?neaiie i?ioeaaeo?u. Aeey aa ?aaeecaoeee iai iiiaaeiayony:iao?eoea m[1..n, 1..n] - iao?eoea niaaeiinoe a?aoa;aniiiiaaoaeueiue iannea visited[1..n], eioi?ue iu aoaeai aeey oiai, /oiau ioia/aoue oaea i?ieaeaiiua aa?oeiu (visited[i]=TRUE <=> aa?oeia
i i?ieaeaia);

ia?aiaiiay f, eioi?ay i?eiao cia/aiea TRUE, eiaaea iooue aoaeao
iaeaeai.

Program depth_first_search;

Const n=9;

m:array[1..n, 1..n] of boolean =

(

(False, True, True, False, False, False, False, False,
False),

(True, False, True, False, False, False, True, True,
False),

(True, True, False, True, True, False, False, False,
False),

(False, False, True, False, True, False, False, False,
False),

(False, False, True, True, False, True, False, True,
False),

(False, False, False, False, True, False, True, True, True
),

(False, True, False, False, False, True, False, True, True
),

(False, True, False, False, True, True, True, False,
False),

(False, False, False, False, False, True, True, False,
False)

);

Var A, B: integer;

Procedure A_to_b(A, B: integer);

Var

Visited: array[1..n] of boolean;

f: boolean;

i: integer;

Procedure Depth(p: integer);

var k: integer;

Begin

Visited[p]:=True;

For k:=1 to n do

If not f then

If m[p, k] and not Visited[k] then

If k=B then

Begin

f:=True;

Write(B);

Break

End

else Depth(k);

If f then write(‘<=', p);End;BeginFor i:=1 to n do Visited[i]:=False;f:=false;Depth(A);If not f then write('Iooe ec ', A, ' a ', B, ' iao')End;Beginwrite('A= '); readln(A);write('B= '); readln(B);A_to_B(A, B)End.Yeea?iau oeeeeu.O?aaoaony iaeoe oeeee, i?ioiaeyuee ii eaaeaeie aeoaa ?iaii iaeei ?ac. Yoo caaea/o aia?aua iinoaaee e ?aoee Eaiia?ae Yeea?, /ai e caeiaeee iniiau oai?ee a?aoia, a niioaaonoaothuea oeeeeu oaia?ue iacuaathony yeea?iauie.Caaea/a aicieeea ec i?aaeeiaeaiiie Yeea?o aieiaieiiee, iieo/eaoae iacaaiea "i?iaeaia eaieanaa?aneeo iinoia". ?aea I?aaaeue, i?ioaea thuay /a?ac Eaeeieia?aae (i?aaeaea ai?iae iacuaaeny Eaieanaa?aii), iiuaaao aeaa ino?iaa. Aa?aaa ?aee naycaiu n ino?iaaie oae, eae yoi iieacaii ia ?enoiea. A aieiaieiiea o?aaiaaeinue iaeoe ia?o?oo, i?ioiaeyuee ii anai o/anoeai nooe oaeei ia?acii, /oiau eaaeaeue ec iinoia aue i?ieaeai ?iaii iaeei ?ac, a ia/aeueiue e eiia/iue ioieou ia?o?ooa niaiaaeaee.Auaa?ai a ea/anoaa aa?oei a?aoa aa?aaa ?aee, a a ea/anoaa ?aaa? - iinou, eo niaaeeiythuea. Iinea yoiai caaea/a noaiiaeony i/aaeaeiie: o?aaiaaiea iainouanoaeii - /oiau aai auiieieoue, /enei aeoa, i?eoiaeyueo e eaaeaeie aa?oeia, aeieaeii auoue /aoiui. A naiii aeaea, iineieueeo ii iaeiiio iinoo iaeuecy i?ioiaeeoue aeaaaeaeu, eaaeaeiio aoiaeo ia aa?aa aeieaeai niioaaonoaiaaoue auoiae.*oi iaiaoiaeeii, /oiau a a?aoa nouanoaiaae yeea?ia oeeee? Ai-ia?auo, a?ao aeieaeai auoue naycaiiui: aeey ethauo aeaoo aa?oei aeieaeai nouanoaiaaoue iooue, eo niaaeeiythuee. Ai-aoi?uo, aeey iai?eaioe?iaaiiuo a?aoia /enei ?aaa? a eaaeaeie aa?oeia aeieaeii auoue /aoiui. Ia naiii aeaea yoiai ieacuaaaony aeinoaoi/ii.Oai?aia. *oiau a naycaiiii a?aoa nouanoaiaae yeea?ia oeeee, iaiaoiaeeii e aeinoaoi/ii, /oiau /enei ?aaa? a eaaeaeie aa?oeia auei /aoiui.Aeieacaoaeuenoai. Iaiaoiaeeiinoue aeieacaia auoa. Aeieacaoaeuenoai aeinoaoi/iinoe eiino?oeoeaii - a ?acoeueoaoa aoaeao iieo/ai o?aaoaiue aeai?eoi.Aeieacaoaeuenoai aaaeaony eiaeoeoeeae ii /eneo ?aaa? a?aoa. Ionoue ooaa?aeaeaiea aeieacaii aeey anao mnil do

Begin

Write(l^.i:3);

l:=l^.next

End

End;

Begin

stack1:=nil;

stack2:=nil;

Write(‘Ia/aeueiay aa?oeia: ‘);readln(v);

Push(v, stack1);

While stack1<>NIL do

Begin

v:=peek(stack1);

i:=1;

While (i<=n) and not m[v, i] do inc(i);If i<=n thenBeginu:=i;Push(u, stack1);m[v, u]:=False;m[u, v]:=False;EndelseBeginpop(x, stack1);push(x, stack2)EndEnd;PrintList(stack2)End.Caaea/a I?eia–E?aneaea.Aeaia ieineay no?aia e a iae n ai?iaeia. Ioaeii niaaeeieoue ana ai?iaea oaeaoiiiie naycueth oae, /oiau iauay aeeeiia oaeaoiiiuo eeiee auea ieieiaeueiie.Eee a oa?ieiao oai?ee a?aoia:Aeai a?ao n n aa?oeiaie; aeeeiu ?aaa? caaeaiu iao?eoeae. Iaeoe inoiaiia aea?aai ieieiaeueiie aeeeiu.I?aaenoaaei naaa, /oi ceiiaueeo inoaaeai iaeioi?ue caian i?iaeoeoia, e aai caaea/ae yaeyaony ninoaaeaiea aeoniiai iaith ia anth ceio. Anee ceiiauee ia/iao n oiai, /oi nia?aa aoaeao anoue naioth aeonioth aaeo (iai?eia?, oieieaae), iioii – aoi?oth ii aeoniinoe (iai?eia?, iyni), oi ii ?eneoao inoaaeoue ia iineaaeiee ianyoe oieueei nieue e ia?aa?ei. Iiaeiaiui ia?acii, anee iioeiaeueiue (aeey ii?aaeaeaiiinoe, ieieiaeueiue) iauaeo no?ieony eae-oi ii oaaai, oi iaeuecy ia ia?aii oaaa auae?aoue /oi-ieaoaeue naiia iaeia, ia aoi?ii oaaa – inoaaoaany naiia iaeia e o.ae. Ca oaeoth iieeoeeo iau/ii i?eoiaeeony ?aniea/eaaoueny ia iineaaeieo oaaao. Oaeie aeai?eoi iacuaaaony aeaaeiui.Oaeeaeoaeueii, ii a caaea/a I?eia–E?aneaea, eioi?ay ia eaaeaony iniaaiii i?inoie, aeaaeiue aeai?eoi aeaao oi/iia iioeiaeueiia ?aoaiea.Eae ecaanoii (yoi eaaei aeieacaoue ii eiaeoeoeee), aea?aai n n aa?oeiaie eiaao n-1 ?aaa?. Ieacuaaaony, eaaeaeia ?aa?i ioaeii auae?aoue aeaaeii (eeoue au ia aicieeaee oeeeeu). Oi anoue n-1 ?ac auae?aoue naiia ei?ioeia ?aa?i ec aua ia aua?aiiia ?aa?i i?e oneiaee, /oi iii ia ia?acoao oeeeea n oaea aua?aiiuie.A eae neaaeeoue, /oiau iiaia ?aa?i ia ia?aciauaaei oeeeea ni noa?uie? Naeaeaoue yoi i?inoi. Aei iino?iaiey aea?aaa ie?anei eaaeaeoth aa?oeio i a ioee/iue io ae?oaeo oeaao i. I?e auai?a i/a?aaeiiai ?aa?a, iai?eia? (i, j), aaea i e j eiatho ?aciua oeaaoa, aa?oeia j e ana, ie?aoaiiua a aa oeaao (o.a. ?aiaa n iae niaaeeiaiiua) ia?ae?aoeaathony a oeaao i. Oaeei ia?acii, auai? aa?oei ?aciiai oeaaoa iaania/eaaao ionoonoaea oeeeeia. Iinea auai?a n-1 ?aaa? ana aa?oeiu iieo/atho iaeei oeaao.Aeieaaeai, /oi iienaiiue aeai?eoi iieo/aao a oi/iinoe ieieiaeueiia ?aoaiea. Aeey aeieacaoaeuenoaa iai iiiaaeiaeony i/aiue i?inoia ooaa?aeaeaiea:Anee e aea?aao aeiaaaeoue ?aa?i, oi a aea?aaa iiyaeony oeeee, niaea?aeauee yoi ?aa?i.Aeaenoaeoaeueii, ionoue aeiaaaeaii ?aa?i (u, v) – “aeiaaaeaii” icia/aao, /oi ?aa?i – iiaia, /oi ?aiueoa aai a aea?aaa ia auei. Iineieueeo aea?aai yaeyaony naycaiiui a?aoii, oi nouanoaoao oeaiue C(u, …, v) ec ianeieueeeo ?aaa?, niaaeeiythuay aa?oeiu u e v. Aeiaaaeaiea ?aa?a (u, v) caiueaao oeaiue, i?aa?auay aa a oeeee.Oai?aia. Aeai?eoi I?eia–E?aneaea iieo/aao ieieiaeueiia inoiaiia aea?aai.. I?aaeiieiaeei aeey i?inoiou aeieacaoaeuenoaa, /oi ana ?aa?a naoe eiatho ?acioth aeeeio, o.a.(1). N oi/iinoueth aei iaicia/aiee(2)ei?i/a ieieiaeueiiai, /oi iaaiciiaeii. Iieo/aiiia i?ioeai?a/ea aeieacuaaao oai?aio aeey naoe ni anaie ?aciuie ?aa?aie.Aeey ?aaeecaoeee aeai?eoia iiiaaeiayony:Matrix – iao?eoea ?annoiyiee, cia/aiea ia?ana/aiee i-ie no?iee e j-ai noieaoea ?aaii ?annoiyieth iaaeaeo i-ie e j-ie aa?oeiaie. Anee oaeiai ?aa?a iao oi cia/aiea ?aaii Infinity – i?inoi aieueoiio /eneo (iaoeiiay aaneiia/iinoue);Color – iannea oeaaoia aa?oei;Ribs – a yoii ianneaa caiiieiathony iaeaeaiiua ?aa?a;a, b – aa?oeiu, niaaeeiyaiua i/a?aaeiui ieieiaeueiui ?aa?iilen – aeeeia aea?aaa.Program Algorithm_PrimaKrascala;Uses Crt;Const MaxSize =100;Infinity =1000;Var Matrix: array[1..MaxSize, 1..MaxSize] of integer;Color: array[1..MaxSize] of integer;Ribs: array[1..MaxSize] of recorda,b: integer;end;n, a, b, k, col, i, len: integer;Procedure Init;Var f: text;i, j: integer;BeginAssign(f, 'INPUT.MTR');Reset(f);Readln(f, n);For i:=1 to n doBeginFor j:=1 to n do read(f, matrix[i, j]);Readln(f)End;For i:=1 to n do color[i]:=i;len:=0End;Procedure Findmin(var a, b: integer);Var min, i, j: integer;Beginmin:=infinity;For i:=1 to n-1 doFor j:=i+1 to n doIf (Matrix[i, j]color[j]) then

Begin

min:=Matrix[i, j];

a:=i;

b:=j

End;

len:=len+min

end;

Begin

Clrscr;

Init;

For k:=1 to n-1 do

Begin

Findmin(a, b);

Ribs[k].a:=a;

Ribs[k].b:=b;

col:=Color[b];

For i:=1 to n do

If color[i]=col then color[i]:=color[a];

End;

For i:=1 to n-1 do

Writeln(ribs[i].a, ‘ ‘, ribs[i].b);

Writeln(‘Length= ‘, len);

Readkey

End.

Aeai?eoi Aeaeeno?u.

Нашли опечатку? Выделите и нажмите CTRL+Enter

Похожие документы
Обсуждение

Оставить комментарий

avatar
  Подписаться  
Уведомление о
Заказать реферат
UkrReferat.com. Всі права захищені. 2000-2019