I am new to Mathematica and wanted to generate undirected graph for data sets that show the relationships of organizations with each other. The data is in a square matrix. The cells of the matrix provide information about the relationships between each pair of organization (0 if no relationship exists; 1 if there is some relationship; 2 if the organizations work as partners; 3 if any of the board members serve in the paired organization). There are 81 organizations in the study, so the matrix is 81x81. The data data structure in CSV looks like the following.

example of data structure

I would very much appreciate if anyone would provide me the steps for generating the social network graph for the stated scenario using Mathematica 9.



  • $\begingroup$ Possible starting point here, but you actually ought to provide the code you are working on or at least the information you gathered through your research. $\endgroup$
    – Sektor
    Jan 26, 2014 at 22:45
  • $\begingroup$ Use AdjacencyGraph to convert an adjacency matrix to a Graph object. If you have trouble with some other step, such as reading the data into Mathematica, please make the question specific to that step. $\endgroup$
    – Szabolcs
    Jan 26, 2014 at 23:05
  • 2
    $\begingroup$ As posed this question is not very attractive to those who could probably answer it. It asks them to do too much work for you -- work that you should have done before you posted the question. Your post would be much more attractive if you were to edit it to include: 1) a markdown formated data set that can copied into Mathematica for use as test data; 2) an image showing what you expect the final output to look like; 3) the code you have tried and and explanation of why you find it unsatisfactory. $\endgroup$
    – m_goldberg
    Jan 27, 2014 at 0:26
  • $\begingroup$ Also please refer to this post. $\endgroup$
    – Silvia
    Jan 27, 2014 at 20:22

2 Answers 2


Here is a start point. First, let's import your data


Now data should be something like this:

{{ ,ABC,DIA,ACE,SJ,KARMA,NOVICE},{ABC,0,2,3,1,2,1},{DIA,1,0,3,1,2,1},{ACE,2,1,0,3,1,2},{SJ,2,3,1,0,1,3},{KARMA,1,2,3,2,0,1},{NOVICE,1,1,2,3,2,0}}

Now you can use WeightedAdjacencyGraph as:

g=WeightedAdjacencyGraph[data[[2;;,2;;]]/.(0-> ∞),VertexLabels->MapIndexed[#2[[1]]-> #1&,data[[1,2;;]]]]

to get:

Example Graph


Here a function to color it (as suggested by @ubpdqn), and some additional formatting.

edgeFormat=(#-> color@PropertyValue[{g, #}, EdgeWeight])&/@EdgeList[g];

g=WeightedAdjacencyGraph[data[[2;;,2;;]]/.(0-> ∞)
        ,VertexLabels->MapIndexed[#2[[1]]-> Placed[#1,Center]&,data[[1,2;;]]]
        ,ImagePadding -> 20

enter image description here


If you wanted to color code the relationships (using Murta's g):

Defining edge styles by weight:

es = Join @@ 
    Thread[#1 -> #2] &, {(#[[All, 1]] & /@ 
       SortBy[GatherBy[{#, PropertyValue[{g, #}, EdgeWeight]} & /@ 
          EdgeList[g], #[[2]] &], #[[2]] &]), {Directive[Red, Thick], 
      Directive[Green, Thick], Directive[Blue, Thick]}}];

Here 1->Red,2->Green, 3->Blue. Visualizing:

WeightedAdjacencyGraph[data[[2 ;;, 2 ;;]] /. (0 -> \[Infinity]), 
 VertexLabels -> MapIndexed[#2[[1]] -> #1 &, data[[1, 2 ;;]]], 
 EdgeStyle -> es, ImagePadding -> 20]

enter image description here

  • $\begingroup$ The suggestions are extremely helpful for me - much appreciated. $\endgroup$
    – Brian
    Jan 27, 2014 at 16:34

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.