【问题标题】:How to keep position and names of nodes in Igraph + C如何在 Igraph + C 中保留节点的位置和名称
【发布时间】:2012-05-06 09:32:35
【问题描述】:

考虑到How to keep position of nodes in igraph + R,我有一个由四个顶点 id(0,1,2,3)、名称(1,2,3,4) 和位置(0,0, 0,1, 1) 组成的图,1, 1,0),举个简单的例子,我想消除顶点 id(1), 并保留其他人的位置和名称。下一个代码说明了一个绘图实现来调试它。我必须在保留其他顶点的位置和名称的同时进行几次消除,如何在 C 中做到这一点?

#include <igraph.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>

#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;


void plot(igraph_t g) {
      FILE *ofile;
      ofile=fopen("test.txt", "w+");
      igraph_write_graph_edgelist(&g,ofile);
      fclose (ofile);

      ofstream gp("data.R");
      gp << "library(igraph)"<<endl;
      gp << "library(Cairo)"<<endl;
      gp << "g1 <-read.table(\"test.txt\")"<< endl;
      gp << "g1 <- t(as.matrix(g1))"<< endl;
      gp << "g<-graph(g1,n=4,dir=FALSE)"<< endl;
      gp << "V(g)$name<-c(1:4)"<< endl;
      gp << "V(g)$label<-V(g)$name"<< endl;
      gp << "V(g)$id<-c(0:3)"<< endl;
      gp << "coords <- c(0,0,0,1,1,1,1,0)"<< endl;
      gp << "coords <- matrix(coords, 4,2,byrow=T)"<< endl;
      gp << "plot(g,layout=coords[V(g)$name,])"<< endl;
      gp.close();

      system("R CMD BATCH data.R");
}


int main() {


      igraph_t g;
      igraph_vector_t v;


      igraph_vector_init(&v,8);
      VECTOR(v)[0]=0; VECTOR(v)[1]=1;
      VECTOR(v)[2]=1; VECTOR(v)[3]=2;
      VECTOR(v)[4]=2; VECTOR(v)[5]=3;
      VECTOR(v)[6]=3; VECTOR(v)[7]=0;

      igraph_create(&g, &v, 0,0);

      //plot(g);

      igraph_delete_vertices(&g,igraph_vss_1(1));

      plot(g);


      igraph_destroy(&g);
      igraph_vector_destroy(&v);

      return 0;
}

根据第 9 章编辑程序。图形、顶点和边属性(Tamás):

我编辑了程序,现在我保留了名称,但是如何根据这些顶点名称获取边,然后将它们写入 igraph_write_graph_edgelist(&g,ofile)?

#include <igraph.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>

#include <vector>
#include <algorithm>
#include <map>
#include <set>

#include <string.h>
#include <stdlib.h>

using namespace std;

void plot(igraph_t g) {
      FILE *ofile;
      ofile=fopen("test.txt", "w+");
      igraph_write_graph_edgelist(&g,ofile);
      fclose (ofile);

      ofstream gp("data.R");
      gp << "library(igraph)"<<endl;
      gp << "library(Cairo)"<<endl;
      gp << "g1 <-read.table(\"test.txt\")"<< endl;
      gp << "g1 <- t(as.matrix(g1))"<< endl;
      gp << "g<-graph(g1,n=4,dir=FALSE)"<< endl;
      gp << "V(g)$name<-c(1:4)"<< endl;
      gp << "V(g)$label<-V(g)$name"<< endl;
      gp << "V(g)$id<-c(0:3)"<< endl;
      gp << "coords <- c(0,0,0,1,1,1,1,0)"<< endl;
      gp << "coords <- matrix(coords, 4,2,byrow=T)"<< endl;
      gp << "plot(g,layout=coords[V(g)$name,])"<< endl;
      gp.close();

      system("R CMD BATCH data.R");
}


int main() {
      igraph_i_set_attribute_table(&igraph_cattribute_table);

      igraph_t g;
      igraph_vector_t v;


      igraph_strvector_t vnames1,vnames2;





      igraph_vector_init(&v,8);
      VECTOR(v)[0]=0; VECTOR(v)[1]=1;
      VECTOR(v)[2]=1; VECTOR(v)[3]=2;
      VECTOR(v)[4]=2; VECTOR(v)[5]=3;
      VECTOR(v)[6]=3; VECTOR(v)[7]=0;

      igraph_create(&g, &v, 0,0);


      igraph_strvector_init(&vnames1, 0);
      igraph_strvector_init(&vnames2, 0);


      SETVAS(&g, "name", 0, "1");
      SETVAS(&g, "name", 1, "2");
      SETVAS(&g, "name", 2, "3");
      SETVAS(&g, "name", 3, "4");



      //plot(g);



      igraph_delete_vertices(&g,igraph_vss_1(1));
      plot(g);

      VASV(&g,"name",&vnames2);

       long int i;
       for (i=0; i<igraph_strvector_size(&vnames2); i++) {
             printf("%s ", STR(vnames2, i));
        }

      igraph_destroy(&g);
      igraph_vector_destroy(&v);

      return 0;
}

提前致谢

放假

【问题讨论】:

  • 你能不能创建一个struct 来保存Node 的所有信息,并从图中删除正确的信息。由于struct 包含所有信息,因此其他Nodes 不应受到影响。
  • 嗯 twain249,感谢您的回答,但我认为会有一些 igraph 函数可以让我这样做。

标签: c++ c igraph


【解决方案1】:

使用顶点属性将名称和任何相关信息附加到节点;这些即使在删除后也会保留。

查看文档的相关部分:Chapter 9. Graph, Vertex and Edge Attributes

【讨论】:

  • 好的,我已经编辑了程序,如何在这些顶点名称的函数中获取边,然后将它们写入 igraph_write_graph_edgelist(&g,ofile)?
  • 使用 igraph_write_graph_ncol 让您指定要用于顶点名称的顶点属性。
  • 谢谢它也有效!,如果我设置这样的坐标图 SETVAN(g,'x',0,1.0), SETVAN(g,'y',0,1.0),你觉得怎么样) 还有另一种方法吗?这个提议是尝试在 C/C++ 中创建一个绘图函数,让我调试我的代码。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 2022-01-21
  • 2016-02-08
  • 2021-08-17
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多