2

I have the following Mathematica code based on the GDB1 instance from the CARP literature:

HighlightGraph[
 Graph[{1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 4, 
   1 \[UndirectedEdge] 7, 1 \[UndirectedEdge] 10, 
   1 \[UndirectedEdge] 12 , 2 \[UndirectedEdge] 3, 
   2 \[UndirectedEdge] 4, 2 \[UndirectedEdge] 9, 
   3 \[UndirectedEdge] 4, 3 \[UndirectedEdge] 5, 
   5 \[UndirectedEdge] 6, 5 \[UndirectedEdge] 11, 
   5 \[UndirectedEdge] 12, 6 \[UndirectedEdge] 7, 
   6 \[UndirectedEdge] 12, 7 \[UndirectedEdge] 8, 
   7 \[UndirectedEdge] 12, 8 \[UndirectedEdge] 10, 
   8 \[UndirectedEdge] 11, 9 \[UndirectedEdge] 10, 
   9 \[UndirectedEdge] 11, 10 \[UndirectedEdge] 11}, 
  EdgeWeight -> {13, 17, 19, 19, 4, 18, 9, 2, 20, 5, 7, 20, 11, 4, 3, 
    8, 18, 3, 10, 16, 14, 12}, EdgeStyle -> Thick, 
  VertexLabels -> Table[i -> Placed[i, Center], {i, 12}], 
  VertexLabelStyle -> Directive[White, Bold, 15], VertexSize -> 0.6, 
  GraphLayout -> {"VertexLayout" -> {"SpringElectricalEmbedding", 
      "EdgeWeighted" -> True}}], {1, Red}]

Which outputs the following weighted graph:

Small weighted graph example

On which I would like to display multiple manually entered directed routes starting and ending at the highlighted vertex 1. These routes can overlap, traversing each edge multiple times, requiring multiple distinct directed arcs between nodes. However, they cannot also be introduced as weighted edges or they will distort the topology of the graph.

I will discern different routes with different coloured edges, so being able to make a single class of different edges between nodes will suffice.

I'm stumped. Any thoughts on how to do this would be fantastic.

EDIT: see post on colouring/formatting of edges here: Weighted graph with multiple different coloured non-weighted paths - styling

  • 1
    does this give something close to what you need: path = {1, 2, 3, 4, 2, 4, 2, 1, 2, 1, 2, 1}; newedges = DirectedEdge @@@ Partition[path, 2, 1]; g2 = SetProperty[ EdgeAdd[g1, newedges], {VertexCoordinates -> GraphEmbedding[g1], VertexStyle -> {1 -> Red}, EdgeStyle -> {Alternatives @@ newedges -> Orange}}]? – kglr Oct 03 '18 at 20:51
  • Bang on. Thank you very much. – Jordan MacLachlan Oct 03 '18 at 21:05
  • Jordan, posted the comment as an answer. – kglr Oct 03 '18 at 21:11
  • Apologies, I believe this fails to stack multiple routes. For example, how could you add two different 'path' and 'newedges' to a single 'g2'? Either by way of passing a g2 to another g3 (which I've tried and doesn't seem to be working) or by manipulating the EdgeAdd method to accept more arguments? – Jordan MacLachlan Oct 03 '18 at 21:37
  • please see the update re multiple routes. – kglr Oct 03 '18 at 21:46
  • Fantastic, thank you very much. – Jordan MacLachlan Oct 03 '18 at 21:47

1 Answers1

4
g1 = Graph[{1 <-> 2, 1 <-> 4, 1 <-> 7, 1 <-> 10, 1 <-> 12 , 2 <-> 3, 
       2 <-> 4, 2 <-> 9, 3 <-> 4, 3 <-> 5, 5 <-> 6, 5 <-> 11,  5 <-> 12, 6 <-> 7, 
       6 <-> 12, 7 <-> 8,  7 <-> 12, 8 <-> 10, 8 <-> 11, 9 <-> 10,  9 <-> 11, 10 <-> 11}, 
     EdgeWeight -> {13, 17, 19, 19, 4, 18, 9, 2, 20, 5, 7, 20, 11, 4,  3, 
         8, 18, 3, 10, 16, 14, 12}, EdgeStyle -> Thick, 
     VertexLabels -> Table[i -> Placed[i, Center], {i, 12}], 
     VertexLabelStyle -> Directive[White, Bold, 15], 
     VertexSize -> 1, 
     GraphLayout -> {"VertexLayout" -> {"SpringElectricalEmbedding", 
             "EdgeWeighted" -> True}}] ;

path = {1, 2, 3, 4, 2, 4, 2, 1, 2, 1, 2, 1};
newedges = DirectedEdge @@@ Partition[path, 2, 1];
g2 = SetProperty[EdgeAdd[g1, newedges], {VertexCoordinates -> GraphEmbedding[g1], 
   VertexStyle -> {1 -> Red}, 
   EdgeStyle -> {Alternatives @@ newedges -> Orange}}] 

enter image description here

Update: for multiple paths

path1 = {1, 2, 3, 4, 2, 4, 2, 1, 2, 1, 2, 1};
path2 = {1, 7, 8, 7, 8, 7, 8, 10, 8, 10, 1, 10, 1, 10, 1};
newedges1 = DirectedEdge @@@ Partition[path1, 2, 1];
newedges2 = DirectedEdge @@@ Partition[path2, 2, 1]; 
g3 = SetProperty[EdgeAdd[g1, Join[newedges1, newedges2]], 
 {VertexCoordinates -> GraphEmbedding[g1], VertexStyle -> {1 -> Red}, 
 EdgeStyle -> {Alternatives @@ newedges1 -> Orange, Alternatives @@ newedges2 -> Green}}] 

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Apologies, another sub-question. With the (original) implementation above, I'm having the same question as the link below, where arcs between the same nodes are being considered the same and therefore are being formatted the same. i.e. the latter format definition is overwriting the prior. Ideas as to how to avoid this?

    https://mathematica.stackexchange.com/questions/91947/how-to-specify-different-colors-for-edges-going-between-the-same-two-vertices-in

    – Jordan MacLachlan Oct 03 '18 at 22:38
  • 1
    @JordanMacLachlan, that's a challenging issue with multiedges. See Graph: Coloring parallel edges individually. You might find Szabolcs's IGraph/M useful: How can I conveniently call igraph from Mathematica?. – kglr Oct 03 '18 at 22:40
  • 1
    Thanks mate - appreciate it. After no success myself I've asked another question about this here for future reference: https://mathematica.stackexchange.com/questions/183086/weighted-graph-with-multiple-different-coloured-non-weighted-paths-styling – Jordan MacLachlan Oct 04 '18 at 00:38