t1nman

graph

Jun 13th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.48 KB | None | 0 0
  1. Unit graph;
  2.  
  3. interface
  4.  
  5. const
  6.     Nm = 20;
  7.     Mm = 20;
  8.  
  9. type
  10.     TInc = array [1..Nm, 1..Mm] of Integer;
  11.     TMas = array [1..Mm] of Integer;
  12.  
  13. procedure Inp(var A: TInc; var Ew,Vw: TMas; var n,m,v,w,s: byte);
  14. procedure Outp(A: TInc; Ew,Vw: TMas; n,m,v,w,s: byte);
  15. procedure VAdj(A: TInc; n,m,v: byte; var adj: TMas);
  16. function CheckAdj(A: TInc; m,v,w: byte): byte;
  17. function Eweight(A: TInc; Ew: TMas; m,v,w: byte): Integer;
  18. function VAdjToEdge(A: TInc; i,s: byte): byte;
  19.  
  20. implementation
  21.  
  22. uses Math;
  23.  
  24. procedure Inp(var A: TInc; var Ew,Vw: TMas; var n,m,v,w,s: byte);
  25. var
  26.     Finc,Fedg,Fvert: text;
  27.     i,j: byte;
  28. begin
  29.     assign(Finc,'inc_graph.txt');
  30.     assign(Fedg,'Eweight.txt');
  31.     assign(Fvert,'Vweight.txt');
  32.     reset(Finc);
  33.     readln(Finc,n,m);
  34.     for i:=1 to n do
  35.         for j:=1 to m do
  36.             read(Finc,A[i,j]);
  37.     close(Finc);
  38.     reset(Fedg);
  39.     for j:=1 to m do
  40.         read(Fedg,Ew[j]);
  41.     close(Fedg);
  42.     reset(FVert);
  43.     for j:=1 to m do
  44.         read(Fvert,Vw[j]);
  45.     close(Fvert);
  46.  
  47.     Repeat
  48.         writeln('enter vertexes:_ v,w');
  49.         readln(v,w);
  50.         writeln('enter edge:_ s');
  51.         readln(s);
  52.     Until
  53.         ((v > 0) and (v <=n ) and (w > 0) and (w <= m) and (s > 0) and (s <= m));
  54.     writeln;
  55. end;
  56.  
  57. procedure Outp(A: TInc; Ew,Vw: TMas; n,m,v,w,s: byte);
  58. var
  59.     i,j,cntr: byte;
  60.     adj,edges: TMas;
  61. begin
  62.     write('вершины ',v,', ',w);
  63.     if ((CheckAdj(A,m,v,w)) <> 0) then
  64.         writeln(' смежны')
  65.     else
  66.         writeln(' не смежны');
  67.  
  68.     write('вершины, смежные с ',v,':_');
  69.     VAdj(A,n,m,v,adj);
  70.     cntr:=adj[1];
  71.     for j:=2 to cntr do
  72.         write(' ',Abs(adj[j]));
  73.     writeln;
  74.  
  75.     if (Eweight(A,Ew,m,v,w) <> -32000) then
  76.         writeln('вес ребра (',v,',',w,'):_ ', Eweight(A,Ew,m,v,w));
  77.  
  78.     writeln('вес вершины ',v,':_ ', Vw[v]);
  79.  
  80.     write('ребра, инцидентные вершине ',v,':_ ');
  81.     VAdj(A,n,m,v,edges);
  82.     cntr:=edges[1];
  83.     for j:=2 to cntr do
  84.         if (edges[j] < 0) then
  85.             write(' (',v,',',Abs(edges[j]),') ')
  86.         else
  87.             write(' (',edges[j],',',v,') ');
  88.     writeln;
  89.    
  90.     write('вершины, инцидентные ребру ',s,':_ ');
  91.         for i:=1 to n do
  92.         begin
  93.             cntr:=VAdjToEdge(A,i,s);
  94.             if (cntr <> 0) then
  95.                 write(i,' ');
  96.         end;
  97.     writeln;
  98. end;
  99.  
  100. function CheckAdj(A: TInc; m,v,w: byte): byte;
  101. {returns edge}
  102. var
  103.     j: byte;
  104. begin
  105.     CheckAdj:=0;
  106.     for j:=1 to m do
  107.         if (A[v,j] <> 0) and (A[w,j] <> 0) then
  108.             CheckAdj:=j;
  109. end;
  110.  
  111. function VAdjToEdge(A: TInc; i,s: byte): byte;
  112. begin
  113.     VAdjToEdge:=A[i,s];    
  114. end;
  115.  
  116. procedure VAdj(A: TInc; n,m,v: byte; var adj: TMas);
  117. var
  118.     i,j,cntr: byte;
  119. begin
  120.     cntr:=0;
  121.     adj[1]:=cntr;
  122.     for j:=1 to m do
  123.     begin
  124.         if (A[v,j] <> 0) then
  125.             for i:=1 to n do
  126.                 if (A[i,j] <> 0) and (i <> v) then
  127.                 begin
  128.                     inc(cntr);
  129.                     if (A[i,j] = 1) then
  130.                         adj[cntr+1]:=i
  131.                     else
  132.                         adj[cntr+1]:=-i;
  133.                 end;
  134.     end;
  135.     adj[1]:=cntr+1;
  136. end;
  137.  
  138. function Eweight(A: TInc; Ew: TMas; m,v,w: byte): Integer;
  139. var
  140.     edge: byte;
  141. begin
  142.     edge:=CheckAdj(A,m,v,w);
  143.     if (edge <> 0) then
  144.         Eweight:=Ew[edge]
  145.     else
  146.         Eweight:=-32000;
  147. end;
  148.  
  149. end.
Advertisement
Add Comment
Please, Sign In to add comment