API Reference
Core Types
BipartiteFactorGraphs.BipartiteFactorGraph
— TypeBipartiteFactorGraph
A type-stable bipartite undirected factor graph implementation that stores data for variables, factors, and edges. Users are responsible for maintaining the bipartite structure.
After the graph is constructed, the user can use Graphs.is_bipartite(graph.graph)
to check if the graph is actually bipartite. Certain functions may work incorrectly and produce unexpected results if the underlying graph is not bipartite.
Fields
graph
: The underlying graph structurevariable_data
: A dictionary of data for variable nodes where the keys are elements of typeInt
and the values are of typeTVar
factor_data
: A dictionary of data for factor nodes where the keys are elements of typeInt
and the values are of typeTFac
edge_data
: A dictionary of data for edges between variables and factors where the keys are elements of typeUnorderedPair
and the values are of typeE
Edge data keys are stored as UnorderedPair
s to avoid duplicate entries or access errors. This means that get_edge_data(g, v1, v2)
is equivalent to get_edge_data(g, v2, v1)
. This also implies that the graph is undirected.
To construct an empty BipartiteFactorGraph with specified variable, factor and edge data types use the following constructor:
BipartiteFactorGraph(::Type{TVar}, ::Type{TFac}, ::Type{E}, dict_type::Type{D}=Dict) where {TVar,TFac,E,D}
Arguments
TVar
: The type of the variable dataTFac
: The type of the factor dataE
: The type of the edge datadict_type
: The type of the dictionary used to store the variable, factor and edge data (defaults to Base.Dict)
As an alternative to the constructor, you can use BipartiteFactorGraph()
as an alias to BipartiteFactorGraph(Any, Any, Any, Dict)
.
Example
julia> g = BipartiteFactorGraph(Int, Float64, String, Dict)
BipartiteFactorGraph{Int64, Float64, String} with 0 variables, 0 factors, and 0 edges
julia> add_variable!(g, 1);
julia> add_factor!(g, 2.0);
julia> add_edge!(g, 1, 2, "Hello");
julia> g
BipartiteFactorGraph{Int64, Float64, String} with 1 variables, 1 factors, and 1 edges
Graph Construction
Functions for creating and modifying graphs:
BipartiteFactorGraphs.add_variable!
— Functionadd_variable!(g::BipartiteFactorGraph{TVar}, data::TVar) where {TVar}
Add a variable node to the graph with associated data and return its ID.
BipartiteFactorGraphs.add_factor!
— Functionadd_factor!(g::BipartiteFactorGraph{TVar,TFac}, data::TFac) where {TVar,TFac}
Add a factor node to the graph with associated data and return its ID.
Graphs.SimpleGraphs.add_edge!
— Functionadd_edge!(g::BipartiteFactorGraph{TVar,TFac,E}, var::Int, fac::Int, data::E) where {TVar,TFac,E}
Add an edge between variable node var
and factor node fac
with associated data. User must ensure that var
is a variable node and fac
is a factor node. Edge data is stored with the original order of vertices (var, fac), but can be accessed in either order using getedgedata.
Data Access
Retrieve data associated with nodes and edges:
BipartiteFactorGraphs.get_variable_data
— Functionget_variable_data(g::BipartiteFactorGraph{TVar}, v::Int) where {TVar}
Get data associated with variable node v.
BipartiteFactorGraphs.get_factor_data
— Functionget_factor_data(g::BipartiteFactorGraph{TVar,TFac}, v::Int) where {TVar,TFac}
Get data associated with factor node v.
BipartiteFactorGraphs.get_edge_data
— Functionget_edge_data(g::BipartiteFactorGraph{TVar,TFac,E}, v1::Int, v2::Int) where {TVar,TFac,E}
Get data associated with edge between nodes v1
and v2
. Since the graph is undirected, the order of v1
and v2
doesn't matter.
Graph Query Functions
Functions that query the structure of the bipartite factor graph:
BipartiteFactorGraphs.is_variable
— Functionis_variable(g::BipartiteFactorGraph, v::Int)
Check if node v is a variable node.
BipartiteFactorGraphs.is_factor
— Functionis_factor(g::BipartiteFactorGraph, v::Int)
Check if node v is a factor node.
BipartiteFactorGraphs.variables
— Functionvariables(g::BipartiteFactorGraph)
Get all variable nodes in the graph.
BipartiteFactorGraphs.factors
— Functionfactors(g::BipartiteFactorGraph)
Get all factor nodes in the graph.
BipartiteFactorGraphs.variable_neighbors
— Functionvariable_neighbors(g::BipartiteFactorGraph, v::Int)
Get all variable neighbors of factor node v. Returns only neighbors that are variable nodes. Note that this is equivalent to neighbors(g, v)
with extra check that the node is a factor. Use neighbors(g, v)
for a version that does not check the node type.
BipartiteFactorGraphs.factor_neighbors
— Functionfactor_neighbors(g::BipartiteFactorGraph, v::Int)
Get all factor neighbors of variable node v. Returns only neighbors that are factor nodes. Note that this is equivalent to neighbors(g, v)
with extra check that the node is a variable. Use neighbors(g, v)
for a version that does not check the node type.
BipartiteFactorGraphs.num_variables
— Functionnum_variables(g::BipartiteFactorGraph)
Get the number of variable nodes in the graph.
BipartiteFactorGraphs.num_factors
— Functionnum_factors(g::BipartiteFactorGraph)
Get the number of factor nodes in the graph.
Graph Properties
Functions that compute properties of the graph:
Graphs.nv
— Functionnv(g::BipartiteFactorGraph)
Return the total number of vertices in the graph. This is the sum of variable and factor nodes.
Graphs.ne
— Functionne(g::BipartiteFactorGraph)
Return the number of edges in the graph.
Graphs.vertices
— Functionvertices(g::BipartiteFactorGraph)
Get all vertices in the graph. Note, that it returns vertices that represent both variable and factor nodes. Use variables
and factors
to get only variable or factor nodes.
Graphs.has_vertex
— Functionhas_vertex(g::BipartiteFactorGraph, v::Int)
Check if vertex v
is in the graph. Note, that it returns true for both variable and factor nodes. Use is_variable
and is_factor
to check existence of a node with a specific type.
Graphs.has_edge
— Functionhas_edge(g::BipartiteFactorGraph, var::Int, fac::Int)
Check if there is an edge between variable node var
and factor node fac
.
Graphs.edges
— Functionedges(g::BipartiteFactorGraph)
Get all edges in the graph.
Graphs.neighbors
— Functionneighbors(g::BipartiteFactorGraph, v::Int)
Get all neighbors of node v.
Graphs.all_neighbors
— Functionall_neighbors(g::BipartiteFactorGraph, v::Int)
Return a list of all neighbors of vertex v
in graph g
. This is equivalent to neighbors(g, v)
for undirected graphs.
Graphs.inneighbors
— Functioninneighbors(g::BipartiteFactorGraph, v::Int)
Return a list of all in-neighbors of vertex v
in graph g
.
Graphs.outneighbors
— Functionoutneighbors(g::BipartiteFactorGraph, v::Int)
Return a list of all out-neighbors of vertex v
in graph g
.
Graphs.degree
— Functiondegree(g::BipartiteFactorGraph[, v])
Return a vector corresponding to the number of edges connected to each vertex in graph g
. If v
is specified, only return the degree for vertex v
.
Graphs.indegree
— Functionindegree(g::BipartiteFactorGraph[, v])
For BipartiteFactorGraph this is identical to degree
since the graph is undirected.
Graphs.outdegree
— Functionoutdegree(g::BipartiteFactorGraph[, v])
For BipartiteFactorGraph this is identical to degree
since the graph is undirected.
Graphs.density
— Functiondensity(g::BipartiteFactorGraph)
Return the density of the graph.
For bipartite graphs, density is defined as the ratio of the number of actual edges to the maximum possible number of edges between variable and factor nodes (which is numvariables(g) * numfactors(g)).
Graphs.is_bipartite
— Functionis_bipartite(g::BipartiteFactorGraph)
Check if the graph is bipartite.
Graphs.is_directed
— Functionis_directed(g::BipartiteFactorGraph)
Check if the graph is directed. For BipartiteFactorGraph this is always false since the graph is undirected.