深度优先搜索(DFS)是一种常用的图遍历算法,用于遍历或搜索图或树的数据结构。它从起始节点开始,沿着图的边走到不能再继续前进的节点,然后回溯到上一个节点,继续探索未搜索的路径,直到遍历完整个图。下面是深度优先搜索的Java示例代码:

public class DFS {
    public void dfs(int[][] graph, int start, boolean[] visited) {
        visited[start] = true;
        System.out.println("Visiting node: " + start);
        for (int i = 0; i <; graph.length; i++) {
            if (graph[start][i] == 1 &;&; !visited[i]) {
                dfs(graph, i, visited);
            }
        }
    }
    public static void main(String[] args) {
        int[][] graph = {{0, 1, 1, 0, 0},
                        {1, 0, 0, 1, 1},
                        {1, 0, 0, 0, 1},
                        {0, 1, 0, 0, 0},
                        {0, 1, 1, 0, 0}};
        boolean[] visited = new boolean[graph.length];
        DFS dfs = new DFS();
        System.out.println("DFS Traversal:");
        dfs.dfs(graph, 0, visited);
    }
}