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

@algorithm.ts/dijkstra-bigint

v2.0.14

Published

Dijkstra algorithm (bigint version) optimized with @algorithm.ts/priority-queue.

Downloads

51

Readme

A typescript implementation of the dijkstra algorithm (bigint version).

The following definition is quoted from Wikipedia (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm):

Dijkstra's algorithm (/ˈdaɪkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

Install

  • npm

    npm install --save @algorithm.ts/dijkstra-bigint
  • yarn

    yarn add @algorithm.ts/dijkstra-bigint
  • deno

    import dijkstra from 'https://raw.githubusercontent.com/guanghechen/algorithm.ts/main/packages/dijkstra-bigint/src/index.ts'

Usage

  • Simple

    import dijkstra from '@algorithm.ts/dijkstra-bigint'
    
    const dist: bigint[] = dijkstra({
      N: 4,
      source: 0,
      edges: [
        { to: 1, cost: 2n },
        { to: 2, cost: 2n },
        { to: 3, cost: 2n },
        { to: 3, cost: 1n },
      ],
      G: [[0], [1, 2], [3], []],
    })
    // => [0n, 2n, 4n, 4n]
    // 
    //    Which means:
    //      0 --> 0: cost is 0n
    //      0 --> 1: cost is 2n
    //      0 --> 2: cost is 4n
    //      0 --> 3: cost is 4n
  • Pass custom dist array.

    import dijkstra from '@algorithm.ts/dijkstra-bigint'
    
    const dist: bigint[] = []
    dijkstra({
      N: 4,
      source: 0,
      edges: [
        { to: 1, cost: 2n },
        { to: 2, cost: 2n },
        { to: 3, cost: 2n },
        { to: 3, cost: 1n },
      ],
      G: [[0], [1, 2], [3], []],
      dist,
    })
    
    dist // => [0n, 2n, 4n, 4n]

Example

  • A solution for leetcode "Number of Ways to Arrive at Destination" (https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/):

    import type { IEdge } from '@algorithm.ts/dijkstra-bigint'
    import dijkstra from '@algorithm.ts/dijkstra-bigint'
    
    const MOD = BigInt(1e9 + 7)
    export function countPaths(N: number, roads: number[][]): number {
      const edges: IEdge[] = []
      const G: number[][] = new Array(N)
      for (let i = 0; i < N; ++i) G[i] = []
      for (const [from, to, _cost] of roads) {
        const cost = BigInt(_cost)
    
        G[from].push(edges.length)
        edges.push({ to, cost })
    
        G[to].push(edges.length)
        edges.push({ to: from, cost })
      }
    
      const source = 0
      const target = N - 1
      const dist: bigint[] = dijkstra({ N, source: target, edges, G, dist: customDist }, { INF: BigInt(1e12) })
    
      const dp: bigint[] = new Array(N).fill(-1n)
      return Number(dfs(source))
    
      function dfs(o: number): bigint {
        if (o === target) return 1n
    
        let answer = dp[o]
        if (answer !== -1n) return answer
    
        answer = 0n
        const d = dist[o]
        for (const idx of G[o]) {
          const e: IEdge = edges[idx]
          if (dist[e.to] + e.cost === d) {
            const t = dfs(e.to)
            answer = modAdd(answer, t)
          }
        }
        dp[o] = answer
        return answer
      }
    }

Related