// Souffle - A Datalog Compiler
// Copyright (c) 2017, The Souffle Developers. All rights reserved
// Licensed under the Universal Permissive License v 1.0 as shown at:
// - https://opensource.org/licenses/UPL
// - <souffle root>/licenses/SOUFFLE-UPL.txt

.type Node <: symbol
.type Card <: number

.decl Edge			(m:Node, n:Node)
.input Edge

Edge(m, n) :- Edge(n, m).

.decl Leaf			(n:Node)
.decl NotLeaf		(n:Node)

NotLeaf(n) :- Edge(a, n), Edge(b, n), a != b.
Leaf(n) :- Edge(_, n), !NotLeaf(n).

.decl InSubtree		(m:Node, n:Node, a:Node)

InSubtree(m, n, n) :- Edge(m, n).
InSubtree(m, n, a) :- Edge(m, n), Edge(n, p), InSubtree(n, p, a), m != p.

.decl TreeCard		(c:Card)
.decl SubtreeCard	(m:Node, n:Node, c:Card)

TreeCard(a + b) :- a = sum 1 : Leaf(_), b = sum 1 : NotLeaf(_).

SubtreeCard(m, n, c) :- Edge(m, n), c = sum 1 : InSubtree(m, n, _).

.decl Centroid		(n:Node)
.decl NotCentroid	(n:Node)

NotCentroid(n) :- TreeCard(c), SubtreeCard(n, _, d), d > c/2.
Centroid(n) :- Edge(n, _), !NotCentroid(n).

.output Centroid
