邻接表:
import java.util.*;
class Graph {
private int numVertices;
private LinkedList<Integer>[] adjLists;
// 构造函数
Graph(int numVertices) {
this.numVertices = numVertices;
adjLists = new LinkedList[numVertices];
for (int i = 0; i < numVertices; i++) {
adjLists[i] = new LinkedList<>();
}
}
// 添加边
void addEdge(int src, int dest) {
adjLists[src].add(dest);
adjLists[dest].add(src); // 如果是无向图
}
// 深度优先遍历 (DFS)
void DFS() {
boolean[] visited = new boolean[numVertices];
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
DFSUtil(i, visited);
}
}
}
// 递归帮助函数用于 DFS
private void DFSUtil(int vertex, boolean[] visited) {
visited[vertex] = true;
System.out.print(vertex + " ");
for (int adj : adjLists[vertex]) {
if (!visited[adj]) {
DFSUtil(adj, visited);
}
}
}
// 广度优先遍历 (BFS)
void BFS() {
boolean[] visited = new boolean[numVertices];
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
BFSUtil(i, visited);
}
}
}
// 帮助函数用于 BFS
private void BFSUtil(int startVertex, boolean[] visited) {
LinkedList<Integer> queue = new LinkedList<>();
visited[startVertex] = true;
queue.add(startVertex);
while (!queue.isEmpty()) {
int vertex = queue.poll();
System.out.print(vertex + " ");
for (int adj : adjLists[vertex]) {
if (!visited[adj]) {
visited[adj] = true;
queue.add(adj);
}
}
}
}
}
public class Main {
public static void main(String[] args) {
Graph graph = new Graph(6);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 4);
graph.addEdge(3, 4);
graph.addEdge(3, 5);
graph.addEdge(4, 5);
System.out.println("DFS:");
graph.DFS();
System.out.println("\nBFS:");
graph.BFS();
}
}
#
二维数组:
import java.util.*;
class Graph {
private int numVertices;
private int[][] adjMatrix;
// 构造函数
Graph(int numVertices) {
this.numVertices = numVertices;
adjMatrix = new int[numVertices][numVertices];
}
// 添加边
void addEdge(int src, int dest) {
adjMatrix[src][dest] = 1;
adjMatrix[dest][src] = 1; // 如果是无向图
}
// 深度优先遍历 (DFS)
void DFS() {
boolean[] visited = new boolean[numVertices];
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
DFSUtil(i, visited);
}
}
}
// 递归帮助函数用于 DFS
private void DFSUtil(int vertex, boolean[] visited) {
visited[vertex] = true;
System.out.print(vertex + " ");
for (int i = 0; i < numVertices; i++) {
if (adjMatrix[vertex][i] == 1 && !visited[i]) {
DFSUtil(i, visited);
}
}
}
// 广度优先遍历 (BFS)
void BFS() {
boolean[] visited = new boolean[numVertices];
for (int i = 0; i < numVertices; i++) {
if (!visited[i]) {
BFSUtil(i, visited);
}
}
}
// 帮助函数用于 BFS
private void BFSUtil(int startVertex, boolean[] visited) {
LinkedList<Integer> queue = new LinkedList<>();
visited[startVertex] = true;
queue.add(startVertex);
while (!queue.isEmpty()) {
int vertex = queue.poll();
System.out.print(vertex + " ");
for (int i = 0; i < numVertices; i++) {
if (adjMatrix[vertex][i] == 1 && !visited[i]) {
visited[i] = true;
queue.add(i);
}
}
}
}
}
public class Main {
public static void main(String[] args) {
Graph graph = new Graph(6);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 4);
graph.addEdge(3, 4);
graph.addEdge(3, 5);
graph.addEdge(4, 5);
System.out.println("DFS:");
graph.DFS();
System.out.println("\nBFS:");
graph.BFS();
}
}