GithubHelp home page GithubHelp logo

Comments (2)

ArthurMiao avatar ArthurMiao commented on June 25, 2024

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.

obf1313 avatar obf1313 commented on June 25, 2024

@loiane it seems wrong, can u check it and modify it, thx.

from learning-javascript-data-structures-and-algorithms-third-edition.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.