Algorithm Search⌘K

Algorithmcore

Grounded extension computation

Also called grounded labelling algorithm.

Computes the grounded extension in linear time. Count the attackers of each argument that are not yet out, accept an argument when its count is 0, and reject every argument it attacks.

Time
Θ(n + m) worst · best
Space
Θ(n + m) extra
Where
m = the number of attacks; n = the number of arguments
Properties
deterministic
Origin
Samer Nofal, Katie Atkinson, Paul E. Dunne, 2021

This algorithm computes the grounded extension of a finite framework in time linear in the number of arguments plus the number of attacks. It keeps, for each argument, the number of its attackers that are not yet rejected. An argument whose count is 0 is accepted, and the arguments it attacks are rejected, which lowers the counts of the arguments they attack. The output is the grounded labelling: the grounded extension, the arguments it attacks, and the rest. The grounded extension page gives the definition and its properties; this page gives the algorithm, its proof and its cost. For the complexity of the decision problems, see argument acceptance.

In plain words

Each argument starts as undecided. An argument that nobody attacks is safe, so accept it. Each argument that an accepted argument attacks is now beaten, so reject it. A beaten argument can no longer hurt anybody, so each argument it attacks has one attacker fewer to worry about. When all the attackers of an argument are beaten, that argument is safe too, so accept it and go on.

To do this fast, keep a counter on each argument: the number of its attackers that are not beaten yet. Put the arguments with a counter of zero in a queue. Take them out one at a time. When the queue is empty, stop. The accepted arguments are the grounded extension. The arguments that stay undecided are the ones that depend on a cycle of attacks that nothing breaks.

The problem

The input is a finite framework (A,R)(A, R): a set AA of nn arguments and a set R⊆A×AR \subseteq A \times A of mm attacks, where (a,b)∈R(a, b) \in R means that aa attacks bb. Self-attacks are allowed. For a set SS, S+S^+ is the set of the arguments that SS attacks, and the characteristic function F(S)F(S) is the set of the arguments that SS defends. The grounded extension GG is the least fixed point of FF (Dung 1995, Definition 20), which is also the least complete extension (Theorem 25). The output is the labelling that maps each member of GG to in, each member of G+G^+ to out, and every other argument to undec.

The algorithm

For each argument aa, the list succ[a]succ[a] holds the arguments that aa attacks. One pass over RR builds these lists and the counts.

  1. GROUNDED(A, R)
  2. 1for each a in A do count[a] ← the number of attackers of a
  3. 2for each a in A do label[a] ← undec
  4. 3Q ← a queue of the arguments a with count[a] = 0
  5. 4while Q is not empty do
  6. 5a ← take the first argument out of Q
  7. 6label[a] ← in ▷ all attackers of a are out
  8. 7for each b in succ[a] do
  9. 8if label[b] = undec then
  10. 9label[b] ← out ▷ b is attacked by an in argument
  11. 10for each c in succ[b] do
  12. 11count[c] ← count[c] - 1
  13. 12if count[c] = 0 then put c in Q
  14. 13return label

Line L3 starts with the arguments that have no attacker. Line L8 skips an argument that is already out, so each argument is labelled out once and its attacks are scanned once. An argument that line L5 takes out of QQ is never out, and an argument that line L7 reaches is never in, so line L8 needs only the one test. A self-attacking argument never reaches count 0, because it is its own attacker and it is never out. The queue can be a stack: the order does not change the result.

Example

Take seven arguments and eight attacks: a→b→c→d→ea \to b \to c \to d \to e, the cycle e↔fe \leftrightarrow f, and an argument gg that attacks itself and dd. So R={(a,b),(b,c),(c,d),(d,e),(e,f),(f,e),(g,g),(g,d)}R = \{(a,b), (b,c), (c,d), (d,e), (e,f), (f,e), (g,g), (g,d)\}.

