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

screeps-min-cut-wall

v1.0.0

Published

Minimize the amount of walls necessary by graph theory!

Downloads

3

Readme

minCutWall

Minimize the amount of walls necessary by graph theory!

I've tried my best to make the code as easy as possible to understand.
Feel free to extend it and use it for your script! 😊

Visualization of minCutWall computation result

Usage

The function minCutWall takes the two arguments:

  • isWall: ({x,y}) => boolean: Treat these as not walkable.
  • isCenter: ({x,y}) => boolean: Don't put walls here. Center positions don't have to be connected.

It returns a list of positions which minimizes the amount of walls you have to place to separate the center (defined by isCenter) from the exits.

Have a look at the tests to get a feel for the returned values.

If you want your center to be a rectangular region, you can use isCenter = isInRegion({ left, top, right, bottom }).

Walls are usually defined by isWall = ({ x, y }) => terrain.get(x, y) === TERRAIN_MASK_WALL.

Explanation

The main idea is to find a minimum cut in the flow network from center to exit.
See Dinic's algorithm and Max-flow min-cut theorem.

Step 1 - createGraph

We create a weighted directed graph where each walkable position is represented by a vertex.
Edges connect neighboring vertices (neighboring in the sense of walking).
Additionally we create a source node for the center and a target node for all exits.

We want to partition the edges of the graph by removing as little vertices as possible.
Since minimum cut algorithms only work for partitioning the vertices, we have to represent the room in a slightly different way:

Build the graph such that each position becomes two vertices - connected by a single edge. All incoming edges are connected to one vertex and all outgoing edges to the other vertex.
The path s -> u -> v -> t in the "room world" will become s -> u_in -> u_out -> v_in -> v_out -> t in the "graph world".

Since we only want to cut these specially created "node-edges", we assign them a capacity/weight of 1. All other edges have a capacity/weight of Infinity.

Step 2 - minCut

Execute a maximum flow algorithm on the graph.

The residual graph of that flow (meaning all capacity the graph has left after subtracting the flow) splits the vertices in at least two partitions.
A minimum cut can be constructed by those edges from the initial graph which connect the source partition with another partition and which got drained in the residual graph (meaning they got fully used by the maximum flow).

The resulting edges in the minimum cut can be traced back to the positions in the room.