运行平台
图的深度优先遍历
javascript
// 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('图不是连通的');
}
// }
图的广度优先遍历
javascript
// 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}`);
}
// }