I am new to Mathematica. I have drawn a node graph, which has approximate 600 vertices and specific edge weight (as shown below). There is a vertex label in each vertex. I want to assign vertex label as a vertex value. I can do it by this.

rplcmntRule = VertexLabels /. Options[g]
newVertices = VertexList[g] /. rplcmntRule;
newEdges = EdgeList[g] /. rplcmntRule
g = Graph[newEdges, VertexLabels -> Placed["Name", {1/2, 1/2}]]

But this method also changes the edge weight. I want to change the vertex only, without changing the edge weight.

enter image description here


You can rename vertices using the VertexReplace function. It will preserve edge weights.

Be careful not to accidentally introduce duplicate names. This would change the graph structure and also remove edge weights.

| improve this answer | |
  • $\begingroup$ I tried but after VertexReplace, EdgeWeights were gone. Wondering if I did any mistake. For example :g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, EdgeLabels -> {1 <-> 2 -> 5, 2 <-> 3 -> 7}] g = VertexReplace[g, {1 -> "A", 2 -> "B"}] $\endgroup$ – Salahuddin Aug 15 '17 at 19:39
  • $\begingroup$ @Salahuddin Can you give me a small example where vertex renaming removes edge weights? Your example above doesn't have edge weights at all. $\endgroup$ – Szabolcs Aug 15 '17 at 20:07
  • $\begingroup$ @Salahuddin With a clearer mind in the morning, I think I see what you mean. Those are not edge weights. They are edge labels. They cannot be used in computation. The EdgeWeight property does get correctly preserved (unlike the EdgeLabels property). It would help if you have a complete and correct example in your next question (see sscce.org) $\endgroup$ – Szabolcs Aug 16 '17 at 7:03

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