运行 ❯
获取您
自己
的网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class Graph: def __init__(self, size): self.adj_matrix = [[None] * size for _ in range(size)] self.size = size self.vertex_data = [''] * size def add_edge(self, u, v, weight): if 0 <= u < self.size and 0 <= v < self.size: self.adj_matrix[u][v] = weight # self.adj_matrix[v][u] = weight 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(lambda x: str(x) if x is not None else '0', row))) print("\nVertex Data:") for vertex, data in enumerate(self.vertex_data): print(f"Vertex {vertex}: {data}") g = Graph(4) 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_edge(0, 1, 3) # A -> B with weight 3 g.add_edge(0, 2, 2) # A -> C with weight 2 g.add_edge(3, 0, 4) # D -> A with weight 4 g.add_edge(2, 1, 1) # C -> B with weight 1 g.print_graph() #Python
#include <stdio.h> #include <stdlib.h> #define SIZE 4 #define NO_EDGE -1 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] = NO_EDGE; } g->vertexData[i] = 0; } } void addEdge(Graph *g, int u, int v, int weight) { if (u >= 0 && u < SIZE && v >= 0 && v < SIZE) { g->adjMatrix[u][v] = weight; } } 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++) { if (g->adjMatrix[i][j] == NO_EDGE) { printf("0 "); } else { 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]); } } int main() { Graph g; initGraph(&g); addVertexData(&g, 0, 'A'); addVertexData(&g, 1, 'B'); addVertexData(&g, 2, 'C'); addVertexData(&g, 3, 'D'); addEdge(&g, 0, 1, 3); // A -> B with weight 3 addEdge(&g, 0, 2, 2); // A -> C with weight 2 addEdge(&g, 3, 0, 4); // D -> A with weight 4 addEdge(&g, 2, 1, 1); // C -> B with weight 1 printGraph(&g); return 0; } //C
class Graph { private Integer[][] adjMatrix; private char[] vertexData; private int size; public Graph(int size) { this.size = size; this.adjMatrix = new Integer[size][size]; this.vertexData = new char[size]; } public void addEdge(int u, int v, int weight) { if (u >= 0 && u < size && v >= 0 && v < size) { adjMatrix[u][v] = weight; } } public void addVertexData(int vertex, char data) { if (vertex >= 0 && vertex < size) { vertexData[vertex] = data; } } public void printGraph() { System.out.println("Adjacency Matrix:"); for (Integer[] row : adjMatrix) { for (Integer cell : row) { System.out.print((cell != null ? cell : 0) + " "); } System.out.println(); } System.out.println("\nVertex Data:"); for (int i = 0; i < size; i++) { System.out.println("Vertex " + i + ": " + vertexData[i]); } } } public class Main { public static void main(String[] args) { Graph g = new Graph(4); g.addVertexData(0, 'A'); g.addVertexData(1, 'B'); g.addVertexData(2, 'C'); g.addVertexData(3, 'D'); g.addEdge(0, 1, 3); // A -> B with weight 3 g.addEdge(0, 2, 2); // A -> C with weight 2 g.addEdge(3, 0, 4); // D -> A with weight 4 g.addEdge(2, 1, 1); // C -> B with weight 1 g.printGraph(); } } //Java
Python 结果
C 结果
Java 结果
邻接矩阵
0 3 2 0
0 0 0 0
0 1 0 0
4 0 0 0
顶点数据
顶点 0:A
顶点 1:B
顶点 2:C
顶点 3:D
邻接矩阵
0 3 2 0
0 0 0 0
0 1 0 0
4 0 0 0
顶点数据
顶点 0:A
顶点 1:B
顶点 2:C
顶点 3:D
邻接矩阵
0 3 2 0
0 0 0 0
0 1 0 0
4 0 0 0
顶点数据
顶点 0:A
顶点 1:B
顶点 2:C
顶点 3:D