+
Skip to content
/ munkres Public

Maximum and minimum cost assignments using the Hungarian/Munkres algorithm for n × m cost matrices

License

Notifications You must be signed in to change notification settings

alg-js/munkres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@alg/munkres

JSR License

Maximum and minimum cost assignments using the Hungarian/Munkres algorithm for n × m cost matrices.

Install

deno add jsr:@alg/munkres
Other Install Options
npx jsr add @alg/munkres
bunx jsr add @alg/munkres
pnpm i jsr:@alg/munkres
yarn add jsr:@alg/munkres
vlt install jsr:@alg/munkres

Example

@alg/munkres provides min/max cost functions and min/max assignment functions.

The cost functions return the cost of a maximum or minimum assignment:

import {minCost, maxCost} from "@alg/munkres";

const costMatrix = [
    [8, 4, 7],
    [5, 2, 3],
    [9, 4, 8],
];
console.log(minCost(costMatrix));  // 15
console.log(maxCost(costMatrix));  // 18

The assignment functions return the indices in the cost matrix for a minimum or maximum assignment:

import {minAssignment, maxAssignment} from "@alg/munkres";

const costMatrix = [
    [8, 4, 7],
    [5, 2, 3],
    [9, 4, 8],
];
console.log(minAssignment(costMatrix));  // [ [ 0, 0 ], [ 1, 2 ], [ 2, 1 ] ]
console.log(maxAssignment(costMatrix));  // [ [ 0, 0 ], [ 1, 1 ], [ 2, 2 ] ]

All cost and assignment functions handle any n × m matrices:

import {minAssignment, maxAssignment} from "@alg/munkres";

const wide = [
    [4, 5, 8, 7],
    [2, 12, 6, 5],
    [7, 8, 3, 9],
];

const long = [
    [11, 13, 13],
    [7, 21, 13],
    [10, 7, 15],
    [17, 11, 13],
    [10, 13, 14],
];

console.log(minAssignment(wide));  // [ [ 0, 1 ], [ 1, 0 ], [ 2, 2 ] ]
console.log(maxAssignment(long));  // [ [ 1, 1 ], [ 2, 2 ], [ 3, 0 ] ]

About

Maximum and minimum cost assignments using the Hungarian/Munkres algorithm for n × m cost matrices

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载