: κ°μ²΄ μ¬μ΄μ μ°κ²°κ΄κ³λ₯Ό ννν μ μλ μλ£κ΅¬μ‘° (μ μ κ³Ό κ°μ λ€μ μ νμ§ν©)
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50
typedef struct GraphType {
int n;
int adj_mat[MAX_VERTICES];
}GraphType;
//κ·Έλν μ΄κΈ°ν
void init(GraphType* g) {
int r, c;
g->n = 0;
for (r = 0;r < MAX_VERTICES;r++) {
for (c = 0;c < MAX_VERTICES;c++) {
g->adj_mat[r][c] = 0;
}
}
}
//μ μ μ½μ
μ°μ°
void insert_vertex(GraphType* g, int v) {
if ((g->n) + 1 > MAX_VERTICES) {
fprintf(stderr, "κ·Έλν: μ μ μ κ°μ μ΄κ³Ό");
return;
}
g->n++;
}
//κ°μ μ½μ
μ°μ°
void insert_edge(GraphType* g, int start, int end) {
if (start >= g->n || end >= g->n) {
fprintf(stderr, "κ·Έλν: μ μ λ²νΈ μ€λ₯");
return;
}
g->adj_mat[start][end] = 1;
g->adj_mat[end][start] = 1;
}
//μΈμ νλ ¬ μΆλ ₯ ν¨μ
void print_adj_mat(GraphType* g) {
for (int i = 0;i < g->n;i++) {
for (int j = 0;j < g->n;j++) {
printf("%2d ", g->adj_mat[i][j]);
}
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50
typedef struct GraphNode {
int vertex;
struct GraphNode* link;
}GraphNode;
typedef struct GraphType {
int n;
GraphNode* adj_list[MAX_VERTICES];
}GraphType;
//κ·Έλν μ΄κΈ°ν
void init(GraphType* g) {
int v;
g->n = 0;
for (v = 0;v < MAX_VERTICES;v++) {
g->adj_list[v] = NULL;
}
}
//μ μ μ½μ
μ°μ°
void insert_vertex(GraphType* g, int v) {
if (((g->n) + 1) > MAX_VERTICES) {
fprintf(stderr, "κ·Έλν: μ μ μ κ°μ μ΄κ³Ό");
return;
}
g->n++;
}
//κ°μ μ½μ
μ°μ°
void insert_edge(GraphType* g, int u, int v) {
GraphNode* node;
if (u >= g->n || v >= g->n) {
fprintf(stderr, "κ·Έλν: μ μ λ²νΈ μ€λ₯");
return;
}
node = (GraphNode*)malloc(sizeof(GraphNode));
node->vertex = v;
node->link = g->adj_list[u];
g->adj_list[u] = node;
}
//κ°μ μΆλ ₯ ν¨μ
void print_adj_list(GraphType* g) {
for (int i = 0;i < g->n;i++) {
GraphNode* p = g->adj_list[i];
printf("μ μ %dμ μΈμ 리μ€νΈ ", i);
while (p != NULL) {
printf("-> %d ", p->vertex);
p = p->link;
}
printf("\n");
}
}
: κ±°λ¦¬κ° κ°κΉμ΄ μ μ μ μμλ‘ νμμ΄ μ§ν
1. μΈμ νλ ¬
2. μΈμ 리μ€νΈ