运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class Graph: def __init__(self, size): self.adj_matrix = [[0] * size for _ in range(size)] self.size = size self.vertex_data = [''] * size def add_edge(self, u, v): if 0 <= u < self.size and 0 <= v < self.size: self.adj_matrix[u][v] = 1 self.adj_matrix[v][u] = 1 def add_vertex_data(self, vertex, data): if 0 <= vertex < self.size: self.vertex_data[vertex] = data def print_graph(self): print("Adjacency Matrix:") for row in self.adj_matrix: print(' '.join(map(str, row))) print("\nVertex Data:") for vertex, data in enumerate(self.vertex_data): print(f"Vertex {vertex}: {data}") def dfs_util(self, v, visited): visited[v] = True print(self.vertex_data[v], end=' ') for i in range(self.size): if self.adj_matrix[v][i] == 1 and not visited[i]: self.dfs_util(i, visited) def dfs(self, start_vertex_data): visited = [False] * self.size start_vertex = self.vertex_data.index(start_vertex_data) self.dfs_util(start_vertex, visited) g = Graph(7) g.add_vertex_data(0, 'A') g.add_vertex_data(1, 'B') g.add_vertex_data(2, 'C') g.add_vertex_data(3, 'D') g.add_vertex_data(4, 'E') g.add_vertex_data(5, 'F') g.add_vertex_data(6, 'G') g.add_edge(3, 0) # D - A g.add_edge(0, 2) # A - C g.add_edge(0, 3) # A - D g.add_edge(0, 4) # A - E g.add_edge(4, 2) # E - C g.add_edge(2, 5) # C - F g.add_edge(2, 1) # C - B g.add_edge(2, 6) # C - G g.add_edge(1, 5) # B - F g.print_graph() print("\nDepth First Search starting from vertex D:") g.dfs('D') #Python
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define SIZE 7 typedef struct { int adjMatrix[SIZE][SIZE]; char vertexData[SIZE]; } Graph; void initGraph(Graph *g) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { g->adjMatrix[i][j] = 0; } g->vertexData[i] = '\0'; } } void addEdge(Graph *g, int u, int v) { if (u >= 0 && u < SIZE && v >= 0 && v < SIZE) { g->adjMatrix[u][v] = 1; g->adjMatrix[v][u] = 1; } } void addVertexData(Graph *g, int vertex, char data) { if (vertex >= 0 && vertex < SIZE) { g->vertexData[vertex] = data; } } void printGraph(Graph *g) { printf("Adjacency Matrix:\n"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("%d ", g->adjMatrix[i][j]); } printf("\n"); } printf("\nVertex Data:\n"); for (int i = 0; i < SIZE; i++) { printf("Vertex %d: %c\n", i, g->vertexData[i]); } } void dfsUtil(Graph *g, int v, bool visited[]) { visited[v] = true; printf("%c ", g->vertexData[v]); for (int i = 0; i < SIZE; i++) { if (g->adjMatrix[v][i] == 1 && !visited[i]) { dfsUtil(g, i, visited); } } } void dfs(Graph *g, char startVertexData) { bool visited[SIZE] = {false}; int startVertex = -1; for (int i = 0; i < SIZE; i++) { if (g->vertexData[i] == startVertexData) { startVertex = i; break; } } if (startVertex != -1) { dfsUtil(g, startVertex, visited); } } int main() { Graph g; initGraph(&g); addVertexData(&g, 0, 'A'); addVertexData(&g, 1, 'B'); addVertexData(&g, 2, 'C'); addVertexData(&g, 3, 'D'); addVertexData(&g, 4, 'E'); addVertexData(&g, 5, 'F'); addVertexData(&g, 6, 'G'); addEdge(&g, 3, 0); // D - A addEdge(&g, 0, 2); // A - C addEdge(&g, 0, 3); // A - D addEdge(&g, 0, 4); // A - E addEdge(&g, 4, 2); // E - C addEdge(&g, 2, 5); // C - F addEdge(&g, 2, 1); // C - B addEdge(&g, 2, 6); // C - G addEdge(&g, 1, 5); // B - F printGraph(&g); printf("\nDepth First Search starting from vertex D:\n"); dfs(&g, 'D'); return 0; } //C
import java.util.Arrays; class Graph { private int[][] adjMatrix; private char[] vertexData; private int size; public Graph(int size) { this.size = size; this.adjMatrix = new int[size][size]; this.vertexData = new char[size]; } public void addEdge(int u, int v) { if (u >= 0 && u < size && v >= 0 && v < size) { adjMatrix[u][v] = 1; adjMatrix[v][u] = 1; } } public void addVertexData(int vertex, char data) { if (vertex >= 0 && vertex < size) { vertexData[vertex] = data; } } public void printGraph() { System.out.println("Adjacency Matrix:"); for (int[] row : adjMatrix) { for (int cell : row) { System.out.print(cell + " "); } System.out.println(); } System.out.println("\nVertex Data:"); for (int i = 0; i < size; i++) { System.out.println("Vertex " + i + ": " + vertexData[i]); } } private void dfsUtil(int v, boolean[] visited) { visited[v] = true; System.out.print(vertexData[v] + " "); for (int i = 0; i < size; i++) { if (adjMatrix[v][i] == 1 && !visited[i]) { dfsUtil(i, visited); } } } public void dfs(char startVertexData) { boolean[] visited = new boolean[size]; int startVertex = new String(vertexData).indexOf(startVertexData); dfsUtil(startVertex, visited); } } public class Main { public static void main(String[] args) { Graph g = new Graph(7); g.addVertexData(0, 'A'); g.addVertexData(1, 'B'); g.addVertexData(2, 'C'); g.addVertexData(3, 'D'); g.addVertexData(4, 'E'); g.addVertexData(5, 'F'); g.addVertexData(6, 'G'); g.addEdge(3, 0); // D - A g.addEdge(0, 2); // A - C g.addEdge(0, 3); // A - D g.addEdge(0, 4); // A - E g.addEdge(4, 2); // E - C g.addEdge(2, 5); // C - F g.addEdge(2, 1); // C - B g.addEdge(2, 6); // C - G g.addEdge(1, 5); // B - F g.printGraph(); System.out.println("\nDepth First Search starting from vertex D:"); g.dfs('D'); } } //Java
Python 结果
C 结果
Java 结果
邻接矩阵
0 0 1 1 1 0 0
0 0 1 0 0 1 0
1 1 0 0 1 1 1
1 0 0 0 0 0 0
1 0 1 0 0 0 0
0 1 1 0 0 0 0
0 0 1 0 0 0 0
顶点数据
顶点 0: A
顶点 1: B
顶点 2: C
顶点 3: D
顶点 4: E
顶点 5: F
顶点 6: G
从顶点 D 开始的深度优先搜索
D A C B F E G
邻接矩阵
0 0 1 1 1 0 0
0 0 1 0 0 1 0
1 1 0 0 1 1 1
1 0 0 0 0 0 0
1 0 1 0 0 0 0
0 1 1 0 0 0 0
0 0 1 0 0 0 0
顶点数据
顶点 0: A
顶点 1: B
顶点 2: C
顶点 3: D
顶点 4: E
顶点 5: F
顶点 6: G
从顶点 D 开始的深度优先搜索
D A C B F E G
邻接矩阵
0 0 1 1 1 0 0
0 0 1 0 0 1 0
1 1 0 0 1 1 1
1 0 0 0 0 0 0
1 0 1 0 0 0 0
0 1 1 0 0 0 0
0 0 1 0 0 0 0
顶点数据
顶点 0: A
顶点 1: B
顶点 2: C
顶点 3: D
顶点 4: E
顶点 5: F
顶点 6: G
从顶点 D 开始的深度优先搜索
D A C B F E G