0 Carteaticafinalpt NODEA

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 69

GRAFURI NEORIENTATE end;

{Dati variante de citire a matricei de adiacenta for i:=1 to n do


in cazul grafurilor neorientate} vi[i]:=0;
var a:array[1..20,1..20] of 0..1; writeln(' care este nodul de plecare ');readln(pl);
i,j,n:integer; vi[pl]:=1;
begin co[1]:=pl;
writeln('n=');readln(n); p:=1;
for i:=1 to n-1 do u:=1;
for j:=i+1 to n do while p<=u do
begin begin
writeln('a[',i,',',j,']=');readln(a[i,j]); el:=co[p];
a[j,i]:=a[i,j]; for j:=1 to n do
end; if (a[el,j]=1) and (vi[j]=0) then begin
for i:=1 to n do u:=u+1;
begin co[u]:=j;
for j:=1 to n do vi[j]:=1;
write(a[i,j],' '); end;
writeln; p:=p+1;
end; end;
for i:=1 to u do
readln; write(co[i],' ');
end. pp(1);
{Parcuregerea in latime nerecursiv si recursive writeln(' parcurgere recursiva in latime ');
la grafuri for i:=1 to u do
ex: write(co[i],' ');
n=6 readln;
8 muchii si anume (1,2),(1,3)(1,4),(2,4),(2,3), end.
(3,4),(3,6),(4,5) {Parcuregerea in adincime la grafuri
=>1 2 3 4 6 5 } neorientate
uses crt; ex:n=5
var a:array[1..20,1..20] of integer; m=4
co,vi:array[1..20] of integer; si anume (1 2),(1 5), ( 2 3),(2 4)
i,n,el,j,p,u,pl,m,x,y:integer; nodul de pelecare este 1 =>
12345
procedure pp(i:integer); }
var j:integer; uses crt;
begin var a:array[1..20,1..20] of integer;
for j:=1 to n do vi:array[1..20] of integer;
if (a[co[i],j]=1) and( vi[j]=0) then begin i,n,el,j,p,u,pl,m,x,y:integer;
u:=u+1;
co[u]:=j; procedure pp(pl:integer);
vi[j]:=1; var j:integer;
end; begin
if i<=u then pp(i+1); write(pl,' ');vi[pl]:=1;
end; for j:=1 to n do
begin if (a[pl,j]=1) and (vi[j]=0) then pp(j);
clrscr; end;
writeln(' n=');readln(n);
writeln(' nuamrul de muchii ');readln(m);
for i:=1 to m do begin
begin clrscr;
writeln('dati muchia ',i); writeln(' n=');readln(n);
writeln( 'primul nod al muchiei ');readln(x); writeln(' numarul de muchii ');readln(m);
writeln(' nodul de sfirsit al muchiei ');readln(y); for i:=1 to m do
a[x,y]:=1;a[y,x]:=1; begin
writeln('dati muchia ',i);

1
writeln( 'primul nod al muchiei ');readln(x); end;
writeln(' nodul de sfirsit al muchiei ');readln(y);
a[x,y]:=1;a[y,x]:=1; begin
end;
for i:=1 to n do write('dati numarul de noduri din graf');
vi[i]:=0; readln(n);
writeln(' care este nodul de plecare ');readln(pl);
pp(pl); for i:=1 to n-1 do
readln; begin
end. for j:=i+1 to n do
{ Se citeste matricea de adiacenta la un graf begin
neorientat write('numarul ',i,',',j,' este ');
Sa se genereze toate ciclurile hamiltoniene ale readln(a[i,j]);
acestuia a[j,i]:=a[i,j];
end;
ex:n=3 end;x[1]:=1;
=>1 3 2 1 k:=2; j:=0;
1 2 3 1} x[k]:=1;
type sir =array[1..100] of integer; while(k>0) do
begin
var x:sir; repeat
p,i,j,k,n,k1:integer; succesor(x,k,as);
as,ev:boolean; if as then valid(x,k,ev)
a:array[1..50,1..50] of integer; until (as and ev) or (not as);
if as then
procedure succesor(var x:sir;k:integer;var if (k=n) then
as:boolean); afis(x,k)
begin else
if (x[k]<n) then begin begin
as:=true; x[k]:=x[k]+1; k:=k+1;
x[k]:=1;
end end
else else
as:=false; k:=k-1 ;
end; end;
readln;
procedure valid(x:sir;k:integer;var ev:boolean); end.
var i:integer; {Stiind ca un graf conex(are o singura
begin componenta conexa) ,
ev:=true; fara virfuri izolate si care are gradele fiecarui
if a[x[k-1],x[k]]=0 then ev:=false virf un numar par
else Sa se determine daca este ciclu hamiltoanian sa
begin nu
for i:=1 to k-1 do ex:n=3
if x[k]=x[i] then ev:=false; m=3 si anume (1,2),(1,3),(2,3)
if (k=n) and(a[x[n],x[1]]=0) then ev:=false; =>1 2 3 1 e ciclu hamiltonian}
end; var eulerian:boolean;
end; e,grd,nd,c1:array[1..20] of integer;
a:array[1..20,1..20] of integer;
procedure afis(x:sir;k:integer); n,m,i,j,k,k1,x,y,l:integer;
var i:integer; viz:array[1..20] of byte;
begin function izolate:boolean;
for i:=1 to k do var i,j,m,intr:byte;
write( x[i] ,' '); begin
write(x[1]:4); izolate:=false;
writeln; for i:=1 to n do

