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

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

int intws(fname) 
char *fname;
{
    static int l1, m1, n1, l2, m2, n2, l3,l4,l5;
    static int minlhs=0, maxlhs=2, minrhs=3, maxrhs=3;   //why the minlhs must equal zero??
    static int tmp1,tmp2;
    /* Check number of inputs (rhs=2) and outputs (lhs=1) */
    CheckRhs(minrhs,maxrhs) ;
    CheckLhs(minlhs,maxlhs) ;

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



int N;
int K;
double p;
int **g;




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


void ws(a,n,m,b,pb,c,d)
int a[];
int b[];
int c[],d[];
int n,m;
double pb[];

{

    int i,j,kk,pass;
    int select;
    N=*(a);
    K=*(b);
    p=*(pb);
    srand((unsigned) time(NULL));

    g=malloc(N*sizeof(int *));
    for (i=0;i<N;i++)
        g[i]=malloc((K/2)*sizeof(int));   //generate node[N][2000]

    for (i=0;i<N;i++)
        for (j=0;j<K/2;j++) {
            if (i+j+1<N)
                g[i][j]=i+j+1;
            else
                g[i][j]=(i+j+1)-N;
        }

    for (i=0;i<N;i++)
        for (j=0;j<K/2;j++) {

            if (rand()/RAND_MAX<p) {
                pass=0;
                while (pass==0) {
                    select=rand()%N;
                    pass=1;
                    for (kk=0;kk<K/2;kk++) {
                        if (select==i||select==g[i][kk]||i==g[select][kk]) {
                            pass=0;
                            break;
                        }
                    }  
                    if (pass)
                        g[i][j]=select;

                }

            }

        }   





    kk=0;
    for (i=0;i<N;i++)
        for (j=0;j<K/2;j++) {
            c[kk]=i+1;
            d[kk]=g[i][j]+1;
            kk++;
        }

}       

