#include "d:\scilab-4.0\routines\stack-c.h"
void assc __PARAMS((double *a, int m, int n, double *b,int *num,double *d));

int inassc(fname) 
char *fname;
{
    static int l1, m1, n1, l2, m2, n2, l3,l4,l5;
    static int minlhs=0, maxlhs=1, minrhs=3, maxrhs=3;   //why the minlhs must equal zero??
    static int tmp1,tmp2,tmp3;
    static int m3,n3,m4,n4;

    CheckRhs(minrhs,maxrhs) ;
    CheckLhs(minlhs,maxlhs) ;


    GetRhsVar(1, "d", &m1, &n1, &l1);   //the following sentence must be wrote together!!!! 
    GetRhsVar(2, "d", &m1, &n1, &l2);
    GetRhsVar(3, "i", &m1, &m1, &l3);

    CreateVar(4, "d", &m1, &m1, &l4);
    assc(stk(l1), m1, n1, stk(l2),istk(l3),stk(l4));
    LhsVar(1) = 4;
    return 0;
}

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


void assc(a,m,n,b,num,d)  //n: edge number *c: the node number
double a[],b[];
int num[];
double d[];
int m,n;
{
    //c is the add edge every time;
    int i;
    int *k;
    double s0,s1,s2;
    int head,tail;
//   int M=(*c);  //the edge number
    int M=n;
    k=(int *)malloc((*num+1)*sizeof(int));


    s0=s1=s2=0;

    for (i=0;i<=*(num);i++)
        k[i]=0;   //Initial the degree distribution

    for (i=0;i<n;i++) { //Calculate the degree
        head=(int)a[i];
        tail=(int)b[i];
        k[head]++;  
        k[tail]++;
    }

    for (i=0;i<n;i++) {

        head=(int)a[i];
        tail=(int)b[i];
        s0+=k[head]*k[tail]; //jk
        s1+=k[head]+k[tail];  //j+k
        s2+=k[head]*k[head]+k[tail]*k[tail]; //j^2+k^2
//	   sciprint("%e %e %e\n",s0,s1,s2);
    }
//   sciprint("%e %e %e\n",s0,s1,s2);
    d[0]=((double)(s0/M)-(double)(1.0/2/M*s1)*(double)(1.0/2/M*s1))/((double)s2/2/M-(double)(s1/2.0/M)*(double)(s1/2.0/M));

}
