Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Enable graphs with different Vertex and Edge types #126

Open
zenna opened this issue Nov 20, 2014 · 4 comments
Open

Enable graphs with different Vertex and Edge types #126

zenna opened this issue Nov 20, 2014 · 4 comments

Comments

@zenna
Copy link

zenna commented Nov 20, 2014

In working on a constraint solving package, I wanted to use Graphs.jl to create graphs with different types of Vertices. I attempted to do this using an abstract Vertex type, and vertices are subtypes of that abstract type.

I found myself having to reimplement several methods such as add_vertex! add_edge! with the same body, but slightly different type signature, because they were not designed for this use case. For illustration consider

function add_vertex!{V}(g::GenericGraph{V}, v::V)

Really, I believe I want something like

function add_vertex!{V1, V1 <: V2}(g::GenericGraph{V1}, v::V2)

But this is not valid Julia, so I am not sure how one would achieve this. Or perhaps there is a simple work around?

@milktrader
Copy link

Why can't you just do

function add_vertex!{V1, V2}(g::GenericGraph{V1}, v::V2)

and ensure that V1 and V2 are valid types defined somewhere in the module?

@zenna
Copy link
Author

zenna commented Nov 20, 2014

I could do that. It is is a looser type constraint (V2 could be anything) but the main problem is that I'll still be duplicating all the code that exists in Graphs.jl already.

@milktrader
Copy link

Hmm, this question might be better answered on the julia-users mailing list. https://groups.google.com/forum/#!forum/julia-users

I would try that forum.

@silgon
Copy link

silgon commented Apr 11, 2016

I encounter a similar problem. I tried to create vertex and edges types because I wanted to create some fixed variables to the vertices and edges:

type VertexGraph
    id::UInt32
    S::Array{Float64,1}
    V::Float64
    function VertexGraph(id::Integer)
        new(id, Float64[0], 0)
    end
end

type EdgeGraph{V} 
    id::UInt32
    source::V
    target::V
    Q::Float64
    action::Array{Float64, 1}
    ap::Array{Float64, 1}
    function EdgeGraph(id::Integer, source::V, target::V)
        new(id, source, target, 0, [0], [0])
    end
end

The problem is that since there's not such thing as AbstractEdge, therefore you're not allowed to use add_edge! that it only supports the type Edge and ExEdge. It throws the following error:

ERROR: LoadError: MethodError: `source` has no method matching source(::EdgeGraph{VertexGraph}, ::Graphs.GenericGraph{VertexGraph,EdgeGraph{VertexGraph},Array{VertexGraph,1},Array{EdgeGraph{VertexGra
ph},1},Array{Array{EdgeGraph{VertexGraph},1},1}})
Closest candidates are:
  source{V}(!Matched::Graphs.Edge{V}, ::Graphs.AbstractGraph{V,E})
  source{V}(!Matched::Graphs.ExEdge{V}, ::Graphs.AbstractGraph{V,E})

In the meantime I'm using ExEdge but a better solution would be implementing my own types based on an abstract vertex and abstract edge to implement constructors and some extra analysis inside the constructor of the type.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants