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 have an undirected simple graph in a .txt file formatted as an adjacency list like this:

100 200
200 300
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
1  
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 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 at 19:41
add comment

1 Answer 1

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:

BetweennessCentrality[g]

{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
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.