Skip to content
Snippets Groups Projects
Commit 0a64573d authored by Liuxi Mei's avatar Liuxi Mei
Browse files

first commit

parents
No related branches found
No related tags found
2 merge requests!2Master,!1presetation
Package: exercise
Type: Package
Title: Lab 3 exercise
Version: 1.0
Date: 2024-09-12
Author:Liuxi Mei
Maintainer: Liuxi Mei <liume102@student.liu.se>
Description: This is a exercise for lab 3.
License: GPL (>= 2)
RoxygenNote: 7.3.2
Encoding: UTF-8
# Generated by roxygen2: do not edit by hand
#' Euclidian 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) {
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
#' @param graph the structure of the graph,
#' 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
all_nodes <- unique(append(graph[,'v1'],graph[,'v2']))
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')
detail_frame['node_distance', ]<- Inf
detail_frame['node_distance',init_node] <- 0
detail_frame['is_best_node',init_node] <- TRUE
checked_nodes <- list(init_node)
unchecked_nodes <- all_nodes[-which( all_nodes == init_node)]
return (list(detail_frame=detail_frame, checked_nodes=checked_nodes, unchecked_nodes=unchecked_nodes))
}
#' @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 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
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()
return (detail_frame)
}
connected_nodes <- graph[graph$v1 == node,]$v2
next_node <- NULL
min_path <- Inf
for (c_node in connected_nodes){
current_distance <- graph[graph$v1 == node & graph$v2 == c_node,]$w
if (node == init_node) {
detail_frame['node_distance',c_node] <- current_distance
detail_frame['previous_node',c_node] <- init_node
if(current_distance < min_path) {
min_path <- current_distance
next_node <- c_node
# detail_frame['best_node',c_node] <-TRUE
}
}else{
# browser()
if (c_node %in% checked_nodes) next
previous_distance <- detail_frame['node_distance',node]
source_distance <- previous_distance + current_distance
exist_distance <- detail_frame['node_distance',c_node]
#差更新前置节点
if (source_distance < exist_distance) {
# update source distance
detail_frame['node_distance',c_node] <- source_distance
# update previous node
detail_frame['previous_node',c_node] <- node
}else{
source_distance <- exist_distance
}
if (source_distance < min_path) {
min_path <- source_distance
next_node <- c_node
}
}
}
checked_nodes <- append(checked_nodes,next_node)
unchecked_nodes <- unchecked_nodes[-which(unchecked_nodes==next_node)]
detail_frame['is_best_node',next_node] <-TRUE
return (update_detail_path(next_node,init_node,detail_frame,graph,checked_nodes,unchecked_nodes))
}
#' @description
#' the dijkstra algorithm to find the shortest path in the graph
#' @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
dijkstra <- function(graph, init_node) {
res<- init_node_distance_list(init_node,graph)
detail_frame <- res$detail_frame
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',])))
}
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
BuildType: Package
PackageUseDevtools: Yes
PackagePath: /Users/meiliuxi/Advanced_R/exercise
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/lab_3_submission.R
\name{dijkstra}
\alias{dijkstra}
\title{the entrance of the dijkstra algorithm}
\usage{
dijkstra(graph, init_node)
}
\arguments{
\item{graph}{a graph to find the shortest path}
\item{init_node}{the start node to find the shortest path in the graph}
}
\description{
the dijkstra algorithm to find the shortest path in the graph
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/lab_3_submission.R
\name{euclidean}
\alias{euclidean}
\title{euclidean algorithm}
\usage{
euclidean(x, y)
}
\arguments{
\item{x}{first number}
\item{y}{second number}
}
\description{
Euclidian algorithm to find the
greatest common divisor of two numbers
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/lab_3_submission.R
\name{init_node_distance_list}
\alias{init_node_distance_list}
\title{initial the detail frame}
\usage{
init_node_distance_list(init_node, graph)
}
\arguments{
\item{init_node}{the start node to find the shortest path}
\item{graph}{the structure of the graph,
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))}
}
\description{
init the detail table
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/lab_3_submission.R
\name{update_detail_path}
\alias{update_detail_path}
\title{update the detail frame when traversing to a new node}
\usage{
update_detail_path(
node,
init_node,
detail_frame,
graph,
checked_nodes,
unchecked_nodes
)
}
\arguments{
\item{node}{the current the node when traversing all the nodes to find the best way}
\item{init_node}{the start node in the algorithm}
\item{checked_nodes}{the nodes that having found the shortest path}
\item{unchecked_nodes}{the nodes that having not found the shortest path}
}
\description{
the update method for detail table to record the shortest path
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment