Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am producing a graph, generated by a list of adjacent nodes. (The list looks like {1 ->583, 1->2977, 2->14, 4->1293, 5->221, ...}. Each node of my graph is a number.

I wanted to get the centrality measures for this graph so I used:

bc = BetweennessCentrality[graph1]

This yields the entire list of centrality measures, (in the order in which the nodes appear in my list i guess?)

{6., 0., 0., 0., 0., 3., 5., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 
1., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 
0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0.}

I would like to find a way to obtain the centrality measure for a precise node, say, node 2. I only know of this kind of way :

bc[[2]]

But this only gives me the second entry in the obtained list, not the centrality measure for node number 2. What I would really need is to generate a list with the first 10 nodes in my graph and their respective centrality measures.

share|improve this question
    
I can't comment on or answer your deleted question, but try Grid[Transpose[{VertexList[graph1], bc}]]. I'll update my answer here instead. –  Szabolcs Feb 18 '12 at 18:32
add comment

3 Answers

up vote 7 down vote accepted

Important note: The order in which BetweennessCentrality (or any other graph-related function, including AdjacencyMatrix) will return results is not the same as the order in which you passed vertices to Graph, nor is it the lexicographic order of vertices. It is the order in which VertexList returns vertices.

Misunderstandings about this are a common source of error, so I thought it important to spell this out. Do not count on vertex n corresponding to the nth element in the result list. Always use VertexList!

Now, an easy way to pick out the betweenness centrality of vertex is

Pick[BetweennessCentrality[graph], VertexList[graph], vertex]

Unfortunately it does not seem to be possible to compute the result for one vertex only.

To get the centrality of more than one node, contained in the list vertices, use

Pick[BetweennessCentrality[graph], VertexList[graph], Alternatives @@ vertices]

To see all vertex names paired up with their centralities in a table, you can use

Grid[Transpose[{VertexList[graph], bc}]]
share|improve this answer
    
Let me know if the performance of this is not sufficient for your particular network. There are faster practical solutions using Ordering. –  Szabolcs Feb 15 '12 at 23:18
add comment

There is a function that gives you a vertex index:

bc[[VertexIndex[graph, vertex]]]
share|improve this answer
add comment

Graph functions in Mathematica 8 return per-vertex results according to the order of the vertices in VertexList[graph]. That is, internally, the vertices are labeled according to the order they are added to the graph, and the vertex list just holds extrinsic labels (which can be of any type).

To answer your specific question, to retrieve the betweenness centrality score for the vertex with a given label, do something like this:

In[1]:= l = Table[DirectedEdge[i, i + 1], {i, 4}];

In[2]:= g1 = Graph[l];

In[3]:= VertexList[g1]

Out[3]= {1, 2, 3, 4, 5}

In[4]:= BetweennessCentrality[g1]

Out[4]= {0., 3., 4., 3., 0.}

In[5]:= g2 = Graph[RotateLeft[l, 1]];

In[6]:= BetweennessCentrality[g2]

Out[6]= {3., 4., 3., 0., 0.}

In[7]:= VertexList[g2]

Out[7]= {2, 3, 4, 5, 1}

In[8]:= BetweennessCentrality[
  g2][[Position[VertexList[g2], 2][[1, 1]]]]

Out[8]= 3.

In practice, you'll probably want to create a Dispatch table for rewriting vertex ordinals to vertex identifiers, and vice versa. This is easy by doing something like

MapIndexed[#2[[1]]->#1&,VertexList[g]]//Dispatch (* creates ordinal to id map *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.