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

How to transform directed graph to undirected graph? #291

Open
GeorgeScrivener opened this issue Nov 4, 2019 · 6 comments
Open

How to transform directed graph to undirected graph? #291

GeorgeScrivener opened this issue Nov 4, 2019 · 6 comments

Comments

@GeorgeScrivener
Copy link

I have a directed graph and need to view it as an undirected graph. Is this implemented already or do I need to create my own adaptor?

@jmcomets
Copy link
Collaborator

jmcomets commented Nov 5, 2019

Could you provide some details on what you mean by "viewing it as an undirected graph"?

If you're speaking of an adapter for traversals & other algorithms, then no it isn't implemented.

@GeorgeScrivener
Copy link
Author

Yeah I meant an adaptor so I can pass my directed graph in to algorithms and have them treat it as an undirected graph.
I'm happy to make a PR for this if you think it would be useful? I think that copying Reversed would be a good start.

@jmcomets
Copy link
Collaborator

jmcomets commented Nov 5, 2019

I'd definitely welcome that! Note that there's quite a few traits that you need to implement for most algorithms to work. I agree that copying Reversed is a good start. 👍

@timthelion
Copy link
Contributor

timthelion commented Dec 22, 2019

FYI you can do this very simply right now with an unsafe transmute: https://timothy.hobbs.cz/rust-play/petgraph-internals.html#The-Graph-data-structure

let undirected_tree : Graph<&str, &str, petgraph::Undirected> = unsafe {std::mem::transmute(tree.clone())};

@GeorgeScrivener
Copy link
Author

I'm having a look at this now and I'm unsure whether I should implement IntoNeighborsDirected for the UndirectedAdaptor (proposed name)?
Intuitively I'd say that it shouldn't be implemented as it is always undirected, however it is implemented for Graph whether or not the edges are directed.
@jmcomets what's your view on this?

@GeorgeScrivener
Copy link
Author

PR for this here: #318

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

3 participants