npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

matroidjs

v1.1.0

Published

A library to model any data structures with [matroids](https://en.wikipedia.org/wiki/Matroid). "A matroid is a structure that abstracts and generalizes the notion of linear independence in vector spaces". For example any finite graphs and matrices can be

Downloads

4

Readme

matroidJS

A library to model any data structures with matroids. "A matroid is a structure that abstracts and generalizes the notion of linear independence in vector spaces". For example any finite graphs and matrices can be viewed as matroids. This library views matroids as a way to model dependencies in any given sets.

Usage

To get started with Matroids you need to define what is considered a dependency in your data. Use Matroid abstract class for this:

class TaskMatroid extends Matroid<Task> {
    // are there tasks with same person working on it?
    public hasCircuit(taskSet: Task[]): boolean {
        const people: string[] = [];
        const taskIds = [];
        for (const task of taskSet) {
            if (taskIds.includes(task.id)) {
                continue;
            }
            taskIds.push(task.id);
            const peopleOnTask = task.contributors.map(contributor => contributor.name);
            for (const person of peopleOnTask) {
                if (people.includes(person)) {
                    return true;
                }
                people.push(person);
            }
        }
        return false;
    }
}

In this example we have Tasks as the model we want to matroidize. Tasks have contributors and contributors have names. Any two different Tasks are considered dependent if there is a person working on both of them. Note the function signature public hasCircuit(taskSet: Task[]): boolean {, the Matroid descendant must be able to tell if there is a dependency in a T[].

After defining your dependency function hasCircuit we can add our data to the new class

const MOCK_TASKS: Task[] = [
    {contributors: [MOCK_PEOPLE[0]], id: "1"},
    {contributors: [MOCK_PEOPLE[0], MOCK_PEOPLE[1]], id: "2"}, 
    {contributors: [MOCK_PEOPLE[2]], id: "3"}, 
    {contributors: [MOCK_PEOPLE[3]], id: "4"}
];
taskMatroid = new TaskMatroid(MOCK_TASKS);

Having the matroid initialized we can access information about our original task set such as its rank taskMatroid.rank, that tells the maximum task set size that could contain only independent tasks. You also have ground and independent properties. ground contains all possible combinations of the initializing set (if not a T[][] was given to the constructor) while independent contains all the sets of ground with only independent items in them.

Then there are the util functions consuming matroids:

findBase():

Returns the first rank sized (biggest possible) independent set of T (T[]). For instance in the example above this would contain 3 Tasks (id 1,3,4). Works on both matroids and subsets (T[][]) of E ground as well

findAllBases():

As opposed to findBase() this returns all the rank sized T sets (T[][]) that are independent, in this example case there are two: the one mentioned above (ids 1,3,4), and an other (ids 2,3,4)

findIndependents():

Returns all independent sets (T[][]) regardless of their size.

getClosure():

This function return a set of sets (T[][]) thats rank is not greater than the original, meaning the maximum number of independent T items in every returned T sets (T[]) is less then or equal to the original set. The parameter for this function is a set or sets (T[][]), not a Matroid. Naturally if the parameter contains a base as well, then the return value will be the ground of the Matroid.