---
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

-- Imports from the Agda Standard Library ---------------------------------------------------
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 ; _โˆˆ_ )

-- Imports from agda-algebras --------------------------------------------------------------
open import Overture.Basic                  using ( ๐Ÿš ; ๐Ÿ› )
open import Setoid.Relations.Continuous     using ( Rel )
open import Examples.Structures.Signatures  using ( Sโˆ… ; S001 ; S021)

-- TODO(#M2-8c): the two imports below await the M3-1 (#260) Classical/ scaffold;
-- the canonical destinations for `signature`, `structure`, and `hom` will live
-- under Classical/ once M3 lands.
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

  -- The (purely) relational structure with
  -- + 2-element domain,
  -- + one binary relation Rแตƒ := \{(0,0), (1, 1)\}

  data Rแตƒ : Pred (๐Ÿš ร— ๐Ÿš) โ„“โ‚€ where
   r1 : (๐Ÿš.๐ŸŽ , ๐Ÿš.๐ŸŽ ) โˆˆ Rแตƒ
   r2 : (๐Ÿš.๐Ÿ , ๐Ÿš.๐Ÿ ) โˆˆ Rแตƒ

  ๐‘จ : structure Sโˆ…    -- (no operation symbols)
                S001  -- (one binary relation symbol)

  ๐‘จ = record { carrier = ๐Ÿš
             ; op = ฮป ()
             ; rel = ฮป _ x โ†’ ((x ๐Ÿš.๐ŸŽ) , (x ๐Ÿš.๐Ÿ)) โˆˆ Rแตƒ
             }

  -- Claim: Given an arbitrary ๐‘ฉ in the signatures Sigโˆ… Sig001, we can construct a homomorphism from ๐‘ฉ to ๐‘จ.
  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

  -- The (purely) relational structure with
  -- + 2-element domain,
  -- + one binary relation: Rแตƒ := { (0,0), (1, 1) }
  -- + two unary relations: Cโ‚€แตƒ := { 0 } , Cโ‚แตƒ := { 1 }

  data Rแตƒ : Pred (๐Ÿš ร— ๐Ÿš) โ„“โ‚€ where
   r1 : (๐Ÿš.๐ŸŽ , ๐Ÿš.๐ŸŽ ) โˆˆ Rแตƒ
   r2 : (๐Ÿš.๐Ÿ , ๐Ÿš.๐Ÿ ) โˆˆ Rแตƒ

  data Cโ‚€แตƒ : Pred ๐Ÿš โ„“โ‚€ where
   cโ‚€ : ๐Ÿš.๐ŸŽ โˆˆ Cโ‚€แตƒ

  data Cโ‚แตƒ : Pred ๐Ÿš โ„“โ‚€ where
   cโ‚ : ๐Ÿš.๐Ÿ โˆˆ Cโ‚แตƒ

  ๐‘จ : structure Sโˆ…    -- (no operations)
                S021  -- (two unary relations and one binary relation)

  ๐‘จ = 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โ‚แตƒ

  -- Claim: Given an arbitrary ๐‘ฉ in the signatures Sโˆ… S021, we can construct a homomorphism from ๐‘ฉ to ๐‘จ.
  -- claim :  (๐‘ฉ : structure Sโˆ… S021 {โ„“โ‚€}{โ„“โ‚€})
  --  โ†’       (โˆ€ (x : ๐Ÿš โ†’ carrier ๐‘ฉ)
  --           โ†’ (rel ๐‘ฉ) ๐Ÿ›.๐ŸŽ x  -- if ((x ๐Ÿš.๐ŸŽ) , (x ๐Ÿš.๐Ÿ)) โˆˆ Rแต‡, then...
  --           โ†’ ((rel ๐‘ฉ) ๐Ÿ›.๐Ÿ (ฮป _ โ†’ (x ๐Ÿš.๐ŸŽ)) โ†’ ยฌ (rel ๐‘ฉ) ๐Ÿ›.๐Ÿ (ฮป _ โ†’ (x ๐Ÿš.๐Ÿ)))
  --             ร— ((rel ๐‘ฉ) ๐Ÿ›.๐Ÿ (ฮป _ โ†’ (x ๐Ÿš.๐Ÿ)) โ†’ ยฌ (rel ๐‘ฉ) ๐Ÿ›.๐Ÿ (ฮป _ โ†’ (x ๐Ÿš.๐ŸŽ)))
  --          --  ร— (x ๐Ÿš.๐ŸŽ โˆˆ Cโ‚แต‡ โ†’ x ๐Ÿš.๐Ÿ โˆ‰ Cโ‚€แต‡))
  --          )
  --  โ†’       hom ๐‘ฉ ๐‘จ
  -- claim ๐‘ฉ x = {!!}
```

(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(โ„š, <).