1
$\begingroup$

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

$\endgroup$

closed as off-topic by Szabolcs, m_goldberg, LCarvalho, MarcoB, Alexey Popkov Oct 22 '17 at 11:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Szabolcs, m_goldberg, LCarvalho, MarcoB, Alexey Popkov
If this question can be reworded to fit the rules in the help center, please edit the question.

0
$\begingroup$

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.

$\endgroup$
  • $\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.