Question: Trying to convert a Digraph to undirected Graph while keeping weights both ways

Here is what I'm trying to do. Say I have a Digraph G1 defined by:

with(GraphTheory):
G1:=Digraph([a,b,c],{[[a,b],2],[[b,c],3],[[c,a],4]});

I would like to produce the undirected graph G2, with the same weights:

G2:=Graph([a,b,c],{[{a,b},2],[{b,c},3],[{c,a},4]});

After looking in the GraphTheory package, I found UnderlyingGraph, which seems to do what I want.

Namely,

G3:=UnderlyingGraph(G1,weighted=true);

I had a first problem: there is a bug in the documentation, as the option is 'weights' in the documentation, whereas the source code shows it must be 'weighted'.

 

But then I had another problem, but maybe I didn't understand the purpose of UnderlyingGraph: apparently, I don't get G2. For instance:

 

DijkstrasAlgorithm(G1,a);
            [[[a], 0], [[a, b], 2], [[a, b, c], 5]]

DijkstrasAlgorithm(G2,a);
              [[[a], 0], [[a, b], 2], [[a, c], 4]]

DijkstrasAlgorithm(G3,a);
            [[[a], 0], [[a, c, b], 0], [[a, c], 0]]

The problem seems to come from the weight matrix, which is not symmetric (it is for G2):

WeightMatrix(G3);
                           [0  2  0]
                           [       ]
                           [0  0  3]
                           [       ]
                           [4  0  0]

Edges(G3,weights=true);

{[{a, b}, 0], [{a, b}, 2], [{a, c}, 0], [{a, c}, 4], [{b, c}, 0], [{b, c}, 3]}

 

However, G3 is undirected:

IsDirected(G3);

          false

So, the graph is undirected, but it has different weights for a-b and b-a. Weird.

Now, I am wondering what UnderlyingGraph is supposed to return. After looking at the source code, it seems the statement EW := EW0 + LinearAlgebra:-Transpose(EW0) builds a symmetric weight matrix, but for some reason it's not what is returned.

Is this a bug in the function? Or did I do something wrong? Is there a better way to achieve what I wanted?

 

Please Wait...