diff --git a/R/lab_3_submission.R b/R/lab_3_submission.R index 8af145a27e53d641d3d527a0b96283a0516d6d2a..e5d604cd52b1d9f867951f1ff6fbf92eba09f521 100644 --- a/R/lab_3_submission.R +++ b/R/lab_3_submission.R @@ -1,6 +1,6 @@ -#' Euclidian algorithm to find the +#' Euclidean algorithm to find the #' greatest common divisor of two numbers #' @title euclidean algorithm #' @param x first number @@ -31,18 +31,19 @@ euclidean <- function(x,y) { #' @description init the detail table #' @title initial the detail frame -#' @param init_node the start node to find the shortest path -#' @param graph the structure of the graph, +#' @param init_node the start node to find the shortest path, input should be one scalar of the graph +#' @param 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)) init_node_distance_list <- function (init_node, graph) { node_distance_list <- list(init_node = 0) - # first, get all unqiue nodes + # first, get all unique nodes all_nodes <- unique(append(graph[,'v1'],graph[,'v2'])) if (!init_node %in% all_nodes) { stop('node is not in graph') } + #built list and data frame for later comparison detail_frame <- data.frame(matrix(ncol=length(all_nodes),nrow = 3)) names(detail_frame) <- all_nodes row.names(detail_frame) <- c('is_best_node','node_distance','previous_node') @@ -58,11 +59,11 @@ init_node_distance_list <- function (init_node, graph) { #' @description #' the update method for detail table to record the shortest path #' @title update the detail frame when traversing to a new node -#' @param node the current the node when traversing all the nodes to find the best way +#' @param node the current node when traversing all the nodes to find the best way #' @param init_node the start node in the algorithm -#' @param checked_nodes the nodes that having found the shortest path -#' @param unchecked_nodes the nodes that having not found the shortest path -#' @param detail_frame to store the shorest path , if it is the best node and the related distance +#' @param checked_nodes the nodes that having been tested to find the shortest path +#' @param unchecked_nodes the nodes that having been tested to find the shortest path +#' @param detail_frame to store the shortest path , if it is the best node and the related distance #' @param graph the graph indicates the relationship of different nodes update_detail_path <- function(node,init_node,detail_frame,graph,checked_nodes,unchecked_nodes) {