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 🙏

© 2026 – Pkg Stats / Ryan Hefner

lin-kernighan

v1.0.1

Published

Adaptive k-opt heuristic improving TSP tours by swapping edges to shorten routes.

Readme

lin-kernighan

Adaptive k-opt heuristic improving TSP (traveling salesman problem) tours by swapping edges to shorten routes.

Installation

npm install lin-kernighan

Usage

import tsp from "lin-kernighan";

tsp([
  { latitude: 40.7128, longitude: -74.006 }, // New York
  { latitude: 34.0522, longitude: -118.2437 }, // Los Angeles
  { latitude: 55.7558, longitude: 37.6173 }, // Moscow
  { latitude: -33.8688, longitude: 151.2093 }, // Sydney
  { latitude: -22.9068, longitude: -43.1729 }, // Rio de Janeiro
]);
// => [
//   { latitude: 40.7128, longitude: -74.006 },   // New York
//   { latitude: -22.9068, longitude: -43.1729 }, // Rio de Janeiro
//   { latitude: -33.8688, longitude: 151.2093 }, // Sydney
//   { latitude: 55.7558, longitude: 37.6173 },   // Moscow
//   { latitude: 34.0522, longitude: -118.2437 }  // Los Angeles
// ]

Algorithm

The Lin-Kernighan algorithm solves the Traveling Salesman Problem by finding the shortest route that visits all cities exactly once and returns to the starting point. Here's how it works in simple terms:

  • You have multiple cities (points) to visit
  • You need to find the shortest possible route that visits each city once
  • The route must return to where you started

Here's a step by step process:

  1. Calculate Distances

    • Measure the distance between every pair of cities
    • Uses the haversine formula for geographic coordinates (accounts for Earth's curvature)
  2. Create Initial Route

    • Start at the first city
    • Always go to the nearest unvisited city next
    • This gives us a "decent" starting route (called nearest neighbor heuristic)
  3. Improve the Route (2-opt)

    • Take any segment of the route and reverse it
    • If this makes the total distance shorter, keep the change
    • Try all possible segments until no improvements are found
  4. Further Improvements (3-opt)

    • When 2-opt can't improve anymore, try more complex changes
    • Rearrange three segments of the route in different ways
    • Again, only keep changes that reduce total distance
  5. Repeat Until Optimal

    • Keep trying improvements until no better route can be found
    • The result is a highly optimized tour

Why It Works:

  • Starts with a reasonable solution instead of random
  • Makes incremental improvements rather than starting over
  • Uses local optimization to escape poor route choices
  • Balances computation time with solution quality

Performance Optimizations

This implementation includes several performance improvements while maintaining algorithm correctness:

  • Delta calculation: Computes only edge differences instead of recalculating entire tour distance
  • Early termination: Breaks loops immediately when improvement found rather than exhaustive search
  • In-place operations: Modifies tours directly to reduce memory allocations
  • First-improvement strategy: Takes first beneficial swap found for faster convergence

These optimizations provide 2-5x speedup for typical TSP instances.