Title: | Connected Components of an Undirected Graph |
---|---|
Description: | Provides a function for fast computation of the connected components of an undirected graph (though not faster than the components() function of the 'igraph' package) from the edges or the adjacency matrix of the graph. Based on this one, a function to compute the connected components of a triangle 'rgl' mesh is also provided. |
Authors: | Stéphane Laurent [cre, aut] |
Maintainer: | Stéphane Laurent <[email protected]> |
License: | GPL-3 |
Version: | 1.0.0 |
Built: | 2024-11-02 04:42:48 UTC |
Source: | https://github.com/stla/concom |
Provides a function which computes the connected components of an undirected graph.
There are three functions in this package: concom
, the main function,
which returns the connected components from the edges of the graph;
concomFromMatAdj
, which returns the connected components from the
adjacency matrix of the graph; concom3d
, which returns the connected
components of a triangle rgl mesh.
Stéphane Laurent.
Maintainer: Stéphane Laurent <[email protected]>
Fast computation of the connected components of an undirected graph.
concom(edges)
concom(edges)
edges |
a matrix with two columns, whose rows represent the edges of the graph; each edge is given by two vertex indices, and it is assumed that the vertex indices are 1, 2, 3, ... |
A list with four elements: indices
, an integer vector
whose i
-th element gives the label of the connected component of
vertex i
; sizes
, an integer vector giving the number of
elements of each connected component; ncomponents
, the number
of connected components; components
, a list of length
ncomponents
, whose j
-th element is the integer vector made
of the labels of the j
-th connected component.
library(concom) edges <- cbind( 1:7, c(2, 3, 1, 5, 6, 7, 4) ) concom(edges)
library(concom) edges <- cbind( 1:7, c(2, 3, 1, 5, 6, 7, 4) ) concom(edges)
Computes the connected components of a triangular 'rgl' mesh.
concom3d(tmesh)
concom3d(tmesh)
tmesh |
a triangular rgl mesh (of class mesh3d) |
A list of rgl meshes, each one corresponding to a connected component.
library(concom) library(rgl) library(rmarchingcubes) # credit to 'ICN5D' for this isosurface f <- function(x, y, z, a, cosb, sinb){ (sqrt((sqrt(x*x + (y*sinb + a*cosb)^2) - 2)^2) - 1)^2 + (sqrt((sqrt(z*z + (y*cosb - a*sinb)^2) - 2)^2) - 1)^2 } a <- 0.6 b <- 0.785 cosb <- cos(b) sinb <- sin(b) x <- z <- seq(-3.5, 3.5, len = 150L) y <- seq(-4.2, 4.2, len = 150L) g <- expand.grid(X = x, Y = y, Z = z) voxel <- array( with(g, f(X, Y, Z, a, cosb, sinb)), dim = c(150L, 150L, 150L) ) contour_shape <- contour3d( griddata = voxel, level = 0.1, x = x, y = y, z = z ) tmesh <- tmesh3d( vertices = t(contour_shape[["vertices"]]), indices = t(contour_shape[["triangles"]]), normals = contour_shape[["normals"]], homogeneous = FALSE ) components <- concom3d(tmesh) colors <- hcl.colors(length(components)) open3d(windowRect = c(50, 50, 562, 562), zoom = 0.9) lapply(1:length(components), function(i){ shade3d(components[[i]], color = colors[i]) })
library(concom) library(rgl) library(rmarchingcubes) # credit to 'ICN5D' for this isosurface f <- function(x, y, z, a, cosb, sinb){ (sqrt((sqrt(x*x + (y*sinb + a*cosb)^2) - 2)^2) - 1)^2 + (sqrt((sqrt(z*z + (y*cosb - a*sinb)^2) - 2)^2) - 1)^2 } a <- 0.6 b <- 0.785 cosb <- cos(b) sinb <- sin(b) x <- z <- seq(-3.5, 3.5, len = 150L) y <- seq(-4.2, 4.2, len = 150L) g <- expand.grid(X = x, Y = y, Z = z) voxel <- array( with(g, f(X, Y, Z, a, cosb, sinb)), dim = c(150L, 150L, 150L) ) contour_shape <- contour3d( griddata = voxel, level = 0.1, x = x, y = y, z = z ) tmesh <- tmesh3d( vertices = t(contour_shape[["vertices"]]), indices = t(contour_shape[["triangles"]]), normals = contour_shape[["normals"]], homogeneous = FALSE ) components <- concom3d(tmesh) colors <- hcl.colors(length(components)) open3d(windowRect = c(50, 50, 562, 562), zoom = 0.9) lapply(1:length(components), function(i){ shade3d(components[[i]], color = colors[i]) })
Connected components of an undirected graph from its adjacency matrix.
concomFromMatAdj(M)
concomFromMatAdj(M)
M |
adjacency matrix; it must be a square symmetric matrix with
numeric or Boolean entries, whose non-zero or |
The output is the same as the one of the concom
function.
matAdj <- rbind( c(0, 1, 0, 0), c(1, 0, 0, 0), c(0, 0, 0, 0), c(0, 0, 0, 1) ) concomFromMatAdj(matAdj)
matAdj <- rbind( c(0, 1, 0, 0), c(1, 0, 0, 0), c(0, 0, 0, 0), c(0, 0, 0, 1) ) concomFromMatAdj(matAdj)