Skip to content

Commit

Permalink
Add ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
scheinerman committed Aug 31, 2024
1 parent aa5c150 commit 6c910a1
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Posets"
uuid = "9a158a65-591e-4f68-ac4f-9ffef2fc51f2"
authors = ["Ed Scheinerman <ers@jhu.edu>"]
version = "0.3.1"
version = "0.3.2"

[deps]
AbstractLattices = "398f06c4-4d28-53ec-89ca-5b2656b7603d"
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ However, the expression `p[a] < p[b]` throws an error in either of these situat
* Using the syntax `p[a]` if `a` is not an element of `p`.
* Trying to compare elements of different posets (even if the two posets are equal).

#### Comparability check
### Comparability check

The functions `are_comparable(p,a,b)` and `are_incomparable(p,a,b)` behave as follows:
* `are_comparable(p,a,b)` returns `true` exactly when `a` and `b` are both in the poset, and one of the following is true: `a<b`, `a==b`, or `a>b`.
* `are_incompable(p,a,b)` returns `true` exactly when `a` and `b` are both in the poset, but none of the follower are true: `a<b`, `a==b`, or `a>b`.

Alternatively, use `p[a] ⟂ p[b]` to test if `a` and `b` are comparable, and use `p[a] ∥ p[b]` to test if `a` and `b` are incomparable.

#### Chain/antichain check
### Chain/antichain check

Given a list of elements `vlist` of a poset `p`:

Expand Down Expand Up @@ -451,6 +451,16 @@ julia> dimension(p)

> **Note**: Computation of the dimension of a poset is NP-hard. The `dimension` function may be slow, even for moderate-size posets.
### Ranking

The function `ranking(p)` returns a ranking (as a dictionary) of the poset
starting from the minimal elements.
That is, the minimal elements are assigned rank 0. Elements that are above only elements of
rank 0 are assigned rank 1. Elements that are only above elements of rank 0 and 1 are assigned
rank 2. And so forth.

Note that not all posets are ranked, but this function always returns a result.


## Standard Posets

Expand Down
2 changes: 1 addition & 1 deletion docs/build/.documenter-siteinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documenter":{"julia_version":"1.10.5","generation_timestamp":"2024-08-29T16:52:21","documenter_version":"1.5.0"}}
{"documenter":{"julia_version":"1.10.5","generation_timestamp":"2024-08-31T15:16:50","documenter_version":"1.5.0"}}
6 changes: 3 additions & 3 deletions docs/build/index.html

Large diffs are not rendered by default.

Binary file modified docs/build/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/build/search_index.js

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ However, the expression `p[a] < p[b]` throws an error in either of these situat
* Using the syntax `p[a]` if `a` is not an element of `p`.
* Trying to compare elements of different posets (even if the two posets are equal).

#### Comparability check
### Comparability check

The functions `are_comparable(p,a,b)` and `are_incomparable(p,a,b)` behave as follows:
* `are_comparable(p,a,b)` returns `true` exactly when `a` and `b` are both in the poset, and one of the following is true: `a<b`, `a==b`, or `a>b`.
* `are_incompable(p,a,b)` returns `true` exactly when `a` and `b` are both in the poset, but none of the follower are true: `a<b`, `a==b`, or `a>b`.

Alternatively, use `p[a] ⟂ p[b]` to test if `a` and `b` are comparable, and use `p[a] ∥ p[b]` to test if `a` and `b` are incomparable.

#### Chain/antichain check
### Chain/antichain check

Given a list of elements `vlist` of a poset `p`:

Expand Down Expand Up @@ -451,6 +451,16 @@ julia> dimension(p)

> **Note**: Computation of the dimension of a poset is NP-hard. The `dimension` function may be slow, even for moderate-size posets.
### Ranking

The function `ranking(p)` returns a ranking (as a dictionary) of the poset
starting from the minimal elements.
That is, the minimal elements are assigned rank 0. Elements that are above only elements of
rank 0 are assigned rank 1. Elements that are only above elements of rank 0 and 1 are assigned
rank 2. And so forth.

Note that not all posets are ranked, but this function always returns a result.


## Standard Posets

Expand Down
1 change: 1 addition & 0 deletions src/Posets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export Poset,
nv,
random_linear_order,
random_poset,
ranking,
realizer,
relations,
rem_relation!,
Expand Down
28 changes: 28 additions & 0 deletions src/up-down.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,31 @@ Return an iterator for all elements `k` such that `a < k < b`.
function between(p::Poset, a::Integer, b::Integer)
return (k for k in 1:nv(p) if p(a, k) && p(k, b))
end

"""
ranking(p::Poset)::Dict{Int,Int}
Create a ranking of the poset `p`. This is a dictionary that
gives a ranking for each element of `p`. Minimals have rank 0.
Elements that are over only minimals have grade 1. And so forth.
"""
function ranking(p::Poset)::Dict{Int,Int}
n = nv(p)
gr = Dict{Int,Int}()
for v in 1:n
gr[v] = 0
end
done = Set{Int}()
todo = Set{Int}(1:n)
g = -1
while length(todo) > 0
g += 1
next = [v for v in todo if below(p, v) done]
for x in next
gr[x] = g
delete!(todo, x)
push!(done, x)
end
end
return gr
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ end
q = random_poset(5)
iso(p * q, q * p)
@test nv(p * q) == nv(p) * nv(q)

p = subset_lattice(4)
r = ranking(p)
mids = [v for v in 1:nv(p) if r[v] == 2]
@test length(mids) == binomial(4, 2)
end

@testset "Connection" begin
Expand Down

0 comments on commit 6c910a1

Please sign in to comment.