# Generating social network graph from a CSV file

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.

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.

Thanks!

-Brian

• 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. – Sektor Jan 26 '14 at 22:45
• 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. – Szabolcs Jan 26 '14 at 23:05
• 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. – m_goldberg Jan 27 '14 at 0:26
• Also please refer to this post. – Silvia Jan 27 '14 at 20:22

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

SetDirectory@NotebookDirectory[]
data=Import["data.csv"]


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:

## Update

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

color[w_]:=Switch[w,1,Directive[Thick,Red],2,Directive[Thick,Darker@Green],3,Directive[Thick,Blue]];
edgeFormat=(#-> color@PropertyValue[{g, #}, EdgeWeight])&/@EdgeList[g];

,VertexLabels->MapIndexed[#2[[1]]-> Placed[#1,Center]&,data[[1,2;;]]]
,VertexSize->0.27
,VertexStyle->White
,EdgeStyle->edgeFormat
]


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]


• The suggestions are extremely helpful for me - much appreciated. – Brian Jan 27 '14 at 16:34