diff --git a/NAMESPACE b/NAMESPACE index 6ae926839dd1829f1016a96f766d970ff184ad97..42580a6fb745ff38e34d71c16a49610097590f59 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,4 @@ # Generated by roxygen2: do not edit by hand +export(dijkstra) +export(euclidean) diff --git a/R/lab_3_submission.R b/R/dijkstra.R similarity index 86% rename from R/lab_3_submission.R rename to R/dijkstra.R index e5d604cd52b1d9f867951f1ff6fbf92eba09f521..33ebc10385892e2538abd20dcdb01a4aa191c383 100644 --- a/R/lab_3_submission.R +++ b/R/dijkstra.R @@ -1,34 +1,3 @@ - - -#' Euclidean algorithm to find the -#' greatest common divisor of two numbers -#' @title euclidean algorithm -#' @param x first number -#' @param y second number - -euclidean <- function(x,y) { - x <- abs(x) - y <- abs(y) - if (!is.numeric(x) | length(x) != 1){ - stop('pls check your input x') - } - if (!is.numeric(y) | length(y) != 1){ - stop('pls check your input y') - } - a <- ifelse(x < y, y, x) - b <- ifelse(x < y, x, y) - repeat{ - mode = a %% b - if (mode == 0) { - return (b) - } - a <- b - b <- mode - } - return (b) -} - - #' @description init the detail table #' @title initial the detail frame #' @param init_node the start node to find the shortest path, input should be one scalar of the graph @@ -43,6 +12,13 @@ init_node_distance_list <- function (init_node, graph) { if (!init_node %in% all_nodes) { stop('node is not in graph') } + all_weights <- unique(graph[,'w']) + if (0 %in% all_weights) { + stop('the weight only exist bwtween two different nodes and should not be zero') + } + dup = duplicated(graph[c("v1", "v2")]) + node_dup_len = dup[dup==TRUE] + #built list and data frame for later comparison detail_frame <- data.frame(matrix(ncol=length(all_nodes),nrow = 3)) names(detail_frame) <- all_nodes @@ -67,6 +43,7 @@ init_node_distance_list <- function (init_node, graph) { #' @param graph the graph indicates the relationship of different nodes update_detail_path <- function(node,init_node,detail_frame,graph,checked_nodes,unchecked_nodes) { + #get connected nodes to node if (length(unchecked_nodes) == 0){ # browser() @@ -120,6 +97,13 @@ update_detail_path <- function(node,init_node,detail_frame,graph,checked_nodes,u #' @title the entrance of the dijkstra algorithm #' @param graph a graph to find the shortest path #' @param init_node the start node to find the shortest path in the graph +#' @export +#' @examples +#' # Example 1: Define a graph and use Dijkstra algorithm to find the shortest path +#' wiki_graph <- data.frame(v1=c(1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,6,6,6), +#' v2=c(2,3,6,1,3,4,1,2,4,6,2,3,5,4,6,1,3,5), +#' w=c(7,9,14,7,10,15,9,10,11,2,15,11,6,6,9,14,2,9)) +#' dijkstra(wiki_graph, 3) dijkstra <- function(graph, init_node) { res<- init_node_distance_list(init_node,graph) @@ -127,10 +111,5 @@ dijkstra <- function(graph, init_node) { checked_nodes <- res$checked_nodes unchecked_nodes <- res$unchecked_nodes updated_detail_frame <- update_detail_path(init_node,init_node,detail_frame,graph,checked_nodes,unchecked_nodes) - return (unname(unlist(updated_detail_frame['node_distance',]))) + return(unname(unlist(updated_detail_frame['node_distance',]))) } - - - - - diff --git a/R/euclidean.R b/R/euclidean.R new file mode 100644 index 0000000000000000000000000000000000000000..db26b528def5f8747a4cb5ac2323de69625a4b7f --- /dev/null +++ b/R/euclidean.R @@ -0,0 +1,34 @@ + + +#' Euclidean algorithm to find the +#' greatest common divisor of two numbers +#' @title euclidean algorithm +#' @param x first number +#' @param y second number +#' @export +#' @examples +#' # Example 1: Find the GCD of 56 and 98 +#' euclidean(56, 98) +#' # Returns: 14 +euclidean <- function(x,y) { + x <- abs(x) + y <- abs(y) + if (!is.numeric(x) | length(x) != 1){ + stop('pls check your input x') + } + if (!is.numeric(y) | length(y) != 1){ + stop('pls check your input y') + } + a <- ifelse(x < y, y, x) + b <- ifelse(x < y, x, y) + repeat{ + mode = a %% b + if (mode == 0) { + return (b) + } + a <- b + b <- mode + } + return (b) +} + diff --git a/READMD.Rmd b/READMD.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/man/dijkstra.Rd b/man/dijkstra.Rd index 74004cc6837dc5872c7a0ebedbe259cbf697cb78..34daa1b12a701203eac28e99c3f5de2f7035b4d3 100644 --- a/man/dijkstra.Rd +++ b/man/dijkstra.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/lab_3_submission.R +% Please edit documentation in R/dijkstra.R \name{dijkstra} \alias{dijkstra} \title{the entrance of the dijkstra algorithm} @@ -14,3 +14,10 @@ dijkstra(graph, init_node) \description{ the dijkstra algorithm to find the shortest path in the graph } +\examples{ +# Example 1: Define a graph and use Dijkstra algorithm to find the shortest path +wiki_graph <- data.frame(v1=c(1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,6,6,6), + v2=c(2,3,6,1,3,4,1,2,4,6,2,3,5,4,6,1,3,5), + w=c(7,9,14,7,10,15,9,10,11,2,15,11,6,6,9,14,2,9)) +dijkstra(wiki_graph, 3) +} diff --git a/man/euclidean.Rd b/man/euclidean.Rd index c3c30c97ffd649b8ac4ca3e0b8488db467e2a55a..b5c7cf7ad2881bc7b33c699e096b28a6e346e362 100644 --- a/man/euclidean.Rd +++ b/man/euclidean.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/lab_3_submission.R +% Please edit documentation in R/euclidean.R \name{euclidean} \alias{euclidean} \title{euclidean algorithm} @@ -12,6 +12,11 @@ euclidean(x, y) \item{y}{second number} } \description{ -Euclidian algorithm to find the +Euclidean algorithm to find the greatest common divisor of two numbers } +\examples{ +# Example 1: Find the GCD of 56 and 98 +euclidean(56, 98) +# Returns: 14 +} diff --git a/man/init_node_distance_list.Rd b/man/init_node_distance_list.Rd index af02e37f6e9a6551219d2f174ea91003c63ec349..3a92a8d875db0c55bf7f6ae2baf2b27ff51a9efa 100644 --- a/man/init_node_distance_list.Rd +++ b/man/init_node_distance_list.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/lab_3_submission.R +% Please edit documentation in R/dijkstra.R \name{init_node_distance_list} \alias{init_node_distance_list} \title{initial the detail frame} @@ -7,9 +7,9 @@ init_node_distance_list(init_node, graph) } \arguments{ -\item{init_node}{the start node to find the shortest path} +\item{init_node}{the start node to find the shortest path, input should be one scalar of the graph} -\item{graph}{the structure of the graph, +\item{graph}{the structure of the graph, input should be data frame or matrix eg:wiki_graph <- data.frame(v1=c(1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,6,6,6), v2=c(2,3,6,1,3,4,1,2,4,6,2,3,5,4,6,1,3,5), w=c(7,9,14,7,10,15,9,10,11,2,15,11,6,6,9,14,2,9))} diff --git a/man/update_detail_path.Rd b/man/update_detail_path.Rd index b7013cc4139d3e0454e5b3906a1b68194e75a90b..077097ac4c2917e05f37c67a087636e7806fb12e 100644 --- a/man/update_detail_path.Rd +++ b/man/update_detail_path.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/lab_3_submission.R +% Please edit documentation in R/dijkstra.R \name{update_detail_path} \alias{update_detail_path} \title{update the detail frame when traversing to a new node} @@ -14,17 +14,17 @@ update_detail_path( ) } \arguments{ -\item{node}{the current the node when traversing all the nodes to find the best way} +\item{node}{the current node when traversing all the nodes to find the best way} \item{init_node}{the start node in the algorithm} -\item{detail_frame}{to store the shorest path , if it is the best node and the related distance} +\item{detail_frame}{to store the shortest path , if it is the best node and the related distance} \item{graph}{the graph indicates the relationship of different nodes} -\item{checked_nodes}{the nodes that having found the shortest path} +\item{checked_nodes}{the nodes that having been tested to find the shortest path} -\item{unchecked_nodes}{the nodes that having not found the shortest path} +\item{unchecked_nodes}{the nodes that having been tested to find the shortest path} } \description{ the update method for detail table to record the shortest path diff --git a/tests/testthat/test.dijkstra.R b/tests/testthat/test.dijkstra.R index f49d110330679e06c1302dc3b1d70be5155ec0ef..d4cdfb90a037e91a77630c4a24f333e21659ad71 100644 --- a/tests/testthat/test.dijkstra.R +++ b/tests/testthat/test.dijkstra.R @@ -6,10 +6,21 @@ wiki_graph <- v2=c(2,3,6,1,3,4,1,2,4,6,2,3,5,4,6,1,3,5), w=c(7,9,14,7,10,15,9,10,11,2,15,11,6,6,9,14,2,9)) -test_that("outputs are correct in the Dijkstra algorithm.", { - expect_equal(dijkstra(wiki_graph,1), c(0,7,9,20,20,11)) - expect_equal(dijkstra(wiki_graph,3), c(9,10,0,11,11,2)) -}) + +wiki_graph_1 <- + data.frame(v1=c(1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,6,6,6), + v2=c(2,3,6,1,3,4,1,2,4,6,2,3,5,4,6,1,3,5), + w=c(0,9,14,7,10,15,9,10,11,2,15,11,6,6,9,14,2,9)) + + +wiki_graph_2 <- + data.frame(v1=c(2,1,1,2,2,2,3,3,3,3,4,4,4,5,5,6,6,6), + v2=c(2,3,6,1,3,4,1,2,4,6,2,3,5,4,6,1,3,5), + w=c(1,9,14,7,10,15,9,10,11,2,15,11,6,6,9,14,2,9)) + + + + test_that("Error messages are returned for erronous input in the Dijkstra algorithm.", {