Message Boards Message Boards

0
|
2538 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Adding another weighted directed edge to a graph

I am trying to figure out how to add a weighted directed edge to a graph, but am not having any success.
Say I have a graph defined as:

g1 = ; g1 = Graph[{2 \[DirectedEdge] 1, 3 \[DirectedEdge] 2, 
    4 \[DirectedEdge] 3, 4 \[DirectedEdge] 1},
   EdgeWeight -> {2 \[DirectedEdge] 1 -> 2 + 5 I, 
     3 \[DirectedEdge] 2 -> 4 - 6 I, 4 \[DirectedEdge] 3 -> 1 + 8 I, 
     4 \[DirectedEdge] 1 -> 7 + 7 I}
   ];

There are 4 vertices (1, 2,3 and 4). With edges between 4->1, 4->3, 3->2, 2->1. The 4 edges have weights (Here I have complex numbers for the weights).
I would like to know how I can add another weighted, directed edge between vertices 3 and 1 and also specify the weight of this new directed edge.
I tried:

g1 = EdgeAdd[g1, DirectedEdge[3, 1, 5 + i]]

to add a new directed, weighted edge from Vertex 3 to Vertex 1, with a weight of 5 + 1 (a complex number) but to no avail.
Can someone provide a way to add new directed, weighted edges to a partially built graph as new directed edges are found when performing some computations?

Attachments:
POSTED BY: Henrick Jeanty
2 Replies

Thank you Rohit. I did try to properly format my code but somehow couldn't get my code to show up properly, I attached the notebook to my post so that you could better see what I was doing,

Your solution helped me find the way to do what I wanted to do.

And again, I apologize for my lack of sophistication in using this very useful help system.

Regards, Henrick Jeanty

POSTED BY: Henrick Jeanty
Posted 2 years ago

Henrick,

Please learn how to format code correctly in your questions. Read this.

The third argument to DirectedEdge is a tag, not a weight, check the documentation. Use Annotate.

g2 = Annotate[{g2, DirectedEdge[3, 1]}, EdgeWeight -> 5 + I]

GraphPlot[g2,
 VertexLabels -> "Name",
 EdgeLabels -> "EdgeWeight"]

enter image description here

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract