具有多个相同元素的列表的排列 Prolog
的语言的任何误用
大家好,请原谅我创建 myPermutation(L1,L2) 所需 。给定一个列表 L1 (其中包含许多连续出现的元素),返回一个列表 L2,该列表按 L1 排序,不存在两个相同的连续元素)
示例:给定列表 L1[1,1,1, 1,1,2,2,3,3,4,4,5,5] L2 应为 [1,2,1,5,1,3,1,4,1,2,3,5,4] 我尝试过随机排列并检查每个排列的一致性,但速度非常慢(L1 大约 24 个 cpu,具有超过 12 个元素)
唯一可能的解决方案是进行一致的排列,而不是检查一个排列 但我该怎么做呢?
即使使用标准序言也可能可以完成,但由于我对逻辑编程的理解很差,所以我无法理解它,
谢谢:D
hello everyone pls forgive any misuse of the language
i need to create myPermutation(L1,L2). which given a list L1 (which has elements with many concecutive appearances) returns a list L2 which is L1 sorted in a way that there are no two concecutive elements which are the same)
example: given the list L1[1,1,1,1,1,2,2,3,3,4,4,5,5] L2 should be [1,2,1,5,1,3,1,4,1,2,3,5,4]
i have tried random permutations and checking each permutation for consistency but it is very slow (aprox 24 cpus for L1 with more than 12 elements)
the only possible solution is to make a consistent permutation instead of checking for one
but how can i do this?
it probalby can be done even with standard prolog but since my understanding of logic programming is poor enough i can't get my head around it
thank you :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过检查列表来构建此类排列。
回溯时,此代码将给出所有有效的排列。我将留给您一个练习,一种丢弃重复排列的方法。
You can build such permutations inspecting the list.
This code will give, upon backtracking, all the valid permutations. I'll leave you as an exercise a way to discard duplicate permutations.
您可以使用
dif/2
快速完成此操作,它限制两个变量具有不同的值,而无需事先知道这些值:使用它,您可以创建一个谓词来约束列表,使得没有两个元素是相同:
现在找到您想要的约束排列:
我不确定
dif/2
是否是标准 Prolog,但主要实现都有它。You can do this quite quickly with
dif/2
, which contrains two variables to have different values without knowing those values beforehand:Using this, you can make a predicate that constrains a list such that no two elements are the same:
Now to find the constrained permutations you want:
I'm not sure if
dif/2
is standard Prolog, but the major implementations have it.我们基于
same_length/2
,list_permuted/2
,dif 和mapadj/2
:多功能的 meta-predicate
mapadj/2
可以这样定义this:这是 OP 给出的示例查询1,2。
我们使用
call_time/2
来测量运行时间(以毫秒为单位)T_ms.
我们需要多长时间才能找到前几个解决方案?
请注意,
T_ms
单调增长:它测量自第一次调用给定目标以来所花费的时间。枚举所有解决方案需要多长时间?
有多少种解决方案?
脚注 1:使用 SICStus Prolog 版本 4.3.2 (x86_64-linux-glibc2.12)。
脚注 2:为了可读性,对 Prolog 处理器给出的答案序列进行了调整。
We define
my_perm/2
based onsame_length/2
,list_permuted/2
, dif, andmapadj/2
:The versatile meta-predicate
mapadj/2
can be defined like this:Here comes the sample query1,2 given by the OP.
We use
call_time/2
for measuring the runtime in milli-secondsT_ms
.How long does it take us to find the first few solutions?
Note that
T_ms
grows monotonically: it measures the time spent since it first called the given goal.How long does it take to enumerate all solutions?
How many solutions are there?
Footnote 1: Using SICStus Prolog version 4.3.2 (x86_64-linux-glibc2.12).
Footnote 2: The answer sequences given by the Prolog processor have been adapted for the sake of readability.