GNNGraph
Documentation page for the graph type GNNGraph provided by GNNGraphs.jl and related methods.
Besides the methods documented here, one can rely on the large set of functionalities given by Graphs.jl thanks to the fact that GNNGraph inherits from Graphs.AbstractGraph.
GNNGraph type
GNNGraphs.GNNGraph — TypeGNNGraph(data; [graph_type, ndata, edata, gdata, num_nodes, graph_indicator, dir])
GNNGraph(g::GNNGraph; [ndata, edata, gdata])A type representing a graph structure that also stores feature arrays associated to nodes, edges, and the graph itself.
The feature arrays are stored in the fields ndata, edata, and gdata as DataStore objects offering a convenient dictionary-like and namedtuple-like interface. The features can be passed at construction time or added later.
A GNNGraph can be constructed out of different data objects expressing the connections inside the graph. The internal representation type is determined by graph_type.
When constructed from another GNNGraph, the internal graph representation is preserved and shared. The node/edge/graph features are retained as well, unless explicitely set by the keyword arguments ndata, edata, and gdata.
A GNNGraph can also represent multiple graphs batched togheter (see MLUtils.batch or SparseArrays.blockdiag). The field g.graph_indicator contains the graph membership of each node.
GNNGraphs are always directed graphs, therefore each edge is defined by a source node and a target node (see edge_index). Self loops (edges connecting a node to itself) and multiple edges (more than one edge between the same pair of nodes) are supported.
A GNNGraph is a Graphs.jl's AbstractGraph, therefore it supports most functionality from that library.
Arguments
data: Some data representing the graph topology. Possible type are- An adjacency matrix
- An adjacency list.
- A tuple containing the source and target vectors (COO representation)
- A Graphs.jl' graph.
graph_type: A keyword argument that specifies the underlying representation used by the GNNGraph. Currently supported values are:coo. Graph represented as a tuple(source, target), such that thek-th edge connects the nodesource[k]to nodetarget[k]. Optionally, also edge weights can be given:(source, target, weights).:sparse. A sparse adjacency matrix representation.:dense. A dense adjacency matrix representation.
:coo, currently the most supported type.dir: The assumed edge direction when given adjacency matrix or adjacency list input datag. Possible values are:outand:in. Default:out.num_nodes: The number of nodes. If not specified, inferred fromg. Defaultnothing.graph_indicator: For batched graphs, a vector containing the graph assignment of each node. Defaultnothing.ndata: Node features. An array or named tuple of arrays whose last dimension has sizenum_nodes.edata: Edge features. An array or named tuple of arrays whose last dimension has sizenum_edges.gdata: Graph features. An array or named tuple of arrays whose last dimension has sizenum_graphs.
Examples
using GNNGraphs
# Construct from adjacency list representation
data = [[2,3], [1,4,5], [1], [2,5], [2,4]]
g = GNNGraph(data)
# Number of nodes, edges, and batched graphs
g.num_nodes # 5
g.num_edges # 10
g.num_graphs # 1
# Same graph in COO representation
s = [1,1,2,2,2,3,4,4,5,5]
t = [2,3,1,4,5,3,2,5,2,4]
g = GNNGraph(s, t)
# From a Graphs' graph
g = GNNGraph(erdos_renyi(100, 20))
# Add 2 node feature arrays at creation time
g = GNNGraph(g, ndata = (x=rand(100, g.num_nodes), y=rand(g.num_nodes)))
# Add 1 edge feature array, after the graph creation
g.edata.z = rand(16, g.num_edges)
# Add node features and edge features with default names `x` and `e`
g = GNNGraph(g, ndata = rand(100, g.num_nodes), edata = rand(16, g.num_edges))
g.ndata.x # or just g.x
g.edata.e # or just g.e
# Collect edges' source and target nodes.
# Both source and target are vectors of length num_edges
source, target = edge_index(g)A GNNGraph can be sent to the GPU, for example by using Flux.jl's gpu function or MLDataDevices.jl's utilities. ```
Base.copy — Functioncopy(g::GNNGraph; deep=false)Create a copy of g. If deep is true, then copy will be a deep copy (equivalent to deepcopy(g)), otherwise it will be a shallow copy with the same underlying graph data.
DataStore
GNNGraphs.DataStore — TypeDataStore([n, data])
DataStore([n,] k1 = x1, k2 = x2, ...)A container for feature arrays. The optional argument n enforces that numobs(x) == n for each array contained in the datastore.
At construction time, the data can be provided as any iterables of pairs of symbols and arrays or as keyword arguments:
julia> ds = DataStore(3, x = rand(Float32, 2, 3), y = rand(Float32, 3))
DataStore(3) with 2 elements:
y = 3-element Vector{Float32}
x = 2×3 Matrix{Float32}
julia> ds = DataStore(3, Dict(:x => rand(Float32, 2, 3), :y => rand(Float32, 3))); # equivalent to aboveThe DataStore has an interface similar to both dictionaries and named tuples. Arrays can be accessed and added using either the indexing or the property syntax:
julia> ds = DataStore(x = ones(Float32, 2, 3), y = zeros(Float32, 3))
DataStore() with 2 elements:
y = 3-element Vector{Float32}
x = 2×3 Matrix{Float32}
julia> ds.x # same as `ds[:x]`
2×3 Matrix{Float32}:
1.0 1.0 1.0
1.0 1.0 1.0
julia> ds.z = zeros(Float32, 3) # Add new feature array `z`. Same as `ds[:z] = rand(Float32, 3)`
3-element Vector{Float32}:
0.0
0.0
0.0The DataStore can be iterated over, and the keys and values can be accessed using keys(ds) and values(ds). map(f, ds) applies the function f to each feature array:
julia> ds2 = map(x -> x .+ 1, ds)
DataStore() with 3 elements:
y = 3-element Vector{Float32}
z = 3-element Vector{Float32}
x = 2×3 Matrix{Float32}
julia> ds2.z
3-element Vector{Float32}:
1.0
1.0
1.0Query
GNNGraphs.adjacency_list — Methodadjacency_list(g; dir=:out)
adjacency_list(g, nodes; dir=:out)Return the adjacency list representation (a vector of vectors) of the graph g.
Calling a the adjacency list, if dir=:out than a[i] will contain the neighbors of node i through outgoing edges. If dir=:in, it will contain neighbors from incoming edges instead.
If nodes is given, return the neighborhood of the nodes in nodes only.
GNNGraphs.edge_index — Methodedge_index(g::GNNGraph)Return a tuple containing two vectors, respectively storing the source and target nodes for each edges in g.
s, t = edge_index(g)GNNGraphs.get_graph_type — Methodget_graph_type(g::GNNGraph)Return the underlying representation for the graph g as a symbol.
Possible values are:
:coo: Coordinate list representation. The graph is stored as a tuple of vectors(s, t, w), wheresandtare the source and target nodes of the edges, andwis the edge weights.:sparse: Sparse matrix representation. The graph is stored as a sparse matrix representing the weighted adjacency matrix.:dense: Dense matrix representation. The graph is stored as a dense matrix representing the weighted adjacency matrix.
The default representation for graph constructors GNNGraphs.jl is :coo. The underlying representation can be accessed through the g.graph field.
See also GNNGraph.
Examples
The default representation for graph constructors GNNGraphs.jl is :coo.
julia> g = rand_graph(5, 10)
GNNGraph:
num_nodes: 5
num_edges: 10
julia> get_graph_type(g)
:cooThe GNNGraph constructor can also be used to create graphs with different representations.
julia> g = GNNGraph([2,3,5], [1,2,4], graph_type=:sparse)
GNNGraph:
num_nodes: 5
num_edges: 3
julia> g.graph
5×5 SparseArrays.SparseMatrixCSC{Int64, Int64} with 3 stored entries:
⋅ ⋅ ⋅ ⋅ ⋅
1 ⋅ ⋅ ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 1 ⋅
julia> get_graph_type(g)
:sparse
julia> gcoo = GNNGraph(g, graph_type=:coo);
julia> gcoo.graph
([2, 3, 5], [1, 2, 4], [1, 1, 1])GNNGraphs.graph_indicator — Methodgraph_indicator(g::GNNGraph; edges=false)Return a vector containing the graph membership (an integer from 1 to g.num_graphs) of each node in the graph. If edges=true, return the graph membership of each edge instead.
GNNGraphs.has_isolated_nodes — Methodhas_isolated_nodes(g::GNNGraph; dir=:out)Return true if the graph g contains nodes with out-degree (if dir=:out) or in-degree (if dir = :in) equal to zero.
GNNGraphs.has_multi_edges — Methodhas_multi_edges(g::GNNGraph)Return true if g has any multiple edges.
GNNGraphs.is_bidirected — Methodis_bidirected(g::GNNGraph)Check if the directed graph g essentially corresponds to an undirected graph, i.e. if for each edge it also contains the reverse edge.
GNNGraphs.khop_adj — Functionkhop_adj(g::GNNGraph,k::Int,T::DataType=eltype(g); dir=:out, weighted=true)Return $A^k$ where $A$ is the adjacency matrix of the graph 'g'.
GNNGraphs.laplacian_lambda_max — Functionlaplacian_lambda_max(g::GNNGraph, T=Float32; add_self_loops=false, dir=:out)Return the largest eigenvalue of the normalized symmetric Laplacian of the graph g.
If the graph is batched from multiple graphs, return the list of the largest eigenvalue for each graph.
GNNGraphs.normalized_laplacian — Functionnormalized_laplacian(g, T=Float32; add_self_loops=false, dir=:out)Normalized Laplacian matrix of graph g.
Arguments
g: AGNNGraph.T: result element type.add_self_loops: add self-loops while calculating the matrix.dir: the edge directionality considered (:out, :in, :both).
GNNGraphs.scaled_laplacian — Functionscaled_laplacian(g, T=Float32; dir=:out)Scaled Laplacian matrix of graph g, defined as $\hat{L} = \frac{2}{\lambda_{max}} L - I$ where $L$ is the normalized Laplacian matrix.
Arguments
g: AGNNGraph.T: result element type.dir: the edge directionality considered (:out, :in, :both).
Graphs.LinAlg.adjacency_matrix — Functionadjacency_matrix(g::GNNGraph, T=eltype(g); dir=:out, weighted=true)Return the adjacency matrix A for the graph g.
If dir=:out, A[i,j] > 0 denotes the presence of an edge from node i to node j. If dir=:in instead, A[i,j] > 0 denotes the presence of an edge from node j to node i.
User may specify the eltype T of the returned matrix.
If weighted=true, the A will contain the edge weights if any, otherwise the elements of A will be either 0 or 1.
Graphs.degree — Methoddegree(g::GNNGraph, T=nothing; dir=:out, edge_weight=true)Return a vector containing the degrees of the nodes in g.
The gradient is propagated through this function only if edge_weight is true or a vector.
Arguments
g: A graph.T: Element type of the returned vector. Ifnothing, is chosen based on the graph type and will be an integer ifedge_weight = false. Defaultnothing.dir: Fordir = :outthe degree of a node is counted based on the outgoing edges. Fordir = :in, the ingoing edges are used. Ifdir = :bothwe have the sum of the two.edge_weight: Iftrueand the graph contains weighted edges, the degree will be weighted. Set tofalseinstead to just count the number of outgoing/ingoing edges. Finally, you can also pass a vector of weights to be used instead of the graph's own weights. Defaulttrue.
Graphs.has_self_loops — Methodhas_self_loops(g::GNNGraph)Return true if g has any self loops.
Graphs.inneighbors — Methodinneighbors(g::GNNGraph, i::Integer)Return the neighbors of node i in the graph g through incoming edges.
See also neighbors and outneighbors.
Graphs.outneighbors — Methodoutneighbors(g::GNNGraph, i::Integer)Return the neighbors of node i in the graph g through outgoing edges.
See also neighbors and inneighbors.
Graphs.neighbors — Methodneighbors(g::GNNGraph, i::Integer; dir=:out)Return the neighbors of node i in the graph g. If dir=:out, return the neighbors through outgoing edges. If dir=:in, return the neighbors through incoming edges.
See also outneighbors, inneighbors.
Transform
GNNGraphs.add_edges — Methodadd_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
add_edges(g::GNNGraph, (s, t); [edata])
add_edges(g::GNNGraph, (s, t, w); [edata])Add to graph g the edges with source nodes s and target nodes t. Optionally, pass the edge weight w and the features edata for the new edges. Returns a new graph sharing part of the underlying data with g.
If the s or t contain nodes that are not already present in the graph, they are added to the graph as well.
Examples
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
julia> w = Float32[1.0, 2.0, 3.0, 4.0, 5.0];
julia> g = GNNGraph((s, t, w))
GNNGraph:
num_nodes: 4
num_edges: 5
julia> add_edges(g, ([2, 3], [4, 1], [10.0, 20.0]))
GNNGraph:
num_nodes: 4
num_edges: 7julia> g = GNNGraph()
GNNGraph:
num_nodes: 0
num_edges: 0
julia> add_edges(g, [1,2], [2,3])
GNNGraph:
num_nodes: 3
num_edges: 2GNNGraphs.add_nodes — Methodadd_nodes(g::GNNGraph, n; [ndata])Add n new nodes to graph g. In the new graph, these nodes will have indexes from g.num_nodes + 1 to g.num_nodes + n.
GNNGraphs.add_self_loops — Methodadd_self_loops(g::GNNGraph)Return a graph with the same features as g but also adding edges connecting the nodes to themselves.
Nodes with already existing self-loops will obtain a second self-loop.
If the graphs has edge weights, the new edges will have weight 1.
GNNGraphs.getgraph — Methodgetgraph(g::GNNGraph, i; nmap=false)Return the subgraph of g induced by those nodes j for which g.graph_indicator[j] == i or, if i is a collection, g.graph_indicator[j] ∈ i. In other words, it extract the component graphs from a batched graph.
If nmap=true, return also a vector v mapping the new nodes to the old ones. The node i in the subgraph will correspond to the node v[i] in g.
GNNGraphs.negative_sample — Methodnegative_sample(g::GNNGraph;
num_neg_edges = g.num_edges,
bidirected = is_bidirected(g))Return a graph containing random negative edges (i.e. non-edges) from graph g as edges.
If bidirected=true, the output graph will be bidirected and there will be no leakage from the origin graph.
See also is_bidirected.
GNNGraphs.perturb_edges — Methodperturb_edges([rng], g::GNNGraph, perturb_ratio)Return a new graph obtained from g by adding random edges, based on a specified perturb_ratio. The perturb_ratio determines the fraction of new edges to add relative to the current number of edges in the graph. These new edges are added without creating self-loops.
The function returns a new GNNGraph instance that shares some of the underlying data with g but includes the additional edges. The nodes for the new edges are selected randomly, and no edge data (edata) or weights (w) are assigned to these new edges.
Arguments
g::GNNGraph: The graph to be perturbed.perturb_ratio: The ratio of the number of new edges to add relative to the current number of edges in the graph. For example, aperturb_ratioof 0.1 means that 10% of the current number of edges will be added as new random edges.rng: An optionalrandom number generator to ensure reproducible results.
Examples
julia> g = GNNGraph((s, t, w))
GNNGraph:
num_nodes: 4
num_edges: 5
julia> perturbed_g = perturb_edges(g, 0.2)
GNNGraph:
num_nodes: 4
num_edges: 6GNNGraphs.ppr_diffusion — Methodppr_diffusion(g::GNNGraph{<:COO_T}, alpha =0.85f0) -> GNNGraphCalculates the Personalized PageRank (PPR) diffusion based on the edge weight matrix of a GNNGraph and updates the graph with new edge weights derived from the PPR matrix. References paper: The pagerank citation ranking: Bringing order to the web
The function performs the following steps:
- Constructs a modified adjacency matrix
Ausing the graph's edge weights, whereAis adjusted by(α - 1) * A + I, withαbeing the damping factor (alpha_f32) andIthe identity matrix. - Normalizes
Ato ensure each column sums to 1, representing transition probabilities. - Applies the PPR formula
α * (I + (α - 1) * A)^-1to compute the diffusion matrix. - Updates the original edge weights of the graph based on the PPR diffusion matrix, assigning new weights for each edge from the PPR matrix.
Arguments
g::GNNGraph: The input graph for which PPR diffusion is to be calculated. It should have edge weights available.alpha_f32::Float32: The damping factor used in PPR calculation, controlling the teleport probability in the random walk. Defaults to0.85f0.
Returns
- A new
GNNGraphinstance with the same structure asgbut with updated edge weights according to the PPR diffusion calculation.
GNNGraphs.rand_edge_split — Methodrand_edge_split(g::GNNGraph, frac; bidirected=is_bidirected(g)) -> g1, g2Randomly partition the edges in g to form two graphs, g1 and g2. Both will have the same number of nodes as g. g1 will contain a fraction frac of the original edges, while g2 wil contain the rest.
If bidirected = true makes sure that an edge and its reverse go into the same split. This option is supported only for bidirected graphs with no self-loops and multi-edges.
rand_edge_split is tipically used to create train/test splits in link prediction tasks.
GNNGraphs.random_walk_pe — Methodrandom_walk_pe(g, walk_length)Return the random walk positional encoding from the paper Graph Neural Networks with Learnable Structural and Positional Representations of the given graph g and the length of the walk walk_length as a matrix of size (walk_length, g.num_nodes).
GNNGraphs.remove_edges — Methodremove_edges(g::GNNGraph, edges_to_remove::AbstractVector{<:Integer})
remove_edges(g::GNNGraph, p=0.5)Remove specified edges from a GNNGraph, either by specifying edge indices or by randomly removing edges with a given probability.
Arguments
g: The input graph from which edges will be removed.edges_to_remove: Vector of edge indices to be removed. This argument is only required for the first method.p: Probability of removing each edge. This argument is only required for the second method and defaults to 0.5.
Returns
A new GNNGraph with the specified edges removed.
Example
julia> using GNNGraphs
# Construct a GNNGraph
julia> g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1])
GNNGraph:
num_nodes: 3
num_edges: 5
# Remove the second edge
julia> g_new = remove_edges(g, [2]);
julia> g_new
GNNGraph:
num_nodes: 3
num_edges: 4
# Remove edges with a probability of 0.5
julia> g_new = remove_edges(g, 0.5);
julia> g_new
GNNGraph:
num_nodes: 3
num_edges: 2GNNGraphs.remove_multi_edges — Methodremove_multi_edges(g::GNNGraph; aggr=+)Remove multiple edges (also called parallel edges or repeated edges) from graph g. Possible edge features are aggregated according to aggr, that can take value +,min, max or mean.
See also remove_self_loops, has_multi_edges, and to_bidirected.
GNNGraphs.remove_nodes — Methodremove_nodes(g::GNNGraph, p)Returns a new graph obtained by dropping nodes from g with independent probabilities p.
Examples
julia> g = GNNGraph([1, 1, 2, 2, 3, 4], [1, 2, 3, 1, 3, 1])
GNNGraph:
num_nodes: 4
num_edges: 6
julia> g_new = remove_nodes(g, 0.5)
GNNGraph:
num_nodes: 2
num_edges: 2GNNGraphs.remove_nodes — Methodremove_nodes(g::GNNGraph, nodes_to_remove::AbstractVector)Remove specified nodes, and their associated edges, from a GNNGraph. This operation reindexes the remaining nodes to maintain a continuous sequence of node indices, starting from 1. Similarly, edges are reindexed to account for the removal of edges connected to the removed nodes.
Arguments
g: The input graph from which nodes (and their edges) will be removed.nodes_to_remove: Vector of node indices to be removed.
Returns
A new GNNGraph with the specified nodes and all edges associated with these nodes removed.
Example
using GNNGraphs
g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1])
# Remove nodes with indices 2 and 3, for example
g_new = remove_nodes(g, [2, 3])
# g_new now does not contain nodes 2 and 3, and any edges that were connected to these nodes.
println(g_new)GNNGraphs.remove_self_loops — Methodremove_self_loops(g::GNNGraph)Return a graph constructed from g where self-loops (edges from a node to itself) are removed.
See also add_self_loops and remove_multi_edges.
GNNGraphs.set_edge_weight — Methodset_edge_weight(g::GNNGraph, w::AbstractVector)Set w as edge weights in the returned graph.
GNNGraphs.to_bidirected — Methodto_bidirected(g)Adds a reverse edge for each edge in the graph, then calls remove_multi_edges with mean aggregation to simplify the graph.
See also is_bidirected.
Examples
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
julia> w = [1.0, 2.0, 3.0, 4.0, 5.0];
julia> e = [10.0, 20.0, 30.0, 40.0, 50.0];
julia> g = GNNGraph(s, t, w, edata = e)
GNNGraph:
num_nodes: 4
num_edges: 5
edata:
e = 5-element Vector{Float64}
julia> g2 = to_bidirected(g)
GNNGraph:
num_nodes: 4
num_edges: 7
edata:
e = 7-element Vector{Float64}
julia> edge_index(g2)
([1, 2, 2, 3, 3, 4, 4], [2, 1, 3, 2, 4, 3, 4])
julia> get_edge_weight(g2)
7-element Vector{Float64}:
1.0
1.0
2.0
2.0
3.5
3.5
5.0
julia> g2.edata.e
7-element Vector{Float64}:
10.0
10.0
20.0
20.0
35.0
35.0
50.0GNNGraphs.to_unidirected — Methodto_unidirected(g::GNNGraph)Return a graph that for each multiple edge between two nodes in g keeps only an edge in one direction.
MLUtils.batch — Methodbatch(gs::Vector{<:GNNGraph})Batch together multiple GNNGraphs into a single one containing the total number of original nodes and edges.
Equivalent to SparseArrays.blockdiag. See also MLUtils.unbatch.
Examples
julia> g1 = rand_graph(4, 4, ndata=ones(Float32, 3, 4))
GNNGraph:
num_nodes: 4
num_edges: 4
ndata:
x = 3×4 Matrix{Float32}
julia> g2 = rand_graph(5, 4, ndata=zeros(Float32, 3, 5))
GNNGraph:
num_nodes: 5
num_edges: 4
ndata:
x = 3×5 Matrix{Float32}
julia> g12 = MLUtils.batch([g1, g2])
GNNGraph:
num_nodes: 9
num_edges: 8
num_graphs: 2
ndata:
x = 3×9 Matrix{Float32}
julia> g12.ndata.x
3×9 Matrix{Float32}:
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0MLUtils.unbatch — Methodunbatch(g::GNNGraph)Opposite of the MLUtils.batch operation, returns an array of the individual graphs batched together in g.
See also MLUtils.batch and getgraph.
Examples
julia> using MLUtils
julia> gbatched = MLUtils.batch([rand_graph(5, 6), rand_graph(10, 8), rand_graph(4,2)])
GNNGraph:
num_nodes: 19
num_edges: 16
num_graphs: 3
julia> MLUtils.unbatch(gbatched)
3-element Vector{GNNGraph{Tuple{Vector{Int64}, Vector{Int64}, Nothing}}}:
GNNGraph(5, 6) with no data
GNNGraph(10, 8) with no data
GNNGraph(4, 2) with no dataSparseArrays.blockdiag — Methodblockdiag(xs::GNNGraph...)Equivalent to MLUtils.batch.
Utils
GNNGraphs.sort_edge_index — Functionsort_edge_index(ei::Tuple) -> u', v'
sort_edge_index(u, v) -> u', v'Return a sorted version of the tuple of vectors ei = (u, v), applying a common permutation to u and v. The sorting is lexycographic, that is the pairs (ui, vi) are sorted first according to the ui and then according to vi.
GNNGraphs.color_refinement — Functioncolor_refinement(g::GNNGraph, [x0]) -> x, num_colors, nitersThe color refinement algorithm for graph coloring. Given a graph g and an initial coloring x0, the algorithm iteratively refines the coloring until a fixed point is reached.
At each iteration the algorithm computes a hash of the coloring and the sorted list of colors of the neighbors of each node. This hash is used to determine if the coloring has changed.
math x_i' = hashmap((x_i, sort([x_j for j \in N(i)]))).`
This algorithm is related to the 1-Weisfeiler-Lehman algorithm for graph isomorphism testing.
Arguments
g::GNNGraph: The graph to color.x0::AbstractVector{<:Integer}: The initial coloring. If not provided, all nodes are colored with 1.
Returns
x::AbstractVector{<:Integer}: The final coloring.num_colors::Int: The number of colors used.niters::Int: The number of iterations until convergence.
Generate
GNNGraphs.knn_graph — Methodknn_graph(points::AbstractMatrix,
k::Int;
graph_indicator = nothing,
self_loops = false,
dir = :in,
kws...)Create a k-nearest neighbor graph where each node is linked to its k closest points.
Arguments
points: A numfeatures × numnodes matrix storing the Euclidean positions of the nodes.k: The number of neighbors considered in the kNN algorithm.graph_indicator: Either nothing or a vector containing the graph assignment of each node, in which case the returned graph will be a batch of graphs.self_loops: Iftrue, consider the node itself among itsknearest neighbors, in which case the graph will contain self-loops.dir: The direction of the edges. Ifdir=:inedges go from thekneighbors to the central node. Ifdir=:outwe have the opposite direction.kws: Further keyword arguments will be passed to theGNNGraphconstructor.
Examples
julia> n, k = 10, 3;
julia> x = rand(Float32, 3, n);
julia> g = knn_graph(x, k)
GNNGraph:
num_nodes = 10
num_edges = 30
julia> graph_indicator = [1,1,1,1,1,2,2,2,2,2];
julia> g = knn_graph(x, k; graph_indicator)
GNNGraph:
num_nodes = 10
num_edges = 30
num_graphs = 2GNNGraphs.radius_graph — Methodradius_graph(points::AbstractMatrix,
r::AbstractFloat;
graph_indicator = nothing,
self_loops = false,
dir = :in,
kws...)Create a graph where each node is linked to its neighbors within a given distance r.
Arguments
points: A numfeatures × numnodes matrix storing the Euclidean positions of the nodes.r: The radius.graph_indicator: Either nothing or a vector containing the graph assignment of each node, in which case the returned graph will be a batch of graphs.self_loops: Iftrue, consider the node itself among its neighbors, in which case the graph will contain self-loops.dir: The direction of the edges. Ifdir=:inedges go from the neighbors to the central node. Ifdir=:outwe have the opposite direction.kws: Further keyword arguments will be passed to theGNNGraphconstructor.
Examples
julia> n, r = 10, 0.75;
julia> x = rand(Float32, 3, n);
julia> g = radius_graph(x, r)
GNNGraph:
num_nodes = 10
num_edges = 46
julia> graph_indicator = [1,1,1,1,1,2,2,2,2,2];
julia> g = radius_graph(x, r; graph_indicator)
GNNGraph:
num_nodes = 10
num_edges = 20
num_graphs = 2References
Section B paragraphs 1 and 2 of the paper Dynamic Hidden-Variable Network Models
GNNGraphs.rand_graph — Methodrand_graph([rng,] n, m; bidirected=true, edge_weight = nothing, kws...)Generate a random (Erdós-Renyi) GNNGraph with n nodes and m edges.
If bidirected=true the reverse edge of each edge will be present. If bidirected=false instead, m unrelated edges are generated. In any case, the output graph will contain no self-loops or multi-edges.
A vector can be passed as edge_weight. Its length has to be equal to m in the directed case, and m÷2 in the bidirected one.
Pass a random number generator as the first argument to make the generation reproducible.
Additional keyword arguments will be passed to the GNNGraph constructor.
Examples
julia> g = rand_graph(5, 4, bidirected=false)
GNNGraph:
num_nodes: 5
num_edges: 4
julia> edge_index(g)
([4, 3, 2, 1], [5, 4, 3, 2])
# In the bidirected case, edge data will be duplicated on the reverse edges if needed.
julia> g = rand_graph(5, 4, edata=rand(Float32, 16, 2))
GNNGraph:
num_nodes: 5
num_edges: 4
edata:
e = 16×4 Matrix{Float32}
# Each edge has a reverse
julia> edge_index(g)
([1, 1, 5, 3], [5, 3, 1, 1])Operators
Base.intersect — Functionintersect(g1::GNNGraph, g2::GNNGraph)Intersect two graphs by keeping only the common edges.
Sampling
GNNGraphs.sample_neighbors — Functionsample_neighbors(g, nodes, K=-1; dir=:in, replace=false, dropnodes=false)Sample neighboring edges of the given nodes and return the induced subgraph. For each node, a number of inbound (or outbound when dir = :out) edges will be randomly chosen. Ifdropnodes=false`, the graph returned will then contain all the nodes in the original graph, but only the sampled edges.
The returned graph will contain an edge feature EID corresponding to the id of the edge in the original graph. If dropnodes=true, it will also contain a node feature NID with the node ids in the original graph.
Arguments
g. The graph.nodes. A list of node IDs to sample neighbors from.K. The maximum number of edges to be sampled for each node. If -1, all the neighboring edges will be selected.dir. Determines whether to sample inbound (:in) or outbound (`:out) edges (Default:in).replace. Iftrue, sample with replacement.dropnodes. Iftrue, the resulting subgraph will contain only the nodes involved in the sampled edges.
Examples
julia> g = rand_graph(20, 100)
GNNGraph:
num_nodes = 20
num_edges = 100
julia> sample_neighbors(g, 2:3)
GNNGraph:
num_nodes = 20
num_edges = 9
edata:
EID => (9,)
julia> sg = sample_neighbors(g, 2:3, dropnodes=true)
GNNGraph:
num_nodes = 10
num_edges = 9
ndata:
NID => (10,)
edata:
EID => (9,)
julia> sg.ndata.NID
10-element Vector{Int64}:
2
3
17
14
18
15
16
20
7
10
julia> sample_neighbors(g, 2:3, 5, replace=true)
GNNGraph:
num_nodes = 20
num_edges = 10
edata:
EID => (10,)Graphs.induced_subgraph — Methodinduced_subgraph(graph, nodes)Generates a subgraph from the original graph using the provided nodes. The function includes the nodes' neighbors and creates edges between nodes that are connected in the original graph. If a node has no neighbors, an isolated node will be added to the subgraph. Returns A new GNNGraph containing the subgraph with the specified nodes and their features.
Arguments
graph. The original GNNGraph containing nodes, edges, and node features.nodes`. A vector of node indices to include in the subgraph.
Examples
julia> s = [1, 2]
2-element Vector{Int64}:
1
2
julia> t = [2, 3]
2-element Vector{Int64}:
2
3
julia> graph = GNNGraph((s, t), ndata = (; x=rand(Float32, 32, 3), y=rand(Float32, 3)), edata = rand(Float32, 2))
GNNGraph:
num_nodes: 3
num_edges: 2
ndata:
y = 3-element Vector{Float32}
x = 32×3 Matrix{Float32}
edata:
e = 2-element Vector{Float32}
julia> nodes = [1, 2]
2-element Vector{Int64}:
1
2
julia> subgraph = Graphs.induced_subgraph(graph, nodes)
GNNGraph:
num_nodes: 2
num_edges: 1
ndata:
y = 2-element Vector{Float32}
x = 32×2 Matrix{Float32}
edata:
e = 1-element Vector{Float32}