argument a b c d e f g
attackers none a b c, g d, f e g
count at the start 0 1 1 2 2 1 1

The run, with the counts of a,…,ga, \ldots, g after each step:

step taken from Q in out counts Q after
start 0 1 1 2 2 1 1 aa
1 aa aa bb 0 1 0 2 2 1 1 cc
2 cc cc dd 0 1 0 2 1 1 1 empty

In step 1, aa is labelled in, its target bb is labelled out, and the scan of the attack (b,c)(b, c) brings the count of cc to 0. In step 2, cc is labelled in, dd is labelled out, and the scan of (d,e)(d, e) brings the count of ee from 2 to 1. Then the queue is empty. The result is G={a,c}G = \{a, c\} with G+={b,d}G^+ = \{b, d\}, and ee, ff and gg stay undec. The run takes 2 queue steps and scans 4 of the 8 attacks: (a,b)(a, b), (b,c)(b, c), (c,d)(c, d) and (d,e)(d, e).

The iteration of the characteristic function gives the same set: F(∅)={a}F(\emptyset) = \{a\}, F({a})={a,c}F(\{a\}) = \{a, c\} and F({a,c})={a,c}F(\{a, c\}) = \{a, c\}. The argument ee is not in GG: its attacker dd is out, but its other attacker ff is attacked only by ee, and nothing breaks the cycle. The argument gg attacks itself, so it is never in, and it stays undec. The framework has two other complete extensions, {a,c,e}\{a, c, e\} and {a,c,f}\{a, c, f\}, and both contain GG.

Correctness

Five invariants hold at line L4. Each count is the number of attackers that are not out. Each argument in ININ or in QQ has all its attackers out. Each out argument has an in attacker. The in, out and queued arguments are disjoint. Each argument is pushed at most once. Two lemmas follow. First, by induction on the run, each in argument is in every complete extension EE, because EE defends it and E=F(E)E = F(E). Second, when the queue is empty, the in set ININ is conflict-free, IN+IN^+ is exactly the out set, and IN=F(IN)IN = F(IN): an argument that ININ defends has count 0, so it was pushed and later labelled in. So ININ is a complete extension that is contained in every complete extension, and that is the grounded extension.

Proof: The queue method returns the grounded labelling and stops after linear workfull

We use the pseudocode of the page. The input is a finite framework (A,R)(A, R) with n=∣A∣n = |A| arguments and m=∣R∣m = |R| attacks. RR is a set, so each attack is listed once. For a set SS, S+S^+ is the set of the arguments that some member of SS attacks, and F(S)F(S) is the set of the arguments that SS defends: a∈F(S)a \in F(S) when each attacker of aa is in S+S^+. A set EE is a complete extension when it is conflict-free and E=F(E)E = F(E) (Dung 1995, Lemma 24). The grounded extension is the least complete extension (Dung 1995, Theorem 25).

Write ININ, OUTOUT and UNDUND for the sets of the arguments with the label in, out and undec. "Pushed" means put in QQ by line L3 or line L12.

Invariants. Each time the test of line L4 runs:

(I1) For each argument cc, count[c]count[c] is the number of attackers of cc that are not in OUTOUT.

(I2) Each argument in ININ or in QQ has all its attackers in OUTOUT.

(I3) Each argument in OUTOUT has an attacker in ININ.

(I4) ININ, OUTOUT and the arguments in QQ are pairwise disjoint, and the arguments in QQ have the label undec.

(I5) Each argument has been pushed at most once, and the arguments pushed so far are exactly the members of ININ and QQ.

Initialization. Line L1 sets each count to the number of attackers, and OUTOUT is empty, so (I1) holds. Line L2 makes every label undec, and line L3 pushes exactly the arguments with no attacker, once each. These have no attacker at all, so (I2) holds, and (I3), (I4) and (I5) hold because ININ and OUTOUT are empty.

