Message Boards Message Boards

How do I add vertex weights?

Posted 10 years ago

This is an extension of a previous question that I asked on this forum. I have an excel file of vertex weights in the first column and their corresponding vertices in the second. The graph comes out as planned, but it is not a weighted graph, as I would expect from the following code:

data = Import["~/Desktop/OHD1.xlsx"]

Graph[Table[Property[data[[1]][[i, 2]], VertexWeight -> data[[1]][[i, 1]]], {i, 1, Length[data[[1]]]}], 
 Table[data[[2]][[i, 1]] \[UndirectedEdge] data[[2]][[i, 2]], {i, 1, Length[data[[2]]]}]]

How do I assign vertex weights? Any response is appreciated.

Attachments:
POSTED BY: Shawn Doyle
6 Replies

That is very strange. I tried different machines and different versions of Mathematica. I always get True.

enter image description here

Have you tried restarting the kernel/Mathematica?Which version of Mathematica are you running? You should have access to the Wolfram Cloud. Can you try running your code there?

M.

POSTED BY: Marco Thiel

Dear Shawn,

usually a weighted graph is a graph which has weights attached to its edges. See http://en.wikipedia.org/wiki/Graph(mathematics)#Weightedgraph

If you want the edges to be weighted you need this: http://reference.wolfram.com/mathematica/ref/EdgeWeight.html

You can, however, also attach the weights in the way you did to edges. So if you do as you say:

data = Import["~/Desktop/OHD1.xlsx"]

gra = Graph[Table[Property[data[[1]][[i, 2]], VertexWeight -> data[[1]][[i, 1]]], {i, 1, Length[data[[1]]]}], Table[data[[2]][[i, 1]] <-> data[[2]][[i, 2]], {i, 1, Length[data[[2]]]}]]

It looks like this:

enter image description here

If you then test whether you have a weighted graph:

WeightedGraphQ[gra]

you get True, so For Mathematica this is a weighted graph. Why do you think that it is not a weighted graph?

M.

POSTED BY: Marco Thiel
Posted 10 years ago

I get the graph and False with the same code.

POSTED BY: Shawn Doyle
Posted 10 years ago

I'm running Mathematica 9.0, and I am not sure how to import the data into Wolfram Cloud. I've quit the kernel and started again, to no avail.

POSTED BY: Shawn Doyle

That is strange. I cannot reproduce your problem.

If you want to upload files to the cloud there is a button for that:

enter image description here

On the top right there is a button that says "New"; to the right of that you have "an arrow that points up/into the sky/cloud". You can upload files using that.

Cheers,

Marco

POSTED BY: Marco Thiel
Posted 10 years ago

The kernel was evaluating locally. I changed it to global, and now the problem is resolved. Thank you for all of your help!

POSTED BY: Shawn Doyle
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract