View source: R/findShortestPath.R
findShortestPath | R Documentation |
Find the shortest path
findShortestPath(G, start, end)
start |
node |
end |
node |
Adjacency |
matrix |
{
## Not run:
G = matrix(c(c(0, 6, 0, 1, 0),
c(6, 0, 5, 2, 2),
c(0, 5, 0, 0, 5),
c(1, 2, 0, 0, 1),
c(0, 2, 5, 1, 0))/10, nrow = 5, byrow = T,
dimnames = list(letters[1:5], letters[1:5]))
# debugonce(findShortestPath)
findShortestPath(G, start = "a", end = "c")
findShortestPath(G, start = "b", end = "c") #
findShortestPath(G, start = "d", end = "c")
findShortestPath(G, start = "e", end = "c")
findShortestPath(G, start = "a", end = "d")
# longest path (only work unweighted network)
findShortestPath(-G, start = "e", end = "a")
chengyu = list(a = c("ning que wu lan"),
b = c("ji quan bu ning"),
c = c("lan yu chong shu"),
d = c("zu chuang mi fang"),
e = c("shu dian wang zu"),
f = c("ji fei gou tiao"),
g = c("ning si bu qu"),
h = c("lan zui ru ni"),
i = c("niu dao ge ji"))
idiomDist = function(idiom, sep = " "){
idiom = t(sapply(idiom, strSplit, sep))
G1 = matrix(0, nrow = nrow(idiom), ncol = nrow(idiom),
dimnames = list(rownames(idiom), rownames(idiom)))
for(mi in rownames(idiom)){
G1[mi,] = idiom[, 1] == idiom[mi, 4]
}
return(G1)
}
x = findShortestPath(-idiomDist(chengyu), start = "b", end = "d")
unlist(chengyu[x$shortestPath], use.names = F)
## End(Not run)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.