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

int intcluster(fname) 
char *fname;
{
    static int l1, m1, n1, l2,m2,l3;
    static int minlhs=0, maxlhs=2, minrhs=1, maxrhs=1;   //why the minlhs must equal zero??

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

    GetRhsVar(1, "i", &m1, &n1, &l1);   //the following sentence must be wrote together!!!! 
    m2=1;
    CreateVar(2, "d", &m2, &m2, &l2);
    CreateVar(3, "d", &m2, &m1, &l3);
    cluster(istk(l1), m1, n1, stk(l2),stk(l3));
    /*  Return C (3)  */
    LhsVar(1) = 2;
    LhsVar(2)= 3;
    return 0;
}

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


int **g;  // Graph
int N;


int test(int i,int j,int z)
{
    int k;
    int xx;
    int yy;
    int x=g[z][i];
    int y=g[z][j];

    if (g[x][0]>=g[y][0]) {
        xx=x;
        yy=y;
    } else {
        xx=y;
        yy=x;
    }
    for (k=1;k<=g[yy][0];k++)
        if (g[yy][k]==xx)
            return 1;
    return 0;
}






void cluster(a,m,n,b,c)
int a[];
double c[];
double b[];
int m,n;
{

    int i,j,z,k;
    int e;
    N=m;

    /////////////////////////////////////////////////
    g=malloc(N*sizeof(int *));
    for (i=0;i<N;i++)
        g[i]=malloc(1000*sizeof(int));   //generate g[N][1000]
    ////////////////////////////////////////////////////

    for (i=0;i<N;i++) {
        for (j=0;j<=a[i];j++) {
            g[i][j]=a[i+j*N];

        }

    }
    b[0]=0;
    for (z=0;z<N;z++) {
        k=g[z][0];
        e=0;
        for (i=1;i<g[z][0];i++)
            for (j=i+1;j<=g[z][0];j++)
                if (test(i,j,z))
                    e++;
                //   sciprint("%i %i\n",e,k);
        if (k>1)
            c[z]=2.0*e/k/(k-1);
        else
            c[z]=0;
        //    sciprint("%e \n",c[z]);

        b[0]+=c[z];

    }

    b[0]=b[0]/N;







}