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

fix solve in exp time with c orrectness output result to satisfy all 2 #28

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
61 changes: 9 additions & 52 deletions src/core/all_local_minimum_vertex_splitters_solver.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,59 +41,16 @@ solve_in_exp_time_with_correctness(ConnectedDAG) ->
<- my_lists:power(
digraph:vertices(ConnectedDAG)),
dependency_digraph:is_vertex_splitter(ConnectedDAG, Vertices)],
Candidates2 =
begin
MinimumVertexSplittersForEveryExitVertex =
lists:foldl(fun(VertexSplitter, Acc) ->
lists:foldl(fun(ExitVertex, Acc2) ->
maps:update_with(ExitVertex,
fun(VertexSplitters) ->
case
compare(length(VertexSplitter),
length(hd(VertexSplitters)))
of
less -> [VertexSplitter];
greater -> VertexSplitters;
equal ->
[VertexSplitter
| VertexSplitters]
end
end,
Acc2)
end,
Acc,
[V
|| V <- VertexSplitter,
is_exit_vertex(ConnectedDAG, V)])
end,
maps:from_keys(exit_vertices(ConnectedDAG),
[digraph:vertices(ConnectedDAG)]),
Candidates),
lists:usort(
lists:map(fun lists:sort/1,
my_lists:flatten(
maps:values(MinimumVertexSplittersForEveryExitVertex))))
end,
[S1
|| S1 <- Candidates2,
not
lists:any(fun(S2) ->
sets:is_subset(
sets:from_list(S2), sets:from_list(S1))
end,
lists:delete(S1, Candidates2))].
lists:filter(fun(Candidate) ->
not
lists:any(fun(Vertices) ->
dependency_digraph:is_vertex_splitter(ConnectedDAG,
Candidate -- Vertices)
end,
lists:delete([], my_lists:power(Candidate)))
end,
Candidates).

%% ===================================================================
%% Private API
%% ===================================================================
-spec compare(term(), term()) -> less | greater | equal.
compare(X, Y) when X < Y -> less;
compare(X, Y) when X > Y -> greater;
compare(_, _) -> equal.

-spec exit_vertices(digraph:graph()) -> [digraph:vertex()].
exit_vertices(Digraph) ->
[V || V <- digraph:vertices(Digraph), is_exit_vertex(Digraph, V)].

-spec is_exit_vertex(digraph:graph(), digraph:vertex()) -> boolean().
is_exit_vertex(Digraph, Vertex) -> digraph:out_degree(Digraph, Vertex) =:= 0.
6 changes: 1 addition & 5 deletions src/core/my_lists.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(my_lists).

-export([shuffle/1, sublist_randomly/1, power/1, flatten/1]).
-export([shuffle/1, sublist_randomly/1, power/1]).

-spec shuffle(list()) -> list().
shuffle(List) -> [X || {_, X} <- lists:sort([{rand:uniform(), Y} || Y <- List])].
Expand All @@ -16,7 +16,3 @@ power(List) -> power_([], List, []).
power_(Subset, [], Family) -> [Subset | Family];
power_(Subset, [Head | Rest], Family) ->
power_(Subset, Rest, power_([Head | Subset], Rest, Family)).

% NOTE: Unlike `lists:flatten/1`, this function removes only one level of the nested list.
-spec flatten([list()]) -> list().
flatten(ListList) -> [X || List <- ListList, X <- List].
33 changes: 32 additions & 1 deletion test/all_local_minimum_vertex_splitters_solver_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,36 @@ solve_in_exp_time_with_correctness_test_() ->
lists:map(fun lists:sort/1, [[2, 3], [5], [8]])),
lists:sort(
lists:map(fun lists:sort/1,
all_local_minimum_vertex_splitters_solver:solve_in_exp_time_with_correctness(G5))))
all_local_minimum_vertex_splitters_solver:solve_in_exp_time_with_correctness(G5)))),
G6 = my_digraph:create(
lists:seq(1, 16),
[{1, 2},
{2, 3},
{3, 4},
{3, 5},
{6, 5},
{7, 4},
{7, 8},
{9, 10},
{10, 8},
{10, 11},
{11, 12},
{11, 13},
{14, 2},
{14, 15},
{15, 12},
{16, 13},
{16, 15}]),
?assertEqual(lists:sort(
lists:map(fun lists:sort/1,
[[4, 8],
[4, 12, 13],
[4, 12, 15],
[5],
[8, 12, 13],
[8, 12, 15],
[12, 13, 15]])),
lists:sort(
lists:map(fun lists:sort/1,
all_local_minimum_vertex_splitters_solver:solve_in_exp_time_with_correctness(G6))))
end}]}.
14 changes: 0 additions & 14 deletions test/prop_my_lists.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,3 @@ prop_power2() ->
?SUCHTHAT(L, list(random_type()), length(L) =< 15),
lists:all(fun(List2) -> lists:sort(List2 ++ List -- List2) =:= lists:sort(List) end,
my_lists:power(List))).

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% my_lists:flatten/1 %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
% NOTE:
% It is easy to give a different implementation,
% so I test by "Wording the specification differently".
prop_flatten1(doc) -> "Wording the specification differently".

prop_flatten1() ->
?FORALL(ListList,
list(list(random_type())),
lists:foldl(fun(List, Acc) -> List ++ Acc end, [], lists:reverse(ListList))
=:= my_lists:flatten(ListList)).
16 changes: 14 additions & 2 deletions test/prop_optimum_supervision_tree_solver.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% optimum_supervision_tree_solver:solve/1 %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
prop_solve(doc) ->
prop_solve1(doc) ->
"When a process is restarted, the processes that depend on it are also restarted".

prop_solve() ->
prop_solve1() ->
?FORALL(Graph,
dependency_graph_generator:dependency_graph(),
begin
Expand Down Expand Up @@ -48,3 +48,15 @@ take_restart_processes({_, Children}) ->
lists:flatten(
lists:map(fun take_restart_processes/1, Children));
take_restart_processes(Child) -> Child.

prop_solve2(doc) -> "About cost, exp time & correct ≤ polynomial time & incorrect".

prop_solve2() ->
?FORALL(Graph,
dependency_graph_generator:dependency_graph(),
supervision_tree:calc_cost(
optimum_supervision_tree_solver:solve(Graph,
fun all_local_minimum_vertex_splitters_solver:solve_in_exp_time_with_correctness/1))
=< supervision_tree:calc_cost(
optimum_supervision_tree_solver:solve(Graph,
fun all_local_minimum_vertex_splitters_solver:solve_in_polynomial_time_without_correctness/1))).