API Reference

Core Types

BipartiteFactorGraphs.BipartiteFactorGraphType
BipartiteFactorGraph

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 structure
  • variable_data: A dictionary of data for variable nodes where the keys are elements of type Int and the values are of type TVar
  • factor_data: A dictionary of data for factor nodes where the keys are elements of type Int and the values are of type TFac
  • edge_data: A dictionary of data for edges between variables and factors where the keys are elements of type UnorderedPair and the values are of type E
Note

Edge data keys are stored as UnorderedPairs 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 data
  • TFac: The type of the factor data
  • E: The type of the edge data
  • dict_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
source

Graph Construction

Functions for creating and modifying graphs:

BipartiteFactorGraphs.add_factor!Function
add_factor!(g::BipartiteFactorGraph{TVar,TFac}, data::TFac) where {TVar,TFac}

Add a factor node to the graph with associated data and return its ID.

source
Graphs.SimpleGraphs.add_edge!Function
add_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.

source

Data Access

Retrieve data associated with nodes and edges:

BipartiteFactorGraphs.get_edge_dataFunction
get_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.

source

Graph Query Functions

Functions that query the structure of the bipartite factor graph:

BipartiteFactorGraphs.variable_neighborsFunction
variable_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.

source
BipartiteFactorGraphs.factor_neighborsFunction
factor_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.

source

Graph Properties

Functions that compute properties of the graph:

Graphs.nvFunction
nv(g::BipartiteFactorGraph)

Return the total number of vertices in the graph. This is the sum of variable and factor nodes.

source
Graphs.neFunction
ne(g::BipartiteFactorGraph)

Return the number of edges in the graph.

source
Graphs.verticesFunction
vertices(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.

source
Graphs.has_vertexFunction
has_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.

source
Graphs.has_edgeFunction
has_edge(g::BipartiteFactorGraph, var::Int, fac::Int)

Check if there is an edge between variable node var and factor node fac.

source
Graphs.edgesFunction
edges(g::BipartiteFactorGraph)

Get all edges in the graph.

Note

This function behaves differently from variables and factors in that it calls Graphs.edges. The closest equivalent for nodes is the neighbors function.

source
Graphs.all_neighborsFunction
all_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.

source
Graphs.inneighborsFunction
inneighbors(g::BipartiteFactorGraph, v::Int)

Return a list of all in-neighbors of vertex v in graph g.

source
Graphs.outneighborsFunction
outneighbors(g::BipartiteFactorGraph, v::Int)

Return a list of all out-neighbors of vertex v in graph g.

source
Graphs.degreeFunction
degree(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.

source
Graphs.indegreeFunction
indegree(g::BipartiteFactorGraph[, v])

For BipartiteFactorGraph this is identical to degree since the graph is undirected.

source
Graphs.outdegreeFunction
outdegree(g::BipartiteFactorGraph[, v])

For BipartiteFactorGraph this is identical to degree since the graph is undirected.

source
Graphs.densityFunction
density(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)).

source
Graphs.is_directedFunction
is_directed(g::BipartiteFactorGraph)

Check if the graph is directed. For BipartiteFactorGraph this is always false since the graph is undirected.

source