当前位置:   article > 正文

【算法可视化】图论专题

【算法可视化】图论专题

运行平台

Algorithm Visualizer

图的深度优先遍历

// import visualization libraries {
const { Tracer, Array1DTracer, GraphTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
// }

// define tracer variables {
const graphTracer = new GraphTracer().directed(false);
const visitedTracer = new Array1DTracer('visited');
const logger = new LogTracer();
Layout.setRoot(new VerticalLayout([graphTracer, visitedTracer, logger]));
graphTracer.log(logger);
const G = Randomize.Graph({ N: 8, ratio: .3, directed: false });
graphTracer.set(G);
Tracer.delay();
// }

function DFS(graph, source) {
  const stack = [[source, null]];
  const visited = [];
  let node;
  let prev;
  let i;
  let temp;
  for (i = 0; i < graph.length; i++) {
    visited.push(false);
  }
  // visualize {
  visitedTracer.set(visited);
  // }

  while (stack.length > 0) {
    temp = stack.pop();
    node = temp[0];
    prev = temp[1];

    if (!visited[node]) {
      visited[node] = true;
      // visualize {
      visitedTracer.patch(node, visited[node]);

      if (prev !== undefined && graph[node][prev]) {
        graphTracer.visit(node, prev);
        Tracer.delay();
      } else {
        graphTracer.visit(node);
        Tracer.delay();
      }
      // }

      for (i = 0; i < graph.length; i++) {
        if (graph[node][i]) {
          stack.push([i, node]);
        }
      }
    }
  }

  return visited;
}

const visited = DFS(G, 0);
let check = true;
for (let i = 0; i < visited.length; i++) check &= visited[i];
// logger {
if (check) {
  logger.println('图是连通的');
} else {
  logger.println('图不是连通的');
}
// }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

图的广度优先遍历

// import visualization libraries {
const { Tracer, GraphTracer, LogTracer, Randomize, Layout, VerticalLayout } = require('algorithm-visualizer');
// }

// define tracer variables {
const tracer = new GraphTracer().directed(false).weighted();
const logger = new LogTracer();
Layout.setRoot(new VerticalLayout([tracer, logger]));
tracer.log(logger);
//随机产生一个6个节点,无向,权值为1的图,边的数量为完全图的30%
const G = Randomize.Graph({ N: 6, ratio: .3, directed: false, weighted: false });
tracer.set(G);
Tracer.delay();
// }

function BFS() {
  const W = []; // W[i] 表示从根节点到i节点的权值
  const Q = [];
  let i;
  for (i = 0; i < G.length; i++) {
    W.push(MAX_VALUE); //将每个节点的权值初始化为无穷大
    // visualize {
    tracer.updateNode(i, MAX_VALUE);
    // }
  }
  W[s] = 0;
  Q.push(s); // 将起点加入队列
  // visualize {
  tracer.visit(s, undefined, 0);
  Tracer.delay();
  // }
  while (Q.length > 0) {
    const node = Q.shift(); // 头节点出队
    for (i = 0; i < G[node].length; i++) {
      if (G[node][i]) { // if the edge from current node to the i-th node exists
        if (W[i] > W[node] + G[node][i]) { // if current path is shorter than the previously shortest path
          W[i] = W[node] + G[node][i]; // update the length of the shortest path
          Q.push(i); // add child node to queue
          // visualize {
          tracer.visit(i, node, W[i]);
          Tracer.delay();
          // }
        }
      }
    }
  }
  return W[e];
}

let s = Randomize.Integer({ min: 0, max: G.length - 1 }); // s = start node
let e; // e = end node
do {
  e = Randomize.Integer({ min: 0, max: G.length - 1 });
} while (s === e);
let MAX_VALUE = 0x7fffffff;
// logger {
logger.println(`图的广度优先搜索查找从起点 ${s} 到终点 ${e} 的最短路径`);
// }
const minWeight = BFS(s);
// logger {
if (minWeight === MAX_VALUE) {
  logger.println(`无法从 起点 ${s} 到终点 ${e} `);
} else {
  logger.println(`从起点 ${s} 到终点 ${e} 的最短路径的最短路径长度为 ${minWeight}`);
}
// }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/267376
推荐阅读
相关标签
  

闽ICP备14008679号