# javascript手撕kruskal算法

kruskal是一种求最小生成树的贪心算法，与prim算法的以点为寻找目标不同，kruskal算法是以边为寻找目标，核心思想很简单，每次找到最小的边，把这些边串起来生成树就可以了，只是这个过程中要注意避免环的产生，这也是这个算法的难点，以下是js代码

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;
const parent = [];
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;
};
export 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, 0, 3, 0], [0, 0, 1, 0], [3, 1, 0, 2], [0, 0, 2, 0]];
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.dir(result);

09-05

12-13 1757
10-28 1083
04-12 7008
07-22 1万+
12-24 2133
01-29
11-07 767
06-21