본문 바로가기
Computer Science

Graph

by OKOK 2019. 1. 14.

1. 인접리스트

2. 포인터 연결

3. 말록

4. 노드 만들어서 연결

5. 포인트 함수 관계 

6. 자료구조 그리기 가능


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <malloc.h>
 
typedef struct adjlistNode
{
    int vertex;
    adjlistNode *next;
}AdjListNode;
 
typedef struct
{
    int num_members;
    AdjListNode *head;
    AdjListNode *tail;
}AdjList;
 
typedef struct
{
    int num_vertices;
    AdjList * adjListArr;
}Graph;
 
AdjListNode * createNode(int v) {
    AdjListNode * newNode = (AdjListNode *)malloc(sizeof(AdjListNode));
    newNode->vertex = v;
    newNode->next = NULL;
    return newNode;
}
 
Graph * createGraph(int n) {
    Graph * graph = (Graph *)malloc(sizeof(Graph));
    graph->num_vertices = n;
    graph->adjListArr = (AdjList *)malloc(n * sizeof(AdjList));
 
    for (int i = 0; i < n; i++) {
        graph->adjListArr[i].head = graph->adjListArr[i].tail = NULL;
        graph->adjListArr[i].num_members = 0;
    }
    return graph;
}
 
void destroyGraph(Graph * graph) {
    if (graph) {
        if (graph->adjListArr) {
            for (int v = 0; v < graph->num_vertices; v++) {
                AdjListNode * adjListPtr = graph->adjListArr[v].head;
                while (adjListPtr) {
                    AdjListNode * tmp = adjListPtr;
                    adjListPtr = adjListPtr->next;
                    free(tmp);
                }
            }
            free(graph->adjListArr);
        }
        free(graph);
    }
}
 
void addEdge(Graph * graph, int src, int dest) {
    AdjListNode * newNode = createNode(dest);
    if (graph->adjListArr[src].tail != NULL) {
        graph->adjListArr[src].tail->next = newNode;
        graph->adjListArr[src].tail = newNode;
    }
    else {
        graph->adjListArr[src].head = graph->adjListArr[src].tail = newNode;
    }
    graph->adjListArr[src].num_members++;
 
    newNode = createNode(src);
    if (graph->adjListArr[dest].tail != NULL) {
        graph->adjListArr[dest].tail->next = newNode;
        graph->adjListArr[dest].tail = newNode;
    }
    else
    {
        graph->adjListArr[dest].head = graph->adjListArr[dest].tail = newNode;
    }
    graph->adjListArr[dest].num_members;
}
 
void displayGraph(Graph * graph, int i) {
    AdjListNode * adjListPtr = graph->adjListArr[i].head;
    while (adjListPtr) {
        printf("%d ", adjListPtr->vertex);
        adjListPtr = adjListPtr->next;
    }
    printf("\n");
}int main() {
    freopen("input.txt""r", stdin);
    int T, V, E, Q, sv, ev;
    scanf("%d"&T);
    for (int test_case = 1; test_case <= T; test_case++) {
        scanf("%d %d %d"&V, &E, &Q);
        Graph * graph = createGraph(V);
 
        for (int i = 0; i < E; i++) {
            scanf("%d %d"&sv, &ev);
            addEdge(graph, sv, ev);
        }
        printf("#%d\n", test_case);
 
        for (int i = 0; i < Q; i++) {
            scanf("%d"&sv);
            displayGraph(graph, sv);
        }
    }
    return 0;
}
cs

 


'Computer Science' 카테고리의 다른 글

Deque  (0) 2019.01.14
Linked List  (0) 2019.01.14
Tree  (0) 2019.01.14
프양연 프리랜서  (0) 2019.01.11
프양연 두 번 이상 등장하는 문자열  (0) 2019.01.08

댓글