1
$\begingroup$

I have imported a spreadsheet from Excel that I want to use as an adjacency matrix. I want to use this matrix as the argument in WeightedAdjacencyGraph, so I have entered infinity symbols in the matrix where there are no edges linking two vertices.

However edges keep appearing in the graph with weight infinity assigned to them. The infinity symbol is imported from Excel as part of the spreadsheet.

If I use the spreadsheet as a list of lists it will construct a graph, but with unwanted edges. When I put the data in MatrixForm, it returns an error saying it is a not a non empty square matrix.

Any ideas where I'm going wrong?

$\endgroup$
  • 2
    $\begingroup$ Have you checked in what format those infinities have been imported? I wouldn't be surprised if Mathematica simply didn't understand them... $\endgroup$ – MarcoB Jun 23 '15 at 0:22
  • $\begingroup$ Expressions wrapped in MatrixForm should not be given to functions as arguments. MatrixForm is wrapper that tells the front-end to do special formatting. Functions that test the heads of their arguments will almost always reject MatrixForm as invalid. $\endgroup$ – m_goldberg Jun 23 '15 at 0:36
3
$\begingroup$

There are a few formatting issues to take care of before you can use the data imported from Excel.

Let's start with a simple adjacency matrix:

WeightedAdjacencyGraph[ {{∞, 1, ∞}, {∞, ∞, 1}, {1, 1, ∞}} ]

which generates the following graph:

original graph

Let's export that adjacency matrix to an Excel file:

Export["adjmat.xlsx", {{∞, 1, ∞}, {∞, ∞, 1}, {1, 1, ∞}}]

Now we are in the same situation you are in, and we can import the data from the Excel file.

import = Import["adjmat.xlsx"][[1]]

(*Out: {{"Infinity", 1., "Infinity"}, {"Infinity", "Infinity", 1.}, {1., 1., "Infinity"}} *)

Notice two things: 1) I selected the first part of the list returned by export, which corresponds to the contents of the first sheet in the Excel file; 2) note that the Infinity values have been imported as strings.

We need to turn those values into Mathematica expressions before we can do anything with them:

converted = ToExpression[import]

(* Out: {{∞, 1., ∞}, {∞, ∞, 1.}, {1., 1., ∞}} *)

Now we can use that expression to generate the graph:

WeightedAdjacencyGraph[converted]

graph from imported

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.