# Algorithm

**URL:** https://swi-prolog.discourse.group/c/help/algorithm/21.md

[Latest](https://swi-prolog.discourse.group/latest.md) · [Categories](https://swi-prolog.discourse.group/categories.md) · [Tags](https://swi-prolog.discourse.group/tags.md)

---

## [About the Algorithm category](https://swi-prolog.discourse.group/t/about-the-algorithm-category/2251)

<div class="topic-metadata">

**Author:** [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Replies:** 0

</div>

Use this category when you need help implementing an algorithm. If you need help with a single predicate then use the Help - Predicate category instead.

---

## [Order and sorting](https://swi-prolog.discourse.group/t/order-and-sorting/9551)

<div class="topic-metadata">

**Author:** [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Replies:** 40\
**Last updated:** [April 16, 2026, 2:56pm UTC](https://swi-prolog.discourse.group/t/order-and-sorting/9551 "2026-04-16T14:56:02Z")

</div>

Disclaimer: I seem to have a preoccupation with ordering and sorting; I have managed to not let it influence my daily life too badly. I lived in Germany once but I left the country. This topic pops up periodically. Thi…

---

## [Enumerating all simple paths in complete undirected graphs](https://swi-prolog.discourse.group/t/enumerating-all-simple-paths-in-complete-undirected-graphs/9039)

<div class="topic-metadata">

**Author:** [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Replies:** 0\
**Last updated:** [May 31, 2025, 8:35pm UTC](https://swi-prolog.discourse.group/t/enumerating-all-simple-paths-in-complete-undirected-graphs/9039 "2025-05-31T20:35:26Z")

</div>

For n\>=2, let G(n) be an undirected complete graph with nodes 1, 2, …, n, and S(n) the set of simple paths in G(n) which connects node 1 and 2. For example, G(2) = ({1,2}, {1-2}), G(3) = ({1,2,3}, {1-2, 2-3, 1-3}),…

---

## [Two-way deterministic zero-padding of a list](https://swi-prolog.discourse.group/t/two-way-deterministic-zero-padding-of-a-list/8675)

<div class="topic-metadata">

**Author:** [@Jamie](https://swi-prolog.discourse.group/u/Jamie)\
**Replies:** 16\
**Last updated:** [December 26, 2024, 1:02pm UTC](https://swi-prolog.discourse.group/t/two-way-deterministic-zero-padding-of-a-list/8675 "2024-12-26T13:02:08Z")

</div>

Hi there, I’ve been learning prolog for a few months now, on and off, and I thought I was getting it, but this one looked simple but has me stumped. I should note I’m using CLP(FD) for this, which I have only just start…

---

## [Capture standard output from write\\1 predicate in C++](https://swi-prolog.discourse.group/t/capture-standard-output-from-write-1-predicate-in-c/8523)

<div class="topic-metadata">

**Author:** [@Lorenzo](https://swi-prolog.discourse.group/u/Lorenzo)\
**Replies:** 4\
**Last updated:** [October 9, 2024, 7:22am UTC](https://swi-prolog.discourse.group/t/capture-standard-output-from-write-1-predicate-in-c/8523 "2024-10-09T07:22:34Z")

</div>

Hey, it’s me again. I’m currently looking for a way to capture an output from a write\\1, so I can manipulate the string as I want. Here is the example: PlCall("assert", PlTermv(PlCompound("myprint :- write('soy un poco…

---

## [Matching an identical subset of two lists](https://swi-prolog.discourse.group/t/matching-an-identical-subset-of-two-lists/7773)

<div class="topic-metadata">

**Author:** [@konokaiden](https://swi-prolog.discourse.group/u/konokaiden)\
**Replies:** 7\
**Last updated:** [September 17, 2024, 2:00pm UTC](https://swi-prolog.discourse.group/t/matching-an-identical-subset-of-two-lists/7773 "2024-09-17T14:00:36Z")

</div>

So, I have two lists which may have overlapping sublists. In that case, I want to delete one sublist. The actual data comes from a binary tree, so if tree format is better, we can pursue that angle instead. But basicall…

---

## [Using bash exit codes in Prolog](https://swi-prolog.discourse.group/t/using-bash-exit-codes-in-prolog/6715)

<div class="topic-metadata">

**Author:** [@Muppet39](https://swi-prolog.discourse.group/u/Muppet39)\
**Replies:** 4\
**Last updated:** [July 17, 2023, 1:18pm UTC](https://swi-prolog.discourse.group/t/using-bash-exit-codes-in-prolog/6715 "2023-07-17T13:18:10Z")

</div>

Hi everyone. I’m using process\_create/3 to run an external bash command and read the output. This is the code: process\_create( path(sshpass), \['-p', Pass, 'ssh', '-l', User, Host, Cmd\], …

---

## [Saving prolog query results in Python](https://swi-prolog.discourse.group/t/saving-prolog-query-results-in-python/6628)

<div class="topic-metadata">

**Author:** [@srydn](https://swi-prolog.discourse.group/u/srydn)\
**Replies:** 10\
**Last updated:** [June 11, 2023, 2:17pm UTC](https://swi-prolog.discourse.group/t/saving-prolog-query-results-in-python/6628 "2023-06-11T14:17:42Z")

</div>

Hello, I am trying to save the prolog.query() results to a file in Python. I tried so many things but I could not find a solution for that. I would be so glad if anyone can help me with this.

---

## [Translating Imogen (an intuitionistic theorem prover for FOL) into a Prolog prover?](https://swi-prolog.discourse.group/t/translating-imogen-an-intuitionistic-theorem-prover-for-fol-into-a-prolog-prover/2924)

<div class="topic-metadata">

**Author:** [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Replies:** 5\
**Last updated:** [September 15, 2020, 6:04pm UTC](https://swi-prolog.discourse.group/t/translating-imogen-an-intuitionistic-theorem-prover-for-fol-into-a-prolog-prover/2924 "2020-09-15T18:04:38Z")

</div>

The question is on this webpage on Stack Overflow.

---

## [Code for chi-square distribution?](https://swi-prolog.discourse.group/t/code-for-chi-square-distribution/6352)

<div class="topic-metadata">

**Author:** [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Replies:** 4\
**Last updated:** [March 9, 2023, 4:02pm UTC](https://swi-prolog.discourse.group/t/code-for-chi-square-distribution/6352 "2023-03-09T16:02:53Z")

</div>

Hi! I want to write a predicate that performs a McNemar test, a non-parametric test for comparing the correctness of two classification models. For the significance level I need a predicate that computes the density D o…

---

## [Permutations - Stuck in a infinite loop](https://swi-prolog.discourse.group/t/permutations-stuck-in-a-infinite-loop/5995)

<div class="topic-metadata">

**Author:** [@tripsytina](https://swi-prolog.discourse.group/u/tripsytina)\
**Replies:** 7\
**Last updated:** [December 3, 2022, 7:09pm UTC](https://swi-prolog.discourse.group/t/permutations-stuck-in-a-infinite-loop/5995 "2022-12-03T19:09:37Z")

</div>

I have done an algorithm wich gets the most time-efficent list from the permutated lists of a certain list. When i try to run it for a small list (with 2 elements) it works, however, when i try with a bigger list, it g…

---

## [Longest path problems and tabling](https://swi-prolog.discourse.group/t/longest-path-problems-and-tabling/5858)

<div class="topic-metadata">

**Author:** [@hardmath](https://swi-prolog.discourse.group/u/hardmath)\
**Replies:** 3\
**Last updated:** [October 17, 2022, 3:01pm UTC](https://swi-prolog.discourse.group/t/longest-path-problems-and-tabling/5858 "2022-10-17T15:01:38Z")

</div>

I’m coding a dynamic programming approach to a longest path problem (in a directed acyclic graph). The vertices are naturally represented by pairs of positive integers. The edges are easy to verify but a little tricky …

---

## [A path counting problem revisited](https://swi-prolog.discourse.group/t/a-path-counting-problem-revisited/5626)

<div class="topic-metadata">

**Author:** [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Replies:** 0\
**Last updated:** [July 21, 2022, 1:30pm UTC](https://swi-prolog.discourse.group/t/a-path-counting-problem-revisited/5626 "2022-07-21T13:30:12Z")

</div>

EDIT 2022-Aug-1 On debugging the revised version, I found the old query log below was wrong. I’m very sorry for that. The following is correct, which is the output of the new version. % ?- time(count\_rect\_path(rect(8…

---

## [Trying to print output instead of true](https://swi-prolog.discourse.group/t/trying-to-print-output-instead-of-true/5479)

<div class="topic-metadata">

**Author:** [@lucolodhla](https://swi-prolog.discourse.group/u/lucolodhla)\
**Replies:** 16\
**Last updated:** [June 9, 2022, 7:53pm UTC](https://swi-prolog.discourse.group/t/trying-to-print-output-instead-of-true/5479 "2022-06-09T19:53:35Z")

</div>

Hi Does anyone know how to print this output to the screen instead of getting true when I run ‘test.’ I am new to Prolog, so I have never printed anything to the console. This is part of my Programming languages assignm…

---

## [Prolog homework](https://swi-prolog.discourse.group/t/prolog-homework/5257)

<div class="topic-metadata">

**Author:** [@barissevinc1](https://swi-prolog.discourse.group/u/barissevinc1)\
**Replies:** 0\
**Last updated:** [April 10, 2022, 4:14pm UTC](https://swi-prolog.discourse.group/t/prolog-homework/5257 "2022-04-10T16:14:42Z")

</div>

---

## [I need help with my homework](https://swi-prolog.discourse.group/t/i-need-help-with-my-homework/5241)

<div class="topic-metadata">

**Author:** [@Spartan](https://swi-prolog.discourse.group/u/Spartan)\
**Replies:** 0\
**Last updated:** [April 8, 2022, 6:38am UTC](https://swi-prolog.discourse.group/t/i-need-help-with-my-homework/5241 "2022-04-08T06:38:01Z")

</div>

My programming teacher let us make an expert system of a farm with: 3 sensors and 3 actuators. A trapezoidal, triangular or bell fuzzy control has to be used. But honestly I don’t know how to implement what we saw in …

---

## [Check array of statements against state array](https://swi-prolog.discourse.group/t/check-array-of-statements-against-state-array/5050)

<div class="topic-metadata">

**Author:** [@panwauu](https://swi-prolog.discourse.group/u/panwauu)\
**Replies:** 9\
**Last updated:** [February 23, 2022, 12:52pm UTC](https://swi-prolog.discourse.group/t/check-array-of-statements-against-state-array/5050 "2022-02-23T12:52:39Z")

</div>

Hi everyone, I am experimenting with this planner: Prolog Planner (unm.edu) I already modified the planner a bit to allow for the action incrementValue which works. My actions look like this: move(plus(), \[value(some…

---

## [Calculate sum of marks](https://swi-prolog.discourse.group/t/calculate-sum-of-marks/4909)

<div class="topic-metadata">

**Author:** [@Armand](https://swi-prolog.discourse.group/u/Armand)\
**Replies:** 8\
**Last updated:** [January 16, 2022, 7:50pm UTC](https://swi-prolog.discourse.group/t/calculate-sum-of-marks/4909 "2022-01-16T19:50:39Z")

</div>

Hello, I’m just a beginner in Prolog Programming, and I have a question about the sum of marks that I have to then find the average. I don’t know how can I do it. mark(3). mark(18). mark(10). sum(S,M):- M1 is M+1, mark…

---

## [How to program a list in Prolog that can contain only the character a but must finish with the character b?](https://swi-prolog.discourse.group/t/how-to-program-a-list-in-prolog-that-can-contain-only-the-character-a-but-must-finish-with-the-character-b/4716)

<div class="topic-metadata">

**Author:** [@Nobody1](https://swi-prolog.discourse.group/u/Nobody1)\
**Replies:** 3\
**Last updated:** [December 5, 2021, 11:08pm UTC](https://swi-prolog.discourse.group/t/how-to-program-a-list-in-prolog-that-can-contain-only-the-character-a-but-must-finish-with-the-character-b/4716 "2021-12-05T23:08:34Z")

</div>

I want to program a list that uses only the characters {a, b}. My objective is that prolog returns true only if the list that the user enter contains n number of a, or at least one a but has to finish with one b only, n…

---

## [Using DCGs for parsing a mustache-like template](https://swi-prolog.discourse.group/t/using-dcgs-for-parsing-a-mustache-like-template/4275)

<div class="topic-metadata">

**Author:** [@Risto-Stevcev](https://swi-prolog.discourse.group/u/Risto-Stevcev)\
**Replies:** 3\
**Last updated:** [August 11, 2021, 9:41am UTC](https://swi-prolog.discourse.group/t/using-dcgs-for-parsing-a-mustache-like-template/4275 "2021-08-11T09:41:47Z")

</div>

Hi Everyone, I want to write a portable library using DCGs to parse mustache-like templates. The contents of anything inside the mustaches is a term (ie: foo {{some(term)}} bar). \] I know about quasi-quotation but I w…

---

## [Best way to weave list processing predicates?](https://swi-prolog.discourse.group/t/best-way-to-weave-list-processing-predicates/4181)

<div class="topic-metadata">

**Author:** [@dmchurch](https://swi-prolog.discourse.group/u/dmchurch)\
**Replies:** 12\
**Last updated:** [July 22, 2021, 11:25am UTC](https://swi-prolog.discourse.group/t/best-way-to-weave-list-processing-predicates/4181 "2021-07-22T11:25:29Z")

</div>

I’m getting stymied by a task that I’d find simple in other languages, but I just don’t know what the best way is to implement it in Prolog. Specifically, I want to perform several processing steps on a stream of data, e…

---

## [Most efficient DCG for text parsing?](https://swi-prolog.discourse.group/t/most-efficient-dcg-for-text-parsing/4136)

<div class="topic-metadata">

**Author:** [@dmchurch](https://swi-prolog.discourse.group/u/dmchurch)\
**Replies:** 4\
**Last updated:** [July 5, 2021, 4:57pm UTC](https://swi-prolog.discourse.group/t/most-efficient-dcg-for-text-parsing/4136 "2021-07-05T16:57:53Z")

</div>

So, sometimes I actually write Prolog, not just C that implements Prolog! :joy: I’m wondering what the most efficient way is to write a DCG for text parsing, like when using phrase\_from\_file/2. Specifically, there’s a t…

---

## [Set Difference of Two Compatible Lists](https://swi-prolog.discourse.group/t/set-difference-of-two-compatible-lists/3906)

<div class="topic-metadata">

**Author:** [@aadilkhan](https://swi-prolog.discourse.group/u/aadilkhan)\
**Replies:** 4\
**Last updated:** [May 4, 2021, 4:18pm UTC](https://swi-prolog.discourse.group/t/set-difference-of-two-compatible-lists/3906 "2021-05-04T16:18:01Z")

</div>

Hey guys, I posted a question here about a week ago on the same topic and I can appreciate the question was quite unspecific ( I had no clue what I was doing at the time ). But now I’ve come some way and need help with a…

---

## [Incrementally solving CLP(FD)](https://swi-prolog.discourse.group/t/incrementally-solving-clp-fd/3839)

<div class="topic-metadata">

**Author:** [@yourein](https://swi-prolog.discourse.group/u/yourein)\
**Replies:** 4\
**Last updated:** [April 21, 2021, 9:05am UTC](https://swi-prolog.discourse.group/t/incrementally-solving-clp-fd/3839 "2021-04-21T09:05:47Z")

</div>

When I tried to solve a problem, I faced a situation when it requires solving a CLP(FD) incrementally. For example, let M be a model of a CLP(FD). To solve it, we just need to query the model in prolog like :- M. Then…

---

## [Prolog immutability semantics?](https://swi-prolog.discourse.group/t/prolog-immutability-semantics/3647)

<div class="topic-metadata">

**Author:** [@vsraptor](https://swi-prolog.discourse.group/u/vsraptor)\
**Replies:** 4\
**Last updated:** [February 25, 2021, 7:31pm UTC](https://swi-prolog.discourse.group/t/prolog-immutability-semantics/3647 "2021-02-25T19:31:25Z")

</div>

Prolog uses IMMUTABLE variables. My question is : when you “extend” a list , internally the Origin list get copied as a new list OR it is just extended. What I want to understand is how exactly/specifically the variabl…

---

## [Remove all substrings in a string](https://swi-prolog.discourse.group/t/remove-all-substrings-in-a-string/3280)

<div class="topic-metadata">

**Author:** [@PrologAxel](https://swi-prolog.discourse.group/u/PrologAxel)\
**Replies:** 7\
**Last updated:** [November 20, 2020, 2:03pm UTC](https://swi-prolog.discourse.group/t/remove-all-substrings-in-a-string/3280 "2020-11-20T14:03:27Z")

</div>

Hello. ?- teto("abab", "cc","ababvvvababeeeeababtttttabab", R). I get the output: But only thanks to write(Q). \[c,c,v,v,v,a,b,a,b,e,e,e,e,a,b,a,b,t,t,t,t,t,a,b,a,b\] \[c,c,v,v,v,c,c,e,e,e,e,a,b,a,b,t,t,t,t,t,a,b,a,b\] \[c…

---

## [How to create a DCG-predicate that is usable as both a parser and a generator?](https://swi-prolog.discourse.group/t/how-to-create-a-dcg-predicate-that-is-usable-as-both-a-parser-and-a-generator/3106)

<div class="topic-metadata">

**Author:** [@Qqwy](https://swi-prolog.discourse.group/u/Qqwy)\
**Replies:** 4\
**Last updated:** [November 6, 2020, 3:48pm UTC](https://swi-prolog.discourse.group/t/how-to-create-a-dcg-predicate-that-is-usable-as-both-a-parser-and-a-generator/3106 "2020-11-06T15:48:07Z")

</div>

Hi everyone! I recently wanted to become better at Prolog (having used it in the past only in an “Introduction to Logic”-course at university). As I am also learning a bit to speak Esperanto, which is a constructed lang…

---

## [Can you please provide advice on big data handling strategy?](https://swi-prolog.discourse.group/t/can-you-please-provide-advice-on-big-data-handling-strategy/2997)

<div class="topic-metadata">

**Author:** [@Rscho314](https://swi-prolog.discourse.group/u/Rscho314)\
**Replies:** 38\
**Last updated:** [October 6, 2020, 10:29am UTC](https://swi-prolog.discourse.group/t/can-you-please-provide-advice-on-big-data-handling-strategy/2997 "2020-10-06T10:29:12Z")

</div>

Hi, I’d like to work on a big database (~30 million facts), that currently is in SQL format. I am aware of libs such as prosqlite or CQL, but I would much prefer to use plain prolog. The SQL data is as follows: HEADE…

---

## [Implementing Quine's algorithm](https://swi-prolog.discourse.group/t/implementing-quines-algorithm/2830)

<div class="topic-metadata">

**Author:** [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Replies:** 104\
**Last updated:** [September 16, 2020, 6:25am UTC](https://swi-prolog.discourse.group/t/implementing-quines-algorithm/2830 "2020-09-16T06:25:27Z")

</div>

I’m using: SWI-Prolog version : SWI-Prolog version 8.3.3 for x86\_64-linux I posted this question here, few days ago, but I’m wasting my time in trying to get a solution : I am convinced that this task is not difficu…

---

## [Improving Wang's algorithm in Prolog](https://swi-prolog.discourse.group/t/improving-wangs-algorithm-in-prolog/2872)

<div class="topic-metadata">

**Author:** [@joseph-vidal-rosset](https://swi-prolog.discourse.group/u/joseph-vidal-rosset)\
**Replies:** 66\
**Last updated:** [September 13, 2020, 2:55pm UTC](https://swi-prolog.discourse.group/t/improving-wangs-algorithm-in-prolog/2872 "2020-09-13T14:55:33Z")

</div>

Hello, I published here a Prolog implementation of Wang’s algorithm that was originally written by Ben Hudson and published here. Ben Hudson’s prover requested two inputs: first the set of premises as list, i.e. \[formula…

[Next page](https://swi-prolog.discourse.group/c/help/algorithm/21.md?page=1)
