Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/melvidoni/rsppfp
Browse files Browse the repository at this point in the history
  • Loading branch information
melinavidoni committed May 31, 2018
2 parents 1824baf + 9d5cde6 commit 290b6d8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
18 changes: 14 additions & 4 deletions R/igraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,19 @@ get_shortest_path <- function(g, origin, dest) {
g.i <- graph_from_data_frame(g)

# Get all the shortest paths to each node
sp <- shortest_paths(g.i, from = origin, to = get_all_nodes(gStar, dest), weights = E(g.i)$weight, output = "both")
sp <- shortest_paths(g.i, from = origin, to = get_all_nodes(g, dest), weights = E(g.i)$weight, output = "both")

# Return the shortest path
sp$vpath[which.min( lapply(sp$epath, function(y)
ifelse(length(y) != 0, sum(E(g.i)$weight[ y[[1]] ]), 999999999)) )] [[1]]
# If there are no path, return NA
if(length(sp$vpath) == 1 & length(sp$vpath[[1]]) == 0)
return(NA)
# Otherwise...
else {
# Return the shortest path
sp$vpath[which.min(
foreach(i = 1:length(sp$epath), .combine = c) %do% {
ifelse( length(sp$epath[i]), sum(E(g.i)$weight[ sp$epath[[i]] ]), 999999999)
}
)]
}

}
6 changes: 3 additions & 3 deletions R/parsers.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ direct_graph <- function(graph, cores = 1L) {
#'
#' @family Parsers
#'
#' @param gStar A graph in data frame format, translated using one of the available functions.
#' @param g A graph in data frame format, translated using one of the available functions.
#' @param originalNode The name of the original node from G, that needs to be searched within G*. It is preferable
#' to use a character format, but this can also be of any simple type. No lists or vectors are allowed.
#'
Expand All @@ -144,9 +144,9 @@ direct_graph <- function(graph, cores = 1L) {
#' get_all_nodes(gStar, "v")
#'
#'
get_all_nodes <- function(gStar, originalNode) {
get_all_nodes <- function(g, originalNode) {
# Get the list of nodes
nodes <- unique(c(gStar$from, gStar$to))
nodes <- unique(c(g$from, g$to))

# Get all the nodes that end on that original node
nodes[sapply(paste0("*\\s|", originalNode, "$"), function(y) grepl(y, nodes))]
Expand Down
2 changes: 1 addition & 1 deletion docs/articles/igraph.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/articles/igraph_files/figure-html/unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/reference/get_all_nodes.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion docs/reference/get_shortest_path.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/get_all_nodes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions vignettes/igraph.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,26 @@ get_shortest_path <- function(g, origin, dest) {
g.i <- graph_from_data_frame(g)
# Get all the shortest paths to each node
sp <- shortest_paths(g.i, from = origin, to = get_all_nodes(gStar, dest), weights = E(g.i)$weight, output = "both")
sp <- shortest_paths(g.i, from = origin, to = get_all_nodes(g, dest), weights = E(g.i)$weight, output = "both")
# Return the shortest path
sp$vpath[which.min( lapply(sp$epath, function(y)
ifelse(length(y) != 0, sum(E(g.i)$weight[ y[[1]] ]), 999999999)) )] [[1]]
sp$vpath[which.min(
foreach(i = 1:length(sp$epath), .combine = c) %do% {
ifelse( length(sp$epath[i]), sum(E(g.i)$weight[ sp$epath[[i]] ]), 999999999)
}
)]
}
```

And it can be used as in the following example:

```{r, warning=FALSE}
# Obtain the shortest path using the simplified function
shortestPath <- get_shortest_path(gStar, "u", "w")
shortestPath <- get_shortest_path(gStar, "u", "t")
shortestPath
```

Also,

It is worth pointing out that a path can only be translated if it is presented as a list or vector of nodes, written sequentially. In iGraph, this is known as `vpaths` (vertexes paths).
Also, it is worth pointing out that a path can only be translated if it is presented as a list or vector of nodes, written sequentially. In iGraph, this is known as `vpaths` (vertexes paths).

```{r}
# Translate the vpath
Expand Down

0 comments on commit 290b6d8

Please sign in to comment.