bitwalker / libgraph

A graph data structure library for Elixir projects
MIT License
524 stars 75 forks source link

Graph.edges/2 does not work with bidirectional edges between vertices #25

Open ctbucha opened 5 years ago

ctbucha commented 5 years ago

Graph.edges/2 only returns a single direction of edges from one vertex :a to another vertex :b instead of both directions from the vertex :a to the other vertex :b.

iex(1)> g = Graph.new() |> Graph.add_edges([{:a, :b, label: "label1"}, {:a, :b, label: "label2"}, {:b, :a, label: "label3"}]) |> Graph.edges(:a)
[
  %Graph.Edge{label: "label1", v1: :a, v2: :b, weight: 1},
  %Graph.Edge{label: "label2", v1: :a, v2: :b, weight: 1}
]

Expected:

[
  %Graph.Edge{label: "label1", v1: :a, v2: :b, weight: 1},
  %Graph.Edge{label: "label2", v1: :a, v2: :b, weight: 1},
  %Graph.Edge{label: "label3", v1: :b, v2: :a, weight: 1}
]
AndrewDryga commented 5 years ago

There are more places that do not support bidirectional graphs. Here is example:

g = (
Graph.new(type: :undirected) 
|> Graph.add_edge(1, 2, weight: 1) 
|> Graph.add_edge(2, 3, weight: 2) 
|> Graph.add_edge(1, 3, weight: 3)
)

Then when we trying to find path in this graph it works like it’s not undirected:

iex(52)> Graph.a_star(g, 1, 2, fn _v -> 0 end)
[1, 2]
iex(53)> Graph.a_star(g, 2, 1, fn _v -> 0 end)
nil
robindaumann commented 5 years ago

I think the issue @ctbucha describes is about multi graphs, which are not supported. See the feature request #18

robindaumann commented 5 years ago

@AndrewDryga is describing a bug this lib haves with undirected graphs. I will look at it and submit a pr.

sgessa commented 4 years ago

any news about this? i have the same problem @AndrewDryga described