1
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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? $\endgroup$ Commented Apr 22, 2014 at 17:21
  • $\begingroup$ 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. $\endgroup$
    – Daniel
    Commented Apr 22, 2014 at 19:41

1 Answer 1

5
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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