# How do I add vertex weights?

**URL:** <https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516>\
**Category:** Questions\
**Tags:** mathematica, discrete-mathematics, graphs-and-networks\
**Created:** [July 1, 2014, 4:07pm UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516 "2014-07-01T16:07:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdoyle](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sdoyle](https://community.wolfram.com/u/sdoyle)\
**Post date:** [July 1, 2014, 4:07pm UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/1 "2014-07-01T16:07:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [July 1, 2014, 9:09pm UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/2 "2014-07-01T21:09:40Z")

</div>

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](http://en.wikipedia.org/wiki/Graph_%28mathematics%29#Weighted_graph)

If you want the edges to be weighted you need this:  
[http://reference.wolfram.com/mathematica/ref/EdgeWeight.html](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:

 ![Graph1.jpg](https://global.discourse-cdn.com/wolfram/original/3X/e/3/e32094477035928f74b8579b25c84f5f51c6519e.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![sdoyle](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sdoyle](https://community.wolfram.com/u/sdoyle)\
**Post date:** [July 1, 2014, 9:35pm UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/3 "2014-07-01T21:35:05Z")

</div>

I get the graph and False with the same code.

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [July 1, 2014, 10:11pm UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/4 "2014-07-01T22:11:33Z")

</div>

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

 ![ScreenShot2014-07-01at23.14.34.png](https://global.discourse-cdn.com/wolfram/original/2X/e/ede11ae62422a840dfeb5f372be6619d1b0b6a29.png)

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.

---

<div class="post-metadata">

**Author:** ![sdoyle](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sdoyle](https://community.wolfram.com/u/sdoyle)\
**Post date:** [July 1, 2014, 10:28pm UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/5 "2014-07-01T22:28:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mthiel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mthiel/32/42877_2.png) [@mthiel](https://community.wolfram.com/u/mthiel)\
**Post date:** [July 2, 2014, 8:44am UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/6 "2014-07-02T08:44:55Z")

</div>

That is strange. I cannot reproduce your problem.

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

 ![ScreenShot2014-07-02at09.38.34.png](https://global.discourse-cdn.com/wolfram/original/3X/6/7/67f396a56bcd1c506f038c0eb0c0e43301ec1984.png)

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

---

<div class="post-metadata">

**Author:** ![sdoyle](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sdoyle](https://community.wolfram.com/u/sdoyle)\
**Post date:** [July 2, 2014, 11:28am UTC](https://community.wolfram.com/t/how-do-i-add-vertex-weights/2516/7 "2014-07-02T11:28:20Z")

</div>

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