0

After loading external graph from disk as edgelist, I want to view the matrix format of this graph. Here is my steps:

> rm(list=ls())
> data <- read.graph("c:\\temp\\graph.txt", format="edgelist")
> adjm <- data.matrix(data)
> adjm
IGRAPH D--- 18 28 -- 
> 

> get.incidence(data)
Error in get.incidence(data) : 
  Not a bipartite graph, supply `types' argument

The matrix is not print. The data format of external graph is like(graph.txt):

0 7
2 7
3 0 
3 2 
3 4 
4 9
5 1 
5 6
6 7
7 12
8 2 
8 3
8 14
10 6
11 12
12 13
13 8
14 15
14 13

As you can see, it only print graph information(number of nodes, direct or undirect). How can I print this graph as incidence matrix? or how can I supply types for incidence command? Thanks

jbaums
  • 27,115
  • 5
  • 79
  • 119
shijie xu
  • 1,975
  • 21
  • 52

3 Answers3

1

The igraph functions make this very straightforward.

g <- graph.ring(10)
g

IGRAPH U--- 10 10 -- Ring graph
+ attr: name (g/c), mutual (g/x), circular (g/x)

get.adjacency(g)

10 x 10 sparse Matrix of class "dgCMatrix"

 [1,] . 1 . . . . . . . 1
 [2,] 1 . 1 . . . . . . .
 [3,] . 1 . 1 . . . . . .
 [4,] . . 1 . 1 . . . . .
 [5,] . . . 1 . 1 . . . .
 [6,] . . . . 1 . 1 . . .
 [7,] . . . . . 1 . 1 . .
 [8,] . . . . . . 1 . 1 .
 [9,] . . . . . . . 1 . 1
[10,] 1 . . . . . . . 1 .

Wrap get.adjacency in as.matrix if you'd like it coerced to a binary matrix.

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • This print adjacency matrix, but how about incidence matrix? – shijie xu Mar 28 '14 at 00:53
  • Apologies, I'm not sure of the correct way to overcome your `Not a bipartite graph` error. You need to correctly specify a `type` argument (a binary vector the same length as the number of vertices in your graph), but I don't know how to determine this. Feel free to "unaccept" this answer, since it doesn't solve your problem. – jbaums Mar 28 '14 at 02:39
0

If you have a 2-column matrix M, then we can create a square matrix of 0s and fill it with 1s at connected nodes by

M <- M + 1
adj <- matrix(0, max(M), max(M))
adj[M] <- 1
Gavin Kelly
  • 2,374
  • 1
  • 10
  • 13
  • This one also works for adjacency matrix after made minor changes adj[m] <- 1. But how about incidence matrix? – shijie xu Mar 28 '14 at 00:56
0

Usage

get.incidence(your_graph, types = NULL, ......)

types: An optional vertex type vector to use instead of the type vertex attribute. You must supply this argument if the graph has no type vertex attribute

let's call the data frame as "your_data_frame" and your graph as "your_graph"

It looks like the graph you created has type not specified. It looks like below

> your_graph
IGRAPH DN-- 65 333 -- 
+ attr: name (v/c)
+ edges (vertex names):

A graph where type is specified as bipartite looks like

> your_graph
IGRAPH DN-B 92 22325 -- 
+ attr: name (v/c), type (v/l)
+ edges (vertex names):

B in DN-B stands for bipartite

To specify the type do the following

V(your_graph)$type = V(your_graph)$name %in% your_data_frame[,1]

Note: your_data_frame should only have two columns. if not use quick indexing like df[c(1,2)]

after performing these steps you can check your_graph, now type should be specified

you can then go ahead and create the incidence matrix using