I have an unweighted graph and I want to get a subgraph that has just the nodes and edges that contain the shortest paths between n known nodes. In this case 3 nodes (11, 29, & 13 are the names).
Question
How can I get a subgraph of shortest path between n nodes in R?
MWE
library(ggraph)
library(igraph)
hs <- highschool[highschool$year == '1958',]
set.seed(11)
graph <- graph_from_data_frame(hs[sample.int(nrow(hs), 60),])
# plot using ggraph
ggraph(graph, layout = 'kk') +
geom_edge_fan() +
geom_node_text(aes(label = name))
Desired Output
The desired output would be the following green subgraph (Or close, I'm eyeballing the graph above and visually picking out what would be the subgraph) ignoring/removing the other nodes and edges.
You can't find the shortest path between n nodes. Since the shortest path is defined only between two nodes.
I think you want shortest path from 1 node to other n-1 node you can use
get_all_shortest_paths(v, to=None, mode=ALL) from igraph library.
v - the source for the calculated paths
to - a vertex selector describing the destination for the
calculated paths. This can be a single vertex ID, a list of vertex
IDs, a single vertex name, a list of vertex names. None means all the vertices.
mode - the directionality of the paths. IN means to calculate
incoming paths, OUT mean to calculate outgoing paths, ALL means to calculate both ones.
Returns: all of the shortest path from the given node to every other reachable node in the graph in a list.
get_all_shortest_paths
So, now you have to create a graph from a list of the shortest paths.
Initialize an empty graph then add all path to it from the list of
the path
adding path in graph
OR
make a graph for every shortest path found and take graphs union.
union igraph
You need a matrix of shortest paths to then create a sub-graph using a union of all edges belonging to those paths.
Let key vertices be those vertices between which your desired sub-graph appears. You say you have three such key vertices.
Consider that the shortest path between any i and j of them is unlist(shortest_paths(g, i, j, mode="all", weights=NULL)$vpath). You'd want to list all i-j combinations (1-2, 1-3, 2-3 in your case) of your key-verticies, and then list all vertices that appear on the paths between them. Sometime, surely, the same vertices appear on the shortest paths of more than one of your ij-pairs (See betweenness centrality). Your desired subgraph should include only these vertices, which you can give to induced_subgraph().
Then arises another interesting problem. Not all edges between your choosen vertices are part of your shortest paths. I'm not sure about what you desire in your sub-graph, but I assume that you only want vertices and edges that are part of shortest paths. The manual for induced_subgraph() says that eids can be provided to filter sub-graphs on edges too, but I didn't get that to work. Comments on that are welcome if anyone cracks it. To create a subgraph with only edges and vertices actually in your shortest path, some surplus edges must be deleted.
Below is an example where some key verticies are chosen at random, the surplus-edge problem of subgraphs is visualized, and a proper shortert-paths-only subgraph is generated:
library(igraph)
N <- 40 # Number of vertices in a random network
E <- 70 # Number of edges in a random network
K <- 5 # Number of KEY vertices between which we are to calculate the
# shortest paths and extract a sub-graph.
# Make a random network
g <- erdos.renyi.game(N, E, type="gnm", directed = FALSE, loops = FALSE)
V(g)$label <- NA
V(g)$color <- "white"
V(g)$size <- 8
E(g)$color <- "gray"
# Choose some random verteces and mark them as KEY vertices
key_vertices <- sample(1:N, 5)
g <- g %>% set_vertex_attr("color", index=key_vertices, value="red")
g <- g %>% set_vertex_attr("size", index=key_vertices, value=12)
# Find shortest paths between two vertices in vector x:
get_path <- function(x){
# Get atomic vector of two key verteces and return their shortest path as vector.
i <- x[1]; j <- x[2]
# Check distance to see if any verticy is outside component. No possible
# connection will return infinate distance:
if(distances(g,i,j) == Inf){
path <- c()
} else {
path <- unlist(shortest_paths(g, i, j, mode="all", weights=NULL)$vpath)
}
}
# List pairs of key vertices between which we need the shortest path
key_el <- expand.grid(key_vertices, key_vertices)
key_el <- key_el[key_el$Var1 != key_el$Var2,]
# Get all shortest paths between each pair of key_vertices:
paths <- apply(key_el, 1, get_path)
# These are the vertices BETWEEN key vertices - ON the shortest paths between them:
path_vertices <- setdiff(unique(unlist(paths)), key_vertices)
g <- g %>% set_vertex_attr("color", index=path_vertices, value="gray")
# Mark all edges of a shortest path
mark_edges <- function(path, edges=c()){
# Get a vector of id:s of connected vertices, find edge-id:s of all edges between them.
for(n in 1:(length(path)-1)){
i <- path[n]
j <- path[1+n]
edge <- get.edge.ids(g, c(i,j), directed = TRUE, error=FALSE, multi=FALSE)
edges <- c(edges, edge)
}
# Return all edges in this path
(edges)
}
# Find all edges that are part of the shortest paths between key vertices
key_edges <- lapply(paths, function(x) if(length(x) > 1){mark_edges(x)})
key_edges <- unique(unlist(key_edges))
g <- g %>% set_edge_attr("color", index=key_edges, value="green")
# This now shoes the full graph and the sub-graph which will be created
plot(g)
# Create sub-graph:
sg_vertices <- sort(union(key_vertices, path_vertices))
unclean_sg <- induced_subgraph(g, sg_vertices)
# Note that it is essential to provide both a verticy AND an edge-index for the
# subgraph since edges between included vertices do not have to be part of the
# calculated shortest path. I never used it before, but eids=key_edges given
# to induced_subgraph() should work (even though it didn't for me just now).
# See the problem here:
plot(unclean_sg)
# Kill edges of the sub-graph that were not part of shortest paths of the mother
# graph:
sg <- delete.edges(unclean_sg, which(E(unclean_sg)$color=="gray"))
# Plot a comparison:
l <-layout.auto(g)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
plot(g, layout=l)
plot(unclean_sg, layout=l[sg_vertices,]) # cut l to keep same layout in subgraph
plot(sg, layout=l[sg_vertices,]) # cut l to keep same layout in subgraph
Related
I'd like to identify if there exist a unique shortest path or multiple shortest paths between two vertices with igraph. If I use length(all_shortest_paths(g, i,j), that actually helps me, but I feel like there are so many redundant operations. I rather prefer first to get one shortest path with get.shortest.paths(g, i,j), and then see if there is another. However, I could not figure out how to do this.
Can someone help me how to identify whether there is another shortest path different than the first one obtained by get.shortest.paths(g, i,j)?
Here is an example graph
library(igraph)
data <- read.table(text="
1 2
1 4
1 5
2 3
2 4
3 4
5 7
5 8
3 6", header=FALSE)
gmatrix <- data.matrix(data, rownames.force = NA) #convert into a matrix to use in igraph
g <- graph_from_edgelist(gmatrix, directed = FALSE)
For instance, if I'd like to find the shortest path from 1 to 3, I use all_shortest_paths(g, 1,3), and it gives me the following result.
$res
$res[[1]]
+ 3/9 vertices, from 634c426:
[1] 1 4 3
$res[[2]]
+ 3/9 vertices, from 634c426:
[1] 1 2 3
What I want is to get the first shortest path. For instance
get.shortest.paths(g, 1,3)
$vpath
$vpath[[1]]
+ 3/9 vertices, from 634c426:
[1] 1 2 3
Now, I want to see if there is any other path different than [1] 1 2 3. In a larger graph, since there are tens of possible shortest paths, I don't want to use all_shortest_paths(g, i,j) to make that query.
Overall, my question is: how can I check whether there exists a unique shortest path between two vertices or not? I will give two vertices as my input, in return I should get TRUE or FALSE indicating if there is a unique shortest path.
After getting responded for How to assign edge weights to certain edges in R igraph, here is one solution. Please note that the initial network is an undirected graph with no edge weights.
p <- get.shortest.paths(g, i, j)$vpath[[1]] #shortest path between node i and node j
g <- set_edge_attr(g, "weight", value = ifelse(E(g) %in% E(g, path = p), 1.50, 1)) #Assign slightly larger edge weights to the edges existing in path p
q <- get.shortest.paths(g, i,j , weights = E(g)$weight)$vpath[[1]] #recalculate the shortest path with edge weights
g <- delete_edge_attr(g, "weight")
if(all(p %in% q))
#If paths p and q are the same, then the shortest path is unique
However, this is definitely not a good solution due to the high running time. When I try this method for 400 nodes, it takes several minutes to stop.
I want to get all the longest shortest paths for iGraph object. There is this function
get.diameter (graph, directed = TRUE, unconnected = TRUE)
But it returns only one path. So if there are many shortest paths of the length of the diameter, then it returns the first one found
You can easily extract which nodes are connected at what lengths using the shortest-distance matrix returned by shortest.paths(graph). In R, you can use which() and arr.ind=TRUE like so:
longest.shortest.paths <- function(graph){
# Return edgelist of all node-pairs between which the shortest path
# in a graph are the longest shortest path observed in that graph.
# Get all the shortest paths of a graph
shortest.paths = shortest.paths(graph)
# Make sure that there are no Inf-values caused by isolates in the graph
shortest.paths[shortest.paths == Inf] <- 0
# What nodes in the distance matrix are linked by longest shortest paths?
el <- which(shortest.paths==max(shortest.paths), arr.ind=TRUE)
colnames(el) <- c("i","j")
(el)
}
graph <- erdos.renyi.game(100, 140, "gnm", directed=FALSE)
longest.shortest.paths(graph)
I'm using the igraph package in R and I'm looking for a way to subgraph the max top 10 shortest path from a previous graph.
Anyone have some suggestions?
First create some graph:
set.seed(1)
require(igraph)
g <- erdos.renyi.game(100,.2)
Then extract all shortest paths and calculate their length:
plist <- do.call(c,
lapply(V(g), function(v) get.shortest.paths(g,v,V(g), output='epath')$epath))
Now figure out which paths are the top ten:
psize <- data.frame(i = 1:length(plist), plength = sapply(plist,length))
top10 <- head(psize[order(-psize$plength),],10)
Now figure out which edges this involves:
elist <- unlist(plist[top10$i])
And finally, get the subgraph which contains these vertices:
finalg <- subgraph.edges(g, elist)
The before and after plots:
I am trying to implement the Kou's algorithm to identify Steiner Tree(s) in R using igraph.
The Kou's algorithm can be described like this:
Find the complete distance graph G' (G' has V' = S (steiner nodes) , and for each pair of nodes (u,v) in VxV there is an edge with weight equal to the weight of the min-cost path between these nodes p_(u,v) in G)
Find a minimum spanning tree T' in G'
Construct the subgraph Gs, of G by substituting every edge of T', which is an edge of G' with the corresponding shortest path of G (it there are several shortest paths, pick an arbitrary one).
Find the minimal spanning tree, Ts, of Gs (If there are several minimal spanning trees, pick an arbitrary one)
Construct a Steiner tree, Th, from Ts by deleting edges in Ts, if necessary, to that all the leaves in Th are Steiner nodes.
The first 2 steps are easy:
g <- erdos.renyi.game(100, 1/10) # graph
V(g)$name <- 1:100
# Some steiner nodes
steiner.points <- sample(1:100, 5)
# Complete distance graph G'
Gi <- graph.full(5)
V(Gi)$name <- steiner.points
# Find a minimum spanning tree T' in G'
mst <- minimum.spanning.tree(Gi)
However, I don't know how to replace the edges in T' for the shortest path in G. I know that with get.shortest.paths I can get the vpath from a pair of nodes, but how I replace and edge in T' with the shortest.path in G?
Many thanks in advance
If I'm understanding the algorithm as you've written it, I think this gets you through step 3, but please clarify if that's not the case:
library(igraph)
set.seed(2002)
g <- erdos.renyi.game(100, 1/10) # graph
V(g)$name <- as.character(1:100)
## Some steiner nodes:
steiner.points <- sample(1:100, 5)
## Complete distance graph G'
Gi <- graph.full(5)
V(Gi)$name <- steiner.points
## Find a minimum spanning tree T' in G'
mst <- minimum.spanning.tree(Gi)
## For each edge in mst, replace with shortest path:
edge_list <- get.edgelist(mst)
Gs <- mst
for (n in 1:nrow(edge_list)) {
i <- edge_list[n,2]
j <- edge_list[n,1]
## If the edge of T' mst is shared by Gi, then remove the edge from T'
## and replace with the shortest path between the nodes of g:
if (length(E(Gi)[which(V(mst)$name==i) %--% which(V(mst)$name==j)]) == 1) {
## If edge is present then remove existing edge from the
## minimum spanning tree:
Gs <- Gs - E(Gs)[which(V(mst)$name==i) %--% which(V(mst)$name==j)]
## Next extract the sub-graph from g corresponding to the
## shortest path and union it with the mst graph:
g_sub <- induced.subgraph(g, (get.shortest.paths(g, from=V(g)[i], to=V(g)[j])$vpath[[1]]))
Gs <- graph.union(Gs, g_sub, byname=T)
}
}
par(mfrow=c(1,2))
plot(mst)
plot(Gs)
Plot of minimum spanning tree on the left, replaced with shortest paths on right:
The following example gives shortest path 1-2-6-7-3-4, where only the weight of edges is considered; and the weight of turn at vertices is not counted for. Can someone suggest a procedure to include the weight at each vertex that is no-turn, right-turn, or left-turn? We can assume the weight for (NT, RT, LT)=(0,0.5,1). When edge weight is combined with turn effect, the shortest path would become 1-2-3-4. Below is the example in question. Thank you.
#
library(igraph)
n <- c(1,2,3,4,5,6,7,8)
x <- c(1,4,7,10,1,4,7,10)
y <- c(1,1,1,1,4,4,4,4)
node <- data.frame(n,x,y)
fm <- c(1,2,3,5,6,7,1,2,3,4)
to<-c(2,3,4,6,7,8,5,6,7,8)
weight<- c(1,4,1,1,1,2,5,1,1,1)
link <- data.frame(fm,to,weight)
g <- graph.data.frame(link,directed=FALSE,vertices=node)
sv <- get.shortest.paths(g,1,4,weights=NULL,output="vpath")
sv
E(g)$color <- "pink"
E(g, path=sv[[1]])$width <- 8
plot(g,edge.color="red")
plot(g,edge.label=weight,edge.label.color="blue",edge.label.cex=2)
As a preprocessing step: for each vertex v with a incoming edges and b outgoing edges, split it into a vertexes connected to those incoming edges and b vertexes connected to those outgoing edges. Then create edges representing turning costs in between.
In principal, Jeffery is describing what we want, but the problem size is such that we need a programmatic solution. Maybe 200,000 vertices with 3 to 6 edges. If there is a way to explode, for instance, the standard intersection of 4 edges in and 4 edges out to the 16 right through left movements and automatically assigning left through and right penalties.
most important is the ability to have lesser penalties for turning at T intersections (ease of wayfinding) than turning at traditional intersections/vertices
Is this tractable for a huge network?