mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-10 16:00:36 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
52 lines
1.9 KiB
Java
52 lines
1.9 KiB
Java
/**
|
|
* File: graph_dfs.java
|
|
* Created Time: 2023-02-12
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_graph;
|
|
|
|
import java.util.*;
|
|
import utils.*;
|
|
|
|
public class graph_dfs {
|
|
/* Depth-first traversal helper function */
|
|
static void dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {
|
|
res.add(vet); // Record the visited vertex
|
|
visited.add(vet); // Mark this vertex as visited
|
|
// Traverse all neighbors of this vertex
|
|
for (Vertex adjVet : graph.adjList.get(vet)) {
|
|
if (visited.contains(adjVet))
|
|
continue; // Skip vertices that have already been visited
|
|
// Recursively visit the neighboring vertex
|
|
dfs(graph, visited, res, adjVet);
|
|
}
|
|
}
|
|
|
|
/* Depth-first traversal */
|
|
// Represent the graph with an adjacency list to access all neighbors of a given vertex
|
|
static List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
|
// Vertex traversal sequence
|
|
List<Vertex> res = new ArrayList<>();
|
|
// Hash set recording visited vertices
|
|
Set<Vertex> visited = new HashSet<>();
|
|
dfs(graph, visited, res, startVet);
|
|
return res;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
/* Initialize the undirected graph */
|
|
Vertex[] v = Vertex.valsToVets(new int[] { 0, 1, 2, 3, 4, 5, 6 });
|
|
Vertex[][] edges = { { v[0], v[1] }, { v[0], v[3] }, { v[1], v[2] },
|
|
{ v[2], v[5] }, { v[4], v[5] }, { v[5], v[6] } };
|
|
GraphAdjList graph = new GraphAdjList(edges);
|
|
System.out.println("\nGraph after initialization:");
|
|
graph.print();
|
|
|
|
/* Depth-first traversal */
|
|
List<Vertex> res = graphDFS(graph, v[0]);
|
|
System.out.println("\nVertex sequence from depth-first traversal (DFS):");
|
|
System.out.println(Vertex.vetsToVals(res));
|
|
}
|
|
}
|