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

quick-dp

v1.0.2

Published

Use dynamic programming quickly

Downloads

229

Readme

quick-dp

Use dynamic programming quickly

Figure 1. Finding the shortest path in a graph using optimal substructure; a straight line indicates a single edge; a wavy line indicates a shortest path between the two vertices it connects (other nodes on these paths are not shown); the bold line is the overall shortest path from start to goal. Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics. In both contexts it refers to simplifying a complicated problem by breaking it down into simpler sub-problems in a recursive manner. While some decision problems cannot be taken apart this way, decisions that span several points in time do often break apart recursively. Likewise, in computer science, a problem that can be solved optimally by breaking it into sub-problems and then recursively finding the optimal solutions to the sub-problems is said to have optimal substructure. --from wikipedia's Dynamic_programming

install

npm install quick-dp --save

example

const DynamicProgramming = require('quick-dp');
// change coin question by dynamic programming
class Coin
{
    constructor(much)
    {
        this.much = much;       
    }
}
let coinTypeList = [new Coin(1),new Coin(2),new Coin(5)];
let coinDP = new DynamicProgramming(coinTypeList,13);
let totalMoney = 0;
let coinResult = coinDP.run((item,itemKey,nowPurpose)=>{
    return Math.ceil(nowPurpose/item.much);
},(item,itemResult,itemKey,nowPurpose,purpose,result)=>{
    let money = item.much*itemResult;
    if (money<=purpose-totalMoney)
    {
        result.push({coin:item,num:itemResult});
        if (totalMoney+money==purpose){return DynamicProgramming.RETURN_FIND;}
        totalMoney+=money;
        return DynamicProgramming.BREAK_FIND;
    }
});
console.log("change coin question:")
/**
 * coinResult:
 * [ { coin: Coin { much: 5 }, num: 2 },
 * { coin: Coin { much: 2 }, num: 1 },
 * { coin: Coin { much: 1 }, num: 1 } ]
 */
console.log(coinResult)

//01 bag question by dynamic programming
class Bag
{
    constructor(weight)
    {
        this.weight = weight;       
    }
    static getMinWeightThing(ThingList)
    {
        let minWeight = 0;
        for(let i =0;i<ThingList.length;i++)
        {
            if(i==0) {
                minWeight = ThingList[i].weight;
            }else{
                if (minWeight>ThingList[i].weight){minWeight = ThingList[i].weight;}
            }
        }
        return minWeight;
    }
}
class Thing
{
    constructor(weight,value)
    {
        this.weight = weight;
        this.value = value;
    }
}
let bag = new Bag(8);
let ThingList = [new Thing(2,3),new Thing(3,4),new Thing(4,5),new Thing(5,6)];
let minWeight = Bag.getMinWeightThing(ThingList);
let bagDP = new DynamicProgramming(ThingList,bag.weight);
let unusedWeight = bag.weight;
let bagResult = bagDP.run((item,itemKey,nowPurpose,singleResultList)=>{
    let beforeRow = itemKey-1;
    if(nowPurpose<item.weight)
    {
        if (beforeRow==-1)
        {
            return 0;
        } else {
            return singleResultList[beforeRow][nowPurpose];
        }
    } else {
        if (beforeRow==-1){return item.value;}
        let maxWeight = singleResultList[beforeRow][nowPurpose-item.weight]+item.value;
        if(item.value>maxWeight)
        {
            return item.value;
        } else {
            return maxWeight;
        }
    }
},(item,itemResult,itemKey,nowPurpose,purpose,result,singleResultList)=>{
    if(unusedWeight-item.weight>=0){
        if(unusedWeight != nowPurpose){return DynamicProgramming.CONTINUE_FIND;}
        if (
            itemKey-1<0 || 
            singleResultList[itemKey][nowPurpose]>singleResultList[itemKey-1][nowPurpose]
        ){
            result.push(item);
        } else {
            return DynamicProgramming.CONTINUE_FIND;
        }
        unusedWeight = unusedWeight-item.weight;
        if (itemKey ==0 || unusedWeight<minWeight){return DynamicProgramming.RETURN_FIND;}
        return DynamicProgramming.BREAK_FIND;
    } else {
        return DynamicProgramming.BREAK_FIND;
    }
});
console.log("bag question:")
console.log(bagResult);

// sort question by dynamic programming
let needSortList = [1,7,6,8,2,5,4,3];
let sortDP = new DynamicProgramming(needSortList,needSortList.length-1);
let countMap = {};
let countIn;
let sortSingleResultList = sortDP.getSingleResultList((item,itemKey,nowPurpose)=>{
    if (countMap[item]==undefined){countMap[item]=0;}
    if (item>needSortList[nowPurpose]){
        countMap[item]++;
    }
    return countMap[item];
});
countIn = 0;
let sortResultASC = sortDP.findResult((item,itemResult,itemKey,nowPurpose,purpose,result)=>{
    result[itemResult] = item;
    countIn++;
    if (countIn>purpose){return DynamicProgramming.RETURN_FIND;} else {return DynamicProgramming.BREAK_FIND;}
},sortSingleResultList)
countIn = 0;
let sortResultDESC = sortDP.findResult((item,itemResult,itemKey,nowPurpose,purpose,result)=>{
    result[purpose-itemResult] = item;
    countIn++;
    if (countIn>purpose){return DynamicProgramming.RETURN_FIND;} else {return DynamicProgramming.BREAK_FIND;}
},sortSingleResultList)

console.log("sort question:")
/**
 * sortResult:
 * [ 1, 2, 3, 4, 5, 6, 7, 8 ]
 */
console.log(sortResultASC)
/**
 * sortResult:
 * [ 8, 7, 6, 5, 4, 3, 2, 1 ]
 */
console.log(sortResultDESC)