
GrapheStatique
By: a guest on Jan 28th, 2012 | syntax:
Pascal | size: 1.87 KB | hits: 57 | expires: Never
program graphesStatiques;
type graph=array[1..5,1..5] of boolean;
function existeChemain(g:graph; s1,s2:integer):boolean;
var i:integer;
tr:boolean;
begin
if g[s1,s2] then existeChemain:=true
else
begin
i:=1;
tr:=false;
while not g[s1,i] do i:=i+1;
while (i<=5) and (not tr) do
begin
if g[s1,i] then
begin
g[i,s1]:=false;
tr:=existeChemain(g,i,s2);
end;
i:=i+1;
end;
existeChemain:=tr;
end
end;
function fortementConnexe(g:graph):boolean;
var i,j:integer;
bon:boolean;
begin
i:=1;
j:=1;
bon:=true;
while (i<=5) and bon do
begin
while (j<=5) and bon do
begin
if (i<>j) then bon:=existeChemain(g,i,j);
j:=j+1;
end;
i:=i+1;
j:=1;
end;
fortementConnexe:=bon;
end;
function plusCourtChemain(g:graph; s1,s2:integer):integer;
var i,j,k:integer; tr:boolean;
begin
if (s1=s2) then plusCourtChemain:=0
else if (existeChemain(g,s1,s2)) then
begin
if g[s1,s2] then plusCourtChemain:=1
else
begin
i:=1;
tr:=false;
while (not tr) do
begin
if g[s1,i] then
begin
g[i,s1]:=false;
tr:=existeChemain(g,i,s2);
end;
i:=i+1;
end;
k:=1+plusCourtChemain(g,i-1,s2);
while (i<=5) do
begin
if g[s1,i] then
if existeChemain(g,i,s2) then
begin
j:=1+plusCourtChemain(g,i,s2);
if (j<k) then k:=j;
end;
i:=i+1;
end;
plusCourtChemain:=k;
end;
end
else plusCourtChemain:=-1;
end;
var g:graph;
i,j:integer;
begin
for i:=1 to 5 do
for j:=1 to 5 do
g[i,j]:=false;
g[1,2]:=true;
g[1,4]:=true;
g[2,1]:=true;
g[2,3]:=true;
g[3,5]:=true;
g[4,1]:=true;
for i:=1 to 5 do
for j:=1 to 5 do
writeln(i,',',j,plusCourtChemain(g,i,j):5);
end.