Maintenance. Line L5 takes aa out of QQ. By (I4), aa has the label undec, and line L6 labels it in. So aa moves from QQ to ININ, and (I2) still holds for aa.

Lines L7 to L9 look at each bb that aa attacks. First, bb is not in ININ: if it were, then by (I2) its attacker aa would be in OUTOUT, but aa was in QQ, which (I4) keeps apart from OUTOUT. Second, bb is not in QQ, for the same reason. So line L8 finds bb either already out, and does nothing, or undec and not in QQ. In the second case line L9 labels bb out. Then bb has the attacker a∈INa \in IN, so (I3) holds for bb. No argument of ININ or QQ changes its label, so (I4) holds.

When bb enters OUTOUT, exactly the attacks (b,c)(b, c) change from "attacker not out" to "attacker out". Lines L10 and L11 decrease count[c]count[c] once for each such attack, so (I1) holds again. If count[c]count[c] reaches 0, then by (I1) every attacker of cc is in OUTOUT, and line L12 pushes cc, which keeps (I2). This cc is not in OUTOUT: by (I3) it would have an attacker in ININ, and that attacker would be in OUTOUT too, against (I4). It is not in ININ or QQ: a count only goes down and never below 0 by (I1), so it reaches 0 at most once, and an argument pushed by line L3 has no attacker, so its count never changes. So cc has the label undec and was never pushed before, and (I4) and (I5) hold.

Termination and work. By (I5), each argument is pushed at most once, so the loop of line L4 runs at most nn times. Line L7 scans the attacks that leave an argument when it enters ININ, and line L10 scans the attacks that leave an argument when it enters OUTOUT. Each argument enters ININ at most once and OUTOUT at most once, and never both by (I4). So each attack (x,y)(x, y) is scanned at most once over the whole run, and lines L7 and L10 together run at most mm times. With the Θ(n+m)\Theta(n + m) work of lines L1 to L3, the run takes Θ(n+m)\Theta(n + m) steps.

Lemma 1 (soundness). Let EE be any complete extension of (A,R)(A, R). At each moment, IN⊆EIN \subseteq E and OUT⊆E+OUT \subseteq E^+.

Proof. By induction on the steps of the run. At the start both sets are empty. When line L6 labels aa in, each attacker of aa is in OUTOUT by (I2), so it is in E+E^+ by the hypothesis. So EE defends aa, and a∈F(E)=Ea \in F(E) = E. When line L9 labels bb out, bb is attacked by aa, and a∈Ea \in E, so b∈E+b \in E^+. □\square

Lemma 2. When the loop stops, ININ is a complete extension.

Proof. Now QQ is empty.

  • OUT=IN+OUT = IN^+. By (I3), OUT⊆IN+OUT \subseteq IN^+. Each a∈INa \in IN went through lines L7 to L9, which leave every argument that aa attacks in OUTOUT. So IN+⊆OUTIN^+ \subseteq OUT.
  • ININ is conflict-free. If a,b∈INa, b \in IN and aa attacks bb, then b∈IN+=OUTb \in IN^+ = OUT, against (I4).
  • IN⊆F(IN)IN \subseteq F(IN). By (I2), each attacker of a member of ININ is in OUT=IN+OUT = IN^+.
  • F(IN)⊆INF(IN) \subseteq IN. Let ININ defend cc. Then each attacker of cc is in IN+=OUTIN^+ = OUT, so count[c]=0count[c] = 0 by (I1). If cc has no attacker, line L3 pushed it. Else its count went from a positive value to 0 at line L11, and line L12 pushed it. By (I5), cc is in ININ or in QQ, and QQ is empty. □\square

Theorem. GROUNDED(A,R)(A, R) stops after Θ(n+m)\Theta(n + m) steps. The arguments labelled in form the grounded extension GG, the arguments labelled out form G+G^+, and the other arguments are labelled undec.

