igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] Simple Graph Analysis


From: Lorenzo Isella
Subject: [igraph] Simple Graph Analysis
Date: Fri, 15 May 2009 14:13:53 +0200

Dear All,
I am trying to do rather basic things with Igraph and R, but I am
experiencing some problems.
I paste below one code I am using: it generates a random graph via the
adjacency matrix.
First, I try to plot it as it is.
Second, I highlight its diameter.
Third (and last): I try to plot the graph and to highlight its largest
component.
Probably I am misunderstanding something, since the diameter in the
second figure I generate does not look like the "longest shortest
path" (http://mathworld.wolfram.com/GraphDiameter.html) and above all
when I try to plot the largest cluster of connected nodes in the
graph, I see also some totally isolated nodes.
Here is the code

rm(list=ls())

library(Matrix)
library(igraph)
library(Cairo)


set.seed(1234)

n<-300
p<-0.005
my_seq <- runif(n*n)

my_sel<-which(my_seq<p)
my_sel_out<-which(my_seq>p)



my_seq[my_sel] <- 1

my_seq[my_sel_out] <- 0



adj_mat <- forceSymmetric(Matrix(my_seq, n))

diag(adj_mat) <- 1


thres_giant = 1/(n-1)

print("The threshold for a giant component is, ")
print(thres_giant)


g <- graph.adjacency(adj_mat, "undirected")


g <- simplify(g)


l <- layout.fruchterman.reingold(g)
l <- layout.norm(l, -1,1, -1,1)


E(g)$color <- "grey"
E(g)$width <- 1
V(g)$label.color <- "blue"
V(g)$color  <- "SkyBlue2"



pdf("random_graph.pdf")

plot(g, layout=layout.fruchterman.reingold,
     vertex.label.dist=0.5,vertex.label=NA, vertex.size=5)


dev.off()

#get minimum spanning tree

d <- get.diameter(g)


E(g, path=d)$color <- "red"
E(g, path=d)$width <- 1

V(g)[ d ]$label.color <- "black"
V(g)[ d ]$color <- "red"




pdf("random_graph_diameter.pdf")


plot(g, layout=layout.fruchterman.reingold,
     vertex.label.dist=0.5,vertex.label=NA, vertex.size=5)

title(main="Diameter of the random graph",
      xlab="created by igraph 0.5")
axis(1, labels=FALSE, tick=TRUE)
axis(2, labels=FALSE, tick=TRUE)

dev.off()

############################################################################################
############################################################################################
############################################################################################



#Now identify the connected components

cluster_structure <- clusters(g)

print ("The overall number of clusters is, ")
print (cluster_structure$no)

print ("The membership of individual nodes  is, ")
print (cluster_structure$membership)



select_vertices <- V(g)[which(cluster_structure$membership==0)]




sub <- subgraph(g, select_vertices)



#test that all the nodes in this graph are connected

print ("The overall number of clusters in sub is, ")
print (clusters(sub)$no)


#now highlight the largest connected component

E(g)$color <- "grey"
E(g)$width <- 1
V(g)$label.color <- "blue"
V(g)$color  <- "SkyBlue2"



V(g)[ select_vertices ]$label.color <- "black"
V(g)[ select_vertices ]$color <- "red"




pdf("clusters.pdf")


plot(g, layout=layout.fruchterman.reingold,
     vertex.label.dist=0.5,vertex.label=NA, vertex.size=5)

title(main="Largest cluster in a random graph",
      xlab="created by igraph 0.5")
axis(1, labels=FALSE, tick=TRUE)
axis(2, labels=FALSE, tick=TRUE)

dev.off()

Any suggestion/hint at what I am doing wrong is very appreciated.
Regards

Lorenzo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]