How do I add vertex weights?

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.

Dear Shawn,

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

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:

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.

I get the graph and False with the same code.

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

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.

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.

That is strange. I cannot reproduce your problem.

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

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

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