---
layout: default
file: "src/Exercises/Complexity/FiniteCSP.lagda.md"
title: "Exercises.Complexity.FiniteCSP module (The Agda Universal Algebra Library)"
date: "2026-05-11"
author: "agda-algebras development team and Libor Barto"
---
### CSP Exercises
This is the [Exercises.Complexity.FiniteCSP][] module of the [Agda Universal Algebra Library][].
Excercises in this module were created by Libor Barto for students at Charles
University in Prague. They were formalized in dependent type theory by the
[agda-algebras development team][].
<!--
```agda
{-# OPTIONS --cubical-compatible --exact-split --safe #-}
module Exercises.Complexity.FiniteCSP where
open import Data.Product using ( _,_ ; _ร_ )
open import Data.Unit.Base using () renaming ( tt to ๐ )
open import Level using () renaming ( 0โ to โโ )
open import Relation.Unary using ( Pred ; _โ_ )
open import Overture.Basic using ( ๐ ; ๐ )
open import Setoid.Relations.Continuous using ( Rel )
open import Examples.Structures.Signatures using ( Sโ
; S001 ; S021)
open import Legacy.Base.Structures.Basic using ( signature ; structure )
open import Legacy.Base.Structures.Homs using ( hom )
open signature
open structure
```
-->
Some exercises below refer to the following relations on ๐ := \{0, 1\} (where i, j โ ๐):
\begin{align*}
Cแตแตข & := \{ i \} \\
Rแต & := \{ (0, 0), (1, 1) \} \\
Nแต & := \{ (0, 1), (1, 0) \} \\
Sแต_{ij} & := ๐ยฒ - \{ (i , j) \}, \\
Hแต & := ๐ยณ - \{ (1, 1, 0) \} \\
Gแตโ & := \{ (0,0,0), (0,1,1), (1,0,1), (1,1,0) \} \\
Gแตโ & := \{ (0,0,1), (0,1,0), (1,0,0), (1,1,1) \}
\end{align*}
**Exercise 1**. Prove that the definitions of CSP(๐ธ) (satisfiability of a list of constraints, homomorphism problem, truth of primitive positive formulas) are equivalent.
**Exercise 2**. Find a polynomial-time algorithm for CSP(A), where
2.1. ๐จ = ({0, 1}, Rแต) = (๐ , \{(0,0), (1, 1)\})
2.2. ๐จ = ({0, 1}, Rแต, Cโแต, Cโแต) = (๐ , \{ (0,0) , (1, 1) \} , \{ 0 \} , \{ 1 \})
2.3. ๐จ = ({0, 1}, Sโโแต) = (๐ , ๐ยณ - \{ (1, 0) \})
2.4. ๐จ = ({0, 1}, Sโโแต, Cโแต, Cโแต) = (๐ , ๐ยณ - \{ (1, 0) \} , \{ 0 \} , \{ 1 \})
2.5. ๐จ = ({0, 1}, Sโโแต, Sโโแต, Cโแต, Cโแต) = (๐ , ๐ยณ - \{ (0, 1) \} , ๐ยณ - \{ (1, 0) \} , \{ 0 \} , \{ 1 \})
2.6. ๐จ = ({0, 1}, Nแต) = (๐ , \{ (0, 1) , (1, 0) \})
2.7. ๐จ = ({0, 1}, Rแต, Nแต, Cโแต, Cโแต) = (๐ , \{ (0,0) , (1, 1) \} , \{ (0, 1) , (1, 0) \} , \{ 0 \} , \{ 1 \})
2.8. ๐จ = ({0, 1}, Rแต, Nแต, Cโแต, Cโแต, ๐โโ, Sโโแต, Sโโแต, Sโโแต) = (๐ , \{ (0,0) , (1, 1) \} , \{ (0, 1) , (1, 0) \} , \{ 0 \} , \{ 1 \} , ๐ยณ - \{ (0, 0) \} , ๐ยณ - \{ (0, 1) \} , ๐ยณ - \{ (1, 0) \} , ๐ยณ - \{ (1, 1) \})
2.9. ๐จ = ({0, 1}, all unary and binary relations)
**Solution 2.1**. If ๐จ = ({0, 1}, Rแต) = (๐ , \{(0,0), (1, 1)\}), then an instance of (the HOM
formulation of) CSP(๐จ) is a relational structure ๐ฉ = (B, Rแตโฉ, where Rแต โ Bยฒ and we must decide
whether there exists a homomorphism f : ๐ฉ โ ๐จ, that is, a map f : B โ A (= ๐) such that
โ (b, b'), if (b, b') โ Rแต, then (f b, f b') โ Rแต.
Of course, the constant map f โก 0 that sends every element of B to 0 (as well as the
constantly-1 map) is such a homomorphism. Let's prove this formally.
```agda
module solution-2-1 where
data Rแต : Pred (๐ ร ๐) โโ where
r1 : (๐.๐ , ๐.๐ ) โ Rแต
r2 : (๐.๐ , ๐.๐ ) โ Rแต
๐จ : structure Sโ
S001
๐จ = record { carrier = ๐
; op = ฮป ()
; rel = ฮป _ x โ ((x ๐.๐) , (x ๐.๐)) โ Rแต
}
claim : (๐ฉ : structure {โโ}{โโ}{โโ}{โโ} Sโ
S001 {โโ}{โโ}) โ hom ๐ฉ ๐จ
claim ๐ฉ = (ฮป x โ ๐.๐) , (ฮป _ _ _ โ r1) , ฮป ()
```
In general, whenever the template structure ๐จ has a one-element subuniverse, say, \{ a \},
then the constant map that always gives a is a homomorphism from any structure in the given
signature to ๐จ. โ
**Solution 2.2**. If ๐จ = (\{ 0, 1 \}, Rแต, Cโแต, Cโแต) = (๐ , \{ (0, 0) , (1, 1) \} , \{ 0 \} , \{ 1 \}),
then an instance of HOM CSP(๐จ) is a relational structure ๐ฉ = (B, Rแต, Cโแต, Cโแต), where
Rแต โ Bยฒ, Cโแต โ B, Cโแต โ B, and we must decide whether there exists a homomorphism
f : hom ๐ฉ ๐จ, that is, a map f : B โ ๐ such that the following conditions hold:
1. โ (x, y) โ Bยฒ, (x, y) โ Rแต implies (f x , f y) โ Rแต,
2. f is constantly 0 on Cโแต, and
3. f is constantly 1 on Cโแต.
The first condition says that if (x, y) โ Rแต, then either f x = 0 = f y or f x = 1 = f y.
Therefore, there exists a homomorphism f : hom ๐ฉ ๐จ iff Rแต โฉ Cโแต ร Cโแต = โ
= Rแต โฉ Cโแต ร Cโแต.
We can check this in polynomial time (in the size of the input instance ๐ฉ) since we just need
to check each pair (x, y) โ Rแต and make sure that the following two implications hold:
1. x โ Cโแต only if y โ Cโแต, and
2. x โ Cโแต only if y โ Cโแต.
```agda
module solution-2-2 where
data Rแต : Pred (๐ ร ๐) โโ where
r1 : (๐.๐ , ๐.๐ ) โ Rแต
r2 : (๐.๐ , ๐.๐ ) โ Rแต
data Cโแต : Pred ๐ โโ where
cโ : ๐.๐ โ Cโแต
data Cโแต : Pred ๐ โโ where
cโ : ๐.๐ โ Cโแต
๐จ : structure Sโ
S021
๐จ = record { carrier = ๐
; op = ฮป ()
; rel = rels
}
where
rels : (r : ๐) โ Rel ๐ (arity S021 r)
rels ๐.๐ x = ((x ๐.๐) , (x ๐.๐)) โ Rแต
rels ๐.๐ x = x ๐ โ Cโแต
rels ๐.๐ x = x ๐ โ Cโแต
```
(The remainder are "todo.")
**Solution 2.3**. ๐จ = ({0, 1}, Sโโแต) = (๐ , ๐ยณ - \{ (1, 0) \})
**Solution 2.4**. ๐จ = ({0, 1}, Sโโแต, Cโแต, Cโแต) = (๐ , ๐ยณ - \{ (1, 0) \} , \{ 0 \} , \{ 1 \})
**Solution 2.5**. ๐จ = ({0, 1}, Sโโแต, Sโโแต, Cโแต, Cโแต) = (๐ , ๐ยณ - \{ (0, 1) \} , ๐ยณ - \{ (1, 0) \} , \{ 0 \} , \{ 1 \})
**Solution 2.6**. ๐จ = ({0, 1}, Nแต) = (๐ , \{ (0, 1) , (1, 0) \})
**Solution 2.7**. ๐จ = ({0, 1}, Rแต, Nแต, Cโแต, Cโแต) = (๐ , \{ (0,0) , (1, 1) \} , \{ (0, 1) , (1, 0) \} , \{ 0 \} , \{ 1 \})
**Solution 2.8**. ๐จ = ({0, 1}, Rแต, Nแต, Cโแต, Cโแต, ๐โโ, Sโโแต, Sโโแต, Sโโแต) = (๐ , \{ (0,0) , (1, 1) \} , \{ (0, 1) , (1, 0) \} , \{ 0 \} , \{ 1 \} , ๐ยณ - \{ (0, 0) \} , ๐ยณ - \{ (0, 1) \} , ๐ยณ - \{ (1, 0) \} , ๐ยณ - \{ (1, 1) \})
**Solution 2.9**. ๐จ = ({0, 1}, all unary and binary relations)
**Exercise 3**. Find a polynomial-time algorithm for CSP({0, 1}, Hแต, Cโแต, Cโแต).
**Exercise 4**. Find a polynomial-time algorithm for CSP({0, 1}, Cโแต, Cโแต, Gโแต, Gโแต).
**Exercise 5**. Find a polynomial-time algorithm for CSP(โ, <).