Proof. Termination and the bound are shown above. By Lemma 2, ININ is a complete extension. By Lemma 1, ININ is a subset of every complete extension. So ININ is the least complete extension, which is the grounded extension GG (Dung 1995, Theorem 25). By the proof of Lemma 2, OUT=G+OUT = G^+, and the remaining arguments keep the label undec from line L2. □\square

The order in which QQ hands out the arguments is not used anywhere in the proof, so a stack or any other bag gives the same result.

Complexity

Lines L1 to L3 take Θ(n+m)\Theta(n + m) time. Each argument is pushed at most once, so lines L5 and L6 run at most nn times, in fact exactly ∣G∣|G| times. Line L7 scans the attacks that leave an argument when it becomes in, and line L10 scans the attacks that leave an argument when it becomes out. No argument becomes both, so each attack is scanned at most once in the whole run, and lines L7 to L12 run at most mm times. The total is Θ(n+m)\Theta(n + m) time in the worst case and in the best case on the word RAM, because the input must be read. The extra space is Θ(n+m)\Theta(n + m) for the lists, the counts, the labels and the queue. The tests count the queue steps and the scanned attacks on random frameworks and on a chain of 3000 arguments, and check that the steps equal ∣G∣≤n|G| \le n and the scans are at most mm.

For comparison, the direct iteration ∅,F(∅),F(F(∅)),…\emptyset, F(\emptyset), F(F(\emptyset)), \ldots computes each F(S)F(S) in Θ(n+m)\Theta(n + m) time and can need about n/2n/2 rounds: on the chain x1→x2→⋯→xnx_1 \to x_2 \to \cdots \to x_n each round adds one argument. That is Θ(n(n+m))\Theta(n(n + m)) time. The labelling loop of Modgil and Caminada, as implemented before 2021, runs in O(n+∣G∣⋅m)O(n + |G| \cdot m) time (Nofal, Atkinson and Dunne 2021, Theorem 3).

The decision problem "is aa in the grounded extension" is P-complete (Dvořák and Dunne, Table 1), so a linear-time sequential algorithm is the best one can hope for, and a fast parallel algorithm is unlikely.

Properties

  • Exact and deterministic. The result does not depend on the order of the arguments or on the order in which the queue hands them out. The tests rename the arguments at random and compare.
  • Gives the labelling, not only the set. The out arguments are exactly G+G^+, so the run also gives the range G∪G+G \cup G^+ and the undec part, at no extra cost. This is the grounded labelling.
  • Sceptical and credulous at once. The grounded extension is unique, so an argument is credulously accepted under grounded semantics exactly when it is sceptically accepted, and both questions are answered by one run.
  • Not incremental. One new attack can change the result far away. In the chain y→z→w→xy \to z \to w \to x the grounded extension is {y,w}\{y, w\}. Add the attack (x,y)(x, y): now the four arguments form a cycle and the grounded extension is empty. So after a change to RR, run the algorithm again.
  • The same pattern as Horn forward chaining. Line L3 is the set of facts, an in argument is a derived fact, and a count is the number of body atoms not yet derived. The difference is the two-step update: an argument becomes in, then its targets become out, and only out arguments lower the counts.

Variants and improvements

  • Fixed-point iteration. Compute F(∅)F(\emptyset), F2(∅)F^2(\emptyset), and so on, until two steps agree. This is the definition of Dung (1995) for finite frameworks and it is easy to check, but it is quadratic in the worst case. The tests use it as the reference.
  • Labelling loop. Modgil and Caminada (2009) label in the undec arguments whose attackers are all out, then label out the arguments that an in argument attacks, and repeat. Each round scans all arguments, which gives the O(n+∣G∣⋅m)O(n + |G| \cdot m) bound above.
  • Recursive form. Nofal, Atkinson and Dunne (2021) also give a recursive version (their Algorithm 4) with the same idea: accept an argument, reject its targets, and recurse on the arguments whose last open attacker was just rejected.
  • Stop at the query. To decide whether one argument qq is in GG, stop when qq is labelled in or out. In the worst case the bound does not change.
  • Games and logic programs. If a move from aa goes to an attacker of aa, then the in, out and undec labels are the lost, won and drawn positions of retrograde analysis for the player to move, and the algorithm is that analysis. The grounded extension of the framework built from a normal logic program corresponds to its well-founded model (Dung 1995, Section 4), but a general program needs more than this algorithm, for example the alternating fixpoint.

