Skip to content

Commit cb45fe5

Browse files
authored
Update the compatibility with Graphs API (#15)
* update the compatibility with Graphs API * 2prev * fix docs * formatting
1 parent fa2f106 commit cb45fe5

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed

docs/src/api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ Functions that compute properties of the graph:
5252
```@docs
5353
nv
5454
ne
55+
vertices
56+
has_vertex
5557
has_edge
5658
edges
5759
neighbors
5860
all_neighbors
61+
inneighbors
62+
outneighbors
5963
degree
6064
indegree
6165
outdegree
6266
density
6367
is_bipartite
68+
is_directed
6469
```

src/BipartiteFactorGraphs.jl

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@ module BipartiteFactorGraphs
22

33
using Graphs
44
import Graphs:
5-
add_edge!, has_edge, edges, neighbors, nv, ne, all_neighbors, degree, indegree, outdegree, density, is_bipartite
5+
AbstractGraph,
6+
vertices,
7+
has_vertex,
8+
add_edge!,
9+
has_edge,
10+
edges,
11+
neighbors,
12+
nv,
13+
ne,
14+
all_neighbors,
15+
inneighbors,
16+
outneighbors,
17+
degree,
18+
indegree,
19+
outdegree,
20+
density,
21+
is_bipartite,
22+
is_directed
623

724
export BipartiteFactorGraph,
825
add_variable!,
@@ -19,18 +36,23 @@ export BipartiteFactorGraph,
1936
num_variables,
2037
num_factors,
2138
# Reexport used Graphs functions
39+
vertices,
40+
has_vertex,
2241
add_edge!,
2342
has_edge,
2443
edges,
2544
neighbors,
2645
nv,
2746
ne,
2847
all_neighbors,
48+
inneighbors,
49+
outneighbors,
2950
degree,
3051
indegree,
3152
outdegree,
3253
density,
33-
is_bipartite
54+
is_bipartite,
55+
is_directed
3456

3557
struct UnorderedPair{T}
3658
a::T
@@ -97,7 +119,7 @@ struct BipartiteFactorGraph{
97119
DVars <: AbstractDict{Int, TVar},
98120
DFacs <: AbstractDict{Int, TFac},
99121
DE <: AbstractDict{UnorderedPair{Int}, E}
100-
}
122+
} <: AbstractGraph{Int}
101123
graph::SimpleGraph{Int}
102124
variable_data::DVars
103125
factor_data::DFacs
@@ -289,6 +311,26 @@ Get the number of factor nodes in the graph.
289311
"""
290312
num_factors(g::BipartiteFactorGraph) = length(g.factor_data)
291313

314+
"""
315+
vertices(g::BipartiteFactorGraph)
316+
317+
Get all vertices in the graph. Note, that it returns vertices that represent both variable and factor nodes.
318+
Use [`variables`](@ref) and [`factors`](@ref) to get only variable or factor nodes.
319+
"""
320+
function Graphs.vertices(g::BipartiteFactorGraph)
321+
return Graphs.vertices(g.graph)
322+
end
323+
324+
"""
325+
has_vertex(g::BipartiteFactorGraph, v::Int)
326+
327+
Check if vertex `v` is in the graph. Note, that it returns true for both variable and factor nodes.
328+
Use [`is_variable`](@ref) and [`is_factor`](@ref) to check existence of a node with a specific type.
329+
"""
330+
function Graphs.has_vertex(g::BipartiteFactorGraph, v::Int)
331+
return Graphs.has_vertex(g.graph, v)
332+
end
333+
292334
"""
293335
has_edge(g::BipartiteFactorGraph, var::Int, fac::Int)
294336
@@ -340,6 +382,24 @@ function all_neighbors(g::BipartiteFactorGraph, v::Int)
340382
return Graphs.neighbors(g.graph, v)
341383
end
342384

