I am using Mathematica Version 8.0

I've tried variations of the following commands, in order to assign the weight $1/k$ for vertex $k$, for a graph defined on {1,2,..,n} via the edge rule $$(k,k') \in E~~\textrm{if and only if}~~LCM(k,k')>n$$ My goal is to calculate the maximal independent set in this graph with respect to these vertex weights. There are no weights assigned at all. Here's the code I used.

f[n_, i_, j_] := If[ (LCM[i, j] <= n), 1, 0];

MM[n_] := 
 Table[Table[1 - f[n, i, j], {i, n}], {j,n}];

MM20 := MM[20]; G := AdjacencyGraph[Table[x, {x, 20}], MM20]; 

Gw :=
  Graph[G, VertexWeight -> Table[1/x, {x, 1, 20}]];

I have tried not including the list of vertices in the MM20 definition, including the weights, all in one go, to no avail. If I ask

PropertyValue[{Gw, 3}, VertexWeight]

of mathematica, it just echoes the command back with no output and a graphical representation of the graph.

I don't want to enter the edges and weights explicitly (as 1<-> 2, etc) since I want to run this for much bigger graphs. There must be something subtly wrong about how I am using the Table command, maybe?

Edit: Here's a screenshot of the problematic output

  • $\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) Take the tour! 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
    Nov 9, 2015 at 0:08
  • $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful $\endgroup$
    – Michael E2
    Nov 9, 2015 at 0:08
  • $\begingroup$ @MichaelE2 thanks, I have addressed the formatting issues, looks much better. $\endgroup$
    – kodlu
    Nov 9, 2015 at 0:16
  • 1
    $\begingroup$ The PropertyValue expression you mention returns 1/3 when I execute it after evaluating the rest of your code. Is that not what you would like? (and thank you for formatting the question properly!) $\endgroup$
    – MarcoB
    Nov 9, 2015 at 0:19
  • $\begingroup$ @MarcoB, yes, that's what I would like, but what I was getting was PropertyValue[{Graph[plot, VertexWeight -> {1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/ 11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20}], 3}, VertexWeight] ,so this is quite strange. I'll try again and see what happens, maybe in a new notebook. PS: I have said plot where a graphical rendering of the whole graph was output. $\endgroup$
    – kodlu
    Nov 9, 2015 at 0:29

1 Answer 1


Here's a slight refactoring of the OP's code, but the original code worked just as well. In particular I used Set (=) instead of SetDelayed (:=) for the graphs so they do not have to be recomputed each time they are used.

mm[n_] := SparseArray[{k_, kk_} /; LCM[k, kk] > n -> 1, {n, n}];

g = AdjacencyGraph[Range[20], mm[20]];

gw = Graph[g, VertexWeight -> 1/Range[20]]

PropertyValue[{gw, 3}, VertexWeight]

(*  1/3  *)

Mathematica graphics

Note that the graph in the OP's screen shot is wrong. My guess is that G has been redefined, or failed to be defined. When I execute the OP's code, I get this for Gw, the same as my gw:

Mathematica graphics

  • $\begingroup$ Michael E2: thanks. I have quit the Kernel, restarted, ran your code in a fresh notebook, and I still get the "wrong" graph and not yours! Quite curious. My version is 8.0 which is the one my department has. $\endgroup$
    – kodlu
    Nov 9, 2015 at 4:36
  • $\begingroup$ @kodlu I may have been hasty in calling it "wrong." It could be that V8 has a different layout algorithm. So while it might look different, it could be the right graph. Unfortunately, I no longer have access to V8 to check. I would encourage you to put the V8 info in the question itself. Perhaps someone with V8, or knowledge of the difference, might help you. $\endgroup$
    – Michael E2
    Nov 9, 2015 at 4:40

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.