https://github.com/stecrotti/indexedfactorgraphs.jl
Factor graphs based on IndexedGraphs.jl
https://github.com/stecrotti/indexedfactorgraphs.jl
factor-graphs factorized-distribution-algorithms graphs message-passing
Last synced: 4 months ago
JSON representation
Factor graphs based on IndexedGraphs.jl
- Host: GitHub
- URL: https://github.com/stecrotti/indexedfactorgraphs.jl
- Owner: stecrotti
- License: mit
- Created: 2024-04-08T09:19:20.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-11-24T21:08:19.000Z (7 months ago)
- Last Synced: 2025-11-28T02:55:28.335Z (7 months ago)
- Topics: factor-graphs, factorized-distribution-algorithms, graphs, message-passing
- Language: Julia
- Homepage:
- Size: 824 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# IndexedFactorGraphs
[](https://stecrotti.github.io/IndexedFactorGraphs.jl/dev)
[](https://github.com/stecrotti/IndexedFactorGraphs.jl/actions/workflows/CI.yml?query=branch%3Amain)
[](https://codecov.io/gh/stecrotti/IndexedFactorGraphs.jl)
A Julia package to work with [factor graphs](https://en.wikipedia.org/wiki/Factor_graph), based on [IndexedGraphs.jl](https://github.com/stecrotti/IndexedGraphs.jl).
## Installation
```julia
julia> using Pkg; Pkg.add("IndexedFactorGraphs")
```
## Basics
A factor graph is a set of variable and factor vertices connected by edges.
In the spirit of [IndexedGraphs.jl](https://github.com/stecrotti/IndexedGraphs.jl), here each edge comes with an index, which can be used to access edge properties (e.g. messages in a message-passing algorithm).
### Graph construction
A `FactorGraph` can be constructed starting from an adjacency matrix, with the convention that rows represent factor vertices and columns represent variable vertices
```julia
julia> using IndexedFactorGraphs
julia> g = FactorGraph([0 1 1 0;
1 0 0 0;
0 0 1 1])
FactorGraph{Int64} with 4 variables, 3 factors, and 5 edges
```
Alternatively, use one of the provided generators for random factor graphs
```julia
julia> g_rand = rand_factor_graph(5, 3, 6)
FactorGraph{Int64} with 5 variables, 3 factors, and 6 edges
```
### Graph navigation
Given a factor graph with $N$ variables and $M$ factors, variables are indexed by $i\in\{1,\ldots,N\}$, factors are indexed by $a\in\{1,\ldots,M\}$.
Properties of a vertex can be queried by wrapping the vertex index in a `v_vertex` or `f_vertex`. For example, the list of neighbors of variable $i=2$ is found by
```julia
julia> ∂i = neighbors(g, variable(2));
julia> collect(∂i)
2-element Vector{Int64}:
1
3
```
where $1,3$ are to be interpreted as indices of factor vertices.
The list of edges adjacent to factor $a=1$ is found by
```julia
julia> ea = inedges(g, factor(1));
julia> collect(ea)
3-element Vector{IndexedGraphs.IndexedEdge{Int64}}:
Indexed Edge 1 => 1 with index 1
Indexed Edge 2 => 1 with index 2
Indexed Edge 5 => 1 with index 6
```
Querying properties of a vertex without specifying whether it's a variable or a factor will throw an error
```julia
julia> outedges(g, 3)
ERROR: ArgumentError: Properties of a vertex of an `AbstractFactorGraph` such as degree, neighbors, etc. cannot be accessed by an integer. Use a `v_vertex` or `f_vertex` wrapper instead.
```
## See also
For less lightweight implementations, also including message-passing algorithms, check out
* [FactorGraph.jl](https://github.com/mcosovic/FactorGraph.jl)
* [ForneyLab.jl](https://github.com/biaslab/ForneyLab.jl)