From c7cee3b80a47a3f71047c30f1b2eccd873d909be Mon Sep 17 00:00:00 2001 From: Renaud Lenne Date: Thu, 16 Dec 2021 00:05:03 +0100 Subject: [PATCH] Fixed some method still using Graph.utils.vertex_id/1 instead of custom vertex identifier --- lib/graph/directed.ex | 6 ++---- lib/graph/pathfinding.ex | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/graph/directed.ex b/lib/graph/directed.ex index 27916d7..4e656df 100644 --- a/lib/graph/directed.ex +++ b/lib/graph/directed.ex @@ -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) @@ -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 diff --git a/lib/graph/pathfinding.ex b/lib/graph/pathfinding.ex index 8905908..e7692e5 100644 --- a/lib/graph/pathfinding.ex +++ b/lib/graph/pathfinding.ex @@ -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) @@ -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 = @@ -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 = @@ -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 [] -> @@ -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) @@ -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)