When to use it

Use it whenever the grounded extension or the grounded labelling of a finite framework is needed: as the sceptical answer of a reasoner, as the first step of a search for preferred or other complete extensions (every complete extension contains GG, and every argument in G+G^+ is out in each of them), and as a fast filter before a call to a SAT solver. It reads the input once, so it is optimal up to a constant factor. It does not compute other semantics: the preferred and stable extensions need search, and the acceptance problems for them are NP-hard or harder.

Pitfalls

  • Lowering counts when an argument becomes in. The count of cc must drop when an attacker of cc becomes out, not when it becomes in. A program that lowers the count of the targets of an in argument accepts wrong arguments.
  • Repeated attacks. If the input is a list with the same attack twice, line L1 counts it twice. Either remove duplicates first or keep one list entry for each copy, so that the count can still reach 0. The Python version takes RR as a set.
  • Labelling an argument out twice. Without the test of line L8, an argument attacked by two in arguments lowers the counts of its targets twice, and a count can reach 0 too early or go below 0.
  • Self-attacks. An argument that attacks itself must count itself as an attacker. If the program drops self-loops, the argument is accepted, and the result is not even conflict-free.
  • Rescanning all arguments after each change. A loop that looks for a new zero-count argument by scanning the whole framework is correct but quadratic.
  • Unknown arguments. An attack that names an argument outside AA breaks the counts. The Python version rejects such input with an error.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

af.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the argumentation pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Finite abstract argumentation frameworks, for the argumentation pages.

A framework is a pair (args, attacks). args is a set of arguments, and attacks
is a set of pairs (a, b), read "a attacks b", with a and b in args. An argument
is any hashable value that sorts with the other arguments, for example a small
int or a short string. A self-attack (a, a) is allowed.

