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 a weighted adjacency matrix as follows:

myAdjacencyMatrix = {{0, 3, 1, 3, 3, 8, 0, 0, 3, 4},
  {1, 0, 2, 0, 0, 16, 5, 3, 0, 6, 1},
  {2, 3, 0, 0, 1, 1, 4, 1, 1, 0, 0},
  {5, 3, 3, 0, 5, 0, 2, 2, 2, 2, 1},
  {1, 0, 0, 6, 0, 1, 2, 6, 10, 2, 4},
  {0, 11, 3, 0, 1, 0, 8, 3, 1, 3, 3},
  {2, 4, 1, 7, 6, 7, 0, 6, 0, 8, 2},
  {1, 2, 1, 3, 8, 4, 4, 0, 4, 3, 0},
  {0, 0, 0, 1, 4, 1, 3, 4, 0, 6, 4},
  {0, 3, 0, 0, 0, 5, 2, 2, 6, 0, 4},
  {0, 1, 0, 0, 2, 1, 2, 0, 1, 2, 0}}

I want to draw a graph with 11 nodes and the edges weighted as described above. If this is impossible, then I will settle for making a graph with the non-weighted adjacency matrix.

If you could just give me the simple code as I am new to mathematica and am working on a tight schedule.

share|improve this question
    
WeightedAdjacencyGraph[Range[11], myAdjacencyMatrix] should do it –  Cameron Murray May 19 '13 at 23:05
5  
If you are really working on a tight schedule, I seriously suggest trying with another language. Mathematica has a steep learning curve and isn't appropriate for rush learning –  belisarius May 19 '13 at 23:06
1  
How do you want the weights to modify the drawing of the graph? –  Szabolcs May 19 '13 at 23:07
4  
mmm ... re reading your questions so far, your "tight schedule" seems dangerously near a "homework delivery deadline" –  belisarius May 19 '13 at 23:13
1  
@image_doctor: english.stackexchange.com/a/6226/1635 –  Rahul Jun 1 '13 at 21:10

1 Answer 1

I'm not too good at graphs, but this seems straightforward.

myAdjacencyMatrix =
{{0, 3, 1, 3, 3, 8, 0, 0, 3, 4, 2},
 {1, 0, 2, 0, 0, 16, 5, 3, 0, 6, 1},
 {2, 3, 0, 0, 1, 1, 4, 1, 1, 0, 0},
 {5, 3, 3, 0, 5, 0, 2, 2, 2, 2, 1},
 {1, 0, 0, 6, 0, 1, 2, 6, 10, 2, 4},
 {0, 11, 3, 0, 1, 0, 8, 3, 1, 3, 3},
 {2, 4, 1, 7, 6, 7, 0, 6, 0, 8, 2},
 {1, 2, 1, 3, 8, 4, 4, 0, 4, 3, 0},
 {0, 0, 0, 1, 4, 1, 3, 4, 0, 6, 4},
 {0, 3, 0, 0, 0, 5, 2, 2, 6, 0, 4},
 {0, 1, 0, 0, 2, 1, 2, 0, 1, 2, 0}} /. 0 -> Infinity

(I added an extra 2 to your first row.) Belisarius proposes the 0 -> Infinity to remove 0 weights.

A graph:

g = WeightedAdjacencyGraph[myAdjacencyMatrix, 
     VertexLabels -> "Name",
     EdgeLabels -> "EdgeWeight", 
     EdgeShapeFunction -> f, 
     VertexLabelStyle -> Directive[Red, 18]];

Then an edge function:

f[pts_List, e_] := 
 Block[{s = 0.015, weight = PropertyValue[{g, e}, EdgeWeight]},
  {Arrowheads[{{s, 0.1}, {s, 0.9}}], 
   AbsoluteThickness[weight * 1.5], 
   Arrow[pts]}]

Then draw the graph:

Show[g]

graph

Still looks too messy to be really useful.

By the way, is it correct to make edge rendering function refer to the graph, and the graph function to call the edge rendering function? Seems a bit circular to me...

Update - "Is there anyway to move the nodes?"

I found out that it's straightforward to control the positions of the nodes in advance. You have to create a list of coordinates - in this case 11 are needed - and provide them to the VertexCoordinates option. For example, here is a set of 11 points, arranged in two layers, of four and six points, around a central point:

vertices = 
  Join[
   {{0, 0}},
   Table[{4 Cos[a], 4 Sin[a]}, {a, Pi/4, 2  Pi, Pi/2}],
   Table[{7 Cos[a], 7 Sin[a]}, {a, 0, 5 Pi/3, Pi/3}]
  ];

Then create the graph using the VertexCoordinates -> vertices:

graph moved vertices

share|improve this answer
1  
To get the "simplified" graph do just myAdjacencyMatrix /. 0 -> Infinity –  belisarius May 20 '13 at 12:43
    
@belisarius That works well, thanks! Updated. –  cormullion May 20 '13 at 13:00
    
Brilliant thankyou. Tried to upvote but wasnt allowed. Is there anyway to move the nodes into a customised format? –  Ryan Durrant May 20 '13 at 19:05
    
Right-click gives some alternatives, but it's complicated stuff... –  cormullion May 20 '13 at 19:16
    
@Ryan I don't think you have enough reputation to upvote... –  cormullion May 20 '13 at 19:21

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.