Comments (2)
That's something I've learned to improve on
const find = (i, parent) => {
while (parent[i]) {
i = parent[i];
}
return i;
}
const union = (u, v, parent) => {
if (u !== v) {
parent[v] = u;
return true;
}
return false;
}
const INF = Number.MAX_SAFE_INTEGER;
const initializeCost = graph => {
const cost = [];
const {length} = graph;
for (let i = 0; i < length; i++) {
cost[i] = [];
for (let j = 0; j < length; j++) {
if (graph[i][j] === 0) {
cost[i][j] = INF;
} else {
cost[i][j] = graph[i][j];
}
}
}
return cost;
};
const kruskal = graph => {
const {length} = graph;
const route = {};
const parent = [];
let ne = 0;
let u;
let v;
const cost = initializeCost(graph);
while (ne < length - 1) {
for (let i = 0, min = INF; i < length; i++) {
for (let j = 0; j < length; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
u = i;
v = j;
}
}
}
if (union(find(u, parent), find(v, parent), parent)) {
route[`${u}-${v}`] = cost[u][v];
ne++;
}
cost[u][v] = cost[v][u] = INF;
}
return route;
};
const graph = [
[0, 2, 4, 0, 0, 0],
[2, 0, 2, 4, 2, 0],
[4, 2, 0, 0, 3, 0],
[0, 4, 0, 0, 3, 2],
[0, 2, 3, 3, 0, 2],
[0, 0, 0, 2, 2, 0]
];
const result = kruskal(graph);
console.log(result);
from learning-javascript-data-structures-and-algorithms-third-edition.
@loiane it seems wrong, can u check it and modify it, thx.
from learning-javascript-data-structures-and-algorithms-third-edition.
Related Issues (11)
- Circular-lineked-list HOT 2
- Calculation of the number of elements in bucket sorting HOT 2
- This condition is unnecessary? HOT 1
- AVL-tree removeNode func is not correct HOT 2
- "TypeError: Cannot read property 'properties' of undefined" webpack-cli error fix HOT 2
- kruskal output different from prim
- verifyRemoveSideEffect possible simplification
- The examples is not contain chapter 10 HOT 1
- Test to fail
- Doubt?return this.rotationLR(node.left); HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learning-javascript-data-structures-and-algorithms-third-edition.