Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let nodes = {
- A: {
- bind: [
- ['B', 2],
- ['C', 10]
- ],
- weight: 0, // Вес самой ноды
- nearest: '', // Близжайшая нода
- },
- B: {
- bind: [
- ['A', 2],
- ['D', 20]
- ],
- weight: Infinity,
- nearest: ''
- },
- C: {
- bind: [
- ['A', 10],
- ['D', 2]
- ],
- weight: Infinity,
- nearest: ''
- },
- D: {
- bind: [
- ['B', 20],
- ['C', 2],
- ['E', 1]
- ],
- weight: Infinity,
- nearest: ''
- },
- E: {
- bind: [
- ['D', 1]
- ],
- weight: Infinity,
- nearest: ''
- }
- }
- let keys = Object.keys(nodes);
- keys.forEach(index => {
- nodes[index].bind.forEach(bind => {
- let name = bind[0];
- let weight = bind[1];
- if(nodes[name].weight > nodes[index].weight + weight) {
- nodes[name].weight = nodes[index].weight + weight;
- nodes[name].nearest = index;
- }
- });
- });
- keys.forEach(key => console.log(`P: ${key}:${nodes[key].nearest} W: ${nodes[key].weight}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement