Skip to content

Commit

Permalink
allow floats as weights
Browse files Browse the repository at this point in the history
  • Loading branch information
georgewhewell committed Mar 26, 2018
1 parent dadfe90 commit 548f314
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/edge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Graph.Edge do
label = Keyword.get(opts, :label)
weight = Keyword.get(opts, :weight, 1)
case {label, weight} do
{_, w} = meta when is_integer(w) ->
{_, w} = meta when is_integer(w) or is_float(w) ->
meta
{_, _} ->
raise ArgumentError, message: "invalid value for :weight, must be an integer"
Expand Down
6 changes: 3 additions & 3 deletions lib/priority_queue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ defmodule PriorityQueue do
{:value, :bar}
"""
@spec push(t, term, integer) :: t
def push(%__MODULE__{priorities: {size, _} = tree} = pq, term, priority) when is_integer(priority) and size > 0 do
def push(%__MODULE__{priorities: {size, _} = tree} = pq, term, priority) when (is_integer(priority) or is_float(priority)) and size > 0 do
case :gb_trees.lookup(priority, tree) do
:none ->
%__MODULE__{pq | priorities: :gb_trees.insert(priority, :queue.in(term, :queue.new), tree)}
{:value, q} ->
%__MODULE__{pq | priorities: :gb_trees.update(priority, :queue.in(term, q), tree)}
end
end
def push(%__MODULE__{priorities: {0, _} = tree} = pq, term, priority) when is_integer(priority) do
def push(%__MODULE__{priorities: {0, _} = tree} = pq, term, priority) when is_integer(priority) or is_float(priority) do
%__MODULE__{pq | priorities: :gb_trees.insert(priority, :queue.in(term, :queue.new), tree)}
end
def push(%__MODULE__{priorities: nil} = pq, term, priority) when is_integer(priority) do
def push(%__MODULE__{priorities: nil} = pq, term, priority) when is_integer(priority) or is_float(priority) do
%__MODULE__{pq | priorities: :gb_trees.insert(priority, :queue.in(term, :queue.new), :gb_trees.empty)}
end

Expand Down
20 changes: 19 additions & 1 deletion test/graph_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ defmodule GraphTest do
assert "#Graph<type: directed, num_vertices: 151, num_edges: 150>" = str
end

test "graph with float weights" do
g = build_floatweight_graph()
assert %{type: :directed} = Graph.info(g)
end

test "get info about graph" do
g = build_basic_cyclic_graph()

assert %{type: :directed, num_vertices: 5, num_edges: 7} = Graph.info(g)
end

Expand Down Expand Up @@ -143,6 +147,12 @@ defmodule GraphTest do
assert ^shortest_len = length(shortest_g)
end

test "shortest path for floatweight graph" do
g = build_floatweight_graph()
shortest_g = Graph.dijkstra(g, :a, :d)
assert shortest_g == [:a, :b, :c, :d]
end

test "shortest path for complex graph" do
g = build_complex_graph()

Expand Down Expand Up @@ -287,6 +297,14 @@ defmodule GraphTest do
assert 20 = Graph.degeneracy(g)
end

defp build_floatweight_graph do
Graph.new
|> Graph.add_edge(:a, :b, weight: 0.1)
|> Graph.add_edge(:b, :c, weight: 0.1)
|> Graph.add_edge(:c, :d, weight: 0.1)
|> Graph.add_edge(:a, :d, weight: 0.3000001)
end

defp build_basic_cyclic_graph do
Graph.new
|> Graph.add_vertex(:a)
Expand Down

0 comments on commit 548f314

Please sign in to comment.