Title: | Simple Graph Data Types and Basic Algorithms |
---|---|
Description: | Simple classic graph algorithms for simple graph classes. Graphs may possess vertex and edge attributes. 'simplegraph' has no dependencies and it is written entirely in R, so it is easy to install. |
Authors: | Gabor Csardi |
Maintainer: | Gabor Csardi <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.1.9000 |
Built: | 2024-12-31 06:03:35 UTC |
Source: | https://github.com/gaborcsardi/simplegraph |
A vertex is adjacent is it is either a successor, or a predecessor.
adjacent_vertices(graph)
adjacent_vertices(graph)
graph |
The graph. |
A named list of character vectors, the adjacent vertices for each vertex.
Other simple queries:
edges()
,
order()
,
vertices()
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) adjacent_vertices(G)
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) adjacent_vertices(G)
Breadth-first search of a graph
bfs(graph, from = vertex_ids(graph))
bfs(graph, from = vertex_ids(graph))
graph |
Input graph. |
from |
Character vector, which vertices to start the search from. By default all vertices are attempted. |
Character vector of the named of the visited vertices, in the order of their visit.
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) bfs(funcs)
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) bfs(funcs)
Degree of vertices
degree(graph, mode = c("out", "in", "total", "all"))
degree(graph, mode = c("out", "in", "total", "all"))
graph |
Input graph. |
mode |
Whether to calculate |
Named numeric vector of degrees.
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) degree(G, mode = "out") degree(G, mode = "in") degree(G, mode = "total")
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) degree(G, mode = "out") degree(G, mode = "in") degree(G, mode = "total")
Edges of a graph
edges(graph)
edges(graph)
graph |
The graph |
Data frame of edge data and metadata. The tail and head vertices are in the fist two columns. The rest of the columns are metadata.
Other simple queries:
adjacent_vertices()
,
order()
,
vertices()
bridges <- graph(list( "Altstadt-Loebenicht" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Kneiphof" = c( "Altstadt-Loebenicht", "Altstadt-Loebenicht", "Vorstadt-Haberberg", "Vorstadt-Haberberg", "Lomse" ), "Vorstadt-Haberberg" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Lomse" = c( "Altstadt-Loebenicht", "Kneiphof", "Vorstadt-Haberberg" ) )) edges(bridges)
bridges <- graph(list( "Altstadt-Loebenicht" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Kneiphof" = c( "Altstadt-Loebenicht", "Altstadt-Loebenicht", "Vorstadt-Haberberg", "Vorstadt-Haberberg", "Lomse" ), "Vorstadt-Haberberg" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Lomse" = c( "Altstadt-Loebenicht", "Kneiphof", "Vorstadt-Haberberg" ) )) edges(bridges)
Graphs can be specified as adjacency lists or (two) data frames.
graph(x, ...)
graph(x, ...)
x |
A data frame, or a named list of character vectors. See details below. |
... |
Additional arguments, see details below. |
If the first argument is a data frame, then it is interpreted as vertex data, and a second data frame must be supplied as edge data. The first column of the vertex data must contain (character) vertex ids. The first two columns of the edge data frame must contain the directed edges of the graph, in the order of tail and head, as characters referring to the nodes ids. Other columns are kept as metadata.
If the first argument is not a data frame, but a list, then it is interpreted as an adjacency list. It must be named, and the names will be used as vertex ids. Each list element must be a character vector containing the successors of each vertex.
A graph object.
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) funcs vertices <- data.frame( stringsAsFactors = FALSE, name = c("Tom Hanks", "Cate Blanchett", "Matt Damon", "Kate Winslet", "Saving Private Ryan", "Contagion", "The Talented Mr. Ripley"), what = c("actor", "actor", "actor", "actor", "movie", "movie", "movie"), born = c("1956-07-09", "1966-05-26", "1970-10-08", "1975-10-05", NA, NA, NA), gender = c("M", "F", "M", "F", NA, NA, NA), year = c(NA, NA, NA, NA, 1998, 2011, 1999) ) edges <- data.frame( stringsAsFactors = FALSE, actor = c("Tom Hanks", "Cate Blanchett", "Matt Damon", "Matt Damon", "Kate Winslet"), movie = c("Saving Private Ryan", "The Talented Mr. Ripley", "Saving Private Ryan", "The Talented Mr. Ripley", "Contagion") ) actors <- graph(vertices, edges) actors
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) funcs vertices <- data.frame( stringsAsFactors = FALSE, name = c("Tom Hanks", "Cate Blanchett", "Matt Damon", "Kate Winslet", "Saving Private Ryan", "Contagion", "The Talented Mr. Ripley"), what = c("actor", "actor", "actor", "actor", "movie", "movie", "movie"), born = c("1956-07-09", "1966-05-26", "1970-10-08", "1975-10-05", NA, NA, NA), gender = c("M", "F", "M", "F", NA, NA, NA), year = c(NA, NA, NA, NA, 1998, 2011, 1999) ) edges <- data.frame( stringsAsFactors = FALSE, actor = c("Tom Hanks", "Cate Blanchett", "Matt Damon", "Matt Damon", "Kate Winslet"), movie = c("Saving Private Ryan", "The Talented Mr. Ripley", "Saving Private Ryan", "The Talented Mr. Ripley", "Contagion") ) actors <- graph(vertices, edges) actors
Incident edges
incident_edges(graph, mode = c("out", "in", "all", "total"))
incident_edges(graph, mode = c("out", "in", "all", "total"))
graph |
Input graph. |
mode |
Whether to use |
A list of data frames, each a set of edges.
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) incident_edges(G, mode = "out") incident_edges(G, mode = "in") incident_edges(G, mode = "all")
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) incident_edges(G, mode = "out") incident_edges(G, mode = "in") incident_edges(G, mode = "all")
A loopy graph has at least one loop edge: an edge from a vertex to itself.
is_loopy(graph)
is_loopy(graph)
graph |
The input graph. |
Logical scalar.
Other multigraphs:
is_multigraph()
,
is_simple()
,
remove_loops()
,
remove_multiple()
,
simplify()
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_loopy(G) G2 <- simplify(G) is_loopy(G2)
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_loopy(G) G2 <- simplify(G) is_loopy(G2)
A multigraph has at least one pair or multiple edges, edges connecting the same (ordered) pair of vertices.
is_multigraph(graph)
is_multigraph(graph)
graph |
Input graph. |
Logical scalar.
Other multigraphs:
is_loopy()
,
is_simple()
,
remove_loops()
,
remove_multiple()
,
simplify()
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_multigraph(G) G2 <- simplify(G) is_multigraph(G2)
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_multigraph(G) G2 <- simplify(G) is_multigraph(G2)
A simple graph contains no loop and multiple edges.
is_simple(graph)
is_simple(graph)
graph |
The input graph. |
Logical scalar.
Other multigraphs:
is_loopy()
,
is_multigraph()
,
remove_loops()
,
remove_multiple()
,
simplify()
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_simple(G) G2 <- simplify(G) is_simple(G2)
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_simple(G) G2 <- simplify(G) is_simple(G2)
Is the graph weighted?
is_weighted(graph)
is_weighted(graph)
graph |
The graph. |
G <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a"), weight = c( 1 , 2 , 1 , 3 , 2 ) ) ) is_weighted(G) G2 <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a") ) ) is_weighted(G2)
G <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a"), weight = c( 1 , 2 , 1 , 3 , 2 ) ) ) is_weighted(G) G2 <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a") ) ) is_weighted(G2)
The order of the graph is the number of vertices.
order(graph)
order(graph)
graph |
The graph. |
Numeric scalar, the number of vertices.
Other simple queries:
adjacent_vertices()
,
edges()
,
vertices()
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) order(G)
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) order(G)
Predecessors and successors
predecessors(graph) successors(graph)
predecessors(graph) successors(graph)
graph |
Input graph |
Named list of character vectors, the predecessors or the successors of each vertex.
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) predecessors(G) successors(G)
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) predecessors(G) successors(G)
Remove loop edges from a graph
remove_loops(graph)
remove_loops(graph)
graph |
Input graph |
Graph, with loop edges removed.
Other multigraphs:
is_loopy()
,
is_multigraph()
,
is_simple()
,
remove_multiple()
,
simplify()
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_loopy(G) is_loopy(remove_loops(G))
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_loopy(G) is_loopy(remove_loops(G))
Remove multiple edges from a graph
remove_multiple(graph)
remove_multiple(graph)
graph |
Input graph. |
Graph, without the multiple edges. (More precisely, from each set of multiple edges, only one, the first one, is kept.)
Other multigraphs:
is_loopy()
,
is_multigraph()
,
is_simple()
,
remove_loops()
,
simplify()
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_multigraph(G) is_multigraph(remove_multiple(G))
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_multigraph(G) is_multigraph(remove_multiple(G))
This is mainly for internal checks, but occasionally it might be useful externally.
sanitize(x, ...)
sanitize(x, ...)
x |
Graph. |
... |
Extra arguments are curently ignored. |
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) sanitize(G) G <- c(G, list("this is not good" = c(1, 2, 3))) try(sanitize(G))
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) sanitize(G) G <- c(G, list("this is not good" = c(1, 2, 3))) try(sanitize(G))
Simple classic graph algorithms for simple graph classes. Graphs may possess vertex and edge attributes. 'simplegraph' has no dependencies and it is writting entirely in R, so it is easy to install.
Useful links:
Report bugs at https://github.com/gaborcsardi/simplegraph/issues
Remove multiple and loop edges from a graph
simplify(graph)
simplify(graph)
graph |
Input graph. |
Another graph, with the multiple and loop edges removed.
Other multigraphs:
is_loopy()
,
is_multigraph()
,
is_simple()
,
remove_loops()
,
remove_multiple()
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_simple(G) G2 <- simplify(G) is_simple(G2)
G <- graph(list(A = c("A", "B", "B"), B = c("A", "C"), C = "A")) is_simple(G) G2 <- simplify(G) is_simple(G2)
The size of the graph is the number of edges
size(graph)
size(graph)
graph |
The graph. |
Numeric scalar, the number of edges.
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) size(G)
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) size(G)
This is also called weighed degree.
strength(graph, mode = c("out", "in", "total", "all"))
strength(graph, mode = c("out", "in", "total", "all"))
graph |
Input graph. |
mode |
Whether to consider incoming ( |
For non-weighted graphs, the degree is returned as a fallback.
Named numeric vector.
G <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a"), weight = c( 1 , 2 , 1 , 3 , 2 ) ) ) strength(G) G2 <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a") ) ) strength(G2)
G <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a"), weight = c( 1 , 2 , 1 , 3 , 2 ) ) ) strength(G) G2 <- graph( data.frame( stringsAsFactors = FALSE, id = c("a", "b", "c", "d") ), data.frame( stringsAsFactors = FALSE, from = c("a", "a", "b", "b", "c"), to = c("b", "d", "d", "c", "a") ) ) strength(G2)
Topological sorting of a graph
topological_sort(graph)
topological_sort(graph)
graph |
Input graph. |
Character vector of vertex ids, in topological order.
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) topological_sort(remove_loops(funcs))
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) topological_sort(remove_loops(funcs))
The transposed graph have the same vertices, and the same number of edges, but all edge directions are opposite comparated to the original graph.
transpose(graph)
transpose(graph)
graph |
Input graph |
Transposed graph.
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) edges(transpose(funcs))
funcs <- graph(list( drop_internal = character(0), get_deps = c("get_description", "parse_deps", "%||%", "drop_internal"), get_description = "pkg_from_filename", parse_deps = "str_trim", cran_file = c("get_pkg_type", "r_minor_version", "cran_file"), download_urls = c("split_pkg_names_versions", "cran_file"), filename_from_url = character(0), get_pkg_type = character(0), pkg_download = c("dir_exists", "download_urls", "filename_from_url", "try_download"), r_minor_version = character(0), try_download = character(0), drop_missing_deps = character(0), install_order = character(0), restore = c("pkg_download", "drop_missing_deps", "install_order", "get_deps"), snap = character(0), `%||%` = character(0), data_frame = character(0), dir_exists = character(0), pkg_from_filename = character(0), split_pkg_names_versions = "data_frame", str_trim = character(0) )) edges(transpose(funcs))
Vertex ids of a graph
vertex_ids(graph)
vertex_ids(graph)
graph |
The graph. |
Character vector of vertex ids.
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) vertex_ids(G)
G <- graph(list(A = c("B", "C"), B = "C", C = "A")) vertex_ids(G)
Vertices of a graph, with metadata
vertices(graph)
vertices(graph)
graph |
The graph. |
Character vector of vertex names.
Other simple queries:
adjacent_vertices()
,
edges()
,
order()
bridges <- graph(list( "Altstadt-Loebenicht" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Kneiphof" = c( "Altstadt-Loebenicht", "Altstadt-Loebenicht", "Vorstadt-Haberberg", "Vorstadt-Haberberg", "Lomse" ), "Vorstadt-Haberberg" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Lomse" = c( "Altstadt-Loebenicht", "Kneiphof", "Vorstadt-Haberberg" ) )) vertices(bridges)
bridges <- graph(list( "Altstadt-Loebenicht" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Kneiphof" = c( "Altstadt-Loebenicht", "Altstadt-Loebenicht", "Vorstadt-Haberberg", "Vorstadt-Haberberg", "Lomse" ), "Vorstadt-Haberberg" = c( "Kneiphof", "Kneiphof", "Lomse" ), "Lomse" = c( "Altstadt-Loebenicht", "Kneiphof", "Vorstadt-Haberberg" ) )) vertices(bridges)