function a=rm(n,p)
  v=rand(1,n*(n-1)/2); a=zeros(n,n); k=1
  for i=1:n
   for j=i+1:n
     if v(k)<p then a(i,j)=1; else a(i,j)=0; end
     k=k+1; a(j,i)=a(i,j)
   end
  end
endfunction

function d=deg(a)
  n=size(a,"r")
  for i=1:n d(i)=sum(a(i,:)); end
endfunction

function c=cluster(a)
  n=size(a,"r"); v=[]
  for i=1:n
   m=1  
   for j=1:n 
    if a(i,j)==1 v(m)=j; m=m+1; end
   end
   b=a(v,v); k=m-1
   if k>1 then c(i)=sum(b)/k/(k-1); else c(i)=0; end
   v=[]
  end
endfunction

function [l, nk]=dist(a,lmax)
  n=size(a,"r"); l=zeros(1,n*(n-1)/2); ak=a
  k=1; flag=%t; nk=[]
  while flag&(k<=lmax)
    m=0; s=0;
    for i=1:n
      for j=i+1:n
        s=s+1;
        if (ak(i,j)>0)&(l(s)==0) then l(s)=k; m=m+1; end
      end
    end
    if m>0 then nk(k)= m; ak=ak*a; k=k+1; else flag=%f; end
  end
endfunction

stacksize(10000000)
ini=["01000";"0.0100"];    // valori assegnati per default
[ok,n,p]=getvalue("Dare i valori dei parametri",..
  ["n";"p"],list("col",1,"col",1),ini);

a=rm(n,p);
mean_deg=sum(a)/n;
mprintf(' grado medio = %f\n', mean_deg)
cluster_coeff=sum(cluster(a))/n;
mprintf(' coeff. di clustering = %f\n', cluster_coeff)
[l nk]=dist(a,20);
mean_dist=sum(l)/sum(nk);
mprintf(' distanza media = %f\n', mean_dist)
conn_coeff=2*sum(nk)/n/(n-1);
mprintf(' perc. di coppie connesse = %f\n', conn_coeff)
