所有子集都包含prolog中列表的所有元素

时间:2015-11-24 20:25:18

标签: list prolog subset dcg powerset

我正在尝试使用其所有元素计算给定列表的所有子集的列表,但到目前为止,我只能成功找到两个元素的子集,但这不是解决我问题的正确方法。谁能帮我?我知道这样的问题是通过使用回溯方法解决的,但在Prolog中,我不确定应该如何编写..源代码是这样的:

  subs(_, [], []).
  subs(H, [H1|Tail], [[H,H1]|Ta]):-
       subs(H, Tail, Ta).

  generatesubs([], []).
  generatesubs([H], [H]).
  generatesubs([H|Tail], [R|Ta]):-
      subs(H, Tail, R),
      generatesubs(Tail, Ta).

  main1([], []).
  main1([H], [H]):-
     is_list(H).
  main1([H|Tail], [H|Ta]):-
     is_list(H),
  main1(Tail, Ta).
  main1([_|Tail], Ta):-
     main1(Tail, Ta).

  main([], []).
  main(H ,R):-
      generatesubs(H, G),
      main1(G,R).

提前致谢! :)

1 个答案:

答案 0 :(得分:4)

使用

list_allsubseqs(Es, Uss) :-
   list_acc_allsubseqs(Es, [[]], Uss).

lists_prependum([]      , _) --> [].
lists_prependum([Es|Ess], E) --> [[E|Es]], lists_prependum(Ess, E).

list_acc_allsubseqs([]    , Uss , Uss).
list_acc_allsubseqs([E|Es], Uss0, Uss) :-
   list_acc_allsubseqs(Es, Uss0, Uss1),
   phrase(lists_prependum(Uss1,E), Uss, Uss1).

示例查询:

?- list_allsubseqs([], Xss).
Xss = [[]].

?- list_allsubseqs([a], Xss).
Xss = [[a], []].

?- list_allsubseqs([a,b], Xss).
Xss = [[a,b], [a], [b], []].

?- list_allsubseqs([a,b,c], Xss).
Xss = [[a,b,c], [a,b], [a,c], [a], 
         [b,c],   [b],   [c], []].

?- list_allsubseqs([a,b,c,d], Xss).
Xss = [[a,b,c,d], [a,b,c], [a,b,d], [a,b], [a,c,d], [a,c], [a,d], [a],
         [b,c,d],   [b,c],   [b,d],   [b],   [c,d],   [c],   [d], []].

那么......与import pymysql import pymysql.cursors connection =pymysql.connect( user='root', host='localhost', database='mysql') mycursor=connection.cursor() mycursor.execute('use test_db') n=0 while n<5000: id=IDlist[n] url='www.example.com/'+str(id) values=requests.get(url) ##some parse omitted input=(values[1],values[2],id) sql="""UPDATE mytable SET COL1=%s, COL2=%s WHERE ID=%s""" mycursor.execute(sql, input) connection.commit() ## here all loops done list_allsubseqs/2相比,findall/3的表现如何?内存消耗怎么样?运行时怎么样? 让我们深入挖掘!

首先,为了完整起见,这里是 good-ol'vanilla list_subseq/2的代码:

list_subseq/2

在下文中,我们使用版本7.3.11(64位)。

:- set_prolog_flag(toplevel_print_anon, false).  % hide some substitutions
:- set_prolog_stack(global, limit(2*10**9)).     % cf. SWI-FAQ on "stack sizes"

我们来调查吧!

?- between(18, 22, N),
   numlist(1, N, _Es),
   member(How, [findall_subseq, list_allsubseqs]),
   garbage_collect,
   call_time(( How =  findall_subseq, findall(Xs,list_subseq(_Es,Xs),_) 
             ; How = list_allsubseqs, list_allsubseqs(_Es,_)), T_in_ms),
   statistics(globalused, Mem_in_B).

  N = 18, How =  findall_subseq, Mem_in_B =    62_915_848, T_in_ms =   185
; N = 18, How = list_allsubseqs, Mem_in_B =    12_584_904, T_in_ms =    22
;
  N = 19, How =  findall_subseq, Mem_in_B =   132_121_888, T_in_ms =   361
; N = 19, How = list_allsubseqs, Mem_in_B =    25_167_888, T_in_ms =    42
;
  N = 20, How =  findall_subseq, Mem_in_B =   276_825_400, T_in_ms =   804
; N = 20, How = list_allsubseqs, Mem_in_B =    50_333_784, T_in_ms =    80
;
  N = 21, How =  findall_subseq, Mem_in_B =   578_815_312, T_in_ms = 1_973
; N = 21, How = list_allsubseqs, Mem_in_B =   100_665_504, T_in_ms =   154
;
  N = 22, How =  findall_subseq, Mem_in_B = 1_207_960_936, T_in_ms = 3_966
; N = 22, How = list_allsubseqs, Mem_in_B =   201_328_872, T_in_ms =   290.