M2algorithmique Vignette

Vincent Runge

LaMME, Evry Paris-Saclay University

March 17, 2025

Package Presentation

Quick Start

Examples

Analysis


Package Presentation

The M2algorithmique R/Rcpp package is an example package designed to help students develop their own R/Rcpp packages as part of the M2 Algorithmic courses in the Data Science master's program at Université d'Évry Paris-Saclay.

This package provides implementations of various algorithmic strategies in both R and Rcpp, including:

This package serves as a reference for students, demonstrating good practices in algorithm implementation and package development in Rcpp.


Quick Start

Prerequisites for Package Development

To develop and use the package, install the necessary dependencies:

install.packages(c("Rcpp", "RcppArmadillo", "devtools", "roxygen2", "testthat"))

Installing the Package from GitHub

To install the M2algorithmique package, use:

devtools::install_github("vrunge/M2algorithmique")

Then, load the package:

library(M2algorithmique)

Setting Up a C++ Compiler (Windows Users)

Since Rcpp requires a C++ compiler, Windows users must install Rtools:

Download and install Rtools from: https://cran.r-project.org/bin/windows/Rtools/

For general R updates and additional package downloads, visit the CRAN: https://cran.r-project.org/

Package Structure


Examples

Sorting Algorithms (Recursive)

n <- 10
v <- sample(n)
v

We implemeted 4 algorithms:

They all have a unique argument: the unsorted vector v. Examples:

insertion_sort(v)
heap_sort_Rcpp(v)

Dijkstra Algorithms (Dynamic Programming)

TSP Algorithms (Heuristic)

We generate n = 20 towns (villes) randomly in a 2D plane using a uniform distribution:

n <- 20
villes <- matrix(runif(2 * n), n, 2)

Visualization of the Towns

plot(villes[,1], villes[,2], xlim = c(0,1), ylim = c(0,1), xlab = "", ylab = "", asp=1, pty ="s")

We implemented four heuristic algorithms and one exact algorithm for solving the Travelling Salesman Problem (TSP):

Each algorithm also has an equivalent Rcpp implementation for performance optimization.

Example

res1 <- TSP_naif(villes)
res2 <- TSP_cheapest(villes)
res3 <- TSP_nearest(villes) 
res4 <- TSP_farthest(villes)
t1 <- tour_length(res1, villes)
t2 <- tour_length(res2, villes)
t3 <- tour_length(res3, villes)
t4 <- tour_length(res4, villes)
par(mfrow=c(2,2),
    oma = c(5,4,0,0) + 0.1,
    mar = c(1.5,1.5,1,0) + 0.1)
plot.TSP(tour = res1, data = villes, main = "naif", value = t1)
plot.TSP(tour = res2, data = villes, main = "cheapest", value = t2)
plot.TSP(tour = res3, data = villes, main = "nearest", value = t3)
plot.TSP(tour = res4, data = villes, main = "farthest", value = t4)

Analysis

The Pour_les_etudiants folder contains the following analysis files:

These documents, written in French, outline the type of analysis students are expected to conduct to validate this course.



vrunge/M2algorithmique documentation built on April 5, 2025, 4:06 a.m.