运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
vertexData = ['A', 'B', 'C', 'D'] adjacency_matrix = [ [0, 1, 1, 1], # Edges for A [1, 0, 1, 0], # Edges for B [1, 1, 0, 0], # Edges for C [1, 0, 0, 0] # Edges for D ] def print_adjacency_matrix(matrix): print("Adjacency Matrix:") for row in matrix: print(row) def print_connections(matrix, vertices): print("\nConnections for each vertex:") for i in range(len(vertices)): print(f"{vertices[i]}: ", end="") for j in range(len(vertices)): if matrix[i][j]: # if there is a connection print(vertices[j], end=" ") print() # new line print_adjacency_matrix(adjacency_matrix) print_connections(adjacency_matrix, vertexData) #Python
#include <stdio.h> void printAdjacencyMatrix(int matrix[4][4], int size) { printf("Adjacency Matrix:\n"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } void printConnections(int matrix[4][4], char vertices[4], int size) { printf("\nConnections for each vertex:\n"); for (int i = 0; i < size; i++) { printf("%c: ", vertices[i]); for (int j = 0; j < size; j++) { if (matrix[i][j]) { // if there is a connection printf("%c ", vertices[j]); } } printf("\n"); } } int main() { char vertexData[4] = {'A', 'B', 'C', 'D'}; int adjacencyMatrix[4][4] = { {0, 1, 1, 1}, // Edges for A {1, 0, 1, 0}, // Edges for B {1, 1, 0, 0}, // Edges for C {1, 0, 0, 0} // Edges for D }; printAdjacencyMatrix(adjacencyMatrix, 4); printConnections(adjacencyMatrix, vertexData, 4); return 0; } //C
public class Main { public static void main(String[] args) { String[] vertexData = {"A", "B", "C", "D"}; int[][] adjacencyMatrix = { {0, 1, 1, 1}, // Edges for A {1, 0, 1, 0}, // Edges for B {1, 1, 0, 0}, // Edges for C {1, 0, 0, 0} // Edges for D }; printAdjacencyMatrix(adjacencyMatrix); printConnections(adjacencyMatrix, vertexData); } public static void printAdjacencyMatrix(int[][] matrix) { System.out.println("Adjacency Matrix:"); for (int[] row : matrix) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } public static void printConnections(int[][] matrix, String[] vertices) { System.out.println("\nConnections for each vertex:"); for (int i = 0; i < vertices.length; i++) { System.out.print(vertices[i] + ": "); for (int j = 0; j < vertices.length; j++) { if (matrix[i][j] == 1) { // if there is a connection System.out.print(vertices[j] + " "); } } System.out.println(); } } } //Java
Python 结果
C 结果
Java 结果
邻接矩阵
[0, 1, 1, 1]
[1, 0, 1, 0]
[1, 1, 0, 0]
[1, 0, 0, 0]
每个顶点的连接
A: B C D
B: A C
C: A B
D: A
邻接矩阵
0 1 1 1
1 0 1 0
1 1 0 0
1 0 0 0
每个顶点的连接
A: B C D
B: A C
C: A B
D: A
邻接矩阵
0 1 1 1
1 0 1 0
1 1 0 0
1 0 0 0
每个顶点的连接
A: B C D
B: A C
C: A B
D: A