Home > Java > Java Tutorial > body text

How to implement depth first search algorithm using java

WBOY
Release: 2023-09-19 16:51:35
Original
1096 people have browsed it

How to implement depth first search algorithm using java

How to use java to implement depth-first search algorithm

Depth-first search (DFS) is a classic search algorithm in graph theory. It is usually used to solve graphs or Tree traversal problem. This article will introduce how to write a depth-first search algorithm using Java and provide specific code examples.

  1. Algorithm Principle

Depth First Search (DFS) starts from a node and goes down along a path until it can go no further, and then rolls back to Previous node, try another path. This search method is similar to the exploration in a maze. We first select a node as the starting point, mark it as visited, and then recursively visit its adjacent nodes until the end point is reached or all nodes have been visited.

  1. Implementation steps

Step 1: Create a graph class to represent the structure of the graph. We can represent graphs using adjacency lists or adjacency matrices.

class Graph {
    private int V; // 图的顶点数
    private LinkedList adj[]; // 邻接表

    // 构造方法
    Graph(int v) {
        V = v;
        adj = new LinkedList[v];
        for(int i=0; i i = adj[v].listIterator();
        while(i.hasNext()) {
            int n = i.next();
            if(!visited[n]) {
                DFS(n, visited);
            }
        }
    }

    // 对图进行深度优先搜索
    void depthFirstSearch(int v) {
        boolean visited[] = new boolean[V]; // 用于记录节点是否已经访问过

        DFS(v, visited);
    }
}
Copy after login

Step 2: Create a test class to verify the correctness of the depth-first search algorithm.

class DFSAlgorithm {
    public static void main(String args[]) {
        Graph graph = new Graph(5); // 创建一个包含5个顶点的图

        graph.addEdge(0, 1);
        graph.addEdge(0, 2);
        graph.addEdge(1, 3);
        graph.addEdge(2, 4);

        System.out.println("深度优先搜索结果:");
        graph.depthFirstSearch(0);
    }
}
Copy after login
  1. Running results

Depth first search results:
0 1 3 2 4

  1. Summary

Depth-first search algorithm is a commonly used graph algorithm that can traverse all nodes in the graph. Through recursion, we can simply implement the depth-first search algorithm. The above code provides an example of a basic depth-first search algorithm, which you can modify and extend according to actual needs.

The above is the detailed content of How to implement depth first search algorithm using java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!