I am new to Mathematica. I want to use the software as it allows analysis of mixed graphs (directed and undirected edges).

I have my network as an Excel spreadsheet of three columns: source, target and directed. The names of the nodes are in source and target, and whether the edge between them is directed or not (directed = 0 or 1).

Please could someone explain how to import this data into a mixed graph format in Mathematica?

Thank you, James

  • 4
    $\begingroup$ Welcome to Mathematica.Stackexchange! There isn't going to be a solution like Import["file.xls", "UndirectedDirectedGraph"] if that's what you think. You will have to create the graph after you've imported the data (as a list of lists), so try to do that and if it you can't get it to work please ask a question with details about what you have tried and include sample data. $\endgroup$
    – C. E.
    Apr 19, 2015 at 18:40
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Apr 19, 2015 at 18:52

1 Answer 1


Assuming the file datagr.xls contains your data, import it using

importeddata = Import["datagr.xls"][[1]];

and construct the edge list using Apply (@@@)

edgelist = {DirectedEdge, UndirectedEdge}[[1 + #3]][#, #2] & @@@ importeddata


edgelist = If[#3==0, DirectedEdge[#,#2], UndirectedEdge[#,#2]]& @@@ importeddata 

or Map

edgelist = If[#[[3]] == 0, DirectedEdge[#[[1]], #[[2]]], 
              UndirectedEdge[#[[1]], #[[2]]]] & /@ importeddata

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.