Skip to content
Snippets Groups Projects
Commit 244b39ae authored by Pranav Pankaj Chandode's avatar Pranav Pankaj Chandode
Browse files

Copying the prud files in the root

parent e5c97452
No related branches found
No related tags found
No related merge requests found
Pipeline #141034 passed
^.*\.Rproj$
^\.Rproj\.user$
...@@ -6,3 +6,39 @@ package.skeleton(list = c("fun"), name="PrUd") ...@@ -6,3 +6,39 @@ package.skeleton(list = c("fun"), name="PrUd")
.GlobalEnv .GlobalEnv
ls(envir = .GlobalEnv) ls(envir = .GlobalEnv)
library(roxygen2) library(roxygen2)
?assert
source("D:/LiU/Semester 1/Advanced R Programming/Lab/lab3/PrUd/R/euclidean.R")
euclidean(123612, 13892347912)
euclidean(100, 1000)
package.skeleton(list = c("dijkstra", "euclidean"), name="PrUd")
package.skeleton(name="PrUd")
devtools::load_all("PrUd")
package.skeleton(list = c("dijkstra", "euclidean"), name="PrUd")
?euclidean
devtools::load_all("PrUd")
?euclidean
?euclidean
?dijkstra
devtools::load_all("PrUd")
?dijkstra
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)
)
getwd()
setwd("D:/LiU/Semester 1/Advanced R Programming/Lab/lab3/PrUd")
usethis::use_data(wiki_graph, overwrite = TRUE)
devtools::load_all(".")
getwd()
setwd("D:/LiU/Semester 1/Advanced R Programming/Lab/lab3/PrUd")
devtools::load_all(".")
?testthis
?testthat
?usethis
?wiki_graph
?dijkstra
import(PrUd)
load(PrUd)
pkgbuild::check_build_tools(debug = TRUE)
covr::gitlab
image: rocker/tidyverse image: rocker/tidyverse
stages: stages:
- build - build
- test - test
- deploy - deploy
building: building:
stage: build stage: build
script: script:
- R -e "remotes::install_deps(dependencies = TRUE, pkgdir = 'PrUd')" - R -e "remotes::install_deps(dependencies = TRUE)"
- R -e 'devtools::check(pkg = "PrUd")' - R -e 'devtools::check()'
# To have the coverage percentage appear as a gitlab badge follow these # To have the coverage percentage appear as a gitlab badge follow these
# instructions: # instructions:
...@@ -15,34 +16,32 @@ building: ...@@ -15,34 +16,32 @@ building:
# The coverage parsing string is # The coverage parsing string is
# Coverage: nd+n.nd+ # Coverage: nd+n.nd+
testing: testing:
stage: test stage: test
allow_failure: true allow_failure: true
when: on_success when: on_success
only: only:
- main - main
coverage: '/coverage: nd+.nd+% of statements/' coverage: '/coverage: nd+.nd+% of statements/'
script:
- Rscript -e 'install.packages("DT")'
- Rscript -e 'install.packages("covr")'
- Rscript -e 'covr::gitlab(quiet = FALSE)'
script: artifacts:
- cd PrUd paths:
- mkdir -p public - public
- ls -la
- Rscript -e 'install.packages("DT")'
- Rscript -e 'install.packages("covr")'
- Rscript -e 'covr::gitlab(quiet = FALSE)'
artifacts:
paths:
- PrUd/public
# To produce a code coverage report as a GitLab page see # To produce a code coverage report as a GitLab page see
# https://about.gitlab.com/2016/11/03/publish-code-coverage-report-with-gitlab-pages/ # https://about.gitlab.com/2016/11/03/publish-code-coverage-report-with-gitlab-pages/
pages: pages:
stage: deploy stage: deploy
dependencies: dependencies:
- testing - testing
script: script:
- ls - ls
artifacts: artifacts:
paths: paths:
- PrUd/public - public
expire_in: 30 days expire_in: 30 days
only: only:
- main - main
\ No newline at end of file
Package: PrUd
Type: Package
Title: Implementing Euclidean and Dijkstra Algorithms in a Package
Version: 1.0
Date: 2024-09-20
Authors@R: c(
person("Uday Shanker", "Mohannan Nair",
email = "udamo524@student.liu.se", role = c("aut", "cre")),
person("Pranav", "Chandode",
email = "prach896@student.liu.se", role = c("aut")))
Description: A package for implementing Euclidean and Dijkstra algorithms. The euclidean algorithm takes two numbers as inputs and computes the GCD of the numbers. The Dijkstra takes a graph as dataframe and a start node as input and returns a vector of the distances of every node from the initial provided node.
License: MIT
RoxygenNote: 7.3.2
Depends:
R (>= 2.10)
Suggests:
testthat
LazyData: true
Encoding: UTF-8
export("euclidean")
export("dijkstra")
\ No newline at end of file
#' Dijkstra's Algorithm for Shortest Paths
#'
#' This function implements Dijkstra's algorithm to find the shortest paths
#' from a source vertex to all other vertices in a weighted graph.
#' The graph is represented as a data frame with edges and their corresponding weights.
#'
#' @param graph A data frame with three columns:
#' \describe{
#' \item{v1}{The starting vertex of the edge.}
#' \item{v2}{The ending vertex of the edge.}
#' \item{w}{The weight of the edge connecting `v1` to `v2`.}
#' }
#' @param source An integer representing the source vertex from which to calculate
#' the shortest paths. Must be present in the `v1` column of `graph`.
#'
#' @return A named numeric vector containing the shortest distance from the source
#' to each vertex in the graph. The names of the vector correspond to the vertex IDs.
#'
#' @examples
#' # Example graph represented as a data frame
#' 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)
#' )
#'
#' # Run Dijkstra's algorithm on the graph
#' shortest_paths <- dijkstra(wiki_graph, source = 1)
#' print(shortest_paths) # Prints the shortest distances from vertex 1
#'
#' @references
#' Dijkstra's Algorithm. (n.d.). In Wikipedia. Retrieved from
#' https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
#'
#' @export
dijkstra <- function(graph,source){
stopifnot(source %in% graph$v1);
distance<-c();
v<-c();
unvisited_set<- c();
for (value in 1:length(unique(graph$v1))) {
distance[value]<-Inf;
unvisited_set<- append(unvisited_set,value);
}
prev_node <-c(numeric(length = length(unvisited_set)))
distance[source]<-0;
while (length(unvisited_set) > 0) {
current_index <- which.min(distance[unvisited_set])
u <- unvisited_set[which.min(distance[unvisited_set])]
unvisited_set <- unvisited_set[-(which.min(distance[unvisited_set]))]
ind_pos_v2 <-c(which(graph$v1== u))
for (neighbor in ind_pos_v2) {
v <- graph$v2[neighbor]
if (v %in% unvisited_set) {
alt <- distance[u] + graph$w[neighbor];
if (alt < distance[v]) {
distance[v] <- alt
prev_node[v] <- u
}
}
}
}
return(distance);
}
\ No newline at end of file
#' Euclidean Algorithm to Compute the GCD
#'
#' This function computes the greatest common divisor (GCD) of two integers
#' using the Euclidean algorithm. The Euclidean algorithm is an efficient method for
#' computing the GCD of two numbers by iterative division.
#'
#' @param num1 An integer.
#' @param num2 An integer.
#'
#' @return An integer representing the GCD of `num1` and `num2`.
#'
#' @examples
#' # Example usage:
#' euclidean(123612, 13892347912) # Returns 4
#' euclidean(48, 18) # Returns 6
#' euclidean(101, 10) # Returns 1
#'
#' @references
#' Euclidean Algorithm. (n.d.). In Wikipedia. Retrieved from
#' https://en.wikipedia.org/wiki/Euclidean_algorithm
#'
#' @export
euclidean <- function(num1, num2){
stopifnot(is.numeric(num1), is.numeric(num2))
smaller <- min(abs(num1), abs(num2))
larger <- max(abs(num1), abs(num2))
while(smaller != 0){
temp <- smaller
smaller <- larger %% smaller
larger <- temp
}
return(larger)
}
\ No newline at end of file
#' A Graph for Dijkstra Algorithm
#'
#' This dataset represents a simple graph used to demonstrate Dijkstra's shortest path algorithm.
#'
#' @format A data frame with 18 rows and 3 variables:
#' \describe{
#' \item{v1}{The source node of the edge (integer)}
#' \item{v2}{The destination node of the edge (integer)}
#' \item{w}{The weight of the edge (numeric)}
#' }
#' @source \url{https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm}
#' @examples
#' data(wiki_graph)
#' print(wiki_graph)
#' dijkstra(wiki_graph, 1)
"wiki_graph"
File added
\name{PrUd-package}
\alias{PrUd-package}
\alias{PrUd}
\docType{package}
\title{
\packageTitle{PrUd}
}
\description{
\packageDescription{PrUd}
}
\details{
%% ~~ An overview of how to use the package, ~~
%% ~~ including the most important functions ~~
}
\author{
\packageAuthor{PrUd}
Maintainer: \packageMaintainer{PrUd}
}
\references{
%% ~~ Literature or other references for background information ~~
}
\keyword{package}
%% Uncomment below to imitate parts of library(help = PrUd)
%\section{The \file{DESCRIPTION} File}{\packageDESCRIPTION{PrUd}}
%\section{Documentation Index}{\packageIndices{PrUd}}
\seealso{
%% ~~ Optional links to other man pages, e.g. ~~
%% ~~ \code{\link[<pkg>:<pkg>-package]{<pkg>}} ~~
}
\examples{
%% ~~ Optional simple examples of the most important functions ~~
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/dijkstra.R
\name{dijkstra}
\alias{dijkstra}
\title{Dijkstra's Algorithm for Shortest Paths}
\usage{
dijkstra(graph, source)
}
\arguments{
\item{graph}{A data frame with three columns:
\describe{
\item{v1}{The starting vertex of the edge.}
\item{v2}{The ending vertex of the edge.}
\item{w}{The weight of the edge connecting `v1` to `v2`.}
}}
\item{source}{An integer representing the source vertex from which to calculate
the shortest paths. Must be present in the `v1` column of `graph`.}
}
\value{
A named numeric vector containing the shortest distance from the source
to each vertex in the graph. The names of the vector correspond to the vertex IDs.
}
\description{
This function implements Dijkstra's algorithm to find the shortest paths
from a source vertex to all other vertices in a weighted graph.
The graph is represented as a data frame with edges and their corresponding weights.
}
\examples{
# Example graph represented as a data frame
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)
)
# Run Dijkstra's algorithm on the graph
shortest_paths <- dijkstra(wiki_graph, source = 1)
print(shortest_paths) # Prints the shortest distances from vertex 1
}
\references{
Dijkstra's Algorithm. (n.d.). In Wikipedia. Retrieved from
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/euclidean.R
\name{euclidean}
\alias{euclidean}
\title{Euclidean Algorithm to Compute the GCD}
\usage{
euclidean(num1, num2)
}
\arguments{
\item{num1}{An integer.}
\item{num2}{An integer.}
}
\value{
An integer representing the GCD of `num1` and `num2`.
}
\description{
This function computes the greatest common divisor (GCD) of two integers
using the Euclidean algorithm. The Euclidean algorithm is an efficient method for
computing the GCD of two numbers by iterative division.
}
\examples{
# Example usage:
euclidean(123612, 13892347912) # Returns 4
euclidean(48, 18) # Returns 6
euclidean(101, 10) # Returns 1
}
\references{
Euclidean Algorithm. (n.d.). In Wikipedia. Retrieved from
https://en.wikipedia.org/wiki/Euclidean_algorithm
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/wiki_graph.R
\docType{data}
\name{wiki_graph}
\alias{wiki_graph}
\title{A Graph for Dijkstra Algorithm}
\format{
A data frame with 18 rows and 3 variables:
\describe{
\item{v1}{The source node of the edge (integer)}
\item{v2}{The destination node of the edge (integer)}
\item{w}{The weight of the edge (numeric)}
}
}
\source{
}
\usage{
wiki_graph
}
\description{
This dataset represents a simple graph used to demonstrate Dijkstra's shortest path algorithm.
}
\examples{
data(wiki_graph)
print(wiki_graph)
dijkstra(wiki_graph, 1)
}
\keyword{datasets}
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html
library(testthat)
library(PrUd)
test_check("PrUd")
context("dijkstra")
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))
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))
})
test_that("Error messages are returned for erronous input in the Dijkstra algorithm.", {
wiki_wrong_graph <- wiki_graph
names(wiki_wrong_graph) <- c("v1, v3, w")
expect_error(dijkstra(wiki_wrong_graph, 3))
wiki_wrong_graph <- wiki_graph[1:2]
expect_error(dijkstra(wiki_wrong_graph, 3))
expect_error(dijkstra(wiki_graph, 7))
expect_error(dijkstra(as.matrix(wiki_graph), 3))
})
\ No newline at end of file
context("euclidean")
test_that("GDC is calculated correctly.", {
expect_equal(euclidean(123612, 13892347912), 4)
expect_equal(euclidean(100, 1000), 100)
expect_equal(euclidean(-100, 1000), 100)
})
test_that("Wrong input throws an error.", {
expect_error(euclidean("100", 1000))
expect_error(euclidean(100, "1000"))
expect_error(euclidean(TRUE, "1000"))
})
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment