2
$\begingroup$

I have a graph with 12 nodes. The x and y coordinates are give as

xcord={154.780094065851,    302.747331652902,   348.235746943268,   959.228504576796,   1021.26721027296,   1114.13365936238,   358.208241883160,   181.741156296042,   838.759299985938,   906.502702816486,   662.579285251179,   536.372745335719};

ycord={347.543166449207,    461.536311618993,   261.753095520236,   431.778284592051,   250.920713280060,   432.113192132818,   1044.66583584371,   1044.70069210915,   1135.19457237168,   984.121301679063,   792.153882418914,   708.946190878097};

Also, these are the edges connecting the nodes.

Edges={{1,2},{1,3},{1,8},{1,11},{1,12},{2,3},{2,4},{2,7},{2,8},{2,11},{2,12},{3,4},{3,5},{3,11},{3,12},{4,5},{4,6},{4,10},{4,11},{4,12},{5,6},{5,11},{5,12},{6,10},{6,11},{6,12},{7,8},{7,9},{7,10},{7,11},{7,12},{8,9},{8,11},{8,12},{9,10},{9,11},{9,12},{10,11},{10,12},{11,12}};

Let's say, The weight of the edges are randomly generated.

I want to plot the graph. The nodes should be depicted as circles with the index within it. I also want to place the weight over the edges.

How can I do it?

$\endgroup$
2
$\begingroup$
xy = Transpose[{xcord, ycord}];

SeedRandom[1];
ew = Round[ RandomReal[1, Length@edges], .01];

wam = SparseArray[Thread[edges -> ew], {12, 12}, Infinity];

WeightedAdjacencyGraph[wam, 
 VertexCoordinates -> xy, 
 PlotTheme -> "IndexLabeled", 
 EdgeLabels -> "EdgeWeight", 
 ImageSize -> Large, 
 EdgeShapeFunction -> "CurvedEdge"] 

enter image description here

Update: Add the option

VertexStyle -> Directive[EdgeForm[{Thick, Blue}], LightBlue]

to color the vertices, and the option

EdgeStyle -> Thread[(DirectedEdge @@@ edges) -> 
  Thread[{Thick, Arrowheads[{-Large, Large}], ColorData["Rainbow"] /@ ew}]]

to style edges and add arrow heads at both ends of edges

enter image description here

To render the edges without arrow heads, either (1) use EdgeShapeFunction -> "CurvedArc" or (2) add the option DirectedEdges -> False and replace (DirectedEdge @@@ edges) with (UndirectedEdge @@@ edges) in EdgeStyle -> ...:

enter image description here

$\endgroup$
8
  • $\begingroup$ thanks a lot for your answer. The edges are not directional. we can have either (1) edges with no arrow at both ends or (2) arrows at both ends. Would you please plot both of this: $\endgroup$ – dipak narayanan Jun 7 at 23:28
  • $\begingroup$ Also, I would prefer colourful plot. nodes with same colour. The edges can take different colours. $\endgroup$ – dipak narayanan Jun 7 at 23:31
  • $\begingroup$ @dipaknarayanan, please see the update. $\endgroup$ – kglr Jun 7 at 23:51
  • $\begingroup$ Thanks a lot. This is exactly what I was hoping. $\endgroup$ – dipak narayanan Jun 8 at 0:08
  • $\begingroup$ the EdgeStyle is nor working! Braket error! $\endgroup$ – dipak narayanan Jun 8 at 8:27

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.