/*
  Name: ER
  Copyright: GPL 
  Author: wUBo
  Date: 28-07-06 13:16
  Description: ER
*/

#include "d:\scilab-4.0\routines\stack-c.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "rand.hpp"

void er __PARAMS((int *a, int n, int m, double *b, int l,int *c,int *d));


int *head;
int *tail;
int t;

int inter(fname) 
char *fname;
{
    static int l1, m1, n1, l2, m2, n2, l3,l4;
    static int minlhs=0, maxlhs=2, minrhs=2, maxrhs=2;   //why the minlhs must equal zero??
    static int tmp1;
    static double tmp2;
    int i,j;
    double p;
    double temp;
    t=0;


    CheckRhs(minrhs,maxrhs) ;
    CheckLhs(minlhs,maxlhs) ;
    GetRhsVar(1, "i", &m1, &m1, &l1);  
    GetRhsVar(2, "d", &m1, &m1, &l2);   

    tmp1=*istk(l1);
    tmp2=*stk(l2);
    m2=(int)(tmp1*tmp2)/2;  //Calculate the edge number;

    head=(int *) malloc((m2+5000) * sizeof(int));
    tail=(int *) malloc((m2+5000) * sizeof(int));

    for (i=0;i<m2+100;i++) {
        head[i]=0;
        tail[i]=0;
    }  

    p=(double)tmp2/(double)(tmp1-1);
    for (i=0;i<tmp1-1;i++) {
        if (i%1000==0)
            sciprint("%i\n",i);
        for (j=i+1;j<tmp1;j++) {

            temp=rand_double();
            if (temp<=p) {
                head[t]=i+1;
                tail[t]=j+1;
                t++;
            }

        }

    }   

    m2=t;


    CreateVar(3, "i", &m1, &m2, &l3);  
    CreateVar(4, "i", &m1, &m2, &l4);
    er(istk(l1), m1, m1, stk(l2),m1,istk(l3),istk(l4));
    /*  Return C (3)  */
    LhsVar(1) = 3;
    LhsVar(2)=  4;
    return 0;
}







void er(a,n,m,b,l,c,d)
int a[];
double b[];
int c[],d[];
int n,m,l;

{


    int i;
    for (i=0;i<t;i++) {
        c[i]=head[i];
        d[i]=tail[i];
    }
    free(head);
    free(tail);



}       


