Prolog - 需要帮助优化我的代码

时间:2017-10-08 20:52:50

标签: database search optimization prolog find

我刚刚开始学习Prolog,我正在做一项学校任务。我已经编写了该程序,并且已经通过"所有被抛出的测试案例,但程序"测试器"仍然没有让我失望,因为超过了时间限制。换句话说,我的程序太慢了。

仅供上下文:在此任务中,我获得了一个"数据库"人们还指定了这些人中的哪些人是朋友"与对方AKA互相认识。在这些人中有蜘蛛"和#34;阴谋家"。他们有以下几点 特性:

  • 网络中的蜘蛛是"朋友"所有的阴谋者(但也许还有那些不是的人)

  • 所有同谋者都不是任何其他同谋的朋友。

  • 所有其他既不是同谋者也不是蜘蛛的人都不认识任何共谋者。

我的任务是识别类似网络的网络(数据库)中的蜘蛛。

我的代码:

% Test with all persons.
spider(X):-
  % Takes the next person from the example
  person(X),
  findSpider(X).

findSpider(X):-
  % Finds this persons Friends
  allFriends(X,Friends),
  % Returns list with friends that don't know each other AKA Possible Conspirators.
  getPConspirators(Friends, [], PossibleConspirators),
  % Checks if all persons are friends with some(any) conspirator. If True then X is a spider.
  isDefiniteSpider(PossibleConspirators),
  !.


allFriends(X, Friends):-
  % Bagof fails if no Friends exist.
  bagof(Friend, friend(X,Friend), Friends).

% Checks if friendship goes both ways.
friend(A,B) :- knows(A,B) ; knows(B,A).


% Check if P knows anyone in the list. True or false.
hasFriend(P, Lst):-
  not(hasFriendHelp(P, Lst)).

% Return true if P doesn't know anybody in the list.
hasFriendHelp(_, []).

hasFriendHelp(P, [H|T]):-
  not(friend(P, H)),
  hasFriendHelp(P, T).


% Takes the spiders friends and returns a list with persons who don't know each other.
getPConspirators([], Res, Res).

getPConspirators([H|T], Conspirators, Res):-
  not(hasFriend(H, Conspirators)), % H has no friend among the conspirators.
  getPConspirators(T, [H|Conspirators], Res). % Move on.

getPConspirators([_|T], Conspirators, Res) :-
  getPConspirators(T, Conspirators, Res).


% Checks if all persons know anybody in the list PossibleConspirators.
% If this is the case return True, otherwise False.
isDefiniteSpider(PossibleConspirators):-
  forall(person(Y),
  (member(Y,PossibleConspirators);hasFriend(Y,PossibleConspirators))).

给定数据的示例:

person(0).
person(1).
person(2).
person(3).
person(4).
person(5).
person(6).
person(7).
person(8).
person(9).
person(10).
person(11).
person(12).
person(13).
person(14).
person(15).
person(16).
person(17).
person(18).
person(19).
person(20).
person(21).
person(22).
person(23).
person(24).
knows(0,1).
knows(0,2).
knows(0,3).
knows(0,4).
knows(0,5).
knows(0,6).
knows(0,7).
knows(0,8).
knows(0,9).
knows(1,2).
knows(1,3).
knows(1,4).
knows(1,5).
knows(1,6).
knows(1,7).
knows(1,8).
knows(1,9).
knows(1,10).
knows(2,3).
knows(2,4).
knows(2,5).
knows(2,6).
knows(2,7).
knows(2,8).
knows(2,9).
knows(2,10).
knows(2,11).
knows(3,4).
knows(3,5).
knows(3,6).
knows(3,7).
knows(3,8).
knows(3,9).
knows(3,10).
knows(3,11).
knows(3,12).
knows(4,5).
knows(4,6).
knows(4,7).
knows(4,8).
knows(4,9).
knows(4,10).
knows(4,11).
knows(4,12).
knows(4,13).
knows(5,6).
knows(5,7).
knows(5,8).
knows(5,9).
knows(5,10).
knows(5,11).
knows(5,12).
knows(5,13).
knows(5,14).
knows(6,7).
knows(6,8).
knows(6,9).
knows(6,10).
knows(6,11).
knows(6,12).
knows(6,13).
knows(6,14).
knows(6,15).
knows(7,8).
knows(7,9).
knows(7,10).
knows(7,11).
knows(7,12).
knows(7,13).
knows(7,14).
knows(7,15).
knows(7,16).
knows(8,9).
knows(8,10).
knows(8,11).
knows(8,12).
knows(8,13).
knows(8,14).
knows(8,15).
knows(8,16).
knows(8,17).
knows(9,10).
knows(9,11).
knows(9,12).
knows(9,13).
knows(9,14).
knows(9,15).
knows(9,16).
knows(9,17).
knows(9,18).
knows(10,11).
knows(10,12).
knows(10,13).
knows(10,14).
knows(10,15).
knows(10,16).
knows(10,17).
knows(10,18).
knows(10,19).
knows(11,12).
knows(11,13).
knows(11,14).
knows(11,15).
knows(11,16).
knows(11,17).
knows(11,18).
knows(11,19).
knows(11,20).
knows(12,13).
knows(12,14).
knows(12,15).
knows(12,16).
knows(12,17).
knows(12,18).
knows(12,19).
knows(12,20).
knows(12,21).
knows(13,14).
knows(13,15).
knows(13,16).
knows(13,17).
knows(13,18).
knows(13,19).
knows(13,20).
knows(13,21).
knows(13,22).
knows(14,15).
knows(14,16).
knows(14,17).
knows(14,18).
knows(14,19).
knows(14,20).
knows(14,21).
knows(14,22).
knows(14,23).
knows(15,16).
knows(15,17).
knows(15,18).
knows(15,19).
knows(15,20).
knows(15,21).
knows(15,22).
knows(15,23).
knows(15,24).
knows(16,17).
knows(16,18).
knows(16,19).
knows(16,20).
knows(16,21).
knows(16,22).
knows(16,23).
knows(16,24).
knows(16,0).
knows(17,18).
knows(17,19).
knows(17,20).
knows(17,21).
knows(17,22).
knows(17,23).
knows(17,24).
knows(17,0).
knows(17,1).
knows(18,19).
knows(18,20).
knows(18,21).
knows(18,22).
knows(18,23).
knows(18,24).
knows(18,0).
knows(18,1).
knows(18,2).
knows(19,20).
knows(19,21).
knows(19,22).
knows(19,23).
knows(19,24).
knows(19,0).
knows(19,1).
knows(19,2).
knows(19,3).
knows(20,21).
knows(20,22).
knows(20,23).
knows(20,24).
knows(20,0).
knows(20,1).
knows(20,2).
knows(20,3).
knows(20,4).
knows(21,22).
knows(21,23).
knows(21,24).
knows(21,0).
knows(21,1).
knows(21,2).
knows(21,3).
knows(21,4).
knows(21,5).
knows(22,23).
knows(22,24).
knows(22,0).
knows(22,1).
knows(22,2).
knows(22,3).
knows(22,4).
knows(22,5).
knows(22,6).
knows(23,24).
knows(23,0).
knows(23,1).
knows(23,2).
knows(23,3).
knows(23,4).
knows(23,5).
knows(23,6).
knows(23,7).
knows(24,0).
knows(24,1).
knows(24,2).
knows(24,3).
knows(24,4).
knows(24,5).
knows(24,6).
knows(24,7).
knows(24,8).

我不知道我的哪个功能是"慢一个"。也许有多个?我该如何加快速度?有小费吗?非常感谢。

0 个答案:

没有答案