0

ノードの位置を igraph + R に保持する方法を考慮すると、4 つの頂点 id(0,1,2,3)、name(1,2,3,4)、positions(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;
}

前もって感謝します

休暇中

4

1 に答える 1

0

頂点属性を使用して、名前と関連情報をノードに添付します。これらは削除後も保持されます。

ドキュメントの関連セクションを参照してください:第 9 章. グラフ、頂点、およびエッジの属性

于 2012-04-25T20:50:09.543 に答える