The functions are the plain definitions of Dung (1995), Caminada (2006) and
Baroni, Caminada and Giacomin (2018), written for clarity and checked by the
tests of each page. The enumerators look at all 2^n subsets of the arguments,
so they accept at most MAX_ARGS arguments. The algorithms of the pages live in
their own files, not here.
"""

from itertools import combinations

MAX_ARGS = 10


def check_framework(args, attacks):
    """Raise ValueError when an attack names an argument that is not in args."""
    for a, b in attacks:
        if a not in args or b not in args:
            raise ValueError(f"attack {(a, b)!r} names an argument outside args")


def attackers(attacks, a):
    """The set of the arguments that attack a."""
    return frozenset(x for x, y in attacks if y == a)


def attacked_by(attacks, s):
    """The set S+ of the arguments that some member of s attacks."""
    return frozenset(y for x, y in attacks if x in s)


def range_of(attacks, s):
    """The range S u S+ of the set s."""
    return frozenset(s) | attacked_by(attacks, s)


def conflict_free(attacks, s):
    """True when no member of s attacks a member of s."""
    return not any(x in s and y in s for x, y in attacks)


def defends(attacks, s, a):
    """True when s attacks every attacker of a (a is acceptable with respect to s)."""
    hit = attacked_by(attacks, s)
    return all(b in hit for b in attackers(attacks, a))


def characteristic(args, attacks, s):
    """The characteristic function F(s): the set of the arguments that s defends."""
    hit = attacked_by(attacks, s)
    return frozenset(a for a in args if all(b in hit for b in attackers(attacks, a)))


def is_admissible(args, attacks, s):
    """Conflict-free, and s defends each of its members."""
    return conflict_free(attacks, s) and frozenset(s) <= characteristic(args, attacks, s)


def is_complete(args, attacks, s):
    """Conflict-free, and s is a fixed point of the characteristic function."""
    return conflict_free(attacks, s) and frozenset(s) == characteristic(args, attacks, s)


def is_stable(args, attacks, s):
    """Conflict-free, and s attacks every argument outside s."""
    return conflict_free(attacks, s) and range_of(attacks, s) == frozenset(args)


def subsets(args):
    """All subsets of args as frozensets, smallest first. At most MAX_ARGS arguments."""
    items = sorted(args)
    if len(items) > MAX_ARGS:
        raise ValueError(f"brute force accepts at most {MAX_ARGS} arguments")
    for k in range(len(items) + 1):
        for c in combinations(items, k):
            yield frozenset(c)


def _maximal(sets):
    """The members of sets that are maximal under set inclusion."""
    return [s for s in sets if not any(s < t for t in sets)]


def _key(s):
    return (len(s), sorted(s))


def admissible_sets(args, attacks):
    return sorted((s for s in subsets(args) if is_admissible(args, attacks, s)), key=_key)


def complete_extensions(args, attacks):
    return sorted((s for s in subsets(args) if is_complete(args, attacks, s)), key=_key)


def grounded_extension(args, attacks):
    """The complete extension that is a subset of every complete extension."""
    comp = complete_extensions(args, attacks)
    least = [s for s in comp if all(s <= t for t in comp)]
    assert len(least) == 1
    return least[0]


def preferred_extensions(args, attacks):
    """The admissible sets that are maximal under set inclusion."""
    return sorted(_maximal(admissible_sets(args, attacks)), key=_key)


def stable_extensions(args, attacks):
    return sorted((s for s in subsets(args) if is_stable(args, attacks, s)), key=_key)


def semi_stable_extensions(args, attacks):
    """The complete extensions whose range is maximal under set inclusion."""
    comp = complete_extensions(args, attacks)
    rng = {s: range_of(attacks, s) for s in comp}
    return sorted((s for s in comp if not any(rng[s] < rng[t] for t in comp)), key=_key)


def labelling(args, attacks, s):
    """The labelling of the set s: in for s, out for S+, undec for the rest."""
    hit = attacked_by(attacks, s)
    return {a: "in" if a in s else "out" if a in hit else "undec" for a in args}
grounded_extension_algorithm.py 57 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/grounded-extension-algorithm/. The authors are listed in prov/XNWEB6.jsonl.

"""Linear-time computation of the grounded extension of a finite framework.