2
begin a[i,j]:=0;
intr:=0; writeln('dati muchiile ');
for j:=1 to n do for i:=1 to m do
intr:=intr+a[i,j]; begin
grd[i]:=intr; writeln('primul nod al muchiei ',i,' este
if intr=0 then begin ');readln(x);
izolate:=true; writeln('urmatorul nod al muchiei ',i,' este
break; ');readln(y);
end; a[x,y]:=1;
end; a[y,x]:=1;
end; end;
function gradepare:boolean; eulerian:=not izolate and gradepare and conex;
var i:byte; if not eulerian then writeln ('graful nu este
begin eulerian ')
gradepare:=true; else
for i:=1 to n do begin
if odd(grd[i]) then begin writeln('este eulerian ');
gradepare:=false; nd[1]:=1;
break; k:=1;
end; repeat
end; for j:=1 to n do
function conex:boolean; if a[nd[k],j]=1 then begin
var b,viz:array[1..20] of byte; k:=k+1;
i,j,p,u,v:byte; nd[k]:=j;
begin a[nd[k-1],j]:=0;
for j:=1 to n do a[j,nd[k-1]]:=0;
viz[j]:=0; grd[j]:=grd[j]-1;
b[1]:=1; grd[nd[k-1]]:=grd[nd[k-1]]-1;
p:=1; break;
u:=1; end;
viz[1]:=1; until nd[k]=1;
while p<=u do begin while k-1 <m do begin
v:=b[p]; for i:=1 to k-1 do
for j:=1 to n do if grd[nd[i]]>0 then begin
if (a[v,j]=1) and (viz[j]=0) then c1[1]:=nd[i];
begin L:=i;
u:=u+1; break;
b[u]:=j; end;
viz[j]:=1; k1:=1;
end; repeat
p:=p+1; for j:=1 to n do
end; if a[c1[k1],j]=1 then begin
conex:=true; k1:=K1+1;
for i:=1 to n do c1[k1]:=j;
if viz[i]=0 then begin a[c1[k1-1],j]:=0;
conex:=false; a[j,c1[k1-1]]:=0;
break; grd[j]:=grd[j]-1;
end; grd[c1[k1-1]]:=grd[c1[k1-1]]-1;
end; break;
begin end;
write(' cite noduri are graful n='); until c1[k1]=c1[1];
readln(n); for j:=k downto L do
write(' cite muchii are graful m='); nd[j+k1-1]:=nd[j];
readln(m); for j:=1 to k1-1 do
for I:=1 to n do nd[j+1]:=c1[j+1];
for j:=1 to n do k:=K+k1-1;

3
end; nr:array[1..25] of integer; a:array[1..20,1..20]
writeln('un ciclu care trece prin toate muchiile of 0..1;
este '); i,j,n,v:integer;c:ref;
for i:=1 to m+1 do begin
write(' ',nd[i]); writeln('n=');readln(n);
end; for i:=1 to n do
readln; begin
end. writeln(' citi vecini are nodul ',i);readln(nr[i]);
{Dati variante de citire a matricei de adiacenta new(c);
in cazul grafurilor neorientate cind se citesc si if (nr[i]=0) then begin
numarul de muchii} c:=nil;
var a:array[1..20,1..20] of 0..1; L[i]:=c;
i,j,n,m,x,y:integer; end
begin else
writeln('n=');readln(n); begin
writeln(' numarul de muchii ');readln(m); writeln(' dati primul vecin ');
for i:=1 to n do readln(v);
for j:=1 to n do c^.nod:=v;
begin c^.urm:=nil;
a[i,j]:=0; L[i]:=c;
end; for j:=1 to nr[i]-1 do
for i:=1 to m do begin
begin writeln(' dati alt vecin ');readln(v);
writeln('dati muchia ',i); new(c);
writeln('nodul de inceput ');readln(x); c^.nod:=v;
writeln('nodul de sfirsit ');readln(y); c^.urm:=L[i];
a[x,y]:=1;a[y,x]:=1; L[i]:=c;
end; end;
for i:=1 to n do end;
begin end;fillchar(a,sizeof(a),0);
for j:=1 to n do for i:=1 to n do
write(a[i,j],' '); begin
writeln; c:=L[i];
end; while c<>nil do
begin
readln; a[i,c^.nod]:=1;
end. c:=c^.urm;
{Cu liste de adiacenta sa se memoreze un graf end;
neorientat flosind alocare dinamica end;
ex: for i:=1 to n do
n=3 begin
1 are 2 vecini pe 2 si 3 for j:=1 to n do
2 are 1 vecin pe 1 write(a[i,j],' ');
3 are un vecin pe 1 writeln;
=> matricea de adiacenta end;
011
100 readln;
1 0 0} end.
type ref=^inr; {Cu liste de adiacenta sa se memoreze un graf
inr=record neorientat folosind tablouri
nod:integer; ex:
urm:ref; n=3
end; 1 are 2 vecini pe 2 si 3
var L:array[1..20] of ref; 2 are 1 vecin pe 1
3 are un vecin pe 1
=> matricea de adiacenta

4
011 writeln(' dati primul nod al muchiei ');
100 readln(e1[i]);
1 0 0} writeln(' dati ultimul nod al muchiei ');
var a,L:array[1..10,1..10] of integer; readln(e2[i]);
nr:array[1..10] of integer; end;
var i,j,n,k:integer; fillchar(a,sizeof(a),0);
begin for i:=1 to m do
writeln('n=');readln(n); begin
for i:=1 to n do a[e1[i],e2[i]]:=1;
begin a[e2[i],e1[i]]:=1;
writeln('dati numarul vecinilor nodului end;
',i);readln(nr[i]); for i:=1 to n do
for j:=1 to nr[i] do begin
begin for j:=1 to n do
writeln('vecinul cu numarul ',j);readln(L[i,j]); write(a[i,j],' ');
end; writeln;
end; end;
fillchar(a,sizeof(a),0);
for i:=1 to n do readln;
begin end.
for j:=1 to nr[i] do 
a[i,l[i,j]]:=1; {Dati variante de citire a matricei de adiacenta
end; in cazul
for i:=1 to n do grafurilor neorientate si punerea datelor in
begin vectorii muchiilor(vectorii
for j:=1 to n do care memoreaza extremitatea primei muchii, a
write(a[i,j],' '); doua muchie ,....}
writeln;
end;
VAR
readln; a:array[1..20,1..20] of 0..1;
end. i,j,n,m,k:integer; E1,E2:ARRAY[1..20] OF
INTEGER;
{Daca se cunosc numarul de noduri si de begin
muchii precum si extremitatile writeln('n=');readln(n);
muchiilor sa se construiasca matricea de for i:=1 to n-1 do
adiacenta for j:=i+1 to n do
ex: begin
n=2 writeln('a[',i,',',j,']=');readln(a[i,j]);
m=3 a[j,i]:=a[i,j];
muchile 1 2 end;
13 for i:=1 to n do
=> begin
matricea de adiacenta for j:=1 to n do
011 write(a[i,j],' ');
100 writeln;
1 0 0} end;
VAR E1,E2:ARRAY[1..20] OF INTEGER;
M,N,I,j:INTEGER;a:array[1..20,1..20] of k:=0;
integer; for i:=1 to n-1 do
BEGIN for j:=i+1 to n do
WRITELN(' CITE NODURI ');READLN(N); if a[i,j]=1 then
WRITELN(' cite muchii ');readln(m); begin
for i:=1 to m do k:=k+1;
begin e1[k]:=i;
writeln(' dati muchia cu numarul ',i); e2[k]:=j;

5
end; writeln;
m:=k; end;
writeln('---------valorile nenule din matrice sunt
pe pozitiile'); readln;
for i:=1 to m do end.
begin
writeln(e1[i],',',e2[i],' ');
writeln(e2[i],',',e1[i],' '); 
end; {Dati variante de citire a matricei de adiacenta
writeln; in cazul
grafurilor neorientate si punerea datelor in
vectorii muchiilor(vectorii
readln; care memoreaza extremitatea primei muchii, a
end. doaua muchie ,....
{Daca se cunosc numarul de noduri si de Se foloseste tipul de date record}
muchii precum si extremitatile
muchiilor( CU TIPUL DE DATE RECORD) sa type mu=record
se construiasca matricea de adiacenta x:byte;
ex: y:byte;
n=2 end;
m=3 VAR E:ARRAY[1..20] OF mu;
muchile 1 2 a:array[1..20,1..20] of 0..1;
13 i,j,n,m,k:integer; E1,E2:ARRAY[1..20] OF
=> INTEGER;
matricea de adiacenta begin
011 writeln('n=');readln(n);
100 for i:=1 to n-1 do
1 0 0} for j:=i+1 to n do
type mu=record begin
x:byte; writeln('a[',i,',',j,']=');readln(a[i,j]);
y:byte; a[j,i]:=a[i,j];
end; end;
VAR E:ARRAY[1..20] OF mu; for i:=1 to n do
M,N,I,j:INTEGER;a:array[1..20,1..20] of begin
integer; for j:=1 to n do
BEGIN write(a[i,j],' ');
WRITELN(' CITE NODURI ');READLN(N); writeln;
WRITELN(' cite muchii ');readln(m); end;
for i:=1 to m do
begin k:=0;
writeln(' dati muchia cu numarul ',i); for i:=1 to n-1 do
writeln(' dati primul nod al muchiei '); for j:=i+1 to n do
readln(e[i].x); if a[i,j]=1 then
writeln(' dati ultimul nod al muchiei '); begin
readln(e[i].y); k:=k+1;
end; e[k].X:=i;
fillchar(a,sizeof(a),0); e[k].Y:=j;
for i:=1 to m do end;
begin m:=k;
a[e[i].x,e[i].y]:=1; writeln('---------valorile nenule din matrice sunt
a[e[i].y,e[i].x]:=1; pe pozitiile');
end; for i:=1 to m do
for i:=1 to n do begin
begin writeln(e[i].X,',',e[i].Y,' ');
for j:=1 to n do writeln(e[i].Y,',',e[i].X,' ');
write(a[i,j],' '); end;

6
writeln; end;

readln;
readln; end.
end. {Cu liste de adiacenta sa se memoreze un graf
orientat folosind tablouri
GRAFURI ORIENTATE ex:
n=3
{Dati variante de citire a matricei de adiacenta 1 are 2 succesor pe 2 si 3
in cazul grafurilor orientate} 2 are 1 succesor pe 1
var a:array[1..20,1..20] of 0..1; 3 are un succesor pe 1
i,j,n:integer; => matricea de adiacenta
begin 011
writeln('n=');readln(n); 100
for i:=1 to n do 1 0 0}
for j:=1 to n do var a,L:array[1..10,1..10] of integer;
begin nr:array[1..10] of integer;
writeln('a[',i,',',j,']=');readln(a[i,j]); var i,j,n,k:integer;
procedure cc;
end; var i,j,k:integer;
for i:=1 to n do begin
begin for i:=1 to n do begin
for j:=1 to n do k:=0;
write(a[i,j],' '); for j:=1 to n do
writeln; if a[i,j]=1 then begin
end; k:=k+1;
L[i,k]:=j;
readln; end;
end. end;
{Dati variante de citire a matricei de adiacenta end;
in cazul
grafurilor orientate cind se citesc si numarul de
begin
muchii}
writeln('n=');readln(n);
var a:array[1..20,1..20] of 0..1;
for i:=1 to n do
i,j,n,m,x,y:integer;
begin
begin
writeln('dati numarul vecinilor nodului
writeln('n=');readln(n);
',i);readln(nr[i]);
writeln(' numarul de muchii ');readln(m);
for j:=1 to nr[i] do
for i:=1 to n do
begin
for j:=1 to n do
writeln('unde ajunge arcul care pelaca din
begin
nodul(succesorul nodului ) ',j);readln(L[i,j]);
a[i,j]:=0;
end;
end;
end;
for i:=1 to m do
fillchar(a,sizeof(a),0);
begin
for i:=1 to n do
writeln('dati muchia ',i);
begin
writeln('nodul de inceput ');readln(x);
for j:=1 to nr[i] do
writeln('nodul de sfirsit ');readln(y);
a[i,l[i,j]]:=1;
a[x,y]:=1;
end;
end;
for i:=1 to n do
for i:=1 to n do
begin
begin
for j:=1 to n do
for j:=1 to n do
write(a[i,j],' ');
write(a[i,j],' ');
writeln;
writeln;
end;

7
ARCELE 1 2
readln; 13
end. =>
matricea de adiacenta
011
{Dati variante de citire a matricei de adiacenta 000
in cazul grafurilor orientate cind se citesc si 0 0 0}
numarul de muchii VAR E1,E2:ARRAY[1..20] OF INTEGER;
Sa se prezinte o modalitate de transformare a M,N,I,j:INTEGER;a:array[1..20,1..20] of
matricei de adiacenta in matricea integer;
drumurilor} BEGIN
var a:array[1..20,1..20] of 0..1; WRITELN(' CITE NODURI ');READLN(N);
k,i,j,n,m,x,y:integer; WRITELN(' cite ARCURI ');readln(m);
begin for i:=1 to m do
writeln('n=');readln(n); begin
writeln(' numarul de muchii ');readln(m); writeln(' dati ARCUL cu numarul ',i);
for i:=1 to n do writeln(' dati primul VARF al ARCULUI ');
for j:=1 to n do readln(e1[i]);
begin writeln(' dati ultimul VARF al ARCULUI ');
a[i,j]:=0; readln(e2[i]);
end; end;
for i:=1 to m do fillchar(a,sizeof(a),0);
begin for i:=1 to m do
writeln('dati muchia ',i); begin
writeln('nodul de inceput ');readln(x); a[e1[i],e2[i]]:=1;
writeln('nodul de sfirsit ');readln(y); end;
a[x,y]:=1; for i:=1 to n do
end; begin
for i:=1 to n do for j:=1 to n do
begin write(a[i,j],' ');
for j:=1 to n do writeln;
write(a[i,j],' '); end;
writeln;
end; readln;
for k:=1 to n do end.
for i:=1 to n do
for j:=1 to n do
if a[i,j]=0 then a[i,j]:=a[i,k]*a[k,j];  {Dati variante de citire a matricei de
writeln(' matricea drumurilor este '); adiacenta in cazul grafurilor orientate si
for i:=1 to n do begin punerea datelor in vectorii muchiilor(vectorii
write(a[i,j]:6); care memoreaza extremitatea primei muchii, a
writeln; doua muchie ,....}
end; VAR
readln; a:array[1..20,1..20] of 0..1;
end. i,j,n,m,k:integer; E1,E2:ARRAY[1..20] OF
INTEGER;
readln; begin
end. writeln('n=');readln(n);
{Daca se cunosc numarul de VIRFURI si de for i:=1 to n do
ARCE precum si extremitatile for j:=1 to n do
muchiilor sa se construiasca matricea de begin
adiacenta LA GRAFURI ORIENTATE writeln('a[',i,',',j,']=');readln(a[i,j]);
ex: a[j,i]:=a[i,j];
n=3 end;
m=2 for i:=1 to n do
begin

8
for j:=1 to n do readln(e[i].y);
write(a[i,j],' '); end;
writeln; fillchar(a,sizeof(a),0);
end; for i:=1 to m do
begin
k:=0; a[e[i].x,e[i].y]:=1;
for i:=1 to n do end;
for j:=1 to n do for i:=1 to n do
if a[i,j]=1 then begin
begin for j:=1 to n do
k:=k+1; write(a[i,j],' ');
e1[k]:=i; writeln;
e2[k]:=j; end;
end;
m:=k; readln;
writeln('---------valorile nenule din matrice sunt end.
pe pozitiile');
for i:=1 to m do
begin 
writeln(e1[i],',',e2[i],' '); {Dati variante de citire a matricei de adiacenta
writeln(e2[i],',',e1[i],' '); in cazul grafurilor orientate si punerea datelor
end; in vectorii muchiilor(vectorii care memoreaza
writeln; extremitatea primei muchii, a doua muchie ,....
readln; Se foloseste tipul de date record}
end. type mu=record
{Daca se cunosc numarul de noduri si de x:byte;
muchii precum si extremitatile y:byte;
ARCELOR( CU TIPUL DE DATE RECORD) end;
sa se construiasca matricea de adiacenta VAR E:ARRAY[1..20] OF mu;
LA GRAFURI ORIENTATE a:array[1..20,1..20] of 0..1;
ex: i,j,n,m,k:integer; E1,E2:ARRAY[1..20] OF
n=2 INTEGER;
m=3 begin
muchile 1 2 writeln('n=');readln(n);
13 for i:=1 to n do
=> for j:=1 to n do
matricea de adiacenta begin
011 writeln('a[',i,',',j,']=');readln(a[i,j]);
000 end;
0 0 0} for i:=1 to n do
type mu=record begin
x:byte; for j:=1 to n do
y:byte; write(a[i,j],' ');
end; writeln;
VAR E:ARRAY[1..20] OF mu; end;
M,N,I,j:INTEGER;a:array[1..20,1..20] of k:=0;
integer; for i:=1 to n do
BEGIN for j:=1 to n do
WRITELN(' CITE NODURI ');READLN(N); if a[i,j]=1 then
WRITELN(' cite muchii ');readln(m); begin
for i:=1 to m do k:=k+1;
begin e[k].X:=i;
writeln(' dati muchia cu numarul ',i); e[k].Y:=j;
writeln(' dati primul nod al muchiei '); end;
readln(e[i].x); m:=k;
writeln(' dati ultimul nod al muchiei ');

9
writeln('---------valorile nenule din matrice sunt for j:=i+1 to n do
pe pozitiile'); if a[i,j]=0 then writeln(i,' ',j);
for i:=1 to m do end;
begin begin
writeln(e[i].X,',',e[i].Y,' '); writeln(' dati numarul de noduri:'); readln(n);
writeln(e[i].Y,',',e[i].X,' '); for i:=1 to n-1 do
end; for j:=i+1 to n do
writeln; begin
readln; writeln('a[',i,',',j,']=');readln(a[i,j]);
end. a[j,i]:=a[i,j];
{Sa se scrie cel putin doua metode care end;
testeaza daca un graf este sau nu complet
Sa se determine cite muchii mai trebuie writeln('graful este sau nu complet',cc);
adaugate ca acesta sa devina complet writeln('graful este sau nu complet',ccc);
Sa se determine nodurile care nu sunt unite writeln('graful complet daca adaug atitea
printr-o muchie} muchii adica',cccc);
type mat=array[1..20,1..20] of integer; writeln(' nodurile neunite printr-o muchie');
sir=array[1..30] of integer; aa;
var a:mat; readln;
i,j,n:integer; o:boolean; end.
function g(i:integer):integer; {Parcurgerea in adincime la grafuri
var s,j:integer; neorientate Sa se determine daca daca dintr-un
begin nod dat se poate ajunge la altele si la care
s:=0; anume
for j:=1 to n do ex:n=3
s:=s+a[i,j]; m=2
g:=s; si anume (1 2)
end; nodul de plecare este 1 =>
function cc:boolean; 1 e accesibil la 2
var i:integer; }
begin uses crt;
cc:=true; var a:array[1..20,1..20] of integer;
for i:=1 to n do vi:array[1..20] of integer;
if g(i)<>n-1 then cc:=false; i,n,el,j,p,u,pl,m,x,y:integer;oK:boolean;
end;
function ccc:boolean; procedure pp(pl:integer);
var i:integer; var j:integer;
begin begin
ccc:=true; vi[pl]:=1;
for i:=1 to n-1 do for j:=1 to n do
for j:=i+1 to n do if (a[pl,j]=1) and (vi[j]=0) then pp(j);
if a[i,j]<>1 then ccc:=false; end;
end;
function cccc:integer;
var nr:integer; begin
begin clrscr;
nr:=0; writeln(' n=');readln(n);
for i:=1 to n-1 do writeln(' numarul de muchii ');readln(m);
for j:=i+1 to n do for i:=1 to m do
if a[i,j]=0 then nr:=nr+1; begin
cccc:=nr; writeln('dati muchia ',i);
end; writeln( 'primul nod al muchiei ');readln(x);
procedure aa; writeln(' nodul de sfirsit al muchiei ');readln(y);
var i,j:integer; a[x,y]:=1;a[y,x]:=1;
begin end;
for i:=1 to n-1 do for i:=1 to n do

10
vi[i]:=0; for j:=1 to n do
writeln(' care este nodul de plecare ');readln(pl); write(a[i,j]:4);
pp(pl); writeln;
writeln('--------'); end;
ok:=false;
for i:=1 to n do writeln;
if (vi[i]=1) and (i<>pl) then write('nodurile end;
acecesibile din nodul ',pl,' SUNT ',i:5);
begin
readln;
end. write('dati numarul de noduri din graf');
{ Sa se genereze toate matricele de adiacenta la readln(n);
un graf neorientat n1:=(n*n-n) div 2;
x[1]:=1;
ex:n=3 k:=2; j:=0;
} x[k]:=-1;
type sir =array[1..100] of integer; while(k>0) do
begin
var x:sir; repeat
n1,p,i,j,k,n,k1:integer; succesor(x,k,as);
as,ev:boolean; if as then valid(x,k,ev)
a:array[1..50,1..50] of integer; until (as and ev) or (not as);
if as then
procedure succesor(var x:sir;k:integer;var if (k=n1) then
as:boolean); afis(x,k)
begin else
if (x[k]<1) then begin begin
as:=true; x[k]:=x[k]+1; k:=k+1;
x[k]:=-1;
end end
else else
as:=false; k:=k-1 ;
end; end;
readln;
procedure valid(x:sir;k:integer;var ev:boolean); end.
var i:integer;
begin
ev:=true;

end; {
Sa se determine daca graful eate bipartit sau nu
procedure afis(x:sir;k:integer); folosind tipul de date
var i,j,kk:integer; multime}
begin type
fillchar(a,sizeof(a),0); sir=array[1..30] of integer;
kk:=1; var
for i:=1 to n-1 do i,j,n:integer; o:boolean;
v1,v2:set of 1..34;
for j:=i+1 to n do a:array[1..23,1..23] of 0..1;
begin begin
a[i,j]:=x[k]; writeln(' dati numarul de noduri:'); readln(n);
kk:=kk+1; for i:=1 to n-1 do
a[j,i]:=a[i,j]; for j:=i+1 to n do
end; begin
for i:=1 to n do writeln('a[',i,',',j,']=');readln(a[i,j]);
begin a[j,i]:=a[i,j];

11
end; a[j,i]:=1;
v1:=[]; end;
v2:=[]; for i:=1 to n do
for i:=1 to n-1 do begin
for j:=i+1 to n do for j:=1 to n do
if a[i,j]=1 then write(a[i,j]:4);
if i in v2 then v1:=v1+[j] writeln;
else end;
begin readln;
v1:=v1+[i]; end.
v2:=v2+[j]; {
end; Sa se determine daca o matrice poate
writeln(' multimile sunt'); reprezenta sau nu un graf neorientat}
for i:=1 to 34 do type mat=array[1..20,1..20] of integer;
if i in v1 then write(i:5); sir=array[1..30] of integer;
writeln; var a:mat;
for i:=1 to 34 do i,j,n:integer; o1,o2,o3,o:boolean;
if i in v2 then write(i:5);
if (v1=[]) or (v2=[]) then writeln(' graful NU begin
este bipartit') writeln(' dati numarul de noduri:'); readln(n);
else for i:=1 to n do
writeln(' graful este bipartit'); for j:=1 to n do
begin
writeln('a[',i,',',j,']=');readln(a[i,j]);
readln; a[j,i]:=a[i,j];
end. end;
{
Sa se determine matricea de adiacenta in care o1:=true;
la graful bipartit prima multime contine numai for i:=1 to n do
numere pare iar cealalata numai impare if (a[i,i]<>0) then o1:=false;;
ex: o2:=true;
n=3 for i:=1 to n-1 do
A=1 ,3 for j:=i+1 to n do
B=2} if not (a[i,j] in [0,1]) then o2:=false;
type o3:=true;
sir=array[1..30] of integer; for i:=1 to n-1 do
var for j:=i+1 to n do
i,j,n:integer; o:boolean; if (a[i,j]<>a[j,i]) then o3:=false;
v1,v2:set of 1..34; o:=o1 and o2 and o3;
a:array[1..23,1..23] of 0..1; writeln(' matricea poate reperezenta un graf sau
begin nu ',o);
writeln('numarul de virfuri ');readln(n); readln;
fillchar(a,sizeof(a),0); end.
for i:=1 to n-1 do {Sa se determine daca un graf este graf partial
for j:=i+1 to n do al unui graf cunoscindu-se matricea de
if (i mod 2=0) and (j mod 2<>0) then adiacenta la ambele( se elimina niste muchii
begin din graful initial nu si nodurile care o sustin)
a[i,j]:=1; ex:
a[j,i]:=1; n=3
end; cu muchiile (1,2),(2,3)
si graful partial cu muchia (1,2)
for i:=1 to n-1 do OBS: IN REST MATRICEA DE ADIACENTA
for j:=i+1 to n do E ZERO}
if (i mod 2<>0) and (j mod 2=0) then type mat=array[1..20,1..20] of integer;
begin sir=array[1..30] of integer;
a[i,j]:=1;

12
var a1,a:mat; begin
i,j,n:integer; o:boolean; ev:=true;

begin end;
writeln(' dati numarul de noduri:'); readln(n);
for i:=1 to n-1 do procedure afis(x:sir;k:integer);
for j:=i+1 to n do var i,j,kk,s:integer;
begin begin
writeln('a[',i,',',j,']=');readln(a[i,j]); s:=0;
a[j,i]:=a[i,j]; for i:=1 to n do
end;o:=true; s:=s+x[i];
writeln(' dati graful care vreti sa aflati daca e if (s<>n) then
subgraf'); begin
for i:=1 to n-1 do writeln('prima multime ');
for j:=i+1 to n do for i:=1 to n do
begin if x[i]=1 then write(i:7);
writeln('a1[',i,',',j,']=');readln(a1[i,j]); writeln;
a1[j,i]:=a1[i,j]; writeln(' a doua multime ');
end; for i:=1 to n do
for i:=1 to n-1 do if x[i]=0 then write(i:7);
for j:=i+1 to n do writeln;
if not (a1[i,j]<=a[i,j]) then o:=false;
if o then writeln(' graful dat prin a1 este graf end;
partial ')
else writeln;
writeln('nu egraf partial '); end;
readln;
end. begin
{ Sa se genereze toate GARFURILE
BIPARTITE complete(adica avem doaua write('dati numarul de noduri din graf');
multimi disjuncte intre care exista legaturi readln(n);
reciproce ca directie )
la un graf neorientat x[1]:=1;
k:=2; j:=0;
ex:n=3 x[k]:=-1;
} while(k>0) do
type sir =array[1..100] of integer; begin
repeat
var x:sir; succesor(x,k,as);
n1,p,i,j,k,n,k1:integer; if as then valid(x,k,ev)
as,ev:boolean; until (as and ev) or (not as);
a:array[1..50,1..50] of integer; if as then
if (k=n) then
procedure succesor(var x:sir;k:integer;var afis(x,k)
as:boolean); else
begin begin
if (x[k]<1) then begin k:=k+1;
as:=true; x[k]:=x[k]+1; x[k]:=-1;
end
end else
else k:=k-1 ;
as:=false; end;
end; readln;
end.
procedure valid(x:sir;k:integer;var ev:boolean);
var i:integer;

13
{Sa se determine toate grafurile partiale( se write('dati numarul de noduri din graf');
elimina niste muchii din graful initial readln(n);
nu si nodurile care o sustin) ale unui graf dat write('dati numarul de muchii din graful partial
ex: n=3 de-l generati');
muchiile (1,2),(1,3)} readln(p);
type sir =array[1..100] of integer; for i:=1 to p do
mu=record begin
x,y:integer;end; writeln('punctul de inceput al muchiei
var x:sir; ',i);readln(m[i].x);
nr,p,i,j,k,n,k1:integer; writeln('punctul de de sfirsit al muchiei
as,ev:boolean; ',i);readln(m[i].y);
a:array[1..50,1..50] of 0..1; end;
m:array[1..50] of mu; nr:=0;
procedure succesor(var x:sir;k:integer;var
as:boolean); k:=1; j:=0;
begin x[k]:=-1;
if (x[k]<1) then begin while(k>0) do
as:=true; x[k]:=x[k]+1; begin
repeat
end succesor(x,k,as);
else if as then valid(x,k,ev)
as:=false; until (as and ev) or (not as);
end; if as then
if (k=p) then
procedure valid(x:sir;k:integer;var ev:boolean); afis(x,k)
var i:integer; else
begin begin
ev:=true; k:=k+1;
x[k]:=-1;
end; end
else
procedure afis(x:sir;k:integer); k:=k-1 ;
var i,j,kk,s:integer; end;
begin readln;
fillchar(a,sizeof(a),0); end.
for i:=1 to p do
if x[i]=1 then begin
a[m[i].x,m[i].y]:=1; { Sa se genereze toate subGARFURILE (se
a[m[i].y,m[i].x]:=1; eleimina noduri si muchiile care se leaga de
end; nodurile eleiminate din grafule initial)
nr:=nr+1;
writeln(' solutia' ,nr); ex:n=4
for i:=1 to n do muchiile (1,2),(1,4),(2,3)
begin }
for j:=1 to n do type sir =array[1..100] of integer;
write(a[i,j]:5);
writeln; var x:sir;
end; m,e1,e2,n1,p,i,j,k,n,k1:integer;
as,ev:boolean;
a:array[1..50,1..50] of integer;

writeln; procedure succesor(var x:sir;k:integer;var


end; as:boolean);
begin
begin if (x[k]<1) then begin
as:=true; x[k]:=x[k]+1;

14
succesor(x,k,as);
end if as then valid(x,k,ev)
else until (as and ev) or (not as);
as:=false; if as then
end; if (k=n) then
afis(x,k)
procedure valid(x:sir;k:integer;var ev:boolean); else
var s,i:integer; begin
begin k:=k+1;
ev:=true; x[k]:=-1;
if k=n then begin end
s:=0; else
for i:=1 to k do k:=k-1 ;
s:=s+x[i]; end;
if (s=0) then ev:=false; readln;
end; end.
end;
{Sa se determine daca un garf este orientat sau
nu dat prin muchii
procedure afis(x:sir;k:integer); VARIANTA:indu-se simpatiile dintre persoane
var i,j,ns,s:integer; numerotate sa se determine daca sunt
begin reciproce
ns:=ns+1; ex:n=2
writeln(' subgraful al ',ns,'lea are nodurile 1 simpatizeaza pe 2
urmatoare '); 2 simpatizeaza pe 1
for i:=1 to k do => sunt reciproce}
if x[i]=1 then write(i:5); type ma=array[1..20,1..20] of integer;
writeln(' muchiile'); var i,j,n:integer;
for i:=1 to k-1 do a:ma;
for j:=i+1 to k do o:boolean;
if (x[i]=1) and(x[j]=1) and(a[i,j]=1) then begin
writeln('(',i,' ',j,')'); write('n=');readln(n);
writeln; fillchar(a,sizeof(a),0);
writeln; for i:=1 to n do
begin
end; write(i,'pe cine simpatizeaza ');
while not seekeoln do
begin begin

write('dati numarul de noduri din graf'); read(j);


readln(n); a[i,j]:=1;
write('dati numarul de muchii din graf'); end;
readln(m);fillchar(a,sizeof(a),0); readln;
for i:=1 to m do end;
begin o:=true;
writeln('primul nod al muchiei ',i);readln(e1); for i:=1 to n-1 do
writeln('al doilea nod al muchiei ',i);readln(e2); for j:=i+1 to n do
a[e1,e2]:=1; if a[i,j]<>a[j,i] then
a[e2,e1]:=1; begin
end; o:=false;
x[1]:=-1; writeln(' nu se potrivesc la simpatii reciproce ',i,'
k:=1; j:=0; cu ',j);
x[k]:=-1; end;
while(k>0) do if o then writeln(' toate simpatiile sunt
begin reciproce');
repeat readln;

15
end. write('descendentul drept:');
readln(subarboredrept[I]);
write('informatica :'); readln(datele[I]);
ARBORI end;
{Prin alocare statica sa se parcurga arborii writeln('preordine :'); preordine(rad); writeln;
binari writeln('inordine :'); inordine(rad); writeln;
ex:nr de noduri n=3 writeln('postordine:'); postordine(rad); writeln;
radacina=1 readln;
pt nodul 1 end.
desc stang 2
desc drept 3
pt nodul 2 {Prin alocare statica sa se determine:
desc stang 0 ('1.listarea');
desc drept 0 ('2.Listare noduri de indice par');
pt nodul 3 ('3.listare chei pare');
desc stang 0 ('4.listare noduri cu 2 descendenti');
desc drept 0 ('5.nodurile care au decat descendentul stang');
} ('6.nodurile care au decat descendentul drept');
program parcurgere; ('7.informatiile nodurilor care au decat
uses crt; descendentul stang');
type sir=array[1..30] of byte; ('8.informatiile nodurilor care au decat
var st,dr,datele:sir; descendentul drept');
rad,n,I:byte; ex:nr de noduri n=3
procedure preordine(I:integer); radacina=1
begin pt nodul 1
write(datele[I]:4); desc stang 2
if (subarborestang[I]<>0) then desc drept 3
preordine(subarborestang[I]); pt nodul 2
if (subarboredrept[I]<>0) then desc stang 0
preordine(subarboredrept[I]); desc drept 0
end; pt nodul 3
procedure inordine(I:integer); desc stang 0
begin desc drept 0}
if(subarborestang[I]<>0)then uses crt;
inordine(subarborestang[I]); type sir=array[1..30] of byte;
write(datele[I]:4); var st,dr,datele:sir;
if(subarboredrept[I]<>0) then rad,n,I,op:byte;
inordine(subarboredrept[I]);
end; procedure preordine(I:integer);
procedure postordine(I:integer); begin
begin write(datele[I]:4);
if (subarborestang[I]<>0) then if (subarborestang[I]<>0) then
postordine(subarborestang[I]); preordine(subarborestang[I]);
if (subarboredrept[I]<>0) then if (subarboredrept[I]<>0) then
postordine(subarboredrept[I]); preordine(subarboredrept[I]);
write(datele[i]:4); end;
end; procedure tiparirea_2(i:integer);
begin clrscr; begin
write('Dati numarul de noduri lae arborelui
n='); readln(n); if (i mod 2=0) then write(datele[I]:4);
write('Dati nodul readcina rad='); readln(rad); if (subarborestang[I]<>0) then
for I:=1 to n do tiparirea_2(subarborestang[I]);
begin if (subarboredrept[I]<>0) then
writeln('pentru nodul ',I,'dati:'); tiparirea_2(subarboredrept[I]);
write('descendentul stang:'); end;
readln(subarborestang[I]); procedure tiparirea_3(i:integer);

16
begin begin
write('Dati numarul de noduri lae arborelui
if (datele[I] mod 2=0) then write(i:4); n='); readln(n);
if (subarborestang[I]<>0) then write('Dati nodul readcina rad='); readln(rad);
tiparirea_3(subarborestang[I]); for I:=1 to n do
if (subarboredrept[I]<>0) then begin
tiparirea_3(subarboredrept[I]); writeln('pentru nodul ',I,'dati:');
end; write('descendentul stang:');
procedure tiparirea_4(i:integer); readln(subarborestang[I]);
begin write('descendentul drept:');
if ((subarborestang[I]<>0) and readln(subarboredrept[I]);
(subarboredrept[I]<>0)) then write(i:4); write('informatia :'); readln(datele[I]);
if (subarborestang[I]<>0) then end;
tiparirea_4(subarborestang[I]);
if (subarboredrept[I]<>0) then
tiparirea_4(subarboredrept[I]); writeln('Operatiile pe care le puteti face sunt :');
end; writeln('1.listare');
procedure tiparirea_5(i:integer); writeln('2.Listare noduri de indice par');
begin writeln('3.listare chei pare');
if ((subarboredrept[I]=0) and writeln('4.listare noduri cu 2 descendenti');
(subarborestang[I]<>0)) then write(i:4); writeln('5.nodurile care au decat descendentul
if (subarborestang[I]<>0) then stang');
tiparirea_5(subarborestang[I]); writeln('6.nodurile care au decat descendentul
if (subarboredrept[I]<>0) then drept');
tiparirea_5(subarboredrept[I]); writeln('7.informatiile nodurilor care au decat
end; descendentul stang');
procedure tiparirea_7(i:integer); writeln('8.informatiile nodurilor care au decat
begin descendentul drept');
if ((subarboredrept[I]=0) and writeln('dati optiunea dorita:');readln(op);
(subarborestang[I]<>0)) then write(i:4); case op of
if (subarborestang[I]<>0) then 1:begin
tiparirea_7(subarborestang[I]); preordine(rad);
if (subarboredrept[I]<>0) then end;
tiparirea_7(subarboredrept[I]); 2:begin
end; tiparirea_2(rad);
procedure tiparirea_6(i:integer); end;
begin 3:begin
tiparirea_3(rad);
if ((subarboredrept[I]=0) and end;
(subarborestang[I]<>0)) then write(i:4); 4:begin
if (subarborestang[I]<>0) then tiparirea_4(rad);
tiparirea_6(subarborestang[I]); end;
if (subarboredrept[I]<>0) then 5: begin
tiparirea_6(subarboredrept[I]); tiparirea_5(rad);
end; end;
procedure tiparirea_8(i:integer); 6:begin
begin tiparirea_6(rad);
write('Dati cheia nodului'); end;
readln(i); 7: begin
if ((subarboredrept[I]<>0) and tiparirea_7(rad);
(subarborestang[I]<>0)) then write(i:4); end;
if (subarborestang[I]<>0) then 8: begin
tiparirea_8(subarborestang[I]); tiparirea_8(rad);
if (subarboredrept[I]<>0) then end;
tiparirea_8(subarboredrept[I]); end;
end; readln;

17
end. if (subarboredrept[i]<>0)
then write(subarboredrept[i]:4)
{Cu alocare statica sa se determine la arbori else write('dreptul nu
binari: exista ');
('1.listarea'); end
('2.Listare noduri terminale'); else begin
('3.listare descendentii nodului nod'); if (subarborestang[i]<>0)
('4.listare nodurile care au ca tata pe nodul then tiparirea_11(subarborestang[i]);
nod'); if (subarboredrept[i]<>0)
('5.descendentul stang al nodului nod'); then tiparirea_11(subarboredrept[i]);
('6.descendentul drept al nodului nod'); end
('7.tatal nodului nod'); end;
('8.nivelul pe care se afla nodul nod'); procedure tiparirea_12(i:integer);
ex:nr de noduri n=3 begin
radacina=1
pt nodul 1 if (i=nod) then begin
desc stang 2 if (subarborestang[i]<>0)
desc drept 3 then write(subarborestang[i]:4);
pt nodul 2 if (subarboredrept[i]<>0)
desc stang 0 then write(subarboredrept[i]:4);
desc drept 0 end
pt nodul 3 else begin
desc stang 0 if (subarborestang[i]<>0)
desc drept 0} then tiparirea_12(subarborestang[i]);
uses crt; if (subarboredrept[i]<>0)
type sir=array[1..30] of byte; then tiparirea_12(subarboredrept[i]);
var st,dr,datele:sir; end
rad,n,I,op:byte; end;
nod:integer; procedure tiparirea_13(i:integer);
nivel:integer; begin
procedure preordine(I:integer);
begin if (i=nod) then begin
write(datele[I]:4); if (subarborestang[i]<>0)
if (subarborestang[I]<>0) then then write(subarborestang[i]:4)
preordine(subarborestang[I]); else write('stangul nu
if (subarboredrept[I]<>0) then exista');
preordine(subarboredrept[I]); end
end; else begin
procedure tiparirea_10(i:integer); if (subarborestang[i]<>0)
begin then tiparirea_13(subarborestang[i]);
if (subarboredrept[i]<>0)
if ((subarboredrept[i]=0) and then tiparirea_13(subarboredrept[i]);
(subarborestang[i]=0) ) then write(datele[i]:4); end
if (subarborestang[i]<>0) then end;
tiparirea_10(subarborestang[i]); procedure tiparirea_14(i:integer);
if (subarboredrept[i]<>0) then begin
tiparirea_10(subarboredrept[i]);
end; if (i=nod) then begin
procedure tiparirea_11(i:integer); if (subarboredrept[i]<>0)
begin then write(subarboredrept[i]:4)
else write('dreptul nu
if (i=nod) then begin exista');
if (subarborestang[i]<>0) end
then write(subarborestang[i]:4) else begin
else write('stangul nu if (subarborestang[i]<>0)
exista '); then tiparirea_14(subarborestang[i]);

18
if (subarboredrept[i]<>0) writeln('4.listare nodurile care au ca tata pe
then tiparirea_14(subarboredrept[i]); nodul nod');
end writeln('5.descendentul stang al nodului nod');
end; writeln('6.descendentul drept al nodului nod');
procedure tiparirea_15(i:integer); writeln('7.tatal nodului nod');
begin writeln('8.nivelul pe care se afla nodul nod');
writeln('dati optiunea dorita:');readln(op);
if ((subarborestang[i]=nod) or case op of
(subarboredrept[i]=nod)) 1:begin
then write(i:4) preordine(rad);
else begin end;
if 2:begin
(subarborestang[i]<>0) then tiparirea_10(rad);
tiparirea_15(subarborestang[i]); end;
if 3: begin write('Dati cheia nodului');
(subarboredrept[i]<>0) then readln(nod);
tiparirea_15(subarboredrept[i]); tiparirea_11(rad);
end end;
end; 4: begin write('Dati cheia nodului');
procedure readln(nod);
tiparirea_16(i:integer;nod:integer; tiparirea_12(rad);
nivel:integer); end;
begin 5:begin write('Dati cheia nodului');
readln(nod);
if (i=nod) then write(nivel) tiparirea_13(rad);
else begin end;
if (subarborestang[i]<>0) 6:begin write('Dati cheia nodului');
then readln(nod);
tiparirea_16(subarborestang[i],nod,nivel+1); tiparirea_14(rad);
if (subarboredrept[i]<>0) end;
then 7: begin write('Dati cheia nodului');
tiparirea_16(subarborestang[i],nod,nivel+1); readln(nod);
end ; tiparirea_15(rad);
end;
end; 8: begin write('Dati cheia nodului');
readln(nod);
begin
write('Dati numarul de noduri lae arborelui tiparirea_16(rad,nod,nivel); writeln(' nivelul
n='); readln(n); este ',nivel);
write('Dati nodul readcina rad='); readln(rad); end;
for I:=1 to n do end;
begin
writeln('pentru nodul ',I,'dati:'); end.
write('descendentul stang:');
readln(subarborestang[I]); {Cu alocare statica sa se determine la arbori
write('descendentul drept:'); binari:
readln(subarboredrept[I]); ('1.listare');
write('informatica :'); readln(datele[I]); ('2.Listare informatii de pe un anumit nivel');
end; ('3.listare nodurilor de pe un anumit nivel si
care contin o anumita informatie');
('4.listare nodurile care au ca tata pe nodul
writeln('Operatiile pe care le puteti face sunt :'); nod');
writeln('1.listare'); ('5.nodurile care au o anumita informatie
writeln('2.Listare noduri terminale'); cheie');
writeln('3.listare descendentii nodului nod'); ('6.nodurile care au o anumita informatie
cheie si sunt terminale');

19
ex:nr de noduri n=3 begin
radacina=1 if (nivel=nod) then
pt nodul 1 begin
desc stang 2 if datele[i]=pp then
desc drept 3 write(datele[i])
pt nodul 2 end
desc stang 0 else begin
desc drept 0 if (subarborestang[i]<>0)
pt nodul 3 then
desc stang 0 tiparirea_11(subarborestang[i],nivel+1,nod,pp);
desc drept 0} if (subarboredrept[i]<>0)
uses crt; then
type sir=array[1..30] of byte; tiparirea_11(subarboredrept[i],nivel+1,nod,pp);
var st,dr,datele:sir; end ;
rad,n,I,op,p,pp:byte;
nod:integer; end;
nivel:integer; procedure
procedure preordine(I:integer); tiparirea_15(i:integer;nod:integer);
begin begin
write(datele[I]:4);
if (subarborestang[I]<>0) then if ((datele[i]=nod) or
preordine(subarborestang[I]); (subarboredrept[i]=nod))
if (subarboredrept[I]<>0) then then write(i:4)
preordine(subarboredrept[I]); else begin
end; if
(subarborestang[i]<>0) then
procedure tiparirea_16(i:integer; tiparirea_15(subarborestang[i],nod);
nivel:integer;nod:integer); if
begin (subarboredrept[i]<>0) then
if (nivel=nod) then write(datele[i]) tiparirea_15(subarboredrept[i],nod);
else begin end
if (subarborestang[i]<>0) end;
then procedure
tiparirea_16(subarborestang[i],nivel+1,nod); tiparirea_151(i:integer;pp:integer);
if (subarboredrept[i]<>0) begin
then
tiparirea_16(subarboredrept[i],nivel+1,nod); if ((subarborestang[i]=0) or
end ; (subarboredrept[i]=0))
then
end; begin
procedure tiparirea_10(i:integer; if datele[i]=pp then
nivel:integer;nod:integer); write(i:4)
begin end
if (nivel=nod) then write(datele[i]) else begin
else begin if
if (subarborestang[i]<>0) (subarborestang[i]<>0) then
then tiparirea_151(subarborestang[i],nod);
tiparirea_10(subarborestang[i],nivel+1,nod); if
if (subarboredrept[i]<>0) (subarboredrept[i]<>0) then
then tiparirea_151(subarboredrept[i],nod);
tiparirea_10(subarboredrept[i],nivel+1,nod); end
end ; end;
begin
end; write('Dati numarul de noduri lae arborelui
procedure tiparirea_11(i:integer; n='); readln(n);
nivel:integer;nod:integer;pp:integer); write('Dati nodul readcina rad='); readln(rad);

20
for I:=1 to n do (' dati nodul pt care calculezi numarul de
begin descendenti
writeln('pentru nodul ',I,'dati:'); ('numarul nodurilor care au ca descenti pe :
write('descendentul stang:'); nod,
readln(subarborestang[I]); ('numarul nodurilor terminale:
write('descendentul drept:'); ('numarul nodurilor care au doi descendenti
readln(subarboredrept[I]); ('numarul nivele :',n8(rad)); writeln;
write('informatia :'); readln(datele[I]); ('numarul nodurilor care au numai descendent
end; writeln('Operatiile pe care le puteti face sting:
sunt :'); (' dati nivelul pe care vreti sa aflati cite noduri
writeln('1.listare'); se afla
writeln('2.Listare informatii de pe un anumit ex:nr de noduri n=3
nivel'); radacina=1
writeln('3.listare nodurilor de pe un anumit nivel pt nodul 1
si care contin o anumita informatie'); desc stang 2
writeln('4.listare nodurile care au ca tata pe desc drept 3
nodul nod'); pt nodul 2
writeln('5.nodurile care au o anumita informatie desc stang 0
cheie'); desc drept 0
writeln('6.nodurile care au o anumita informatie pt nodul 3
cheie si sunt terminale'); desc stang 0
writeln('dati optiunea dorita:');readln(op); desc drept 0
case op of }
1:begin program parcurgere;
preordine(rad); uses crt;
end; type sir=array[1..30] of byte;
2:begin writeln(' nivelul cautat ');readln(p); var st,dr,datele:sir;
tiparirea_10(rad,1,p); nod,rad,n,I:byte;
end; procedure preordine(I:integer);
3: begin writeln(' nivelul cautat ');readln(p); begin
writeln(' informatia cautat ');readln(pp); write(datele[I]:4);
tiparirea_11(rad,1,p,pp); if (subarborestang[I]<>0) then
end; preordine(subarborestang[I]);
4: begin if (subarboredrept[I]<>0) then
write('Dati cheia nodului '); preordine(subarboredrept[I]);
readln(nod); end;
tiparirea_15(rad,nod); function n1(i:integer):integer;
end; var ns,nd:integer;
5: begin begin
write('Dati cheia nodului '); if subarborestang[i]<>0 then
readln(nod); ns:=n1(subarborestang[i]) else ns:=0;
tiparirea_151(rad,nod); end; if subarboredrept[i]<>0 then
nd:=n1(subarboredrept[i]) else nd:=0;
end; n1:= ns+nd +1;
end;
end. function n11(i:integer):integer;
var ns,nd:integer;
{Prin alocare statica sa se parcurga arborii begin
binari if subarborestang[i]<>0 then
('preordine :'); ns:=n11(subarborestang[i]) else ns:=0;
-sa se determine: if subarboredrept[i]<>0 then
('numarul nodurilor nd:=n11(subarboredrept[i]) else nd:=0;
('numarul nodurilor cu numar de ordine pare : if i mod 2=0 then n11:=1+ns+nd else
('numarul nodurilor care contin informatie n11:=ns+nd;
para end;
function n111(i:integer):integer;

21
var ns,nd:integer; if (subarboredrept[i]<>0) then
begin nd:=n7(subarboredrept[i]) else nd:=0;
if subarborestang[i]<>0 then if (subarborestang[i]<>0) and
ns:=n111(subarborestang[i]) else ns:=0; (subarboredrept[i]=0) then n7:=1 +ns+nd else
if subarboredrept[i]<>0 then n7:=ns+nd;
nd:=n111(subarboredrept[i]) else nd:=0; end;
if datele[i] mod 2=0 then n111:=1+ns+nd else function n8(i:integer):integer;
n111:=ns+nd; var ns,nd:integer;
end; begin
function n4(i:integer;nod:integer):integer; if subarborestang[i]<>0 then
var nr,ns,nd:integer; ns:=n8(subarborestang[i]) else
begin ns:=0;
if i=nod then begin if (subarboredrept[i]<>0) then
nr:=0; nd:=n8(subarboredrept[i]) else nd:=0;
if subarborestang[i]<>0 then nr:=nr+1; if ns>nd then n8:=1+ns else n8:=1+nd;
if subarboredrept[i]<>0 then nr:=nr+1; end;
end function
else nr:=0; n9(i:integer;nivel:integer;p:integer):integer;
if subarborestang[i]<>0 then var ns,nd:integer;
ns:=n4(subarborestang[i],nod) else ns:=0; begin
if subarboredrept[i]<>0 then if (nivel=p) then n9:=1
nd:=n4(subarboredrept[i],nod) else nd:=0; else begin
n4:=nr+ns+nd; if subarborestang[i]<>0 then
end; ns:=n9(subarborestang[i],nivel+1,p) else ns:=0;
function n5(i:integer):integer; if subarboredrept[i]<>0 then
var ns,nd:byte; nd:=n9(subarboredrept[i],nivel+1,p) else
begin nd:=0;
if (subarborestang[i]=0) and n9:=ns+nd;
(subarboredrept[i]=0) then n5:=1 else end;
begin end;
if subarborestang[i]<>0 then
ns:=n5(subarborestang[i]) else ns:=0;
if subarboredrept[i]<>0 then begin clrscr;
nd:=n5(subarboredrept[i]) else nd:=0; write('Dati numarul de noduri lae arborelui
n5:=ns+nd; n='); readln(n);
end; write('Dati nodul readcina rad='); readln(rad);
end; for I:=1 to n do
function n6(i:integer):integer; begin
var ns,nd:integer; writeln('pentru nodul ',I,'dati:');
begin write('descendentul stang:');
if subarborestang[i]<>0 then readln(subarborestang[I]);
ns:=n6(subarborestang[i]) else write('descendentul drept:');
ns:=0; readln(subarboredrept[I]);
if (subarboredrept[i]<>0) then write('informatia :'); readln(datele[I]);
nd:=n6(subarboredrept[i]) else nd:=0; end;
if (subarborestang[i]<>0) and writeln('preordine :'); preordine(rad); writeln;
(subarboredrept[i]<>0) then n6:=1 +ns+nd else writeln('numarul nodurilor :',n1(rad)); writeln;
n6:=ns+nd; writeln('numarul nodurilor cu numar de ordine
end; pare :',n11(rad)); writeln;
function n7(i:integer):integer; writeln('numarul nodurilor care contin
var ns,nd:integer; informatie para :',n111(rad)); writeln;
begin writeln(' dati nodul pt care calculezi numarul de
if subarborestang[i]<>0 then descendenti ');readln(nod);
ns:=n7(subarborestang[i]) else writeln('numarul nodurilor care au ca descenti
ns:=0; pe :',nod,'sunt ',n4(rad,nod)); writeln;

22
writeln('numarul nodurilor terminale:',n5(rad)); if subarboredrept[i]<>0 then
writeln; sd:=s1(subarboredrept[i]) else sd:=0;
writeln('numarul nodurilor care au doi s1:=datele[i]+ss+sd;
descendenti :',n6(rad)); writeln; end;
writeln('numarul nivele :',n8(rad)); writeln; function ss1(i:integer):integer;
writeln('numarul nodurilor care au numai var ss,sd:integer;
descendent sting:',n7(rad)); writeln; begin
writeln(' dati nivelu pe care vreti sa aflati cite if subarborestang[i]<>0 then
noduri se afla ');readln(nod); ss:=ss1(subarborestang[i]) else ss:=1;
writeln(n9(rad,1,nod)); if subarboredrept[i]<>0 then
sd:=ss1(subarboredrept[i]) else sd:=1;
readln; ss1:=datele[i]*ss*sd;
end. end;
function s11(i:integer):integer;
var ss,sd:integer;
{Prin alocare statica sa se parcurga arborii begin
binari si as se determine : if subarborestang[i]<>0 then
(' suma informatiilor nodurilor de pe nivelul ss:=s11(subarborestang[i]) else ss:=0;
',nod,citit de la tastatura if subarboredrept[i]<>0 then
(' suma informatiilor nodurilor' sd:=s11(subarboredrept[i]) else sd:=0;
(' produsul informatiilor nodurilor' if i mod 2=0 then s11:=datele[i]+ss+sd
(' suma informatiilor nodurilor cu ordin par else s11:=ss+sd;
este end;
(' suma informatiilor nodurilor terminale function s111(i:integer):integer;
(' suma informatiilor nodurilor care au un var ss,sd:integer;
singur descendet sting begin
ex:nr de noduri n=3 if subarborestang[i]<>0 then
radacina=1 ss:=s111(subarborestang[i]) else ss:=0;
pt nodul 1 if subarboredrept[i]<>0 then
desc stang 2 sd:=s111(subarboredrept[i]) else sd:=0;
desc drept 3 if (subarborestang[i]=0) and
pt nodul 2 (subarboredrept[i]=0) then s111:=datele[i]
desc stang 0 +ss+sd
desc drept 0 else s111:=ss+sd;
pt nodul 3 end;
desc stang 0 function s1111(i:integer):integer;
desc drept 0 var ss,sd:integer;
} begin
program parcurgere; if subarborestang[i]<>0 then
uses crt; ss:=s1111(subarborestang[i]) else ss:=0;
type sir=array[1..30] of byte; if subarboredrept[i]<>0 then
var st,dr,datele:sir; sd:=s1111(subarboredrept[i]) else sd:=0;
nod,rad,n,I:byte; if (subarborestang[i]<>0) and
procedure preordine(I:integer); (subarboredrept[i]=0) then s1111:=datele[i]
begin +ss+sd
write(datele[I]:4); else s1111:=ss+sd;
if (subarborestang[I]<>0) then end;
preordine(subarborestang[I]); function
if (subarboredrept[I]<>0) then s5(i:integer;p:integer;nivel:integer):integer;
preordine(subarboredrept[I]); var ss,sd:integer;
end; begin
function s1(i:integer):integer; if nivel=p then s5:=datele[i] else begin
var ss,sd:integer; if subarborestang[i]<>0 then
begin ss:=s5(subarborestang[i],p,nivel+1) else ss:=0;
if subarborestang[i]<>0 then if subarboredrept[i]<>0 then
ss:=s1(subarborestang[i]) else ss:=0; sd:=s5(subarboredrept[i],p,nivel+1) else sd:=0;

23
s5:=ss+sd; desc stang 0
end; desc drept 0
end; }
program parcurgere;
begin clrscr; uses crt;
write('Dati numarul de noduri lae arborelui type sir=array[1..30] of byte;
n='); readln(n); var st,dr,datele:sir;
write('Dati nodul readcina rad='); readln(rad); nod,rad,n,I:byte;
for I:=1 to n do procedure preordine(I:integer);
begin begin
writeln('pentru nodul ',I,'dati:'); write(datele[I]:4);
write('descendentul stang:'); if (subarborestang[I]<>0) then
readln(subarborestang[I]); preordine(subarborestang[I]);
write('descendentul drept:'); if (subarboredrept[I]<>0) then
readln(subarboredrept[I]); preordine(subarboredrept[I]);
write('informatica :'); readln(datele[I]); end;
end; function s1(i:integer):boolean;
writeln('preordine :'); preordine(rad); writeln; var ss,sd:boolean;
write('Dati nivelu pe care faci suma='); begin
readln(nod); if subarborestang[i]<>0 then
writeln(' suma informatiilor nodurilor de pe ss:=s1(subarborestang[i]) else ss:=true;
nivelul ',nod,'este ',s5(rad,nod,1)); if subarboredrept[i]<>0 then
writeln(' suma informatiilor nodurilor',s1(rad)); sd:=s1(subarboredrept[i]) else sd:=true;
writeln(' produsul informatiilor if datele[i] mod 2=0 then s1:=ss and sd else
nodurilor',ss1(rad)); s1:=false;
writeln(' suma informatiilor nodurilor cu ordin
par este ',s11(rad)); end;
writeln(' suma informatiilor nodurilor
terminale',s111(rad)); function
writeln(' suma informatiilor nodurilor care au un s5(i:integer;p:integer;nivel:integer):boolean;
singur descendet sting',s1111(rad)); var ss,sd:boolean;
readln; begin
end. if nivel=p then begin
if datele[i] mod 2=0 then s5:=true else s5:=false
end
{Prin alocare statica sa se parcurga arborii else
binari si sa se verifice begin
(' paritatea informatiilor nodurilor de pe nivelul if subarborestang[i]<>0 then
',nod ss:=s5(subarborestang[i],p,nivel+1) else
(' paritatea informatiilor nodurilor' ss:=true;
(' paritatea informatiilor nodurilor terminale if subarboredrept[i]<>0 then
(' paritatea informatiilor la cel putin un nod sd:=s5(subarboredrept[i],p,nivel+1) else
(' daca exista cel putin un nod pe nivelul ',nod, sd:=true;
(' paritatea informatiilor nodurilor terminale la s5:=ss and sd;
cel putin unul end;
(' verifica daca nu contine noduri a carui end;
inormatie e numar par function v3(i:integer):boolean;
ex:nr de noduri n=3 var ss,sd:boolean;
radacina=1 begin
pt nodul 1 if ((subarborestang[i]=0) and
desc stang 2 (subarboredrept[i]=0) )then begin
desc drept 3 if datele[i] mod 2 =0 then v3:=true else
pt nodul 2 v3:=false end
desc stang 0 else
desc drept 0 begin
pt nodul 3

24
if subarborestang[i]<>0 then function v7(i:integer):boolean;
ss:=v3(subarborestang[i]) else ss:=true; var ss,sd:boolean;
if subarboredrept[i]<>0 then begin
sd:=v3(subarboredrept[i]) else sd:=true;
v3:=ss and sd;
end; if subarborestang[i]<>0 then
end; ss:=v7(subarborestang[i]) else ss:=true;
function v4(i:integer):boolean; if subarboredrept[i]<>0 then
var ss,sd:boolean; sd:=v7(subarboredrept[i]) else sd:=true;
begin if not (datele[i] mod 2=0 ) then v7:=ss and sd
else v7:=false;
if datele[i] mod 2 =0 then v4:=true
else end;
begin begin clrscr;
if subarborestang[i]<>0 then write('Dati numarul de noduri lae arborelui
ss:=v4(subarborestang[i]) else ss:=false; n='); readln(n);
if subarboredrept[i]<>0 then write('Dati nodul readcina rad='); readln(rad);
sd:=v4(subarboredrept[i]) else sd:=false; for I:=1 to n do
v4:=ss or sd; begin
end; writeln('pentru nodul ',I,'dati:');
end; write('descendentul stang:');
function readln(subarborestang[I]);
s55(i:integer;p:integer;nivel:integer):boolean; write('descendentul drept:');
var ss,sd:boolean; readln(subarboredrept[I]);
begin write('informatia :'); readln(datele[I]);
if nivel=p then begin end;
if datele[i] mod 2=0 then s55:=true else writeln('preordine :'); preordine(rad); writeln;
s55:=false end write('Dati nivelu pe care faci verificati
else paritatea='); readln(nod);
begin writeln(' paritatea informatiilor nodurilor de pe
if subarborestang[i]<>0 then nivelul ',nod,'este ',s5(rad,nod,1));
ss:=s55(subarborestang[i],p,nivel+1) else writeln(' paritatea informatiilor
ss:=false; nodurilor',s1(rad));
if subarboredrept[i]<>0 then writeln(' paritatea informatiilor nodurilor
sd:=s55(subarboredrept[i],p,nivel+1) else terminale',v3(rad));
sd:=false; writeln(' paritatea informatiilor la cel putin un
s55:=ss or sd; nod',v4(rad));
end; write('Dati nivelu pe care faci verificati dacaare
end; cel putin un nod='); readln(nod);
function v6(i:integer):boolean; writeln(' daca exista cel putin un nod pe nivelul
var ss,sd:boolean; ',nod,'avem',s55(rad,nod,1));
begin writeln(' paritatea informatiilor nodurilor
if ((subarborestang[i]=0) and terminale la cel putin unul',v6(rad));
(subarboredrept[i]=0)) then begin writeln(' verifica daca nu contine noduri a carui
if datele[i] mod 2 =0 then v6:=true else inormatie e numar par',v7(rad));
v6:=false; readln;
end end.
else
begin
if subarborestang[i]<>0 then
ss:=v6(subarborestang[i]) else ss:=false;
if subarboredrept[i]<>0 then
sd:=v6(subarboredrept[i]) else sd:=false;
v6:=ss or sd;
end;
end;

25
repeat
write(a[imax],' ');
imax:=u[imax]
untiL imax=0;
readLn;
end.
PROGRAMARE DINAMICA 
{Prin programare dinamica sa se determine
 intr-un triunghi dreptunghic format din
{Prin programare dinamica sa se determine un numere suma maxima daca se poate parcurge
subsir crescator cu numar de termeni in jos sau spre ipotenuza cu o pozitie
minim si distanta dintre termeni ca pozitie sa fie ex:
maxima n=3
ex:n=5 7
sirul 4 2 7 6 7 38
=>4 7 pt ca distanta dintre ei este maxima fiind 810
primul si ultimul} =>suma maxima e 8 mergind numai in jos}
program secv; program triunghi;
type vector=array[1..20] of integer; const nmax=30;
var a,L,u:vector; type tablou=array[1..nmax,1..nmax] of integer;
n,i,j,ind,max,imax:integer; var a,s:tablou;
procedure citire(var a:vector;var n:integer); i,j,n:integer;
var i:integer; begin
begin write('Numarul de linii al triunghiului:');
write('dati Lungimea siruLui:');readLn(n); readln(n);
for i:=1 to n do write('Dati triunghiul de numere:');
read(a[i]); for i:=1 to n do
end; for j:=1 to i do
read(a[i,j]);
begin {construim tabloul s}
citire(a,n); for j:=1 to n do s[n,j]:=a[n,j];
{construirea vectoriLor 1 si u} for i:=n-1 downto 1 do
L[n]:=1;u[n]:=0; for j:=1 to i do
for i:=n-1 downto 1 do if s[i+1,j]>s[i+1,j+1] then s[i,j]:=a[i,j]
max:=0;imax:=0; +s[i+1,j]
begin else s[i,j]:=a[i,j]+s[i+1,j+1];
for j:=i+1 to n do writeln('Suma maxima care se poate obtine este:
if (L[j]>max) and (a[i]<=a[j]) then ',s[1,1]);
begin readln;
max:=L[j]; end.
imax:=j 
end; {Prin programare dinamica sa se determine
L[i]:=1+max; numarul minim de inmultiri care sa se faca
u[i]:=imax; cind se dau dimensiunile matricelor
end; ex:n=4
{determinarea Lungimii subsiruLui maxim} 10 1 10 1 10}
max:=L[1];imax:=1;
for j:=2 to n do program inm_optima;
if L[j]>max then const nmax=20;
begin type vector=array[1..nmax] of word;
max:=L[j]; tabl=array[1..nmax,1..nmax] of word;
imax:=j var p:vector;
end; m:tabl;n,i,j,k,imin:integer;min,v:word;
writeLn('Lungimea maxima a unui subsir procedure paranteze(i,j:integer);
crescator este',max); var k:integer;
writeLn('un astfeL de subsir este:');

26
begin Sa se transforme in numar minim de operatii
if i<j then begin sirul a a in b
k:=m[j,i]; ex:a=IOANA
if i<>k then begin b=dana}
write('('); program transform;
paranteze(i,k); const nmax=50;
write(')'); type tip_op=(pastrez,modif,sterg,ins);
end elem=record
else paranteze(i,k); nr:integer;
write('x'); op:tip_op
if k+1<>j then begin end;
write('('); tabl=array[0..nmax,0..nmax] of elem;
paranteze(k+1,j); var t:tabl;a,b:string;n,m,i,j:integer;
write(')') procedure calcul(var t:tabl);
end var i,j,aux:integer;aux_op:tip_op;
else paranteze(k+1,j); begin
end for j:=1 to m do t[0,j].nr:=j;
else write('A',i) for i:=0 to n do t[i,0].nr:=i;
end; for i:=1 to n do
begin for j:=1 to m do
write('Nr. de matrici:'); if a[i]=b[j] then begin t[i,j].nr:=t[i-1,j-1].nr;
readln(n); t[i,j].op:=pastrez;
writeln('Dimensiuni matricelor:'); end
for i:=1 to n+1 do read(p[i]); else begin
for i:=n downto 1 do aux:=t[i-1,j].nr+1;aux_op:=sterg;
for j:=i+1 to n do if aux>t[i-1,j-1].nr+1 then begin
begin aux:=t[i-1,j-1].nr+1;
min:=m[i,i]+m[i+1,j] aux_op:=modif
+p[i]*p[i+1]*p[j+1]; end;
imin:=i; if aux>t[i,j-1].nr+1 then begin
for k:=i+1 to j-1 do aux:=t[i,j-i].nr+1;
begin aux_op:=ins
v:=m[i,k]+m[k+1,j]+p[i]*p[k+1]*p[j+1]; end;
if min>v then begin t[i,j].nr:=aux;
min:=v; t[i,j].op:=aux_op;
imin:=k end
end; end;
end; begin
m[i,j]:=min; writeln('Dati sirul a');
m[j,i]:=imin; readln(a);
end; writeln('Dati sirul b');
writeln('Numarul minim de inmultiri readln(b);
este:',m[1,n]); n:=length(a);m:=length(b);
writeln('Aceasta se obtine pentru urmatoarea calcul(t);
ordine a inmultirilor'); writeln('Numarul minim de transformari
paranteze(1,n); este:',t[n,m].nr);
readln; writeln('Un astfel de sir de transformari este
end. urmatorul:');
{{Prin programare dinamica sa se i:=n;j:=m;
determineTRANFORMAREA DIN SIRUL DE while (i>0) and (j>0) do
LITERE A CU dimensiunea n in sirul de litere case t[i,j].op of
b de dimensiune m dupa regulile: pastrez:begin i:=i-1;j:=j-1;end;
s(i) sterge litera de pe pozitia i modif: begin
I(j,c) insereaza litera c pe pozitia j write('M(',i,',',b[j],')=>');
M(i,c) inlocuieste litera de pozitia i cu C a[i]:=b[j];
writeln(a);

27
i:=i-1; sol:array[1..nmax,1..nmax,'a'..'c'] of record
j:=j-1; k:integer;
end; c1,c2:alfabet
sterg:begin end;
write('S(',i,')=>'); procedure det_matr(var a:tablou);
delete(a,i,1); {determina matricea A si matricea sol}
writeln(a); var i,j,k,d:integer;c,c1,c2:alfabet;
i:=i-1 begin
end; for i:=1 to n do
ins:begin for j:=1 to n do
write('I(',i+1,',',b[j],')=>'); for c:='a' to 'c' do
insert(b[j],a,i+1); a[i,j,c]:=false;
writeln(a); for i:=1 to n do a[i,i,x[i]]:=true;
j:=j-1 for d:=1 to n-1 do
end; {pentru fiecare din cele n-1 diagonala este
end; deasupra diagonalei principale determinam
while i>0 do elementele egale cu true}
begin for i:=1 to n-d do
write('S(',i,')=>'); begin
delete(a,i,1); j:=i+d;
writeln(a); for k:=i to j-1 do
i:=i-1 for c1:='a' to 'c' do
end; for c2:='a' to 'c' do
while j>0 do if a[i,k,c1] and a[k+1,j,c2] then
begin begin
write('I(',1,',',b[j],')=>'); a[i,j,t[c1,c2]]:=true;
insert(b[j],a,1); sol[i,j,t[c1,c2]].k:=k;
writeln(a); sol[i,j,t[c1,c2]].c1:=c1;
j:=j-1 sol[i,j,t[c1,c2]].c2:=c2
end end
end. end;
end;
 procedure paranteze(i,j:integer;c:alfabet);
{Prin programare dinamica sa se un algoritm var k:integer;c1,c2:alfabet;
care determina sirulx=(x1,x2,...,xn) begin
astfel cu paranteze si caractre di multimea if i=j then write(x[i])
S=(a,b,c) sa obtinem din x un element y din else begin
S ,unde x si y sunt citite. k:=sol[i,j,c].k;
Se da legea de compozitie: c1:=sol[i,j,c].c1;
c2:=sol[i,j,c].c2;
b|ba if i<>k then begin
--|------
c|ba write('(');
a|cc paranteze(i,k,c1);
care nu e comutativa si nici asociativa write(')');
Obs: (bb)=b end
ex:x=bbba si y=a=>(b(bb))(ba)=(bB) else paranteze(i,k,c1);
(ba)=((Bb)a)=a } if k+1<>j then begin
program inmultiri; write('(');
const nmax=20; paranteze(k+1,j,c2);
t:array['a'..'c','a'..'c'] of 'a'..'c'=(('b','b','a'), write(')')
('c','b','a'),('a','c','c')); end
type tablou=array[1..nmax,1..nmax,'a'..'c'] of else paranteze(k+1,j,c2);
boolean; end
alfabet='a'..'c'; end;
var a:tablou;x:string;y:alfabet;n:integer;

28
begin c[cerc,pct]:=min;
write('Dati sirul x:'); end;
readln(x); if c[n,punctaj]=0 then writeln('imposibil')
n:=length(x); else writeln('numarul minim de
write('Dati caracterul y (a/b/c):'); lovituri este:',c[n,punctaj]);
readln(y); readln;
det_matr(a); end.
if a[1,n,y] then begin
writeln('este posibil');
paranteze(1,n,y)
end
else writeln('Nu este posibil');
readln;
end.
{Prin programare dinamica sa se determine
numarul minim de lovituri de tir CIND
CUNOASTEM NUMARUL DE PUNCTE N
CE TREBUIE SA-L OBTINEM, daca avem
cercuri concentrice si fiecare sector are un
anumit punctaj
Ex:numar de regiuni(sectoare)=3
punctajele v=(1,2,3)
punctajul care trebiue sa-l obtin = 9
=>
3 lovituri in sectorul de 3 puncte trebuie sa
fac}
program tir;
const nmax=10;
kmax=1000;
var v:array[1..nmax] of word;
c:array[1..nmax,0..kmax] of word;
n,lin,j,punctaj,cerc,pct,min:word;
begin
write('dati numarul de regiuni ale tintei');
readln(n);
writeln('dati in ordine crescatoare valorile
atasate acestora');
for cerc:=1 to n do
begin
write('v[',cerc,']=');
read(v[cerc])
end;
write('dati punctajul');
readln(punctaj);
for cerc:=1 to n do c[cerc,0]:=0;
for cerc:=1 to n do
for pct:=1 to punctaj do
begin
min:=0;
for j:=1 to cerc do
for lin:=1 to cerc do
if ((pct-v[j]>0)and(c[lin,pct-v[j]]<>0))
or(pct=v[j]) then
if(min=0) or (min>1+c[lin,pct-v[j]])
then
min:=1+c[lin,pct-v[j]];

29
CAPITOL SPECIAL DE GARFURI

ALGORITMUL BELLMAN-KALABA

Se considera un graf orientat G=(V,E), conex,fara circuite si o functie


de cost a:E->R. Desemnadu-se un varf x, se cere determinarea itinerariulurilor
de cost maxim de la toate celelalte varfuri la el,itinerariuluri care includ un
numar impus de arce(pas).
ETAPA I:
Stabilim varfurile:x0,x1,…xn
Pentru i=1..n-1 , valorile minime dela Xi la Xn sunt:
Vi=min(Vj+c[I,j]),i=1..n-1,j<>I,j=0..n, Vn=0
Care de fapt e un system cu solutiile v[i,0],v[i,1],……..astfel:
-pt k=0 v[i,0]=c[I,n],v[n,0]=0,i=0..n=1
-pt k=1 ,v[i,1]=min{v[j,0]+c[I,j]),v[n,1]=0;i=0..n-1,j=0..n
………..
-pt rangul k avem:
v[i,k]=min{v[j,0]+c[i,j]},j<>i,v[n,1]=0,i=0..n-1,j=0..n
- iteratiile se opresc cand v[i,k]=v[i,k-1],i=0..n-1

EX:6
Nod1 Nod2 Cost
1 2 2
1 3 2
2 3 4
2 4 2
2 5 1
3 5 1
4 6 2
5 4 1
5 6 7
NODUL DE PLECARE pl=1
nodul de final x=6
numarul de muchii prin care se trece 3
=>
1 2 5 6 cu costul maxim de 10}

const v=-1; sir=array[0..200 ] of byte;


sirb=array[0..200] of boolean;
type ma=array[0..200,0..200] of boolean; interval=0..200;

30
mc=array[0..100,0..100] of integer; write('Itinerariul max',pl,'->',x);
var t,c,a:mc; writeln('prin',pasi,'arce');
pasi,pl,k,n,m,x:integer; writeln('costul',c[pl,pasi]);
procedure dateledeintrare; itinerariul(pL,pasi);
var i,j:integer; write(' ',x);
begin end
for i:=0 to n do else writeln('Nu exista');
for j:=0 to n do begin end;
c[i,j]:=-1; t[i,j]:=0; procedure citireagrafului(var a:mc;var
end; n,m:integer;s:string;digraf:boolean;v:inte
for j:=1 to n do begin ger);
c[j,1]:=a[j,x]; var i,j,co:integer;f:text;
if c[j,1]<>-1 then t[j,1]:=j; begin
end; assign(f,s);
end; reset(f);
procedure bellman; readln(f,n);
var i,j:integer; m:=0;
ok:boolean; for i:=1 to n do
begin for j:=1 to n do
k:=2; a[i,j]:=v;
repeat while not seekeof(f) do begin
for i:=1 to n do readln(f,i,j,co);a[i,j]:=co;
for j:=1 to n do inc(m);
if (a[i,j]<>-1) and (c[j,k-1]<>-1) then end;
if c[i,k]<c[j,k-1]+a[i,j] then begin close(f);
c[i,k]:=c[j,k-1]+a[i,j]; end;
t[i,k]:=j; begin
end; citireagrafului(a,n,m,'in15.PAS',TRUE,v
ok:=true; );
for i:=1 to n do writeln(' nodul de plecare(DE REGULA
if c[i,k-1]<>c[i,k] then NODUL 1) pL=');
ok:=false; readln(pl);
inc(k); writeln(' nodul de FINAL PINA UNDE
until ok; VRETI SA CALCULATI
end; ITINERARIULUL MAXIM(DE
procedure itinerariul(x,y:integer); REGULA NODUL ULTIM EX: X=6)
begin X=');
write(' ',x); readln(x);
if y<>1 then itinerariul(t[x,y],y-1); writeln('numarul de muchii care sa le
end; contina itinerariulul maxim Ex:PASI=3
ADICA PASI=?');
procedure afiseazarezultatul; READLN(PASI);
begin dateledeintrare;
if c[pl,pasi]<>-1 then begin bellman;
while c[pl,pasi]=c[pl,pasi-1] do afiseazarezultatul;
dec(pasi); readln;

31
end.

ALGORITMUL LUI YEN

Consider matircea costurilor w asociata unui graf si doresc sa obtin mai scurta distanta de
la un varf la oricare celelalte.
ETAPA 1:
D=W

ETAPA 2:
I=1;h(k)=0,h(j0)=1 pt 1<=j<=n,j<>k

ETAPA III

Calculez min{d[k,j], 1<=j<=n,h(j)=1}


Determin j0 astefel incat h(j0)=1 si d[k,j0]= min{d[k,j], 1<=j<=n,h(j)=1}
B(j0)=d[k,j0],h(j0)=0
D[k,j]=min{d[k,j],d[k,j0],d[j0,j]},pt toti j, 1<=j<=n,
I=i+1

ETAPA IV:
Daca i<n ma intorc la etapa 3 altfel stop.
Exemplu:

1)scriu matircea atasata d;


2)i=1,h=(0,1,1,1,1),b(1)=0;
j0=3,b(3)=2,h=(0,1,0,1,1)

∞ 3 2 6 3
3 ∞ 5 ∞ ∞
D= 2 5 ∞ 4 1
7 ∞ 4 ∞ ∞
4 1 ∞ ∞ ∞

32
i=2
3)i<5 revin la etapa 3
j0=2 b(2)=3,h=(0 0 0 1 1 )

………

{ALGORITMUL LUI YEN:sa se determine cea mai scurta distanta de la un varf la


oricare celelalte.Daca legatura nu exista se pune 10000(a[I,i]=0 si nu se mai cere,pt
i=1...n)
ex:n=5
3274
3 5 10000 1000
2541
7 10000 4 10000
4 10000 1 10000
de la nodul 1
=>distantele minime
1----->2 e 3
.....
}
var d,w:array[1..20,1..20] of real; writeln('varful dorit ');readln(k);
k,i,j,n,L,j0:byte; for i:=1 to n do v[i]:=1;
v,b:array[1..20] of real; v[k]:=0;b[k]:=0;
min:real; for i:=1 to n do
function minim(x,y:real):real; begin
begin min:=10000;
if x<y then minim:=x else minim :=y; for j:=1 to n do
end; if (v[j]=1) and (d[k,j]<min) then begin
begin j0:=j;
writeln('n=');readln(n); min:=d[k,j];
for i:=1 to n do end;
for j:=1 to n do for j:=1 to n do
if i<>j then d[k,j]:=minim(d[k,j],d[k,j0]+d[j0,j]);
begin v[j0]:=0;
write('w[',i,' ',j,']='); b[j0]:=d[k,j0];
readln(w[i,j]); end;
d[i,j]:=w[i,j]; writeln(' distantele minime de la ',k, ' la
end celelalte varfuri ' );
else for i:=1 to n do
begin begin
w[i,j]:=10000; write(k,'------>',i);
d[i,j]:=10000; writeln(':',b[i]);
end; end;

33
readln;
end.

ALGORITMUL LUI LEE

Presupunem ca avem un graf in care toate muchiile au lungimea egala


Numarul nodurilor este de n iar al muchiilor de m si stiind legaturile intre
muchii care exista sa se determine lungimea minima dintre doua noduri
care le citim de la tastatura
ETAPA I:
-pornesc de la nodul final marches toti vecinii lui cu 1
-pentru fiecare din vecini marches cu 2 vecinii acestora nemarcati ( adica se marcheaza
fiecare cu nodul tata)
-la fel procedez pentru toti
ETAPA II
-daca marchez iar nodul x atunci matcajul lui este distanta minima cand ajung la el
ex:n=4
m=4

var n,m,i,j,nc,nd,nd,nuc:integer; gata:boolean;


a:array[1..30,1..30] of 0..1; begin
costurile:array[1..30] of integer; p:=1;u:=1;
procedure costurile[x]:=-
parcurginlatimegraful(x:integer); 1;costuldelaunnodlaaltul:=0;c[p]:=x;
var gata:=false;
p,u,virf,j,costuldelaunnodlaaltul:integer; while (p<=u) and not gata do begin
c:array[1..30] of integer; virf:=c[p];

34
costuldelaunnodlaaltul:=costuldelaunnod write(' dati numarul de muchii
laaltul+1; m=');readln(m);
for j:=1 to n do writeln(' muchiile ');
if (a[virf,j]=1) and (costurile[j]=0) then for i:=1 to m do
begin begin
u:=u+1;c[u]:=j; writeln('primul nod al muchiei
costurile[j]:=costuldelaunnodlaaltul; ',i);readln(nc);
if j=nd then begin writeln('al doile nod al muchiei
gata:=true;writeln('lantul are lungimea ',i);readln(nd);
minima ',costuldelaunnodlaaltul); a[nc,nd]:=1; a[nd,nc]:=1;
end; end;
end; write('nodul de plecare ');readln(nd);
p:=p+1; write('nodul de sosire ');readln(nuc);
end;end; parcurginlatimegraful(nuc);
begin if costurile[nd]=0 then writeln(' nu este
write(' dati numarul de noduri lant intre ',nd, ' si nodul ',nuc);
n=');readln(n); readln;
end.
ALGORITMUL LUI DANTZING

Se da un graf orientate G=(V,M) si o functie c:M x R →R .Se da un nod de plecare .Se


cer drumurile minime de la nodul p la orice nod i.

EXEMPLU:
NR DE NODURI=6
NOD1 NOD2 COST
1 2 2
1 3 2
2 3 4
2 4 2
2 5 1
3 5 1
4 6 2
5 4 1
5 6 7
NODUL DE PLECARE P=1
=>pt nodul 6 costul minim e de 6
ETAPA I:

35
Determin un graf partial al sau care poate cuprinde nodurile 1 si 2 pentru ca costul cel
mai mica care pleaca din 1 este 2 deci avem d[2 ]=cost(1,2)=2 deci marchez faptul ca
am trecut prin nodul 2 .

ETAPA II
O alta posibilitate este arboreal
partial format din nodurile 1 si 3
adica d[3]= 2

ETAPA III

Cum pot sa ajung la nodul 5 ?


La garful partial anterior pot sa mai
adaug un arc care cuprinde
nodurile 3 si 5 sau pot sa mai
adaug un arc care cuprinde
nodurile 2 si 5 adica d[5]=3=
cost(1,2)+cost(2,5)= =1 + 2

36
ETAPA IV

Cum pot sa ajung la nodul 4?

ANALOG D[4]=4=COST(1,2)+COST(2,5)+COST(5,4) =2+1 +1 sau


=cost(1,2)+cost(2,4)=2+2

ETAPA V

Avem adaugarea arcului format din nodurile 4 si 6


Deci d[6]=6=cost(1,2)+cost(2,4)+cost(4,6)
Concluzia:
Traseele
12 4 6
12546
13546
care sunt de cost minim 6.
{(Algoritmul lui Dantzig)
Se considera un graf orientat G=(V,E) si o functie cost:ExR->R.Multimea
E contine n varfuri. Se desemneaza un varf p de plecare. Pentru orice varf
i apartine lui V se cere determinarea tuturor drumurilor de la p la i care
au costul minim.
EX:
6
122
132
234
242
251
351

37
462
541
567
NODUL DE PLECARE P=1
=>pt nodul 6 costul minim e de 6}

const v=10000; begin


for i:=1 to n do
type ma=array[0..200,0..200] of boolean; if (b[st[k-1],i]<v) and not sel[i] then
sir=array[0..200 ] of byte; begin
sirb=array[0..200] of boolean; if i<>sos then begin
interval=0..200; sel[i]:=true;
mc=array[0..100,0..100] of integer; st[k]:=i;
var a,b:mc; drum(k+1);
i,j,n,m,min,k,sos,p:integer; sel[i]:=false;
d,st:sir; sel:sirb;f:text; end
procedure minim; else begin
var i,j:integer; for j:=1 to k-1 do
begin write(st[j],' ');
min:=v; write(sos,' de cost');
for i:=1 to n do writeln(d[sos]);
for j:=1 to n do end;
if(sel[i]) and (not sel[j]) then end;
if (min>d[i]+a[i,j]) then end;
begin procedure load_graf_cost(var a:mc;var
min:=d[i]+a[i,j]; n,m:integer;s:string;digraf:boolean;v:inte
k:=j; ger);
end; var i,j,co:integer;f:text;
end; begin
procedure dantzig; assign(f,s);
var i,j:integer; reset(f);
begin readln(f,n);
for i:=1 to n-1 do begin m:=0;
minim; for i:=1 to n do
if min<>v then begin for j:=1 to n do
sel[k]:=true; a[i,j]:=v;
d[k]:=min; while not seekeof(f) do begin
for j:=1 to n do readln(f,i,j,co);a[i,j]:=co;
if sel[j] and (d[j]+a[j,k]=min) then inc(m);
b[j,k]:=a[j,k]; end;
end; close(f);
end; end;
end;
procedure drum(k:integer); begin
var i,j:integer;

38
load_graf_cost(a,n,m,'in12.pas',FALSE,v for sos:=1 to n do
); if p<>sos then begin
for i:=1 to n do for j:=1 to n do sel[j]:=false;
for j:=1 to n do b[i,j]:=v; sel[p]:=true;
for i:=1 to n do b[i,i]:=0; st[1]:=p;
fillchar(sel,sizeof(sel),FALSE); drum(2);
writeln(' nodul de plecare(DE REGULA end;
SE DA NODUL P=1) p='); readln;
readln(p); sel[p]:=true; d[p]:=0; end.
dantzig;
ALGORITMUL UNGAR

Se considera n muncitori care produc mai multe rebuturi la n masini.Se cere o repartitie
optimala a acestora pe masini.Un graf este bipartite daca multimea varfurilor sale poate fi
partitionata in doua submultimi disjuncte X1 si X2 astfel ca oricare ar fi arcul (ai,aj) ,ai
apartine de X1,aj apartine de X2.
Prin cuplaj intr-o retea bipartite intelegem o multime de arce neadiacente.
Prin support a matricei associate unei retele date intelegem o multime de linii si
coloane care contin toate zerourile matricei.
Se scrie matricea costurilor notata cu A.
Etapa I
-in matricea se scade minorantul fiecarei linii din toate elementele liniei respective.Din
matricea ramasa se face acelasi lucru cu minorantii coloanelor.Notam Ά matricea ce
rezulta.

Etapa I I

-incadram un zerou situate pe linia cu cele mai multe zerouri si baram toate celelalte
zerouri asezate pe linia si coloana zerourului incadrat
-repetam operatia precedenta ,abstractie facand de zerourile incadrate si barate,pan ace
zerourile matricei Ά au fost incadrate sau barate.
Daca la sfarsitul etapei I I am obtinut exact n zerouri incadrate ,algoritmul ia sfarsit,
altfel trec la etapa I I I.

Etapa I I I
-marcam liniile care nu contin zerouri incadrate si coloanele care au zerouri barate
pe linii marcate
- marcarea liniilor cu zero incadrat pe coloane marcate ,……..
Operatia de marcare merge cat se poate.

Etapa I V

In matricea Ά ,taiem toate liniile nemarcate si coloanele marcate.


-scadem minorantul elementelor netaiate din elementele netaiate ,il adunam
elementelor dublu taiate ,lasand neschimbate restul elementelor.

39
Etapa V
In matricea obtinuta repetam operatiile din etapa a I I pana cand se obtine cuplajul
maxim cu cost total minim.
Exemplu:
3 4 5 2 3 1
3 4 6 3 2 3
2 1 3 4 5 6
6 3 5 4 3 5
2 4 2 2 5 3
1 3 4 5 5 3
Se sca elementele minime de pe fiecare linie ca mai sus si avem:

2 3 4 1 2 0
1 2 4 1 0 1
1 0 2 3 4 5
3 0 2 1 0 2
0 2 0 0 3 1
0 2 3 4 4 2

Incadram zerourile care sunt singure pe linia sau coloana lor;


2 3 4 1 2
0
1 2 4 1
0 1
1
0 2 3 4 5
3 0 2 1 0 2
0 2
0
0 3 1

0 2 3 4 4 2

-pe linia si coloana zerourului incadrat baram toate zerourile(zero taiat)


2 3 4 1 2
0
1 2 4 1
0 1
1
0 2 3 4 5
3
02 1
02
0 2
003 1

0 2 3 4 4 2
-liniile sau coloanele care nu contin nici un zerou incadrat:

40
2 3 4 1 2
0
1 2 4 1
0 1
1
0 2 3 4 5
3
02 1
02
0 2
003 1

0 2 3 4 4 2

-marcam coloanele ce contin zero taiat pe linia marcata:

2 3 4 1 2
0
1 2 4 1
0 1
1
0 2 3 4 5
3
0 2 1
0 2

0 2
00 3 1

0 2 3 4 4 2

-marchez liniile care nu contin zerouri barate pe coloane marcate

2 3 4 1 2
0
1 2 4 1
0 1 -u mai avem zero taiat pe liniile 2 si 3 deci se taie liniile
nemarcate si coloanele marcate
1
0 2 3 4 5 2 3 4 1 2 0
-
1 2 4 1
0 1
-
3
0 2 1
0 2
1
0 2 3 4 5
-
-
0 2
00 3 1
-
-
0 2 3 4 4 2 3
0 2 1
0 2
-
-
- 0 2 00 3 1
-
- 0 2 34 4 2
41
- nu mai avem zero taiat pe liniile 2 si 3 deci se taie coloanele marcate

2 3 4 1 2 0 -Observam ca
1 2 4 1
0 1

Este element taiat si pe


1
0 2 3 4 5
linie si pe coloana
deci dublu taiat si ii
3
0 2 1
0 2 adaug elementul
minim (de pe liniile si
0 2 00 3 1 coloanele netaiate) la
linia care contine zero
0 2 34 4 2 dublu taiat iara din
celelalte se scade

1 este elementul minim de pe liniile si coloanele netaiate

-se obtine(taiind zerourile neincadrate:


2 4 4 1 3 0
0 2 3 0
0 0
0 0 2 3 4 5

3
02 0 0 1

0 3
0 0 4 1

42
0 33 4 5 2
Sursa pascal este:
{ APLICATIE LA ALGORITMUL UNGAR:Se citesc n care este numarul de
muncitori si
n masini pecare acestia pot produce rebuturi care sunt citite intr-o matrice
Se cere sa se gaseasca o repartizare optima(a[i,x[i]]=muncitorul i produce x[i]
rebuturi)
a celor n muncitori la cele n masini astfel incit pierderile sa fie minime
ex:n=4
1000
2100
3213
2010

=> costul minim 2


11
24
33
4 2}
type sir =array[1..100] of integer;

var s,x:sir;
min,v,p,i,j,k,n,k1:integer;
as,ev:boolean;
a:array[1..50,1..50] of integer;

procedure succesor(var x:sir;k:integer;var as:boolean);


begin
if (x[k]<n) then begin
as:=true; x[k]:=x[k]+1;

end
else
as:=false;
end;

procedure valid(x:sir;k:integer;var ev:boolean);


var i:integer;
begin
ev:=true;
for i:=1 to k-1 do
if x[k]=x[i] then ev:=false;

end;

43
begin
min:=3200;
write('dati numarul de numere din matrice ');
readln(n);
for i:=1 to n do
for j:=1 to n do

begin
write('numarul ',i,' ,',j,'este ');
readln(a[i,j]);
end;
k:=1;
x[k]:=0;
while(k>0) do
begin
repeat
succesor(x,k,as);
if as then valid(x,k,ev)
until (as and ev) or (not as);
if as then
if (k=n) then
begin
v:=0;
for i:=1 to n do
v:=v+a[i,x[i]];
if v< min then
for i:=1 to n do
begin
s[i]:=x[i];
min:=v;
end;
end

else
begin
k:=k+1;
x[k]:=0;
end
else
k:=k-1 ;
end; v:=0;
for i:=1 to n do
v:=v+a[i,s[i]];

44
write(' costul este ',v);
for i :=1 to n do
begin
write(i:6,s[i]:4);
writeln;
end;
readln;
end.

Iar sursa C++ este:


#include <stdio.h>
#include <stdlib.h>

#define dim 100


#define big 32000
#define true 1
#define false 0

#define fin "t2.in"


#define fout "t2.out"

typedef struct cel


{
unsigned incadrat:1;
unsigned marcat:1;
unsigned taiat:1;
unsigned barat:1;
int u;
} cel;

int a[dim][dim],b[dim][dim];
cel x[dim],y[dim];
int n;

void faza_i()
{
int i,j,min;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (a[i][j]>=0) b[i][j]=a[i][j];
else b[i][j]=big;
for (i=1;i<=n;i++)
{
min=big;
for (j=1;j<=n;j++)
if (b[i][j]<min) min=b[i][j];
for (j=1;j<=n;j++) b[i][j]-=min;
}
for (i=1;i<=n;i++)
{
min=b[1][i];
for (j=1;j<=n;j++)

45
if (b[j][i]<min) min=b[j][i];
for (j=1;j<=n;j++) b[j][i]-=min;
}
}

int count(int k)
{
int i,c=0;
if (y[k].barat) return c;
for (i=1;i<=n;i++)
if ((b[k][i]==0) && (!x[i].barat)) c++;
return c;
}

void reseteaza(cel &c)


{
c.incadrat= false;
c.marcat= false;
c.barat= false;
c.taiat= false;
c.u= 0;
}

void init(cel *v)


{
int i;
for (i=1;i<=n;i++) reseteaza(v[i]);
}

int faza_ii()
{
int nr_zerouri=0,gata,min,tmp,poz,i;
do
{
gata=true;
min=big;poz=0;
for (i=1;i<=n;i++)
if (((tmp=count(1))<min) && (tmp>0))
{
poz=i;
min=tmp;
}
if (poz>0)
{
i=1;
while ((b[poz][i])||(x[i].barat)) i++;
y[poz].incadrat=true;
y[poz].barat=true;
y[poz].u=i;
x[i].incadrat=true;
x[i].barat=true;
x[i].u=poz;
gata=false;
nr_zerouri++;
}
} while (!gata);
if (nr_zerouri==n) return true;

46
else return false;
}

void faza_iii()
{
int gata,i,j;
for (i=1;i<=n;i++)
if (!y[i].incadrat) y[i].marcat=true;
do
{
gata=true;
for (i=1;i<=n;i++)
if (y[i].marcat)
for (j=1;j<=n;j++)
if ((b[i][j]==0) && (!x[j].marcat) && ((i!
=x[j].u)||(j!=y[i].u)))
{
gata=false;
x[j].marcat=true;
x[j].taiat=true;
}
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (x[i].marcat && y[j].incadrat && (!
y[j].marcat))
{
gata=false;
y[j].marcat=true;
}
} while (!gata);
for (i=1;i<=n;i++)
if (!y[i].marcat) y[i].taiat=true;
}

void faza_iv()
{
int i,j,min;
min=big;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if ((!x[j].taiat) && (!y[i].taiat))
if (b[i][j]<min) min=b[i][j];
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (x[j].taiat)
{
if (y[i].taiat)
if (b[i][j]!=big) b[i][j]+=min;
}
else if (!y[i].taiat)
{
if (y[i].taiat)
if (b[i][j]!=big) b[i][j]-=min;
}
}

void main()

47
{
int i,j,v=0;
char s[15], t[15];
FILE *f;

f=fopen(fin,"rt");
if (f==NULL)
{
printf("\nEroare la deschiderea fisierului %s\n",fin);
exit(1);
}
fscanf(f,"%d",&n);
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
fscanf(f,"%d",&a[i][j]);
fclose(f);
faza_i();
init(x);
init(y);/*
while (!faza_ii())
{
faza_iii();
faza_iv();
init(x);
init(y);
}*/
f=fopen(fout,"wt");
if (f==NULL)
{
printf("\nEroare la deschiderea fisierului %s\n",fout);
exit(1);
}
v=0;
for (i=1;i<=n;i++)
v+=a[i][y[i].u];
fprintf(f,"Costul cuplajelor: %d\n",v);
fprintf(f,"Cuplajele:\n");
for (i=1;i<=n;i++)
fprintf(f,"R:%3d A:%3d\n",i,y[i].u);
fclose(f);
}

APLICATII DIVERSE

48
 {Daindu-se un graf prin matricea costurilor am n=3 cu arcele
sa se determine muchia de (1 2 ) costul e de 1
cost minim (1 ,3) costul e de 2
ex:m=2,n=3 L1=2,L2=2
(1,2) costul 1 => doua drumuri posibile }
(1,3) , costul 4 type ma=array[1..23,1..23] of integer;
=>(1,2) e minima} sti=array[1..23] of integer;
TYPE MU=RECORD mul=set of byte;
EST,EDR,CO:INTEGER; var st:sti;a,b:ma;n,L1,L2,p:integer;
END; mn:mul;
GR=ARRAY[1..50] OF MU; procedure c;
VAR V:GR;M,N,MIN:INTEGER; var i,j:integer;
PROCEDURE C; begin
VAR i:INTEGER; writeln(' nr de benzinarii ');readln(n);
BEGIN for i:=1 to n do
WRITELN(' MUCHIILE '); a[i,i]:=0;
FOR I:=1 TO M DO for i:=1 to n do
with v[i] do for j:=1 to n do
begin if i<>j then
write(' muchia ',i,' '); begin
writeln(' nodul de inceput '); write(' costul arcului de la ',i,' la ',j);
readln(est); readln(a[i,j]);
writeln(' nodul de sfirsit '); end;
readln(edr); writeln('L1=');readln(L1);
writeln(' costul ');readln(co); writeln('L2=');readln(L2);
end; end;
end; procedure t(p:integer);
function cc:integer; var i,j:integer;m:mul;
var i,mmin:integer; begin
begin m:=[];
mmin:=v[1].co; for i:=1 to p do
for i:=2 to m do m:=m+[st[i]];
if v[i].co<mmin then mmin:=v[i].co; for i:=1 to n do
cc:=mmin; if i in mn-m then write(i,' ');writeln;
end; writeln(' drumuri ramase ');
procedure aa(min:integer); for i:=1 to n do
var i:integer; for j:=1 to n do
begin if b[i,j]<>0 then write('(',i,', ',j,') ');
writeln(' muchiile de cost minim ',min,' are writeln;
capetele'); writeln('----');
for i:=1 to m do end;
with v[i] do function v(p:integer):boolean;
if co=min then writeln(est,' ',edr); var i,j,x,s:integer;
end; begin
begin b:=a;
writeln(' cite muchii aveti ');readln(m); for i:=1 to p do
writeln('cite noduri ');readln(n); for x:=1 to n do
c;min:=cc; begin
aa(min); b[x,st[i]]:=0;b[st[i],x]:=0;
readln; end;
end. s:=0;
 {Fiind dat un graf orientat .Sa se determine for i:=1 to n do
drumurile pentru care costul total al arcelor for j:=1 to n do
este intre s:=s+b[i,j];
limitele L1 si L2 if (L1<=s) and (s<=L2) then
VARIANTA: avem niste benzinarii care terbuie v:=true
eliminate astfel incit costul else
total al transportului intre ele sa fie in limitele v:=false;
L1 si L2 end;
ex: procedure ba;
var i:integer; 13
begin 23
p:=1; 14
repeat 00
while st[p]<n do =>
begin 32415}
st[p]:=st[p]+1; var a:array[1..100,1..100] of byte;
if v(p) then t(p); pz:array[1..100]of byte;
p:=p+1; n,m,i,j,r1,r2,tr:integer;
st[p]:=st[p-1];end; p:=p-1; found,ok:boolean;
until p=0; f,f2:text;
end; begin
begin assign(f,'in.pas');
c;mn:=[1..n]; reset(f);
ba; assign(f2,'out.pas');
readln; rewrite(f2);
end. readln(f,n,m);
while not((n=0) and (n=0)) do
 {In cladirea cntv exista un panou de plecare, begin
situat in sala de calculatoarelor,din care fillchar(a,sizeof(a),0);
pleaca for i:=1 to n do
N cabluri ce ajung la panoul de pz[i]:=i;
destinatie,situat la secretariat.Cablurile sunt for i:=1 to m do
numerotate in begin
ordine de la 1la N,de la stanga la dreapta, readln(f,r1,r2);
comform punctelor de plecare.Este posibil ca a[r1,r2]:=1;
electricianul Fero sa fi intersectat end;
cablurile.Doua cabluri se pot intersecta cel ok:=true;
mult o data for i:=1 to m do
si doar daca sunt 'vecine'imediat inainte de begin
intersectare(au fost initial vecine,sau au found:=false;
devenit for j:=1 to n-1 do
vecine pe parcurs);de asemenea,se presupune if a[pz[j],pz[j+1]]=1 then
ca nu exista un punct in care sa se intesecteze begin
mai found:=true;
mult de doua cabluri. tr:=pz[j];
Electricianul ION a desenat pe unul dintre pz[j]:=pz[j+1];
peretii salii calculatoarelor un graf pentru a pz[j+1]:=tr;
tine j:=n-1;
minte cum s-au intersectat cablurile undefa pe end;
parcurs. if not found then
Varfurile reprezinta cabluri,iar varfurile a si b begin
sunt legate printr-o muchie daca si numai daca i:=m;
cablurile a si b s-au intersectat undeva pe ok:=false;
parcurs.
Din pacte electicienul Fero a decedat end;
subit.administratia,situata langa sala end;
calculatoarelor,face if ok then
apel la un alt electrician.Este disponibil graful begin
desenat pe perete. Electricianul trebuie sa afle for i:=1 to n do
ordinea in care apar extremitatile cablurilor write(f2,pz[i],' ');
pe panoul de la secretariat,fara a se deplasa writeln(f2);
pana end
acolo. Deoarece electricianul nu cunoaste
teoria grafurilor,vi se cere voua sa furnizati else
aceasta writeln(f2,'imposible');
ordine. readln(f,n,m);
ex: end;
54 close(f2);
12 close(f);
begin
end. repeat
s(x,k,as);
 {Sa se determine toate lanturile if as then v(x,k,ev);
elementare de la nodul NS la nodul ND until (as and ev) or ( not as);
ex:n=3,m=3 if as then begin
(1,2),(2,3),(1,3) if k<=n then
=>1 3 if x[k]=ns then aa(x,k)
1 2 3} else
type sir=array[1..23] of integer; begin
ma=array[1..23,1..23] of integer; k:=k+1;
var e1,e2:integer; x[k]:=0;
i,j,n,m,ns,np,k:integer; end;
a:ma; end
x:sir; else
as,ev:boolean; k:=k-1;
procedure s(var x:sir;k:integer;var end;
as:boolean); readln;
begin end.
if x[k]<n then begin
as:=true;  {Sa se determine toate ciclurile elementare
x[k]:=x[k]+1; de lungime p
end ex:n=4,m=5
else (1,2),(2,3),(1,3),(3,4),(2,4)
as:=false; lungimea ciclurilor p=3
end; =>1 2 3 1
procedure aa(x:sir ;k:integer); ...}
var i:integer; type sir=array[1..23] of integer;
begin ma=array[1..23,1..23] of integer;
for i:=1 to k do var e1,e2:integer;
write(x[i]:4); i,j,n,m,ns,np,k,lu,n1,p:integer;
writeln; a:ma;
end; x:sir;
procedure v(x:sir;k:integer;var ev:boolean); as,ev:boolean;
var i:integer; procedure s(var x:sir;k:integer;var
begin as:boolean);
ev:=true; begin
if (a[x[k-1],x[k]]=0) then ev:=false if x[k]<n then begin
else as:=true;
for i:=1 to k-1 do x[k]:=x[k]+1;
if x[k]=x[i] then ev:=false; end
end; else
begin as:=false;
writeln('n=');readln(n); end;
writeln('m=');readln(m); procedure aa(x:sir ;k:integer);
fillchar(a,sizeof(a),0); var i:integer;
for i:=1 to m do begin
begin for i:=1 to k do
writeln('dati muchia ',i); write(x[i]:4); write(x[1]:4);
writeln(' nodul de inceput ');readln(e1); writeln;
writeln(' nodul de sfirsit ');readln(e2); end;
a[e1,e2]:=1;
a[e2,e1]:=1; procedure v(x:sir;k:integer;var ev:boolean);
end; var i:integer;
writeln(' nodul de unde plec ');readln(np); begin
writeln(' nodul de unde ajung ');readln(ns); ev:=true;
x[1]:=np; if (a[x[k-1],x[k]]=0) then ev:=false
k:=2; else
x[k]:=0; for i:=1 to k-1 do
while k>1 do if x[k]=x[i] then ev:=false;
if (k=p) and (a[x[k],x[1]]=0) then ev:=false; begin
end; vi[pl]:=1;
for j:=1 to n do
if (a[pl,j]=1) and (vi[j]=0) then pp(j);
begin end;
writeln('n=');readln(n);
writeln('m=');readln(m);
fillchar(a,sizeof(a),0); begin
for i:=1 to m do nr:=0;
begin clrscr;
writeln('dati muchia ',i); writeln(' n=');readln(n);
writeln(' nodul de inceput ');readln(e1); writeln(' numarul de muchii ');readln(m);
writeln(' nodul de sfirsit ');readln(e2); for i:=1 to m do
a[e1,e2]:=1; begin
a[e2,e1]:=1; writeln('dati muchia ',i);
end; writeln( 'primul nod al muchiei ');readln(x);
writeln(' dati lungimea ciclurilor ');readln(p); writeln(' nodul de sfirsit al muchiei
x[1]:=1; ');readln(y);
k:=2; a[x,y]:=1;a[y,x]:=1;
x[k]:=1; end;
while k>0 do for i:=1 to n do
begin vi[i]:=0;
repeat for i:=1 to n do
s(x,k,as); if vi[i]=0 then begin nr:=nr+1;
if as then v(x,k,ev); pp(i);
until (as and ev) or ( not as); end;
if as then begin writeln(' avem atitea componente conexe',nr);
if k=p then if nr=1 then writeln(' avem o singura pestera
aa(x,k) ');
else readln;
begin end.
k:=k+1;  {Parcurgerea in adincime la grafuri
x[k]:=1; neorientate
end; Sa se determine cite componente conexe avem
end si care sunt acestea
else ex:n=5
k:=k-1; m=3
end; si anume (1 5), ( 2 3),(2 4)
readln; =>
end. avem doua componente conexe
15
 {Parcurgerea in adincime la grafuri 234
neorientate }
Sa se determine cite componente conexe avem uses crt;
VARIANTA:Avem niste culoare subterane.Sa var a:array[1..20,1..20] of integer;
se determine daca apartin unei singure vi:array[1..20] of integer;
pesteri sa la mai multe aceste culoare nr,i,n,el,j,p,u,pl,m,x,y:integer;
ex:n=5
m=3 procedure pp(pl:integer);
si anume (1 5), ( 2 3),(2 4) var j:integer;
=> begin
avem doua componente conexe vi[pl]:=1; write(pl:4);
nu avem o singura pestera } for j:=1 to n do
uses crt; if (a[pl,j]=1) and (vi[j]=0) then pp(j);
var a:array[1..20,1..20] of integer; end;
vi:array[1..20] of integer;
nr,i,n,el,j,p,u,pl,m,x,y:integer;
begin
procedure pp(pl:integer); nr:=0;
var j:integer; clrscr;
writeln(' n=');readln(n); writeln(' nodul de sfirsit al muchiei
writeln(' numarul de muchii ');readln(m); ');readln(y);
for i:=1 to m do a[x,y]:=1;a[y,x]:=1;
begin end;
writeln('dati muchia ',i); for i:=1 to n do
writeln( 'primul nod al muchiei ');readln(x); vi[i]:=0;
writeln(' nodul de sfirsit al muchiei for i:=1 to n do
');readln(y); if vi[i]=0 then begin
a[x,y]:=1;a[y,x]:=1; pp(i);
end; for i1:=1 to n-1 do
for i:=1 to n do for i2:=i1+1 to n do
vi[i]:=0; if (vi[i1]=1) and (vi[i2]=1) then write('{',i1,',
for i:=1 to n do ',i2,'}');
if vi[i]=0 then begin nr:=nr+1; for i1:=1 to n do
writeln(' componenta conexa cu numarul ',nr, if vi[i1]=1 then vi[i1]:=vi[i1]+1;
'are nodurile urmatoare '); writeln;
pp(i); writeln;end;
writeln; readln;
writeln; end.
end;  {Parcurgerea in adincime la grafuri
write(' avem atitea componente conexe',nr); neorientate
readln; Sa se determine componenta conexa care
end. contine cele mai multe noduri si care
 {Parcurgerea in adincime la grafuri sunt acestea
neorientate ex:
Sa se determine perechile de noduri intre care n=5
avem cel putin un lant m=3
ex:n=5 si anume (1 5), ( 2 3),(2 4)
m=3 =>2 3 4}
si anume (1 5), ( 2 3),(2 4) uses crt;
=> var a:array[1..20,1..20] of integer;
avem 1 5 vi,co:array[1..20] of integer;
23 ma,nr,i,n,el,j,p,u,pl,m,x,y:integer;
34 va:array[1..20] of set of 1..88;
24 procedure pp(pl:integer;nr:integer);
} var j:integer;
uses crt; begin
var a:array[1..20,1..20] of integer; vi[pl]:=nr;
vi:array[1..20] of integer; for j:=1 to n do
i1,i2,nr,i,n,el,j,p,u,pl,m,x,y:integer; if (a[pl,j]=1) and (vi[j]=0) then pp(j,nr);
end;
procedure pp(pl:integer);
var j:integer;
begin begin
vi[pl]:=1; nr:=0; ma:=0;
for j:=1 to n do clrscr;
if (a[pl,j]=1) and (vi[j]=0) then pp(j); writeln(' n=');readln(n);
end; writeln(' numarul de muchii ');readln(m);
for i:=1 to m do
begin
begin writeln('dati muchia ',i);
nr:=0; writeln( 'primul nod al muchiei ');readln(x);
clrscr; writeln(' nodul de sfirsit al muchiei
writeln(' n=');readln(n); ');readln(y);
writeln(' numarul de muchii ');readln(m); a[x,y]:=1;a[y,x]:=1;
for i:=1 to m do end;
begin for i:=1 to n do
writeln('dati muchia ',i); vi[i]:=0;
writeln( 'primul nod al muchiei ');readln(x); for i:=1 to n do
if vi[i]=0 then begin nr:=nr+1;
pp(i,nr); procedure aa(a:ma;n:integer);
co[nr]:=0; var i,j:integer;
va[nr]:=[]; begin
for j:=1 to n do writeln(' matricea de adiacenta');
if vi[j]=nr then for i:=1 to n do begin
begin for j:=1 to n do
co[nr]:=co[nr]+1; write(a[i,j]:6);
va[nr]:=va[nr]+[j]; writeln;
end; end;
if co[nr]>ma then
ma:=co[nr]; end;
end; begin
for i:=1 to nr do writeln(' cite muchii aveti ');readln(m);
if co[i]=ma then writeln('cite noduri ');readln(n);
begin c;
write('-------------------------'); writeln(' costul mediu ',cc:3:1);
for j:=1 to 88 do writeln(' matricea de adiacenta ');
if (j in va[i]) then write(j:6); aa(a,n);
writeln; readln;
end; end.
readln;  {Fie un graf neorientat cu n virfuri si m
end. muchii.Se dau c culori sa se coloreze
 {Daindu-se un graf prin matricea costurilor muchiile lui astfel ca doua incidente sa nua
sa se determine matricea de adiacenta aiba aceiasi culoare
si costul mediu al muchiilor ex:n=3 m=3 c=3}
ex:m=2,n=3 type mu=record
(1,2) costul 1 n1,n2:integer;
(1,3) , costul 2 end;
=>costul mediu 1.5} gr=array[1..30] of mu;
TYPE MU=RECORD sti=array[1..20] of integer;
EST,EDR,CO:INTEGER; var v:gr;c,n,m,nrsol:integer;st:sti;
END; procedure cc;
GR=ARRAY[1..50] OF var i:integer;
MU;ma=array[1..50,1..50] of 0..1; begin
VAR V:GR;M,N,MIN:INTEGER;a: ma; for i:=1 to m do
PROCEDURE C; begin
VAR i:INTEGER; writeln(' muchia ',i);
BEGIN writeln('nodul de inceput ');
WRITELN(' MUCHIILE '); readln(v[i].n1);
FOR I:=1 TO M DO writeln('nodul de sfirsit ');
with v[i] do readln(v[i].n2);
begin end;
write(' muchia ',i,' '); end;
writeln(' nodul de inceput '); procedure t;
readln(est); var i:integer;
writeln(' nodul de sfirsit '); begin
readln(edr); nrsol:=nrsol+1;
writeln(' costul ');readln(co); writeln(' solutia ',nrsol);
a[est,edr]:=1; for i:=1 to m do
a[edr,est]:=1; writeln('muchia ',v[i].n1,' ,',v[i].n2,' are
end; culoarea ',st[i]);
end; end;
function cc:real; function va(p:integer):boolean;
var i,s:integer; var L:integer;
begin begin
s:=0; va:=true;
for i:=1 to m do for L:=1 to p-1 do
s:=s+v[i].co; if ((v[L].n1=v[p].n1) or (v[L].n2=v[p].n1) or
cc:=s/m; (v[L].n1=v[p].n2) or (v[L].n2=v[p].n2))
end; and(st[p]=st[L]) then
va:=false; for j:=1 to n do
end; write(a[i,j],' ');
procedure ba(p:integer); writeln;
var i:integer; end;
begin writeln;
for i:=1 to c do end;
begin function v:boolean;
st[p]:=i; var o:boolean;
if va(p) then p,i,j:integer;
if p=m then begin
t o:=true;
else if n1<>n then o:=false;
ba(p+1); if o then
end; begin
end; for i:=1 to n-1 do
begin for j:=i+1 to n do
writeln('n=');readln(n); if (gp[i,j]=1) and (a[i,j]=0) then
writeln('m=');readln(m); o:=false;
writeln(' numarul de culori ');readln(c); end;
cc; v:=o;
nrsol:=0; end;
ba(1); begin
if nrsol=0 then writeln(' nu avem destule culori writeln(' n=');readln(n);
') writeln(' m=');readln(m);
else c(a,n,m);
writeln('sunt ',nrsol,'solutii '); writeln(' cite noduri vreti sa aiba graful
readln; partial ');readln(n1);
end. writeln(' cite muchii vreti sa aiba graful
 {Dindu-se cite noduri are un graf G si un partial ');readln(m1);
altul Gp. c(gp,n1,m1);
Sa se determine daca al doilea este graf partial aa(a,n,m);
al primului aa(gp,n,m);
ex:n=3 m=3 if v then writeln('gp e graf partial al lui g')
(1,2),(1,3),(2,3) else
n1=3 m1=2 writeln('gp NU e graf partial al lui g') ;
(1,2),(1,3) readln;
=> al doilea e graf partial al primului} end.
type ma=array[1..23,1..23] of integer;
var a,gp:ma;n,m,n1,m1:integer;  {Sa se determine daca un graf este subgraf al
procedure c(var a:ma;n,m:integer); altuia
var i,j,x,y:integer; ex:n=3 m=3
begin (1,2),(1,3),(2,3)
for i:=1 to n do n1=2
for j:=1 to n do a[i,j]:=0; m1=1
for i:=1 to m do (1,2)
begin => al doile este subgraf al primului}
writeln(' muchia cu numarul ',i); type mu=record
repeat x,y:integer;
writeln(' x=');readln(x); end;
writeln(' y=');readln(y); ve=array[1..23] of mu;
until(x>=1) and( x<=n) and(y>=1) and ma=array[1..23,1..23] of 0..1;
(y<=n); var a:ma;v:ve;n,nm:integer;m,mgr:set of byte;
a[x,y]:=1;a[y,x]:=1; procedure c;
end; var i,j:integer;
end; begin
procedure aa(a:ma;n,m:integer); write('n=');readln(n);
var i,j:integer; for i:=1 to n-1 do
begin for j:=i+1 to n do
for i:=1 to n do begin
begin repeat
writeln('avem muchie intre ',i,' si ',j,'dati zero ex:n=3 m=3
daca nue sau unu daca e'); (1,2),(1,3),(2,3),(2,4)
readln(a[i,j]); p=2
until(a[i,j]=0) or (a[i,j]=1); m1=1
a[j,i]:=a[i,j]; (1,3)
end; => al doile este subgraf al primului}
end; type mu=record
procedure cc; x,y:integer;
var x,i:integer; end;
begin mul=set of byte;
m:=[]; ve=array[1..23] of mu;
writeln(' dati modurile subgrafului '); ma=array[1..23,1..23] of 0..1;
while not seekeoln do var a,b:mul;n,m,p:integer;v:ve;
begin procedure c;
read(x);m:=m+[x]; var i,j:integer;
end; begin
writeln(' dati numarul de muchii ale write('n=');readln(n);
subgrafuluim');readln(nm); write('m=');readln(m);
for i:=1 to nm do
begin for i:=1 to m do
write(' muchia ',i);readln(v[i].x,v[i].y);
end; begin
end; writeln(' muchia ',i);
function g(i,j:integer):boolean; writeln('nodul de inceput ');readln(v[i].x);
var k:integer; writeln('nodul de sfirsit ');readln(v[i].y);
begin end;
if a[i,j]=0 then g:=true else end;
g:=false; procedure su;
for k:=1 to nm do var x,i,j,nr:integer;
if ((v[k].x=i) and (v[k].y=j)) or (( v[k].x=j) begin
and (v[k].y=i)) then g:=true; a:=[v[1].x,v[1].y];b:=[];
end; for i:=2 to m do
function vv:boolean; b:=b+[i];
var s:boolean;i,j:integer; nr:=2;
begin repeat
mgr:=[]; i:=1;
for i:=1 to n do while (i<=n) and (nr<p) do
mgr:=mgr+[i]; begin
if m<=mgr then s:=true else if i in a then
s:=false; begin
for i:=1 to nm do j:=1;
if a[v[i].x,v[i].y]<>1 then s:=false; while (j<=m) and (nr<p) do
if s then begin
for i:=1 to n-1 do if j in b then
for j:=i+1 to n do if v[j].x=i then
if (i in m) and (j in m) then begin
if not g(i,j) then s:=false; a:=a+[v[j].y];nr:=nr+1;b:=b-[j];
vv:=s; end
end; else
begin if v[j].y=i then
c;cc; begin
if vv then writeln('gp e SUBgraf al lui g') a:=a+[v[j].x];nr:=nr+1;b:=b-[j];
else end;
writeln('gp NU e SUBgraf al lui g') ; j:=j+1;
readln; end;end;
end. i:=i+1;
end;
until nr=p;
 {Sa se determine un subgraf( are p virfuri) al for i:=1 to m do
altuia cu n virfuri si m muchii if i in b then
if (v[i].x in a ) and (v[i].y in a) then b:=b-[i]; procedure b(p:integer);
end; var i:integer;
procedure t; begin
var i:integer; for i:=1 to n do
begin if (a[st[p-1],i]=1) and ( not (ge[i]) )then
write(' virfurile subgrafului '); begin
for i:=1 to n do st[p]:=i;
if i in a then write (i,' '); ge[i]:=true;
writeln; if i=v2 then
writeln(' muchilee subgrafului '); begin
for i:=1 to m do g:=true;
if not (i in b) then write('(',v[i].x,', ',v[i].y,')'); t(p);
end; end
begin else
c;write('p=');readln(p); b(P+1);
su; ge[i]:=false;
t; end;
readln; end;
end. begin
c;
fillchar(ge,sizeof(ge),false);
 {Sa se determine toate lanturile elementare write(' v1');readln(v1);
dintr-un graf write(' v2=');readln(v2);
ex:n=3 m=3 g:=false;
(1,2),(1,3),(2,3) st[1]:=v1;
v1=1 ge[v1]:=true;
v2=3 b(2);
=>1 2 3 if not g then writeln(' nu este nici un lant intre
1 3} ',v1,' si ',v2);
type readln;
ve=array[1..23] of boolean; end.
ma=array[1..23,1..23] of 0..1;
var a:ma;ge:ve;n,v1,v2:integer;g:boolean;  {Sa se determine o modalitate de ajungere
st:array[1..23] of integer; din localitatea 1 la localitatea n
procedure c; daca se elimina niste noduri in care se ajunge
var i,j:integer; in mod direct printr-0 muchie
begin
write('n=');readln(n); ex:n=5
for i:=1 to n do a[i,i]:=0; (1,2),(1,3),(2,3),(3,4),(2,4),(4,5),(3,5)
=>
for i:=1 to n-1 do 1245
for j:=i+1 to n do (1,2),(2,4),(4,5)}
begin type
repeat mu=set of 1..100;
writeln('avem muchie intre ',i,' si ',j,'dati zero
daca nue sau unu daca e'); ma=array[1..23,1..23] of 0..1;
readln(a[i,j]); var a:ma;m:mu;n,i,j,n1:integer;
until(a[i,j]=0) or (a[i,j]=1); d:array[1..100] of integer;g:boolean;
a[j,i]:=a[i,j];a[j,i]:=a[i,j]; procedure c;
end; var i,j:integer;
begin
end; write('n=');readln(n);
procedure t(p:integer); for i:=1 to n do d[i]:=0;
var k:integer; for i:=1 to n do a[i,i]:=0;
begin for i:=1 to n-1 do
for k:=1 to p do for j:=i+1 to n do
write(st[k]:4,' '); begin
writeln; repeat
end; writeln('avem muchie intre ',i,' si ',j,'dati zero
daca nue sau unu daca e');
readln(a[i,j]); procedure c;
until(a[i,j]=0) or (a[i,j]=1); var i,j:integer;
a[j,i]:=a[i,j];a[j,i]:=a[i,j]; begin
if a[i,j]=1 then begin write('n=');readln(n);
d[i]:=d[i]+1;d[j]:=d[j]+1; write('ARCE m=');readln(m);
end;
end; for i:=1 to m do

end; begin
begin writeln(' ARCUL ',i);
writeln('nodul de inceput ');readln(v[i].x);
c; writeln('nodul de sfirsit ');readln(v[i].y);
m:=[]; writeln(' costul
for i:=1 to n do ');readln(v[i].co);cc:=cc+v[i].co ;
m:=m+[i]; end;
n1:=n; end;
repeat procedure con;
g:=false; var i,j:integer;
i:=1; begin
while(i<=n) and (not g) do for i:=1 to n do
if d[i]=n1-1 then g:=true for j:=1 to n do
else a[i,j]:=0;
i:=i+1; for i:=1 to m do
if g then a[v[i].x,v[i].y]:=1;
begin end;
m:=m-[i]; procedure aa;
for j:=1 to n do var i,j:integer;
if j<>i then d[j]:=d[j]-1; begin
for j:=1 to n do for i:=1 to n do
begin begin
a[j,i]:=0;a[i,j]:=0; for j:=1 to n do
end; write(a[i,j]:4);
n1:=n1-1; writeln;
end; end;
until not g; end;
writeln(' varfurile '); begin
for i:=1 to n do c;con;aa;writeln(' costul mediu ',cc/m:5:2);
if i in m then write(i, ' '); readln;
writeln; end.
writeln(' muchiile ');  {Sa se determine gradul unui nod(cite muchii
for i:=1 to n-1 do se leaga de el)
for j:=i+1 to n do si daca un graf este ciclu hamiltonian
if a[i,j]=1 then ex:n=5
write('[',i,',',j,'],'); (1,3),(1,5),(3,5),(4,5),(2,4),(2,3)
writeln(#8#8,' '); => este
readln; }
end. type mat=array[1..20,1..20] of integer;
var a:mat;
 {Sa se determine costul mediu al arcelor i,j,n:integer; o,o1,o2:boolean;
unui graf orientat function g(i:integer):integer;
(1,2) cu costul de 1, var s,j:integer;
(1,3) cu costul de 1, begin
=>1.50 costul mediu} s:=0;
type mu=record for j:=1 to n do
x,y,co:integer; s:=s+a[i,j];
end; g:=s;
mul=set of byte; end;
ve=array[1..23] of mu;
ma=array[1..23,1..23] of 0..1; begin
var n,m,p:integer;v:ve; a:ma;cc:real; writeln(' dati numarul de noduri:'); readln(n);
for i:=1 to n-1 do cost:real;
for j:=i+1 to n do end;
begin vector=array[1..100] of pereche;
writeln('a[',i,',',j,']=');readln(a[i,j]); var
a[j,i]:=a[i,j]; a:vector;n:byte;f1,f:text;m:word;cost_tot:real;
end; procedure citeste;
o1:=(n>=3);o2:=true; begin
for i:=1 to n do assign(f,'In4.pas');reset(f);
if not(g(i)>=n div 2 ) then o2:=false; readln(f,n);
o:=o1 and o2; m:=0;
if o then writeln(' este ciclu eulerian ') while not eof (f) do
else begin
writeln(' nu e ciclu hamiltonian '); m:=m+1;
with a[m] do readln(f,calc1,calc2,cost);
readln; end;
end. close(f);
 { Sa se realizeze o retea de n calculatoare de end;
cost minim astfel incat oricare doua procedure sort;
calculatoare sa poata comunica intre ele. Se var i:word;inv:boolean;temp:pereche;
cunoaste costul legarii in retea a oricaror begin
doua repeat
calculatoare. inv:=false;
Datele de intrare se citese din fisierul for i:=1 to m-1 do
Input.dat si contin: if a[i].cost>a[i+1].cost then
- pe prima linie valoarea lui n (nr. de
calculatoare, n<=100) begin
- pe urmatoarele linii se afla perechi de doua temp:=a[i];a[i]:=a[i+1];a[i+1]:=temp;
numere naturale nenule reprezentand doua inv:=true;
calculatoare si o valoare reala -reprezentand end;
costul legarii celor doua calculatoare. Valorile until not inv;
numerice vor fi separate de unul sau mai multe end;
spatii. procedure APM;
Datele de iesire vor fi scrise in fisierul var conex:array[1..100] of byte;
Output.dat si va consta in scrierea pe linii i,k,r:word;
succesive begin
a cate assign(f1,'out.pas');rewrite(f1);
unei perechi de valori numerice for i:=1 to n do conex[i]:=i;
corespunzatoare calculatoarelor care vor fi k:=1;r:=0;
conectate astfel ca in while r<n-1 do
final costul total al conexiunii sa fie minim. begin
if conex[a[k].calc1]<conex[a[k].calc2]
Rezolvare then
Problema se reduce la determinarea arborelui begin
partial de cost minim prin algoritmul for i:=1 to n do
Kruskal. if(conex[a[k].calc2]=conex[i])and
a- este un tablou al muchiilor (i<>a[k].calc2)then
n- numarul de varfuri ale grafului(numarul conex[i]:=conex[a[k].calc1];
de calculatoare) . conex[a[k].calc2]:=conex[a[k].calc1];
m- numarul de muchii r:=r+1;
ex:4 cost_tot:=cost_tot+a[k].cost;
4 57 5 writeln(f1,a[k].calc1:4,a[k].calc2:4,a[k].cost:
7 8 11 7:1);
376
=> 4 7 5
376 end
7 8 11} else
if conex[a[k].calc1]>conex[a[k].calc2]
program Kruskal; then
type pereche=record begin
calc1,calc2:byte; for i:=1 to n do
if close(f);
(conex[a[k].calc1]=conex[i])and(i<>a[k].cal end;
c1)then procedure process;
conex[i]:=conex[a[k].calc2]; var i,j,init:integer;
conex[a[k].calc1]:=conex[a[k].calc2]; begin
r:=r+1; i:=nmax;
cost_tot:=cost_tot+a[k].cost; while (i>0) and (g[t]=0) do
writeln(f,a[k].calc1:4,a[k].calc2:4,a[k].cost:7: begin
1); for j:=(t-i) downto i do
end; if (g[j]<>0) and (g[i+j]=0) then
k:=k+1; begin
end; g[i+j]:=i;
writeln(f1,cost_tot:9:2); if g[j]=-1 then inc(d[i+j]);
close(f1); d[i+j]:=d[j]+1;
end; end;
begin g[i]:=-1;
citeste; inc(d[i]);
sort; dec(a[i]);
APM ; while a[i]=0 do dec(i);
readln; end;
end. end;
procedure afis;
 {Pescarul ION a prins n pesti. La mal a var p:integer;
determinat greutatea ti pentru fiecare begin
peste. Naum lucreaza pentru o companie de while t<>0 do
pescuit si are dreptul sa ia acasa if g[t]<>-1 then
cel mult t grame de peste. El doreste sa ia un begin
numar cat mai mic de pesti, dar writeln(f,g[t]);
care sa aiba o greutate maxima posibilal(<=t). t:=t-g[t];
Decideti ce pesti trebuie sa ia acasa end
pescarul.} else
const begin
nma=35000; writeln(f,t);
fo='out.pas'; t:=0;
type end
domeniu=0..nma; end;
pesti=array [domeniu] of byte; begin
var {fi:=paramstr(1);}
a:pesti; read_d;
fi:string; process;
g,d:array [0..7000] of integer; assign(f,fo);
t,nmax:integer; rewrite(f);
f:text; while (g[t]=0) and (t>0) do dec(t);
procedure read_d; writeln(f,d[t]);
var f:text; afis;
n,x,i:longint; close(f);
begin end.
assign(f,'in1.pas');  {Fiind dat un graf orientat si legaturile intre
reset(f); arce sa se determine componentele tare conexe
read(f,n,t); (adica intre orice doua noduri exista drum de
for i:=1 to n do la unul la altul si invers
begin si nu mai este un alt subgraf care sa aiba
readln(f); aceasta proprietate)
read(f,x); OBS:se stie ca prin drum se intelege o
if x<=t then succesiune de noduri dar intre
begin oricare doua noduri consecutive din
inc(a[x]); succesiune avem un arc )
if x>nmax then nmax:=x; ex:n=6
end; arcurile (1,2),(2,3),(3,2),(1,4),(4,6),(6,5),(5,4)
end; =>1 2 3
4 5 6} sosesc(i);
type multime=set of byte; pleaca(i);
var matriceadeadiacenta:array[1..20,1..20] of c[i]:=(s[i]*p[i])+[i];
integer; L:=L+c[i];
s,p,c:array[1..20] of multime; L:multime; writeln(' componenta conexa ',nc, ' este ');
n,nc:integer; for k:=1 to n do
procedure citire; if k in c[i] then write(k:3);
var i,j:integer; writeln;
begin end;end;
write('cite virfuri are graful ');readln(n); begin
for i:=1 to n citire;
do for j:=1 to n do afisare;
begin drum;
writeln('matriceadeadiacenta[',i,',',j,']='); afisare;
readln(matriceadeadiacenta[i,j]); tareconexe;
end; readln;
end; end.
procedure afisare;
var i,j:integer;
begin
writeln(' matricea este '); writeln;  {La un turneu de fotbal participa echipele
for i:=1 to n do x1,..xn.Daca rezultatele nu pot fi egale
for j:=1 to n do sa se determine clasamentul final
write(matriceadeadiacenta[i,j]:6); writeln; ex:n=3
end; 011
procedure drum; 100
var i,j,k:integer; 100
begin => clasamentul echipelor 1
for k:=1 to n do 3
for i:=1 to n do 2
for j:=1 to n do Varianta:graf orientat unde
if matriceadeadiacenta[i,j]=0 then nodurile sunt echipe
matriceadeadiacenta[i,j]:=matriceadeadiacent iar rezultatele sunt arce}
a[i,k]*matriceadeadiacenta[k,j]; type ma=array[1..23,1..23] of 0..1;
end; ve=array[1..23] of integer;
procedure sosesc(i:integer); var i,j,m,n:integer;a:ma;x:ve;
var k:integer; procedure ss(var x:ve;i,j:integer);
begin var au:integer;
s[i]:=[]; begin
for k:=1 to n do if a[x[i],x[j]]<>1 then
if matriceadeadiacenta[i,k]=1 then s[i]:=s[i] begin
+[k]; au:=x[i];x[i]:=x[j];x[j]:=au;
end; end;
procedure pleaca(i:integer); end;
var k:integer; procedure ii(var x:ve;m,p,q:integer);
begin var b:ve;i,j,k:integer;
p[i]:=[]; begin
for k:=1 to n do i:=p;
if matriceadeadiacenta[k,i]=1 then j:=m+1;
p[i]:=p[i]+[k]; k:=1;
end; while (i<=m) and (j<=q) do
procedure tareconexe; if a[x[i],x[j]]=1 then begin
var i,k:integer; b[k]:=x[i];i:=i+1;k:=k+1;
begin end
L:=[]; else begin
nc:=0; b[k]:=x[j];j:=j+1;k:=k+1;
for i:=1 to n do end;
if not (i in L) then if i<=m then
begin for j:=i to m do
nc:=nc+1; begin
b[k]:=x[j];k:=k+1; for i:=1 to n do
end begin
else for j:=1 to n do begin
for i:=j to q do writeln('a[',i,',',j,']');
begin read(a[i,j]);end;
b[k]:=x[i];k:=k+1; readln;
end; end;
k:=1;
for i:=p to q do end;
begin procedure aa;
x[i]:=b[k];k:=k+1; var i,j:integer;
end; begin
end; for i:=1 to n do
procedure dd(var x:ve;p,q:integer); begin
var m,i,j:integer; for j:=1 to n do
begin write(a[i,j]:5);
if abs(p-q)<=1 then ss(x,p,q) else writeln;
begin end;
m:=(p+q) div 2;dd(x,p,m);dd(x,m+1,q); writeln;
ii(x,m,p,q); end;
end;end;
procedure cc; procedure dd;
begin var j,nn:integer;
write(' n=');readln(n); begin
fillchar(a,sizeof(a),0); nn:=1;
for i:=1 to n-1 do for j:=1 to n do
for j:=i+1 to n do if a[x,j]=1 then n:=nn+1;
begin writeln(' ies atitea noduri adica ',nn);
write(i,' a invins sau nu pe ',j,'dati zero daca end;
nu si 1 daca da '); procedure ddd;
readln(a[i,j]); var i,nnn:integer;
a[j,i]:=1-a[i,j]; begin
end; nnn:=0;
end; for i:=1 to n do
begin if a[i,x]=1 then nnn:=nnn+1;
cc; writeln(' intra atitea noduri adica ',nnn);
for i:=1 to n do x[i]:=i; end;
dd(x,1,n); begin
for i:=1 to n do fillchar(a,sizeof(a),0);
write(x[i]:5); cc;aa;write('x=');readln(x);
readln;
end. aa;dd;ddd;readln;
end.
 {Sa se determine cite arcuri intra sau ies  {Sa se determine toate circuitele elementare
dintr-un nod x si neelementare(drumuri
ex: elementare care sunt si cicluri ) intr-un graf
n=3 orientat
011 ex:n=5
100 m=3
100 (1,2),(2,3),(3,4),(3,5),(4,1)
x=3 =>1 2 3 4 1
=>1 ....
1} }
type ma=array[1..23,1..23] of integer; var a:array[1..20,1..20] of integer;
var a:ma;x,n:integer;f:text; ns,n,m,nr:integer;
procedure cc; st:array[1..23] of integer;
var i,j:integer; procedure cc;
begin var x,y,i,j:integer;
writeln('n='); begin
readln(n); writeln('n=');readln(n);
writeln('m=');readln(m); begin
for i:=1 to n do ns:=ns+1;
for j:=1 to n do tt(p);
a[i,j]:=0; end;
for i:=1 to m do bb(p+1);
begin end;
writeln(' arcul ',i); end;
repeat end;
writeln(' dati nodul de inceput ');readln(x); begin
writeln(' dati nodul de sfirsit ');readln(y); cc;aa;ii;bb(1);
until (x>=1) and (x<=n) and (y>=1) and writeln(' sunt atitea solutii ',ns);
(y<=n); readln;
a[x,y]:=1; end.
end;  {Sa se determine daca graful orientat este sau
end; nu conex
procedure aa; ex:n=4
var i,j:integer; m=2
begin muchiile (1,2),(3,4)
for i:=1 to n do => nu e conex}
begin type ma=array[1..23,1..23] of 0..1;
for j:=1 to n do var a:ma;n,m:integer;co:boolean;
write(a[i,j]:5);writeln; procedure cc;
end; var i,j,x,y:integer;
writeln; begin
end; writeln('n=');readln(n);
procedure ii; for i:=1 to n do
var i:integer; for j:=1 to n do
begin a[i,j]:=0;
for i:=1 to 30 do writeln('m=');readln(m);
st[i]:=0; for i:=1 to m do
end; begin
procedure tt(p:integer); write(' arcul ',i);
var i:integer; writeln(' dati varful de inceput ');readln(x);
begin writeln(' dati varful de sfirsit ');readln(y);
for i:=1 to p do write(st[i]:5); a[x,y]:=1;
writeln; end;
end; end;
function vv(p:integer):boolean; procedure bb(x:integer);
var o:boolean;i,j:integer; var i,j,v,p,ps,pi:integer;viz,c:array[1..20] of
begin integer;
o:=true; begin
if p>1 then for i:=1 to n do
if a[st[p-1],st[p]]=0 then viz[i]:=0;
o:=false; c[1]:=x;ps:=1;pi:=1;
if o then while ps<=pi do
for i:=2 to p-2 do begin
for j:=i+1 to p-1 do v:=c[ps];
if ((st[i]=st[j]) and (st[i+1]=st[j+1])) or for i:=1 to n do
((st[i]=st[j+1]) and (st[i+1]=st[j])) then if (a[v,i]=1) and (viz[i]=0) then begin
o:=false; pi:=pi+1;c[pi]:=i;viz[i]:=1;
vv:=o; end;
end; for j:=1 to n do
procedure bb(p:integer); if (a[j,v]=1) and (viz[j]=0) then begin
var va:integer; pi:=pi+1;c[pi]:=j;viz[j]:=1;
begin end;
for va:=1 to n do ps:=ps+1;
begin end;
st[p]:=va;if vv(p) then co:=true;
begin p:=1;
if (p>=3) and (st[p]=st[1]) then for i:=1 to pi do
p:=p*viz[i]; function compar_drumuri
if p=0 then co:=false; (dr:matrice; n:integer) : boolean ;
end; var i,j : integer;
begin begin
cc;bb(1); compar_drumuri:=true;
if co then writeln(' graful e conex ') for i:=1 to n do
else for j:=1 to n do
writeln(' nu e conex '); if d[i,j]<>dr[i,j] then
readln; compar_drumuri:=false;
end. end;
 {Se da un graf orientat cu n noduri prin procedure drumuri;
matricea sa de adiacenta. Se var i,j,k,i1,j1,nm:integer;
cere sa se construiasca un alt graf avand dr,s:matrice ;
aceleasi varfuri, aceeasi matrice begin
a drumurilor si avand un numar minim de nm:=0; dr:=a; s:=a;
muchii for i1:=1 to n do
ex:4 for j1:=1 to n do
0010 if s[i1,j1]<>0 then begin
0000 s[i1,j1]:=0;
0101 dr:=s;
1100 for k:=1 to n do
=> elimin un arc de coord (3,2)} for i:=1 to n do
if i<>k then
type matrice=array[1..20,1..20] for j:=1 to n do
of byte; if (j<>k) and (dr[i,j]=0) then
var a,d:matrice; dr[i,j]:=dr[i,k]*dr[k,j];
n:integer; if compar_drumuri(dr,n) then
f:text; inc(nm)
else s[i1,j1]:=1; end;
procedure citeste ; writeln('matricea de adiacenta a nodului
var i,j: integer ; graf:');
begin scrie(s,n);
assign(f,'in2.pas');reset(f); writeln('am eliminat ',nm ,' arce');
read(f,n); end;
for i:=1 to n do
for j:=1 to n do begin
read(f,a[i,j]); citeste;
close(f); writeln('matricea initiala de adiacenta:');
end; scrie(a,n);
procedure scrie(t:matrice; drum;
n:integer); drumuri;
var i,j: integer; end.
begin
for i:=1 to n do begin  {Se citeste un arbore cu nodurile 1,2,3...,n,
writeln ; care este memorat
for j:=1 to n do sub forma listelor de adiacenta . Se citeste, de
write(t[i,j],' '); end; asemenea un nod, care va fi
end; considerat radacina. Se cere sa se afiseze
procedure drum; vectorul tata prin care se poate
var i,j,k:integer; memora arborele.
begin ex:n=8
d:=a; 30
for k:=1 to n do 350
for i:=1 to n do 1240
if i<>k then 30
for j:=1 to n do 2670
if (j<>k)and(d[i,j]=0)then 50
d[i,j]:=d[i,k]*d[k,j]; 580
scrie(d,n); 70
end; nodul radacina 3
=>3 3 0 3 2 5 5 7} s1:=s1+[y];
TYPE LISTA=^NOD; while stop<>0 do begin
NOD=RECORD s2:=[];
INFO:INTEGER; for j:=1 to n do
URM:LISTA; if j in s1 then begin
end; p:=v[j];
vector=array[1..20] of lista; while p<>nil do begin
multime=set of byte; if viz[p^.info]=0 then begin
var v:vector; viz[p^.info]:=1;
n,niv,stop,y,j:integer; tata[p^.info]:=j;
viz:array[1..20] of byte; dec(stop);
s1,s2:multime; s2:=s2+[p^.info] end;
p:lista; tata:array[1..20] of byte; p:=p^.urm; end;
procedure pune(var end;
prim,ultim:lista;x:integer); s1:=s2; inc(niv);
var q:lista; end;
begin write('vector tata: ');
new(q); for j:=1 to n do
q^.info:=x; write(tata[j],' ');
if prim=nil then begin END.
prim:= q;ultim:=q;
ultim^.urm:=nil;  {in mod evident, numarul de niveluri ale unui
end arbore depinde de radacina
else begin aleasa. Fiind dat un arbore cu nodurile 1,2,...,n,
ultim^.urm:=q; care este numarul
q^.urm:=nil; minim de niveluri si pentru ce radacina?
ultim:=q; ex:n=8
end; 30
end; 350
procedure citire(var n:integer); 1240
var i,x:integer; 30
u:lista; 2670
begin 50
write('n='); 580
readln(n); 70
for i:=1 to n do begin => nivel minim 4 pt nodul 2}
new(v[i]);
new(u);
v[i]:=nil;
write('lista nodului ',i,'este ');
read(x);
while x<>0 do begin TYPE LISTA=^NOD;
pune(v[i],u,x);read(x); end; NOD=RECORD
end; INFO:INTEGER;
end; URM:LISTA;
end;
vector=array[1..20] of lista;
multime=set of byte;
var v:vector;
n,niv,stop,y,j:integer;
viz:array[1..20] of byte;
BEGIN s1,s2:multime; min,vf_min:integer;
citire(n); p:lista; tata:array[1..20] of byte;
s1:=[ ]; procedure pune(var
write('dati nodul de plecare:'); prim,ultim:lista;x:integer);
read(y); var q:lista;
fillchar(viz,20,0); viz[y]:=1; begin
tata[y]:=0; new(q);
q^.info:=x;
stop:=n; dec(stop); if prim=nil then begin
prim:= q;ultim:=q; end;
ultim^.urm:=nil; writeln('nivel minim ',min,' pentru varful
end ', vf_min);
else begin END.
ultim^.urm:=q;
q^.urm:=nil;  {Se citeste un arbore care este introdus prin
ultim:=q; legatura de tip tata
end; Nodurile sale sunt 1,2...n. Se cere sa se afiseze
end; nodurile sale terminale
procedure citire(var n:integer); ex:n=8
var i,x:integer; 33032557
u:lista; =>1 4 6 8 }
begin var tata, v:array[1..20] of byte;
write('n='); n,i:byte;
readln(n); begin
for i:=1 to n do begin write('n='); read(n);
new(v[i]); for i:=1 to n do begin
new(u); write('tata[',i,'] = ');
v[i]:=nil; read(tata[i]); end;
write('lista nodului ',i,'este '); fillchar(v,20,0);
read(x); for i:=1 to n do
while x<>0 do begin if tata[i]=0 then v[i]:=1
pune(v[i],u,x);read(x); end; else v[tata[i]]:=1;
end; write('nodurile terminale: ');
end; for i:=1 to n do
if v[i]=0 then write(i,' ');
writeln;
BEGIN end.
citire(n);
min:=n; vf_min:=1;  {Fie vectorul a cu n elemente numere intregi.
for y:=1 to n do begin Se considera un fisier text
niv:=1; cu numele 'IN2.pas' care contine pe prima linie
s1:=[ ]; numarul n de elemente din
fillchar(viz,20,0); viz[y]:=1; vectorul a, iar pe-a doua linie cele n elemente
{cond de oprire, parcurgerea din vectorul a. Se cere sa
tuturor nodurilor } se construiasca fisierul 'OUT.TXT'care sa
stop:=n; dec(stop); contina : pe primele n linii cele
s1:=s1+[y]; n elemente ale vectorului a, afisate dupa
while stop<>0 do begin transformarea in baza numerica 2
s2:=[]; si k pe urmatoarele linii sa se afiseze grupele
for j:=1 to n do de numere care verifica
if j in s1 then begin urmatoarea proprietate: orice doua numere ale
p:=v[j]; aceleiasi grupe, au in
while p<>nil do begin reprezentare binara un numar egal de cifre de
if viz[p^.info]=0 then begin 1.}
viz[p^.info]:=1; program transformare_binara;
dec(stop); type vector=array[1..100] of integer;
s2:=s2+[p^.info]; end; var a,b,c:vector;
p:=p^.urm; f:text;
end; i,j,n,k,l,p:integer;
end; BEGIN
s1:=s2; inc(niv); assign(f,'IN2.pas');
end; reset(f);
if min>niv then readln(f,n);
begin for i:=1 to n do read(f,a[i]);
min:=niv; close(f);
assign(f,'OUT.pas');
rewrite(f);
vf_min:=y; for i:=1 to n do begin
end; p:=a[i];
c[i]:=0; VAR M,N,I,J,K,NR,CoM,co:INTEGER;
k:=0; V:VE;ST:VE1;A,MAT,SOL:MA;
repeat procedure cc;
K:=k+1; var f:text;
b[k]:=p mod 2; begin
c[i]:=c[i]+p mod 2; assign(f,'in1.pas');reset(f);readln(f,n,m);
p:=p div 2; fillchar(a,sizeof(a),0);
until p=0; for i:=1 to m do
for j:=k downto 1 do write(f,b[j]); begin
writeln(f); readln(f,v[i].x,v[i].y,v[i].c);
end; a[v[i].x,v[i].y]:=1; a[v[i].y,v[i].x]:=1;
l:=1; end;
while l=1 do begin close(f);
l:=0; end;
for i:=1 to n-1 do function conex(a:ma):boolean;
if c[i]<c[i+1] then begin var i,j,k:integer;
p:=c[i]; begin
c[i]:=c[i+1]; for k:=1 to n do
c[i+1]:=p; for i:=1 to n do
p:=a[i]; for j:=1 to n do
a[i]:=a[i+1]; if a[i,j]=0 then a[i,j]:=a[i,k]*a[k,j];
a[i+1]:=p; conex:=true;
l:=1; for i:=2 to n do
end; if a[1,i]=0 then conex:=false;
end; end;
write(f,a[1]); procedure verific;
for i:=1 to n-1 do var i,n1:integer;nod:ve1;
if c[i]=c[i+1] then write(f,' ',a[i+1]) begin
else begin writeln(f); fillchar(mat,sizeof(a),0);fillchar(nod,sizeof(no
write(f,a[i+1]); d),0);
end; for i:=1 to n-1 do
close(f); begin
END. mat[ v[st[i]].x,v[st[i]].y]:=1;
mat[v[st[i]].y,v[st[i]].x]:=1;
 {Sa se genereze toti arborii partiali de cost nod[v[st[i]].x]:=1; nod[v[st[i]].y]:=1;
minim cunoscind numarul de noduri end;
, numarul de muchii si costul pentru fiecare n1:=0;
muchie for i:=1 to n do
ex: if nod[i]=1 then n1:=n1+1;
58 if (n1=n) and (conex(mat)) then begin
1 2 13 co:=0;
1 3 12 for i:=1 to n-1 do
1 4 13 co:=co +v[st[i]].c;
1 5 15 if co<com then begin
2 3 16 nr:=1;
2 4 13 com:=co;
3 4 17 for i:=1 to n-1 do
3 5 13 sol[nr,i]:=st[i];
=>un arbore de cost minim are muchiile end
12 else
13 if co=com then
14 begin nr:=nr+1;
3 5} for i:=1 to n-1 do sol[nr,i]:=st[i];
TYPE MA=ARRAY[1..20,1..20] OF end;
INTEGER; end;
MU=RECORD end;
X,Y,C:INTEGER; procedure back(k:integer);
END; var i:integer;
VE=ARRAY[1..20] OF MU; begin
VE1=ARRAY[1..20] OF INTEGER; if k=n then
verific type ve=array[1..30] of integer;
else var a:array[1..30,1..30] of integer;
if k=1 then n,co:integer;s,t,c:ve;
for i:=1 to m do procedure cc;
begin var i,j:integer;
st[k]:=i;back(k+1); begin
end writeln(' numarul total de localitati + resedinta
else de judet ');readln(n);
for i:=st[k-1]+1 to m do for i:=1 to n do
begin a[i,i]:=0;
st[k]:=i;back(k+1); for i:=1 to n do
end; for j:=i+1 to n do
end; begin
procedure tipar; writeln(' costul a[',i,' ,',j,']=');readln(a[i,j]);
begin a[j,i]:=a[i,j];
for i:=1 to nr do end;
begin end;
for j:=1 to n-1 do procedure aa;
write('[',v[sol[i,j]].x,',',v[sol[i,j]].y,']'); var i:integer;
writeln; begin
end; for i:=2 to n do
end; writeln(t[i]:5,i:6);
begin co:=0;
cc; for i:=1 to n do
if not conex(a) then writeln(' nu e conex ') else co:=co+c[i];
begin end;
com:=maxint; back(1); procedure ff;
tipar; var k,i,j,st,n1,n2,com:integer;
end; begin
readln; for i:=1 to n do
end. begin
s[i]:=0;t[i]:=0;c[i]:=0;
end;
 {APLICATIE LA ARBORE PARTIAL DE st:=1;s[st]:=1;
COST MINIM KRUSKAL:Fie niste localitati for k:=1 to n-1 do
care dorim sa aiba legatura printr-un drum begin
de resedinta de judet cunoscind distantele com:=maxint;n1:=-1;n2:=-1;
dintre ele.Ce muchii trebuie construite for i:=1 to n do
ca drumul de la resedinta la oricare localitate for j:=1 to n do
sa fie minim si cit e if (s[i]=1) and (s[j]=0) then
ex:n=5 if a[i,j]<com then begin
nod nod cost com:=a[i,j];n1:=i;n2:=j;
1 2 13 end;
1 3 12 s[n2]:=1;t[n2]:=n1;c[n2]:=a[n1,n2];
1 4 13 end;
1 5 15 end;
2 3 16 begin cc;ff;aa;
2 4 13 writeln(' costul minim e ',co );
2 5 300 readln;
3 4 17 end.
3 5 13  {Sa se determine toate subgrafurile
4 5 300 unui graf care sa ramina conex
daca se elimina un nod
=> costul minim este 51 ex: n=4
cu muchiile m=4
12 muchiile 1 2
13 13
14 23
35 34
} =>subgraful
23
3 4}
VAR A:ARRAY[1..10,1..10] OF 0..1;
pu:ARRAY[1..10] OF boolean;
nr, N,M,I,J,K,V1,V2,t,np:INTEGER;
Y:SET OF 2..10;EX:BOOLEAN;
pROCEDURE II;
BEGIN
WRITE('N=');READLN(N);
FOR I:=1 TO N DO
FOR J:=1 TO N DO
A[I,J]:=0;
WRITELN('M=');READLN(M);
WRITELN(' DATI muchiile ');
for i:=1 to m do
begin
writeln('muchia ',i);
writeln(' dati nodul de inceput
');readln(V1);
writeln(' dati nodul de sfirsit
');readln(V2);
a[V1,V2]:=1;a[V2,V1]:=1;

end;

end;

begin
ii;
k:=1;np:=1;
pu[k]:=true;
k:=1;
repeat
k:=k+1;
until (k>n) or ((a[np,k]=1) and not
pu[k]) ;

writeln(' un posibil nod este ',np);

readln;
end.

You might also like