Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an undirected simple graph in a .txt file formatted as an adjacency list like this:

100 200
200 300
400 500 600 700 800 900

Every number is a node id and the graph is unweighted. The first number in each row is that row's node and any following numbers are adjacent nodes. In this example node 100 has node 200 as its neighbor, node 200 has nodes 100 and 300 as its neighbors, and node 300 has node 200 as its neighbor.

I am trying to find the betweenness centrality of each node. I found out about the Input, BetweennessCentrality, and FromAdjacencyLists functions but I cannot seem to use them correctly.
Does my data need to be formatted differently?
How do I get from inputing my data to using BetweennessCentrality?

I don't know much about using Mathematica so I might be missing something obvious.

share|improve this question
Your data format is not really clear. I guess the numbers are Edgeweigths, or are they distances? And since you seem to use records with varying length I don't really understand how they code for specific nodes. Could you also provide the code that you tried? – Sjoerd C. de Vries Apr 22 '14 at 17:21
I added an edit to explain my data. I don't really have much code to speak of, I just tried putting the input result into a variable and putting that variable into FromAdjacencyLists BetweennessCentrality but neither worked. – Daniel Apr 22 '14 at 19:41

First import your data:

adjlist = Import["data.txt", "Table"]

{{2, 10}, {1, 8, 9}, {6, 7, 8, 9}, {5, 8}, {4, 9}, {3, 8, 9}, {3,
10}, {2, 3, 4, 6}, {2, 3, 5, 6, 10}, {1, 7, 9}}

Get edges from adjlist (I assumed a graph is undirected simple.):

edges = Union[
  Sort /@ Flatten[MapIndexed[Thread[First[#2] <-> #] &, adjlist]]];

Define a graph using edges:

g = Graph[Range[Length[adjlist]], edges];

Compute centrality:


{0.95, 4.9, 5.23333, 1., 2., 0.533333, 0.95, 6.83333, 12.2667, \ 4.33333}

Hope this helps.

share|improve this answer

Your Answer


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.