Declaratieve Talen/oplossingGraafafstand
een oplossing
%korste afstand tss 2 grafen min_distance(G1,G2,R) :- findall(D,distance(G1,G2,D),Ds), min_element(Ds,R). %korste element uit rij min_element([X],R) :- R = X. min_element([L|List],Result) :- min_element(List,Rrecur), (Rrecur < L -> Result = Rrecur; Result = L). %afstand graaf G1 tot G2 distance(G1,G2,R) :- knopen(G1,V1), knopen(G2,V2temp), permu(V2temp,V2), distance(G1,G2,V1,V2,R). %afstand graaf G1 tot graaf G2 gegeven functie f:V1 -> V2 distance(_,_,[],[],R) :- R is 0. distance(G1,G2,[V1|V1rest],[V2|V2rest],R) :- graad(G1,V1,Graad1), graad(G2,V2,Graad2), (Graad1 > Graad2 -> Verschil is Graad1 - Graad2; Verschil is Graad2 - Graad1), distance(G1,G2,V1rest,V2rest,Rrecursief), R is Verschil + Rrecursief. %graad van knoop V in graaf G graad(G,V,R) :- findall(_,(graaf(G,V,_);graaf(G,_,V)),List), length(List,R). %alle knopen van graaf 'Index' knopen(Index, Result) :- findall(X,(graaf(Index,X,_);graaf(Index,_,X)),Knopen), list_to_set(Knopen,Result). %permutaties permu([],[]). permu(X,[Y|Ys]) :- del(Y,X,Rs), permu(Rs,Ys). %deletes X del(X,[X|Xs],Xs). del(X,[Y|Ys],[Y|Zs]) :- del(X,Ys,Zs).
feedback welkom --Beau 14 jun 2006 18:07 (CEST)