2012-04-25 107 views
0

考慮到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中做到這一點?基於第9章圖形如何保留Igraph中節點的位置和名稱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; 
} 

編輯程序,頂點和邊屬性(陶):

我編輯的程序,現在我把名字,但如何在這些頂點名稱的功能獲得邊緣和然後寫在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; 
} 

在此先感謝

vacing

+0

你可以創建一個'struct'一起持有'Node'的所有信息,並從圖中正確的。由於'struct'保存了所有的信息,所以其他的'Nodes'都不會受到影響。 – twain249 2012-04-25 19:51:55

+0

嗯twain249,謝謝你的回答,但我認爲會有一些igraph函數讓我這樣做。 – user1310873 2012-04-26 11:38:58

回答

0

附上姓名和任何相關信息,使用頂點屬性的節點;這些將在刪除後保留。

參見文檔的相關章節:Chapter 9. Graph, Vertex and Edge Attributes

+0

好的,我已經編輯了程序,如何在這些頂點名稱的函數中獲得邊緣,然後將它們寫入igraph_write_graph_edgelist(&g,ofile)? – user1310873 2012-04-26 10:52:29

+0

使用igraph_write_graph_ncol可讓您指定要用於頂點名稱的頂點屬性。 – 2012-04-26 18:20:34

+0

謝謝你的工作!你覺得如果我像這樣設置座標圖SETVAN(g,'x',0,1.0),SETVAN(g,'y',0,1.0)有什麼方法嗎?這個建議試圖在C/C++中創建一個繪圖函數,讓我調試我的代碼。有關它的任何建議? – user1310873 2012-04-27 08:56:41

相關問題