Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add files via upload #769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions bfs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// import visualization libraries {
import org.algorithm_visualizer.*;
import java.util.LinkedList;
import java.util.Queue;
// }

class Main {
// 可视化组件
private GraphTracer graphTracer = new GraphTracer("BFS");
private LogTracer logTracer = new LogTracer("控制台");
private Array1DTracer visitedTracer = new Array1DTracer("访问状态");

// 图结构定义
private int[][] adjMatrix = {
{0,1,1,0,0},
{1,0,0,0,1},
{1,0,0,1,0},
{0,0,1,0,1},
{0,1,0,1,0}
};
private boolean[] visited = new boolean[5];

void bfs(int start) {
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
visited[start] = true;

// 初始节点可视化
graphTracer.visit(start);
visitedTracer.patch(start, 1);
Tracer.delay(1000);

while (!queue.isEmpty()) {
int current = queue.poll();

for (int neighbor=0; neighbor<adjMatrix.length; neighbor++) {
if (adjMatrix[current][neighbor] == 1 && !visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);

// 可视化访问过程
graphTracer.visit(neighbor);
graphTracer.select(current, neighbor);
visitedTracer.patch(neighbor, 1);
Tracer.delay(800);
}
}
}
}

Main() {
// 初始化可视化布局
Layout.setRoot(new VerticalLayout(new Commander[]{
graphTracer,
visitedTracer,
logTracer
}));

// 设置初始状态
graphTracer.set(adjMatrix);
visitedTracer.set(new int[5]);
logTracer.println("BFS 启动...");
Tracer.delay(1000);

// 执行BFS
bfs(0);
}

public static void main(String[] args) {
new Main();
}
}