/** * \ingroup python_interface_edgeseq * \brief Allocate a new edge sequence object for a given graph * \param g the graph object being referenced * \return the allocated PyObject */ PyObject* igraphmodule_EdgeSeq_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) { igraphmodule_EdgeSeqObject* o; o=(igraphmodule_EdgeSeqObject*)PyType_GenericNew(subtype, args, kwds); if (o == NULL) return NULL; igraph_es_all(&o->es, IGRAPH_EDGEORDER_ID); o->gref=0; o->weakreflist=0; RC_ALLOC("EdgeSeq", o); return (PyObject*)o; }
/** * \ingroup python_interface_edgeseq * \brief Initialize a new edge sequence object for a given graph * \return the initialized PyObject */ int igraphmodule_EdgeSeq_init(igraphmodule_EdgeSeqObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { "graph", "edges", NULL }; PyObject *g, *esobj=Py_None; igraph_es_t es; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|O", kwlist, &igraphmodule_GraphType, &g, &esobj)) return -1; if (esobj == Py_None) { /* If es is None, we are selecting all the edges */ igraph_es_all(&es, IGRAPH_EDGEORDER_ID); } else if (PyInt_Check(esobj)) { /* We selected a single edge */ long int idx = PyInt_AsLong(esobj); if (idx < 0 || idx >= igraph_ecount(&((igraphmodule_GraphObject*)g)->g)) { PyErr_SetString(PyExc_ValueError, "edge index out of range"); return -1; } igraph_es_1(&es, (igraph_integer_t)idx); } else { /* We selected multiple edges */ igraph_vector_t v; igraph_integer_t n = igraph_ecount(&((igraphmodule_GraphObject*)g)->g); if (igraphmodule_PyObject_to_vector_t(esobj, &v, 1)) return -1; if (!igraph_vector_isininterval(&v, 0, n-1)) { igraph_vector_destroy(&v); PyErr_SetString(PyExc_ValueError, "edge index out of range"); return -1; } if (igraph_es_vector_copy(&es, &v)) { igraphmodule_handle_igraph_error(); igraph_vector_destroy(&v); return -1; } igraph_vector_destroy(&v); } self->es = es; Py_INCREF(g); self->gref = (igraphmodule_GraphObject*)g; return 0; }
int main(int argc, char* argv[]) { igraph_i_set_attribute_table(&igraph_cattribute_table); char * in_graph_fn; char * out_graph_fn; if (argc < 3) { printf("[ERROR] usage: mm-writer in_graph_fn out_graph_fn\n"); exit(-1); } else { in_graph_fn = &argv[1][0]; out_graph_fn = &argv[2][0]; } igraph_t g; FILE *ifile, *ofile; ifile = fopen(in_graph_fn, "r"); if (ifile == 0) { return 10; } igraph_read_graph_graphml(&g, ifile, 0); fclose(ifile); igraph_to_directed(&g, IGRAPH_TO_DIRECTED_ARBITRARY); printf("The graph stats:\n"); printf("Vertices: %li\n", (long int) igraph_vcount(&g)); printf("Edges: %li\n", (long int) igraph_ecount(&g)); printf("Directed: %i\n", (int) igraph_is_directed(&g)); ofile = fopen(out_graph_fn, "w"); if (ofile == 0) { return 10; } // Write MM format fprintf(ofile, "%li %li %li\n", (long int) igraph_vcount(&g), (long int) igraph_vcount(&g), (long int) igraph_ecount(&g)); igraph_integer_t to; igraph_integer_t from; igraph_integer_t eid; igraph_real_t weight; for (eid = 0; eid < (long int) igraph_ecount(&g); eid++) { igraph_edge(&g, eid, &from, &to); weight = igraph_cattribute_EAN(&g, "weight",eid); // TODO: time fprintf(ofile, "%i %i %f\n", from, to, weight); //printf("Edge %i => %i --> %i\n", eid, from, to); //printf("Edge %i has weight %f\n", eid, igraph_cattribute_EAN(&g, "weight",eid)); // TODO: time } // For all. TODO: time #if 0 igraph_es_t eids; igraph_es_all(&eids, IGRAPH_EDGEORDER_ID); // Order edges igraph_vector_t result; igraph_vector_init(&result, (int long) igraph_ecount(&g)); igraph_cattribute_EANV(&g, "weight", eids, &result); igraph_integer_t i; for (i = 0; i < (int long) igraph_ecount(&g); i++) printf("Edge %i value: %f\n", i, VECTOR(result)[i]); // Free memory igraph_es_destroy(&eids); igraph_vector_destroy(&result); #endif igraph_destroy(&g); fclose(ofile); return 0; }
int igraph_to_undirected(igraph_t *graph, igraph_to_undirected_t mode) { long int no_of_nodes=igraph_vcount(graph); long int no_of_edges=igraph_ecount(graph); igraph_vector_t edges; igraph_t newgraph; if (mode != IGRAPH_TO_UNDIRECTED_EACH && mode != IGRAPH_TO_UNDIRECTED_COLLAPSE) { IGRAPH_ERROR("Cannot undirect graph, invalid mode", IGRAPH_EINVAL); } if (!igraph_is_directed(graph)) { return 0; } IGRAPH_VECTOR_INIT_FINALLY(&edges, 0); if (mode==IGRAPH_TO_UNDIRECTED_EACH) { igraph_es_t es; igraph_eit_t eit; IGRAPH_CHECK(igraph_vector_reserve(&edges, no_of_edges*2)); IGRAPH_CHECK(igraph_es_all(&es, IGRAPH_EDGEORDER_ID)); IGRAPH_FINALLY(igraph_es_destroy, &es); IGRAPH_CHECK(igraph_eit_create(graph, es, &eit)); IGRAPH_FINALLY(igraph_eit_destroy, &eit); while (!IGRAPH_EIT_END(eit)) { long int edge=IGRAPH_EIT_GET(eit); igraph_integer_t from, to; igraph_edge(graph, edge, &from, &to); IGRAPH_CHECK(igraph_vector_push_back(&edges, from)); IGRAPH_CHECK(igraph_vector_push_back(&edges, to)); IGRAPH_EIT_NEXT(eit); } igraph_eit_destroy(&eit); igraph_es_destroy(&es); IGRAPH_FINALLY_CLEAN(2); IGRAPH_CHECK(igraph_create(&newgraph, &edges, no_of_nodes, IGRAPH_UNDIRECTED)); IGRAPH_FINALLY(igraph_destroy, &newgraph); igraph_vector_destroy(&edges); IGRAPH_I_ATTRIBUTE_DESTROY(&newgraph); IGRAPH_I_ATTRIBUTE_COPY(&newgraph, graph, 1,1,1); IGRAPH_FINALLY_CLEAN(2); igraph_destroy(graph); *graph=newgraph; } else if (mode==IGRAPH_TO_UNDIRECTED_COLLAPSE) { igraph_vector_t seen, nei; long int i,j; IGRAPH_CHECK(igraph_vector_reserve(&edges, no_of_edges*2)); IGRAPH_VECTOR_INIT_FINALLY(&seen, no_of_nodes); IGRAPH_VECTOR_INIT_FINALLY(&nei, 0); for (i=0; i<no_of_nodes; i++) { IGRAPH_CHECK(igraph_neighbors(graph, &nei, i, IGRAPH_ALL)); for (j=0; j<igraph_vector_size(&nei); j++) { long int node=VECTOR(nei)[j]; if (VECTOR(seen)[node] != i+1 && node >= i) { IGRAPH_CHECK(igraph_vector_push_back(&edges, i)); IGRAPH_CHECK(igraph_vector_push_back(&edges, node)); VECTOR(seen)[node]=i+1; } } } igraph_vector_destroy(&nei); igraph_vector_destroy(&seen); IGRAPH_FINALLY_CLEAN(2); IGRAPH_CHECK(igraph_create(&newgraph, &edges, no_of_nodes, IGRAPH_UNDIRECTED)); IGRAPH_FINALLY(igraph_destroy, &newgraph); igraph_vector_destroy(&edges); IGRAPH_I_ATTRIBUTE_DESTROY(&newgraph); IGRAPH_I_ATTRIBUTE_COPY(&newgraph, graph, 1,1,0); /* no edge attributes */ IGRAPH_FINALLY_CLEAN(2); igraph_destroy(graph); *graph=newgraph; } return 0; }