M2 icon indicating copy to clipboard operation
M2 copied to clipboard

Use of == in Graphs

Open pzinn opened this issue 3 years ago • 2 comments

Example of a problem with the Graphs package:

i1 : needsPackage "Graphs"

o1 = Graphs

o1 : Package

i2 : G=graph{{a,1}};

i3 : H=graph{{b,2}};

i4 : cartesianProduct(G,H)
stdio:4:1:(3): error: no method for binary operator == applied to objects:
--            1 (of class ZZ)
--     ==     a (of class Symbol)

The issue is, cartesianProduct tries to compare vertex labels with ==. This is problematic since vertex labels can be anything (in the help, both symbols like a,b,c and numbers 1,2,3 are used). I suspect === should be used. There are I believe quite a few other functions defined in Graphs that suffer from the same problem.

pzinn avatar Oct 21 '21 02:10 pzinn

This may be because the vertex set is build using

    V := toList(set vertexSet G ** set vertexSet H);

Could someone propose another way to build the cartesian product of two vertexSet ?

fchapoton avatar Aug 22 '22 13:08 fchapoton

You could use V := vertexSet G ** vertexSet H.

By the way, it's strange that vertexSet returns a list, not a set.

Also, the illegal use of == is later in the function:

cartesianProduct(Graph, Graph) := Graph => (G, H) -> (
    V := toList(set vertexSet G ** set vertexSet H);
    E := flatten for u in V list for v in V list
        if (u_0 == v_0 and member(set {u_1, v_1}, edges H))
        or (u_1 == v_1 and member(set {u_0, v_0}, edges G))
        then {u, v} else continue;
    graph(V, E, EntryMode => "edges")
    )

DanGrayson avatar Aug 22 '22 13:08 DanGrayson