View source: R/network_analysis.R
get_valid_edges | R Documentation |
The function get_valid_edges
gets valid edges from a graph.
A valid edge is defined as follows:
(1) take from a graph g the two measured vertices with indices i, j;
(2) obtain all shortest paths between the measured vertices;
(3) assign edge = 1 (valid), if there is/are ONLY one/multiple unmeasured
intermediate vertex/vertices or ZERO intermediate vertices within the shortest
paths
(4) do not assign edge = 1 (not valid), if there is AT LEAST one measured
intermediate vertex within shortest path
get_valid_edges(vertices, paths, vertex_attr_logical)
vertices |
vertices of |
paths |
list of |
vertex_attr_logical |
logical vector storing information if a vertex is measured |
Measured vertices will be denoted via vertex_attr_logical
.
The function get_valid_edges
is a helper function for
collapse_network
.
Thomas Naake, thomasnaake@googlemail.com
edges <- data.frame(from = c("A", "A", "B", "C", "E", "F"),
to = c("B", "E", "C", "D", "F", "D"))
g <- igraph::graph_from_data_frame(edges, directed = TRUE)
## assign data availability to nodes, TRUE for measured vertices
igraph::V(g)$data_available <- igraph::V(g)$name %in% c("A", "D", "E")
## check if graph is directed
is_directed <- igraph::is_directed(graph = g)
## obtain the vertex attributes and create a vector, vertex_attr_logical,
## that stores information if a measurement is available (TRUE) or
## not (FALSE)
vertex_attr <- igraph::vertex_attr(graph = g, name = "data_available")
vertex_attr_logical <- logical(length(vertex_attr))
if (is.numeric(vertex_attr) | is.character(vertex_attr))
vertex_attr_logical[!is.na(vertex_attr)] <- TRUE
if (is.logical(vertex_attr))
vertex_attr_logical[vertex_attr] <- TRUE
## obtain the vertices where information is available
vertices_measured <- igraph::V(g)[vertex_attr_logical]
vertex_1 <- vertices_measured[1]
vertex_2 <- vertices_measured[3]
## find the shortest path from vertex_1 to vertex_2
path_1 <- suppressWarnings(igraph::all_shortest_paths(graph = g,
from = vertex_1, to = vertex_2)$res)
## find the shortest path from vertex_2 to vertex_1
path_2 <- suppressWarnings(igraph::all_shortest_paths(graph = g,
from = vertex_2, to = vertex_1)$res)
## check and add paths if valid, add a valid path when between
## shortest paths there are only "unmeasured" vertices
## apply the helper function add_valid_edges for path_1 and path_2
get_valid_edges(vertices = igraph::V(g), paths = path_1,
vertex_attr_logical = vertex_attr_logical)
get_valid_edges(vertices = igraph::V(g), paths = path_2,
vertex_attr_logical = vertex_attr_logical)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.