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 a dataset in format .txt of a directed network, containing a matrix A of dimension m x 2, where each row gives the labels of two nodes showing a direct link.

For example, when the network has $4$ nodes, the matrix A could be

A = [1 2; 1 3; 2 1; 2 4; 3 2; 4 1; 4 2]

where the first row means that there is a link from 1 to 2, the second row means that there is a link from 1 to 3, etc.

Could you suggest me a way to draw the network specified by A in Mathematica?

share|improve this question
Probably you can do: DirectedEdge @@@ Import["text.txt", "Data"] // Graph. Please take a look at all those symbols in documentation if there are doubts. – Kuba May 22 '14 at 11:20
I guess you're aware that your line of code is NOT valid in Mathematica. – Dr. belisarius May 22 '14 at 11:37

First you need to get a minimum involved in what you are planning to do and search for "network" inside Mathematica:

Then you want to construct a Graph so you go to:

and see: Graph — construct a graph from vertices and edges:

where the first example is:

Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}]

Mathematica graphics

So hey! Why not giving the same structure to A:

A = {1 -> 2, 1 -> 3, 2 -> 1, 2 -> 4, 3 -> 2, 4 -> 1, 4 -> 2}

Mathematica graphics

Regarding the Import part (like Kuba suggested):

mat = First@ToExpression@Import["", "Data"]
Graph[DirectedEdge @@@ mat]
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.