Beispiel #1
0
igraph_vector_t *ggen_analyze_lowest_single_ancestor(igraph_t *g)
{
	unsigned long i,v,l,vid,r;
	int err = 0;
	igraph_vector_t toposort,itopo;
	igraph_vector_t *lsa;
	igraph_t tree;
	igraph_vs_t vs;
	igraph_vit_t vit;
	lca_metadata md;

	if(g == NULL)
		return NULL;

	err = igraph_vector_init(&toposort,igraph_vcount(g));
	if(err) return NULL;

	err = igraph_topological_sorting(g,&toposort,IGRAPH_OUT);
	if(err) goto d_tp;

	/* build a reverse index of the toposort */
	err = igraph_vector_init(&itopo,igraph_vcount(g));
	if(err) goto d_tp;

	for(i = 0; i < igraph_vcount(g); i++)
	{
		v = VECTOR(toposort)[i];
		VECTOR(itopo)[v] = i;
	}

	err = igraph_empty(&tree,1,IGRAPH_DIRECTED);
	if(err) goto d_i;

	lsa = calloc(1,sizeof(igraph_vector_t*));
	if(lsa == NULL) goto cleanup;

	err = igraph_vector_init(lsa,igraph_vcount(g));
	if(err) goto f_l;

	for(v = 1; v < igraph_vcount(g); v++)
	{
		vid = VECTOR(toposort)[v];

		tree_lca_metadata_init(&tree,&md);
		tree_lca_preprocessing(&tree,0,&md);

		/* iterate over parents of v in g
		 * The lsa of a node is the LCA of all its parents in our
		 * special tree.
		 */
		igraph_vs_adj(&vs, vid, IGRAPH_IN);
		igraph_vit_create(g,vs,&vit);
		l = VECTOR(itopo)[IGRAPH_VIT_GET(vit)];
		IGRAPH_VIT_NEXT(vit);

		for(vit;!IGRAPH_VIT_END(vit); IGRAPH_VIT_NEXT(vit))
		{
			tree_lca_query(&tree,l,VECTOR(itopo)[IGRAPH_VIT_GET(vit)],&r,&md);
			l = r;
		}

		igraph_vit_destroy(&vit);
		igraph_vs_destroy(&vs);
		tree_lca_metadata_free(&tree,&md);

		// update tree
		err = igraph_add_vertices(&tree,1,NULL);
		if(err) goto d_l;

		err = igraph_add_edge(&tree,l,v);
		if(err) goto d_l;

		VECTOR(*lsa)[vid] = VECTOR(toposort)[l];
	}
	goto cleanup;
d_l:
	igraph_vector_destroy(lsa);
f_l:
	free(lsa);
	lsa = NULL;
cleanup:
	igraph_destroy(&tree);
d_i:
	igraph_vector_destroy(&itopo);
d_tp:
	igraph_vector_destroy(&toposort);
	return lsa;
}
Beispiel #2
0
igraph_vector_t * ggen_analyze_longest_path(igraph_t *g)
{
	igraph_vector_t topology;
	igraph_vector_t lengths;
	igraph_vector_t preds;
	igraph_vs_t vs;
	igraph_vit_t vit;
	igraph_vector_t *res = NULL;
	int err;
	unsigned long v,i,f,t;
	long maxv;
	if(g == NULL)
		return NULL;

	v = igraph_vcount(g);
	err = igraph_vector_init(&topology,v);
	if(err)	return NULL;

	err = igraph_vector_init(&lengths,v);
	if(err) goto error_il;

	err = igraph_vector_init(&preds,v);
	if(err) goto error_ip;

	res = malloc(sizeof(igraph_vector_t));
	if(res == NULL) goto cleanup;

	err = igraph_vector_init(res,v);
	if(err) goto error_ir;

	// sort topologically the vertices
	err = igraph_topological_sorting(g,&topology,IGRAPH_OUT);
	if(err) goto error;
	// igraph is stupid, it returns 0 even if the graph isn't a dag
	if(igraph_vector_size(&topology) != v)
		goto error;

	// find the best path incomming from every node
	igraph_vector_null(&lengths);
	igraph_vector_fill(&preds,-1);
	maxv = -1;
	for(i = 0; i < v; i++)
	{
		f = VECTOR(topology)[i];
		err = igraph_vs_adj(&vs,f,IGRAPH_OUT);
		if(err) goto error;
		err = igraph_vit_create(g,vs,&vit);
		if(err)
		{
			igraph_vs_destroy(&vs);
			goto error;
		}

		for(vit; !IGRAPH_VIT_END(vit); IGRAPH_VIT_NEXT(vit))
		{
			t = IGRAPH_VIT_GET(vit);
			if(VECTOR(lengths)[t] < VECTOR(lengths)[f] + 1)
			{
				VECTOR(lengths)[t] = VECTOR(lengths)[f] +1;
				VECTOR(preds)[t] = f;
			}
			if(maxv == -1 || VECTOR(lengths)[t] > VECTOR(lengths)[maxv])
				maxv = t;
		}
		igraph_vs_destroy(&vs);
		igraph_vit_destroy(&vit);

	}
	// build the path, using preds and maxv
	f = 0;
	while(maxv != -1)
	{
		VECTOR(*res)[f++] = maxv;
		maxv = VECTOR(preds)[maxv];
	}

	// finish the path correctly, resizing and reversing the array
	err = igraph_vector_resize(res,f);
	if(err) goto error;

	err = igraph_vector_reverse(res);
	if(err) goto error;

	goto cleanup;
error:
	igraph_vector_destroy(res);
error_ir:
	free(res);
	res = NULL;
cleanup:
	igraph_vector_destroy(&preds);
error_ip:
	igraph_vector_destroy(&lengths);
error_il:
	igraph_vector_destroy(&topology);
	return res;
}
static PyObject *ignp_fun_propagate(PyObject *self, PyObject *args) {
    long int num_active = 0;
    long int num_susc = 1;
    long int limit = 30;
    long int i;
    float lrAct;
    PyObject* mem_addr_o;
    long int mem_addr;
    
    /* StateTracker Vars */
    PyArrayObject *py_trkr; //   'i64'
    
    /* By EdgeID */
    PyArrayObject *py_tie_r; //  'f32'
    
    /* By NodeID */
    PyArrayObject *py_act_n;   // 'i8'
    PyArrayObject *py_thr_n;   // 'f32'
    PyArrayObject *py_exp_n;   // 'i64'

    /* By Infection Order*/
    PyArrayObject *py_deg;   //  i64
    PyArrayObject *py_nSuc;  //  i64
    PyArrayObject *py_nAct;  //  i64
    PyArrayObject *py_lrAct; //  f32
    PyArrayObject *py_hom;   //  i64
    PyArrayObject *py_eComp; //  i64
    PyArrayObject *py_iComp; //  i64
    PyArrayObject *py_eTri;  //  i64
    PyArrayObject *py_iTri;  //  i64
    PyArrayObject *py_thr;   //  i32
    PyArrayObject *py_exp;   //  i64
    PyArrayObject *py_cTime; //  i64

    PyObject *g_obj;
    igraph_t *g;
    igraph_t gc;
    
    long int randID;
    long int low  = 0;
    long int high = -1;
    long int ctime = 0;
    igraph_rng_t *rGen;
    igraph_vit_t nbr_iter;
    igraph_vs_t  nbr_sel;
    igraph_integer_t eid;
    igraph_integer_t vdeg;
    igraph_integer_t e_comp = 0;
    igraph_integer_t i_comp = 0;
    igraph_integer_t e_tri = 0;
    igraph_integer_t i_tri = 0;
    int actv_nbr_count;
    //int res, j;
    igraph_vector_t temp;
    //igraph_vector_t actv_nbrs;

    //PySys_WriteStdout("Parse Started\n");
    if (!PyArg_ParseTuple(args, "OO!O!O!O!O!O!O!O!O!O!O!O!O!O!O!O!O!",
                           &g_obj,
                           &PyArray_Type, &py_trkr,  //  i64
                           &PyArray_Type, &py_tie_r, //  'f32'
                           &PyArray_Type, &py_act_n, //  'i8'
                           &PyArray_Type, &py_thr_n, //  'i32'
                           &PyArray_Type, &py_exp_n, //  'i64'
                           &PyArray_Type, &py_deg,   //  i64
                           &PyArray_Type, &py_nSuc,  //  i64
                           &PyArray_Type, &py_nAct,  //  i64
                           &PyArray_Type, &py_lrAct, //  f32
                           &PyArray_Type, &py_hom,   //  i64
                           &PyArray_Type, &py_eComp, //  i64
                           &PyArray_Type, &py_iComp, //  i64
                           &PyArray_Type, &py_eTri,  //  i64
                           &PyArray_Type, &py_iTri,  //  i64
                           &PyArray_Type, &py_thr,   //  i64
                           &PyArray_Type, &py_exp,   //  i64 
                           &PyArray_Type, &py_cTime  //  i64                           
                        )) {
        printf("Parse Failed\n");
        Py_RETURN_NONE;
    }
    //PySys_WriteStdout("Getting Tracker Vars\n");
    num_active =  (long) ax_i64(py_trkr, 0);
    num_susc   =  (long) ax_i64(py_trkr, 1);
    limit      =  (long) ax_i64(py_trkr, 2);
    
    mem_addr_o = PyObject_CallMethod(g_obj, "_raw_pointer", "()");
    mem_addr = PyInt_AsLong(mem_addr_o);
    Py_DECREF(mem_addr_o);
    if (mem_addr == -1) { 
        printf("PyInt to Long Failed");
        return NULL;
    }
    g = (igraph_t*) mem_addr;
    
    //Setup Vars
    rGen = igraph_rng_default();
    //igraph_rng_init(rGen, time(NULL));
    high += (long) igraph_vcount(g);
    
    //PySys_WriteStdout("Propagate Starting with %li active of target %li with %li open\n",
    //                    num_active, limit, num_susc);
    //Propagate
    do {
        // get random node
        ctime += 1;
        randID = igraph_rng_get_integer(rGen, low, high);
        if ( ax_i8(py_act_n, randID) != 1 && ax_i64(py_exp_n, randID)>=ax_i32(py_thr_n, randID) ){
            //activate
            ax_i8(py_act_n,randID) = 1;
            lrAct = 0;
            
            //update nbrs
            actv_nbr_count = 0;
            igraph_vs_adj( &nbr_sel, randID, IGRAPH_ALL);
            igraph_vit_create(g, nbr_sel, &nbr_iter);
            igraph_vs_size(g, &nbr_sel, &vdeg);
            igraph_vector_init(&temp, vdeg);
            while( !IGRAPH_VIT_END(nbr_iter) ){
                i = (long int) IGRAPH_VIT_GET(nbr_iter);
                ax_i64( py_exp_n, i ) += 1;
                
                /* update active nbr count and collect id of active */
                if ( ax_i8(py_act_n, i) == i ) {
                    VECTOR(temp)[actv_nbr_count]=i;
                    actv_nbr_count += 1;
                }
                
                /* update num_susc */
                if ( ax_i8(py_act_n, i) == 0 && \
                     ax_i32(py_thr_n, i) >  (float) (ax_i64(py_exp_n, i)-1) && \
                     ax_i32(py_thr_n, i) <= (float) ax_i64(py_exp_n, i)      ){
                     /*PySys_WriteStdout("%li <  %i <= %li\n", 
                                         (ax_i64(py_exp_n, i)-1),
                                         ax_i32(py_thr_n, i),
                                         ax_i64(py_exp_n, i) );*/
                     num_susc += 1; 
                }
                
                /* Get #active long ties */
                if ( ax_i8(py_act_n, i) == 1 ){
                    igraph_get_eid(g, &eid, randID, i, 0, 1);
                    lrAct +=  ax_f32( py_tie_r, eid )>2 ;
                }
                IGRAPH_VIT_NEXT(nbr_iter);
            }
            igraph_vit_destroy(&nbr_iter);
            igraph_vs_destroy(&nbr_sel);

            //Compute Components (among all and active nbrs)
            igraph_vs_adj( &nbr_sel, randID, IGRAPH_ALL);
            igraph_induced_subgraph(g, &gc, nbr_sel, IGRAPH_SUBGRAPH_CREATE_FROM_SCRATCH);                         
            igraph_clusters(&gc, NULL, NULL, &e_comp, IGRAPH_WEAK);
            e_tri = igraph_vcount(&gc);
            igraph_destroy(&gc);
            igraph_vs_destroy(&nbr_sel);

        
            igraph_induced_subgraph(g, &gc, igraph_vss_vector(&temp), \
                                     IGRAPH_SUBGRAPH_CREATE_FROM_SCRATCH);
            igraph_clusters(&gc, NULL, NULL, &i_comp, IGRAPH_WEAK);
            i_tri = igraph_vcount(&gc);
            
            //Clean up
            igraph_destroy(&gc); 
            igraph_vector_destroy(&temp);
            
            //PySys_WriteStdout("e_comp: %i,  i_comp: %i\n", e_comp, i_comp);
            //PySys_WriteStdout("e_tri:  %i,  i_tri:  %i\n", e_tri, i_tri);

            //update tracking vars
            ax_f32( py_lrAct, num_active ) = (npy_float32) lrAct;
            ax_i32( py_thr,   num_active)  =  ax_i32(py_thr_n, randID);

            ax_i64( py_deg,   num_active) = (npy_int64) vdeg; 
            ax_i64( py_nSuc,  num_active) = (npy_int64) num_susc;
            ax_i64( py_nAct,  num_active) = (npy_int64) num_active;
            //ax_i64( py_hom,   num_active) = (npy_int64) num_susc;
            ax_i64( py_eComp, num_active) = (npy_int64) e_comp;
            ax_i64( py_iComp, num_active) = (npy_int64) i_comp;
            ax_i64( py_eTri,  num_active) = (npy_int64) e_tri;
            ax_i64( py_iTri,  num_active) = (npy_int64) i_tri;
            ax_i64( py_exp,   num_active) = ax_i64(py_exp_n, randID);
            ax_i64( py_cTime, num_active) = (npy_int64) ctime;
            num_active += 1;
        }
    } while( num_susc > num_active && num_active < limit);
    //PySys_WriteStdout("Propagate Finished with %li active of target %li with %li open\n",
    //                   num_active, limit, num_susc);

    //igraph_rng_destroy(rGen);
    ax_i64(py_trkr, 0) = (npy_int64) num_active;
    ax_i64(py_trkr, 1) = (npy_int64) num_susc  ;
    ax_i64(py_trkr, 2) = (npy_int64) limit     ;

    Py_RETURN_NONE;

}