ABOUT ME

Today
Yesterday
Total
  • 방향그래프 구현(위상정렬)
    카테고리 없음 2019. 11. 27. 19:48
    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
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    #pragma warning(disable:4996)
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int n;
    int m;
    int *in;
    int cnt;
    char *check;
     
    typedef struct Node //노드 정의
    {
        char data;
        struct Node *next;
    }Node;
    typedef struct //Queue 구조체 정의
    {
        Node *front; //맨 앞(꺼낼 위치)
        Node *rear; //맨 뒤(보관할 위치)
        int count;//보관 개수
    }Queue;
    Queue queue;
    void InitQueue(Queue *queue)
    {
        queue->front = queue->rear = NULL; //front와 rear를 NULL로 설정
        queue->count = 0;//보관 개수를 0으로 설정
    }
    int IsEmpty(Queue *queue)
    {
        return queue->count == 0;    //보관 개수가 0이면 빈 상태
    }
    void Enqueue(Queue *queue, char data)
    {
        Node *now = (Node *)malloc(sizeof(Node)); //노드 생성
        now->data = data;//데이터 설정
        now->next = NULL;
     
        if (IsEmpty(queue))//큐가 비어있을 때
        {
            queue->front = now;//맨 앞을 now로 설정      
        }
        else//비어있지 않을 때
        {
            queue->rear->next = now;//맨 뒤의 다음을 now로 설정
        }
        queue->rear = now;//맨 뒤를 now로 설정  
        queue->count++;//보관 개수를 1 증가
    }
     
    int Dequeue(Queue *queue)
    {
        char re = 0;
        Node *now;
        if (IsEmpty(queue))//큐가 비었을 때
        {
            return 0;
        }
        now = queue->front;//맨 앞의 노드를 now에 기억
        re = now->data;//반환할 값은 now의 data로 설정
        queue->front = now->next;//맨 앞은 now의 다음 노드로 설정
        free(now);//now 소멸
        queue->count--;//보관 개수를 1 감소
        return re;
    }
     
    typedef struct
    {
        char vertex;
        int indegree;
        struct node *link;
    }node;
    node **g;
    void initializeGraph()
    {
        g = (node**)malloc(sizeof(node*)*(n + 1));
        for (int i = 0; i <= n; i++)
        {
            g[i] = (node*)malloc(sizeof(node));
            g[i]->link = NULL;
            in[i] = 0;
        }
    }
     
    void insertVertex(char vName,int i)
    {
        g[i]->vertex = vName;
        g[i]->indegree = 0;
    }
    void insertDirectedEdge(char uName, char wName)
    {
        node *newnode;
        node *cur;
        newnode = (node*)malloc(sizeof(node));
        newnode->vertex = wName;
        newnode->link = NULL;
        cur = NULL;
        for (int i = 0; i < n; i++)
        {
            if (g[i]->vertex == uName)
            {
                cur = g[i];
                break;
            }
        }
         
        newnode->link = cur->link;
        cur->link = newnode;
        for (int i = 0; i < n; i++)
        {
            if (g[i]->vertex == wName)
            {
                g[i]->indegree++;
            }
        }
    }
     
    void buildGraph()
    {
        initializeGraph();
         
        char vName;
        char uName;
        char wName;
         
        getchar();
        for (int i = 0; i < n; i++)
        {
            scanf("%c", &vName);
            insertVertex(vName, i);
            getchar();
        }
        scanf("%d", &m);
        getchar();
        for (int i = 0; i < m; i++)
        {
            scanf("%c %c", &uName, &wName);
            insertDirectedEdge(uName, wName);
            getchar();
        }
         
    }
    int index(char Name)
    {
        for (int i = 0; i < n; i++)
        {
            if (g[i]->vertex == Name)
            {
                return i;
            }
        }
    }
    int topologicalSort()
    {
        node *cur;
        char Name;
        int j = 0;
        int flag = 0;
        int flag2 = 0;
        cnt = 0;
        InitQueue(&queue);
        cur = (Node*)malloc(sizeof(Node));
        cur->link = NULL;
        for (int i = 0; i < n; i++)
        {
            in[i] = g[i]->vertex;
            if (g[i]->indegree == 0)
            {
                Enqueue(&queue,g[i]->vertex);
                check[cnt++] = g[i]->vertex;
            }
        }
        while (1)
        {
            Name = Dequeue(&queue);
            j = index(Name);
            cur = g[j]->link;
            if (n == cnt)
                break;
            while (cur != NULL)
            {
                for (int i = 0; i < n; i++)
                {
                    if (cur->vertex == g[i]->vertex)
                    {
                        g[i]->indegree--;
                    }
                }
                cur = cur->link;
            }
            for (int i = n; i >= 0; i--)
            {
                if (g[i]->indegree == 0)
                {
                    for (int k = cnt; k >= 0; k--) {
                        if (g[i]->vertex == check[k])
                        {
                            flag2 = 1;
                            break;
                        }
                    }
                    if (flag2 == 0)
                    {
                        Enqueue(&queue, g[i]->vertex);
                        check[cnt++] = g[i]->vertex;
                    }
                    else
                        flag2 = 0;
                }
            }
     
     
            if (IsEmpty(&queue) != 0 ) {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            return 0;
        else
            return 1;
    }
    void main(void)
    {
        int finish;
        char c;
        node *cur;
        InitQueue(&queue);//큐 초기화
        scanf("%d", &n);
        in = (int*)malloc(sizeof(int)*(n + 1));
        check = (char*)malloc(sizeof(char)*(n + 1));
        buildGraph();
         
        finish = topologicalSort();
     
     
        if (finish == 0)
            printf("0");
        else   
        {
            for (int i = 0; i < cnt; i++)
            {
                printf("%c ", check[i]);
            }
        }
        free(in);
        free(check);
    }
     
     
    </stdlib.h></string.h></stdio.h>

    댓글

Designed by Tistory.