A framework is a pair (args, attacks): args is a set of hashable arguments and
attacks is a set of pairs (a, b), read "a attacks b". The method keeps, for each
argument, the number of its attackers that are not yet labelled out. An argument
whose count reaches 0 is labelled in, and each argument that it attacks is
labelled out. This is the counter method of unit propagation and of Horn-SAT
forward chaining, applied to the attack graph.
"""

from collections import deque


def grounded_labelling(args, attacks, stats=None):
    """Return the grounded labelling as a dict that maps each argument to in, out or undec.

    The in arguments form the grounded extension, the out arguments are the ones
    it attacks, and the rest are undec. Time Theta(n + m) for n arguments and m
    attacks. If stats is a dict, it receives the number of queue pops and the
    number of attack edges scanned in the main loop.
    """
    succ = {a: [] for a in args}
    count = {a: 0 for a in args}  # @line L1
    for a, b in attacks:
        if a not in succ or b not in succ:
            raise ValueError(f"attack {(a, b)!r} names an argument outside args")
        succ[a].append(b)
        count[b] += 1  # attacks is a set, so each attacker counts once
    label = {a: "undec" for a in args}  # @line L2
    queue = deque(a for a in sorted(args) if count[a] == 0)  # @line L3
    pops = scans = 0
    while queue:  # @line L4
        a = queue.popleft()  # @line L5
        pops += 1
        label[a] = "in"  # @line L6
        for b in succ[a]:  # @line L7
            scans += 1
            if label[b] == "undec":  # @line L8
                label[b] = "out"  # @line L9
                for c in succ[b]:  # @line L10
                    scans += 1
                    count[c] -= 1  # @line L11
                    if count[c] == 0:  # @line L12
                        queue.append(c)
    if stats is not None:
        stats["pops"] = pops
        stats["scans"] = scans
    return label  # @line L13


def grounded_extension_algorithm(args, attacks, stats=None):
    """Return the grounded extension of (args, attacks) as a frozenset."""
    label = grounded_labelling(args, attacks, stats)
    return frozenset(a for a, x in label.items() if x == "in")
test_grounded_extension_algorithm.py 104 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/grounded-extension-algorithm/. The authors are listed in prov/XNWEB6.jsonl.

import random

import pytest

import af
from grounded_extension_algorithm import grounded_extension_algorithm, grounded_labelling

# The framework of the page: a -> b -> c -> d -> e, e <-> f, g attacks g and d.
EXAMPLE_ARGS = set("abcdefg")
EXAMPLE_ATTACKS = {("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("f", "e"), ("g", "g"), ("g", "d")}


def least_fixed_point(args, attacks):
    """Iterate the characteristic function from the empty set (Dung 1995, Definition 20)."""
    s = frozenset()
    while True:
        t = af.characteristic(args, attacks, s)
        if t == s:
            return s
        s = t


def random_framework(rng, n, p):
    args = set(range(n))
    attacks = {(a, b) for a in range(n) for b in range(n) if rng.random() < p}
    return args, attacks


def test_example() -> None:
    stats = {}
    label = grounded_labelling(EXAMPLE_ARGS, EXAMPLE_ATTACKS, stats)
    assert label == {"a": "in", "b": "out", "c": "in", "d": "out", "e": "undec", "f": "undec", "g": "undec"}
    assert stats == {"pops": 2, "scans": 4}
    assert grounded_extension_algorithm(EXAMPLE_ARGS, EXAMPLE_ATTACKS) == frozenset("ac")
    assert af.grounded_extension(EXAMPLE_ARGS, EXAMPLE_ATTACKS) == frozenset("ac")


def test_small_cases() -> None:
    assert grounded_extension_algorithm(set(), set()) == frozenset()
    assert grounded_extension_algorithm({1}, set()) == frozenset({1})
    assert grounded_extension_algorithm({1}, {(1, 1)}) == frozenset()
    assert grounded_extension_algorithm({1, 2}, {(1, 2), (2, 1)}) == frozenset()
    assert grounded_extension_algorithm({1, 2, 3}, {(1, 2), (2, 3), (3, 1)}) == frozenset()
    # A chain 0 -> 1 -> ... -> 9: the even positions are in.
    chain = {(i, i + 1) for i in range(9)}
    assert grounded_extension_algorithm(set(range(10)), chain) == frozenset(range(0, 10, 2))
    # A self-attacker is never in, so an argument that only it attacks stays undec.
    assert grounded_labelling({1, 2}, {(2, 2), (2, 1)}) == {1: "undec", 2: "undec"}


def test_rejects_unknown_argument() -> None:
    with pytest.raises(ValueError):
        grounded_labelling({1, 2}, {(1, 3)})


def test_against_brute_force() -> None:
    rng = random.Random(20240601)
    for _ in range(400):
        n = rng.randint(0, 8)
        args, attacks = random_framework(rng, n, rng.choice([0.1, 0.2, 0.3, 0.5]))
        got = grounded_extension_algorithm(args, attacks)
        assert got == af.grounded_extension(args, attacks)
        assert got == least_fixed_point(args, attacks)
        assert af.is_complete(args, attacks, got)
        assert grounded_labelling(args, attacks) == af.labelling(args, attacks, got)


def test_against_fixed_point_on_larger_frameworks() -> None:
    rng = random.Random(77)
    for _ in range(60):
        n = rng.randint(10, 120)
        args, attacks = random_framework(rng, n, rng.choice([0.005, 0.01, 0.03, 0.08]))
        got = grounded_extension_algorithm(args, attacks)
        assert got == least_fixed_point(args, attacks)
        assert grounded_labelling(args, attacks) == af.labelling(args, attacks, got)


def test_linear_work() -> None:
    rng = random.Random(5)
    cases = [random_framework(rng, n, p) for n, p in [(50, 0.02), (200, 0.005), (300, 0.01), (100, 0.2)]]
    # A long chain needs n rounds of the characteristic function, but linear work here.
    cases.append((set(range(3000)), {(i, i + 1) for i in range(2999)}))
    for args, attacks in cases:
        stats = {}
        label = grounded_labelling(args, attacks, stats)
        n_in = sum(1 for x in label.values() if x == "in")
        assert stats["pops"] == n_in <= len(args)
        # Each attack (x, y) is scanned at most once: when x becomes in or when x becomes out.
        assert stats["scans"] <= len(attacks)


def test_order_does_not_matter() -> None:
    rng = random.Random(11)
    for _ in range(50):
        args, attacks = random_framework(rng, 30, 0.06)
        names = list(range(30))
        rng.shuffle(names)
        renamed_args = {names[a] for a in args}
        renamed_attacks = {(names[a], names[b]) for a, b in attacks}
        got = grounded_extension_algorithm(args, attacks)
        assert grounded_extension_algorithm(renamed_args, renamed_attacks) == frozenset(names[a] for a in got)

History

Dung defined the grounded extension in 1995 as the least fixed point of the characteristic function, and showed that for finitary frameworks it is the limit of the iteration from the empty set. Modgil and Caminada described labelling algorithms for the main semantics in 2009, and the early solvers implemented their loop for the grounded case. Nofal, Atkinson and Dunne published the counter method with a proof of the linear bound in 2021. The same counting idea is older in logic: it is the linear-time Horn satisfiability algorithm of Dowling and Gallier (1984).

References

  1. Samer Nofal, Katie Atkinson and Paul E. Dunne, Computing grounded extensions of abstract argumentation frameworks, The Computer Journal 64(1), 54 to 63 (2021), Algorithm 3 with Theorems 1 and 2 (the counter method and its O(|V| + |E|) bound), and Algorithm 2 with Theorem 3 (the earlier labelling loop and its O(|V| + |S||E|) bound)
  2. Phan Minh Dung, On the acceptability of arguments and its fundamental role in nonmonotonic reasoning, logic programming and n-person games, Artificial Intelligence 77(2), 321 to 357 (1995), Section 2, Definitions 16 and 20, Lemma 24, Theorem 25 and Lemma 28 (pp. 328 to 330, the characteristic function and the grounded extension)
  3. Sanjay Modgil and Martin Caminada, Proof theories and algorithms for abstract argumentation frameworks, in Guillermo Simari and Iyad Rahwan (eds), Argumentation in Artificial Intelligence, Springer (2009), pp. 105 to 129 (labelling algorithms, including the one for the grounded extension)
  4. Wolfgang Dvořák and Paul E. Dunne, Computational problems in formal argumentation and their complexity, in Pietro Baroni, Dov Gabbay, Massimiliano Giacomin and Leendert van der Torre (eds), Handbook of Formal Argumentation, volume 1, College Publications (2018), Section 3.3, Table 1 and Reduction 3.8 (grounded acceptance is P-complete)
  5. William F. Dowling and Jean H. Gallier, Linear-time algorithms for testing the satisfiability of propositional Horn formulae, Journal of Logic Programming 1(3), 267 to 284 (1984), the whole paper (the same counter method for Horn formulas)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example