Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/graph/directed.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ defmodule Graph.Directed do
@moduledoc false
@compile {:inline, [in_neighbors: 2, in_neighbors: 3, out_neighbors: 2, out_neighbors: 3]}

import Graph.Utils, only: [vertex_id: 1]

def topsort(%Graph{vertices: vs} = g) do
l = reverse_postorder(g)

Expand Down Expand Up @@ -87,8 +85,8 @@ defmodule Graph.Directed do
end
end

def reachable(%Graph{vertices: vertices} = g, vs) when is_list(vs) do
vs = Enum.map(vs, &vertex_id/1)
def reachable(%Graph{vertices: vertices, vertex_identifier: vertex_identifier} = g, vs) when is_list(vs) do
vs = Enum.map(vs, &vertex_identifier.(&1))
for id <- :lists.append(forest(g, &out_neighbors/3, vs, :first)), do: Map.get(vertices, id)
end

Expand Down
36 changes: 18 additions & 18 deletions lib/graph/pathfinding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Graph.Pathfinding do
@moduledoc """
This module contains implementation code for path finding algorithms used by `libgraph`.
"""
import Graph.Utils, only: [vertex_id: 1, edge_weight: 3]
import Graph.Utils, only: [edge_weight: 3]

@type heuristic_fun :: (Graph.vertex() -> integer)

Expand Down Expand Up @@ -33,12 +33,12 @@ defmodule Graph.Pathfinding do
the edge between it and the current vertex.
"""
@spec a_star(Graph.t(), Graph.vertex(), Graph.vertex(), heuristic_fun) :: [Graph.vertex()] | nil
def a_star(%Graph{type: :directed, vertices: vs, out_edges: oe} = g, a, b, hfun)
def a_star(%Graph{type: :directed, vertices: vs, out_edges: oe, vertex_identifier: vertex_identifier} = g, a, b, hfun)
when is_function(hfun, 1) do
with a_id <- vertex_id(a),
b_id <- vertex_id(b),
with a_id <- vertex_identifier.(a),
b_id <- vertex_identifier.(b),
{:ok, a_out} <- Map.fetch(oe, a_id) do
tree = Graph.new() |> Graph.add_vertex(a_id)
tree = Graph.new(vertex_identifier: vertex_identifier) |> Graph.add_vertex(a_id)
q = PriorityQueue.new()

q =
Expand All @@ -61,12 +61,12 @@ defmodule Graph.Pathfinding do
end
end

def a_star(%Graph{type: :undirected, vertices: vs} = g, a, b, hfun)
def a_star(%Graph{type: :undirected, vertices: vs, vertex_identifier: vertex_identifier} = g, a, b, hfun)
when is_function(hfun, 1) do
a_id = vertex_id(a)
b_id = vertex_id(b)
a_id = vertex_identifier.(a)
b_id = vertex_identifier.(b)
a_all_edges = all_edges(g, a_id)
tree = Graph.new() |> Graph.add_vertex(a_id)
tree = Graph.new(vertex_identifier: vertex_identifier) |> Graph.add_vertex(a_id)
q = PriorityQueue.new()

q =
Expand All @@ -90,9 +90,9 @@ defmodule Graph.Pathfinding do
Returns `nil` if no path can be found.
"""
@spec all(Graph.t(), Graph.vertex(), Graph.vertex()) :: [Graph.vertex()]
def all(%Graph{vertices: vs, out_edges: oe} = g, a, b) do
with a_id <- vertex_id(a),
b_id <- vertex_id(b),
def all(%Graph{vertices: vs, out_edges: oe, vertex_identifier: vertex_identifier} = g, a, b) do
with a_id <- vertex_identifier.(a),
b_id <- vertex_identifier.(b),
{:ok, a_out} <- Map.fetch(oe, a_id) do
case dfs(g, a_out, b_id, [a_id], []) do
[] ->
Expand Down Expand Up @@ -121,18 +121,18 @@ defmodule Graph.Pathfinding do

defp do_bfs(
q,
%Graph{type: :directed, out_edges: oe} = g,
%Graph{type: :directed, out_edges: oe, vertex_identifier: vertex_identifier} = g,
target_id,
%Graph{vertices: vs_tree} = tree,
hfun
) do
case PriorityQueue.pop(q) do
{{:value, {v_id, ^target_id, _}}, _q1} ->
v_id_tree = Graph.Utils.vertex_id(v_id)
v_id_tree = vertex_identifier.(v_id)
construct_path(v_id_tree, tree, [target_id])

{{:value, {v1_id, v2_id, v2_acc_weight}}, q1} ->
v2_id_tree = Graph.Utils.vertex_id(v2_id)
v2_id_tree = vertex_identifier.(v2_id)

if Map.has_key?(vs_tree, v2_id_tree) do
do_bfs(q1, g, target_id, tree, hfun)
Expand Down Expand Up @@ -169,18 +169,18 @@ defmodule Graph.Pathfinding do

defp do_bfs(
q,
%Graph{type: :undirected} = g,
%Graph{type: :undirected, vertex_identifier: vertex_identifier} = g,
target_id,
%Graph{vertices: vs_tree} = tree,
hfun
) do
case PriorityQueue.pop(q) do
{{:value, {v_id, ^target_id, _}}, _q1} ->
v_id_tree = vertex_id(v_id)
v_id_tree = vertex_identifier.(v_id)
construct_path(v_id_tree, tree, [target_id])

{{:value, {v1_id, v2_id, v2_acc_weight}}, q1} ->
v2_id_tree = vertex_id(v2_id)
v2_id_tree = vertex_identifier.(v2_id)

if Map.has_key?(vs_tree, v2_id_tree) do
do_bfs(q1, g, target_id, tree, hfun)
Expand Down