Guest

GrapheStatique

By: a guest on Jan 28th, 2012  |  syntax: Pascal  |  size: 1.87 KB  |  hits: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. program graphesStatiques;
  2.  
  3. type graph=array[1..5,1..5] of boolean;
  4. function existeChemain(g:graph; s1,s2:integer):boolean;
  5. var i:integer;
  6.         tr:boolean;
  7. begin
  8.         if g[s1,s2] then existeChemain:=true
  9.         else
  10.                 begin
  11.                         i:=1;
  12.                         tr:=false;
  13.                         while not g[s1,i] do i:=i+1;
  14.                         while (i<=5) and (not tr) do
  15.                                 begin
  16.                                         if g[s1,i] then
  17.                                                 begin
  18.                                                         g[i,s1]:=false;
  19.                                                         tr:=existeChemain(g,i,s2);
  20.                                                 end;
  21.                                         i:=i+1;
  22.                                 end;
  23.                         existeChemain:=tr;     
  24.                 end
  25. end;
  26.  
  27.  
  28. function fortementConnexe(g:graph):boolean;
  29. var i,j:integer;
  30.         bon:boolean;
  31. begin
  32.         i:=1;
  33.         j:=1;
  34.         bon:=true;
  35.         while (i<=5) and bon do
  36.                 begin
  37.                         while (j<=5) and bon do
  38.                                 begin
  39.                                         if (i<>j) then bon:=existeChemain(g,i,j);
  40.                                         j:=j+1;
  41.                                 end;
  42.                         i:=i+1;
  43.                         j:=1;
  44.                 end;
  45.         fortementConnexe:=bon;                         
  46. end;
  47.  
  48. function plusCourtChemain(g:graph; s1,s2:integer):integer;
  49. var i,j,k:integer; tr:boolean;
  50. begin
  51.         if (s1=s2) then plusCourtChemain:=0
  52.         else if (existeChemain(g,s1,s2)) then
  53.                 begin
  54.                         if g[s1,s2] then plusCourtChemain:=1
  55.                         else
  56.                                 begin
  57.                                         i:=1;
  58.                                         tr:=false;
  59.                                         while (not tr) do
  60.                                                 begin
  61.                                                         if g[s1,i] then
  62.                                                                 begin
  63.                                                                         g[i,s1]:=false;
  64.                                                                         tr:=existeChemain(g,i,s2);
  65.                                                                 end;   
  66.                                                         i:=i+1;
  67.                                                 end;
  68.                                         k:=1+plusCourtChemain(g,i-1,s2);
  69.                                         while (i<=5) do
  70.                                                 begin
  71.                                                         if g[s1,i] then
  72.                                                                 if existeChemain(g,i,s2) then
  73.                                                                         begin
  74.                                                                                 j:=1+plusCourtChemain(g,i,s2);
  75.                                                                                 if (j<k) then k:=j;
  76.                                                                         end;
  77.                                                         i:=i+1;
  78.                                                 end;
  79.                                         plusCourtChemain:=k;
  80.                                 end;
  81.                 end
  82.         else plusCourtChemain:=-1;                                                                     
  83. end;
  84.        
  85. var g:graph;
  86.         i,j:integer;
  87. begin
  88. for i:=1 to 5 do
  89.         for j:=1 to 5 do
  90.                 g[i,j]:=false;
  91. g[1,2]:=true;
  92. g[1,4]:=true;
  93. g[2,1]:=true;
  94. g[2,3]:=true;
  95. g[3,5]:=true;
  96. g[4,1]:=true;
  97.  
  98. for i:=1 to 5 do
  99.         for j:=1 to 5 do
  100.                 writeln(i,',',j,plusCourtChemain(g,i,j):5);
  101.  
  102. end.