setwd('/Users/ovitek/Dropbox/Olga/Teaching/CS6220/Fall15/LectureNotes/8-graphs') library(igraph) # Generate a random graph # p.96 of the notes # 20 vertices. prob(edge between two nodes) # g <- random.graph.game(20,5/20, directed=TRUE) # depreciated g <- sample_gnp(20,5/20, directed=TRUE) # Graph(node,probability) # another possible way to simulate graphs # g <- sample_gnm(20,40, directed=TRUE) # Graph(node,edge) plot(g) page.rank(g) # Generate a graph structure from an adjecency matrix # p.104 of the notes A <- matrix(c( 0,1,1,0,0,0, 1,0,1,1,1,1, 0,1,0,0,0,0, 0,0,1,0,1,0, 0,1,1,0,0,0, 0,0,1,0,0,0 ), nrow=6, ncol=6, byrow=TRUE) vertexLabels <- c('Wikipedia', 'Google', 'Bing', 'Yahoo', 'Altavista', 'Rediffmail') dimnames(A)[[2]] <- vertexLabels G <- graph.adjacency(A, mode=c('directed'), weighted=NULL) plot(G) # Graph properties #------------------- degree(G) shortest.paths(G) betweenness(G) page.rank(G)$vector # A preview of the HITS algorithm #----------------------------------- a1 <- t(A) %*% rep(1, 6) a1 <- a1/sqrt(sum(a1^2)) h1 <- A %*% a1 h1 <- h1/sqrt(sum(h1^2)) a2 <- t(A) %*% a1 a2 <- a2/sqrt(sum(a2^2)) # A preview of neighborhood-based methods #-------------------------------------------- V(G) # list of vertices V(G)['Yahoo'] # select the 'Yahoo' vertice V(G)[nei(V(G)['Yahoo'])] #'nei' is only used while subsetting