Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indices of node in induced subgraphs #82

Closed
AlexisSchOrange opened this issue Aug 26, 2024 · 2 comments
Closed

Indices of node in induced subgraphs #82

AlexisSchOrange opened this issue Aug 26, 2024 · 2 comments

Comments

@AlexisSchOrange
Copy link

AlexisSchOrange commented Aug 26, 2024

Hello,
I've an issue regarding the function induced_subgraph with metagraphs: in the metadata, the index of the new nodes is not changed correctly.

See the following example:

mg = MetaGraph(
    Graph(),
    Int,
    Dict,
    Dict,
    "mg"
)
add_vertex!(mg, 1, Dict(:name=>"Thierry Henry"))
add_vertex!(mg, 2, Dict(:name=>"Kyky Mbappe"))
add_vertex!(mg, 3, Dict(:name=>"Benjamin Pavard"))
add_edge!(mg, 1, 2, Dict(:type=>"Love"))
add_edge!(mg, 2, 3, Dict(:type=>"Adoration"))
add_edge!(mg, 3, 1, Dict(:type=>"Respect"))

submg = induced_subgraph(mg, [2, 3])[1]
for i in vertices(submg)
    print(submg[i])
end

Will print a KeyError: key 1 not found

Perhaps I'm not using this correctly ? Otherwise, I've been looking in the source code, the _copy_props! is not working correctly ?

Muchas gracias,

@gdalle
Copy link
Member

gdalle commented Aug 26, 2024

Hi!
This comes from a confusion on using a MetaGraph as a dictionary. The syntax mg[i] retrieves the vertex associated to a given label (which can have arbitrary type), not to a given code, or index (which is always an integer). That is why the documentation recommends not using integers as labels as well, because it is very easy to mix up both concepts.
In the subgraph you create, the vertex labels remain [2, 3] but the vertex codes are renumbered to 1:2. More generally, labels are preserved through graph modifications, but codes are not.
To fix your program, here are two options:

julia> for l in labels(submg)
           println(l, " - ", submg[l])
       end
2 - Dict(:name => "Kyky Mbappe")
3 - Dict(:name => "Benjamin Pavard")

julia> for i in vertices(submg)
           println(i, " - ", submg[label_for(submg, i)])
       end
1 - Dict(:name => "Kyky Mbappe")
2 - Dict(:name => "Benjamin Pavard")

@AlexisSchOrange
Copy link
Author

Thanks for the fast reply! It is more clear now. Perhaps I should create me a new package MetaGraphsButOnlyWithLabelThatAreSimpleIntegers.jl ... Have a good day

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

No branches or pull requests

2 participants