385+
"""
386+
inneighbors(g::BipartiteFactorGraph, v::Int)
387+
388+
Return a list of all in-neighbors of vertex `v` in graph `g`.
389+
"""
390+
function inneighbors(g::BipartiteFactorGraph, v::Int)
391+
return Graphs.inneighbors(g.graph, v)
392+
end
393+
394+
"""
395+
outneighbors(g::BipartiteFactorGraph, v::Int)
396+
397+
Return a list of all out-neighbors of vertex `v` in graph `g`.
398+
"""
399+
function outneighbors(g::BipartiteFactorGraph, v::Int)
400+
return Graphs.outneighbors(g.graph, v)
401+
end
402+
343403
"""
344404
degree(g::BipartiteFactorGraph[, v])
345405
@@ -405,4 +465,13 @@ function is_bipartite(g::BipartiteFactorGraph)
405465
return Graphs.is_bipartite(g.graph)
406466
end
407467

468+
"""
469+
is_directed(g::BipartiteFactorGraph)
470+
471+
Check if the graph is directed. For BipartiteFactorGraph this is always false since the graph is undirected.
472+
"""
473+
function is_directed(g::BipartiteFactorGraph)
474+
return false
475+
end
476+
408477
end # module

test/graph_api_tests.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
g = BipartiteFactorGraph(Float64, String, Bool)
66

7+
@test !is_directed(g)
8+
79
# Test empty graph properties
810
@test Graphs.nv(g) == 0
911
@test Graphs.ne(g) == 0
1012
@test Graphs.density(g) == 0.0
13+
@test Graphs.eltype(g) == Int
14+
15+
@test isempty(vertices(g))
1116

1217
# Add some nodes and edges
1318
v1 = add_variable!(g, 1.0)
@@ -17,15 +22,68 @@
1722
f1 = add_factor!(g, "factor1")
1823
f2 = add_factor!(g, "factor2")
1924

25+
@test !isempty(vertices(g))
26+
@test length(vertices(g)) == 5
2027
@test length(edges(g)) == 0
2128

29+
@test v1 in vertices(g)
30+
@test f1 in vertices(g)
31+
@test v2 in vertices(g)
32+
@test f2 in vertices(g)
33+
@test v3 in vertices(g)
34+
35+
@test v1 in variables(g)
36+
@test v2 in variables(g)
37+
@test v3 in variables(g)
38+
39+
@test f1 in factors(g)
40+
@test f2 in factors(g)
41+
42+
@test !(v1 in factors(g))
43+
@test !(v2 in factors(g))
44+
@test !(v3 in factors(g))
45+
46+
@test !(f1 in variables(g))
47+
@test !(f2 in variables(g))
48+
49+
@test has_vertex(g, v1)
50+
@test has_vertex(g, v2)
51+
@test has_vertex(g, v3)
52+
@test has_vertex(g, f1)
53+
@test has_vertex(g, f2)
54+
@test !has_vertex(g, -1)
55+
56+
@test !has_edge(g, v1, f1)
57+
@test !has_edge(g, v2, f1)
58+
@test !has_edge(g, v2, f2)
59+
@test !has_edge(g, v3, f2)
60+
@test !has_edge(g, v1, f2)
61+
2262
add_edge!(g, v1, f1, true)
2363
add_edge!(g, v2, f1, true)
2464
add_edge!(g, v2, f2, false)
2565
add_edge!(g, v3, f2, true)
2666

2767
@test length(edges(g)) == 4
2868

69+
@test has_edge(g, v1, f1)
70+
@test has_edge(g, v2, f1)
71+
@test has_edge(g, v2, f2)
72+
@test has_edge(g, v3, f2)
73+
@test !has_edge(g, v1, f2)
74+
75+
@test inneighbors(g, v1) == [f1]
76+
@test inneighbors(g, v2) == [f1, f2]
77+
@test inneighbors(g, v3) == [f2]
78+
@test inneighbors(g, f1) == [v1, v2]
79+
@test inneighbors(g, f2) == [v2, v3]
80+
81+
@test outneighbors(g, v1) == [f1]
82+
@test outneighbors(g, v2) == [f1, f2]
83+
@test outneighbors(g, v3) == [f2]
84+
@test outneighbors(g, f1) == [v1, v2]
85+
@test outneighbors(g, f2) == [v2, v3]
86+
2987
# Test graph properties
3088
@test Graphs.nv(g) == 5 # 3 variables + 2 factors
3189
@test Graphs.ne(g) == 4 # 4 edges

0 commit comments

Comments
 (0)