#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX 30
typedef struct graphType {
int n;
int adjMatrix_Arr[MAX_VERTEX][MAX_VERTEX];
}graphType;
void CreateGraph(graphType* ptr) {
ptr->n = 0;
for (int i = 0; i < MAX_VERTEX; i++) {
for (int j = 0; j < MAX_VERTEX; j++) {
ptr->adjMatrix_Arr[i][j] = 0;
}
}
}
void InsertVertex(graphType* ptr, int n) {
if (((ptr->n) + 1) > MAX_VERTEX) printf("\n 그래프 정점의 개수를 초과했습니다!");
ptr->n++;
}
void InsertEdge(graphType* ptr, int u, int v) {
if (u >= ptr->n || v >= ptr->n) printf("\n 그래프에 없는 정점입니다!");
ptr->adjMatrix_Arr[u][v] = 1;
}
void PrintGraph(graphType* ptr) {
char letter = 65;
for (int i = 0; i < (ptr->n); i++) {
printf("\n\t\t");
printf("%c", letter + i);
for (int j = 0; j < (ptr->n); j++) {
printf(" %3d", ptr->adjMatrix_Arr[i][j]);
}
}
}
int main() {
char letter = 65;
graphType* G1, * G2, * G3, * G4;
G1 = (graphType*)malloc(sizeof(graphType));
G2 = (graphType*)malloc(sizeof(graphType));
G3 = (graphType*)malloc(sizeof(graphType));
G4 = (graphType*)malloc(sizeof(graphType));
CreateGraph(G1);
CreateGraph(G2);
CreateGraph(G3);
CreateGraph(G4);
for (int i = 0; i < 4; i++) {
InsertVertex(G1, i);
}
InsertEdge(G1, 0, 1); InsertEdge(G1, 0, 3);
InsertEdge(G1, 1, 0); InsertEdge(G1, 1, 2); InsertEdge(G1, 1, 3);
InsertEdge(G1, 2, 1); InsertEdge(G1, 2, 3);
InsertEdge(G1, 3, 0); InsertEdge(G1, 3, 1); InsertEdge(G1, 3, 2);
printf("\nG1의 인접 행렬\t A B C D");
PrintGraph(G1); printf("\n");
for (int i = 0; i < 3; i++) {
InsertVertex(G2, i);
}
InsertEdge(G2, 0, 1); InsertEdge(G2, 0, 2);
InsertEdge(G2, 1, 0); InsertEdge(G2, 1, 2);
InsertEdge(G2, 2, 0); InsertEdge(G2, 2, 1);
printf("\nG2의 인접 행렬\t A B C");
PrintGraph(G2); printf("\n");
for (int i = 0; i < 4; i++) {
InsertVertex(G3, i);
}
InsertEdge(G3, 0, 1); InsertEdge(G3, 0, 3);
InsertEdge(G3, 1, 2); InsertEdge(G3, 1, 3);
InsertEdge(G3, 2, 3);
printf("\nG3의 인접 행렬\t A B C D");
PrintGraph(G3); printf("\n");
for (int i = 0; i < 3; i++) {
InsertVertex(G4, i);
}
InsertEdge(G4, 0, 1); InsertEdge(G4, 0, 2);
InsertEdge(G4, 1, 0); InsertEdge(G4, 1, 2);
printf("\nG4의 인접 행렬\t A B C");
PrintGraph(G4); printf("\n");
}