Skip to content

Instantly share code, notes, and snippets.

View mlbj's full-sized avatar

mauro brandão mlbj

  • Brazilian Synchrotron Light Laboratory
  • LinkedIn in/mlbj
View GitHub Profile
@mlbj
mlbj / merge-sort.cpp
Created September 28, 2025 11:26
Concise C++ implementation of merge sort for quick reference
#include <vector>
#include <stdio.h>
#include <cmath>
// Merge function
void merge(std::vector<int>& target, int left, int mid, int right){
// Sizes
int n1 = mid - left + 1;
int n2 = right - mid;
@mlbj
mlbj / ring-buffer.c
Last active September 25, 2025 13:32
Byte-addressable ring buffer implementation in external memory
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
// Ring buffer typedef
typedef struct{
volatile uint8_t *buffer;
volatile uint16_t head;
volatile uint16_t tail;
@mlbj
mlbj / naive_dijkstra.py
Created February 21, 2024 19:07
A O(mn) implementation of Dijkstra's algorithm
# A very naive implementation of Dijkstra's algorithm in O(nm)
# computational time, where n is the number of nodes and m is
# the number of edges. This can be improved to O(m+nlogn) with
# a min-heap.
# It takes a weighted adjacency list representation of a graph
# as input. We assume that all labels can be uniquely identified
# in the set {1,2,..,n}, where n is the second parameter.
# The output is a dictionary containing the minimum distance from
# source_node to every other node in the graph. If node_t is
# passed, then the algorithm terminates early and only the
@mlbj
mlbj / kosaraju.py
Created February 20, 2024 23:20
A simple implementation of the Kosaraju's algorithm
# A simple implementation of the Kosaraju's algorithm to find the strongly
# connected components of a graph. It takes a graph represented as an edge
# list as input, which is preferred since it is easily reversible. We are
# also assuming that nodes are labeled from 1 to n in the input graph.
# The output is is a dictionary containing 'node':'leader_node' pairs.
import sys
sys.setrecursionlimit(100000) # we will use deep recursions