repl.it
@pankajyd/

Largest Strongly Connected Component

C

This code finds the length of Largest Strongly Connected Component in given graph

fork
loading
Files
  • main.c
main.c
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int size = 0;
int *p;

typedef struct vertex{
    int v;
    int color;
    struct list *neighbour;
}vertex;

typedef struct list{
    int v;
    struct list* next;
}list;

void free_adjList(vertex* V, int nv){
    list* tmp;
    for(int i = 0; i < nv; i++){
        list* head = V[i].neighbour;
        while(head != NULL){
            tmp = head;
            head = head->next;
            free(tmp);
        }
    }
}

void DFS_Visit(vertex* V, int i){
    V[i].color = 1;
    //printf("%d->", i);
    list* neighbour = V[i].neighbour;
    
    while(neighbour != NULL){
        if(V[neighbour->v].color == 0){
            DFS_Visit(V, neighbour->v);
        }
        neighbour = neighbour->next;
    }
    
    V[i].color = 2;
    *p = i;
    p++;
}

void DFS_Visit2(vertex* V, int i){
    V[i].color = 1;
    //printf("%d->", i);
    list* neighbour = V[i].neighbour;
    
    while(neighbour != NULL){
        if(V[neighbour->v].color == 0){
            DFS_Visit2(V, neighbour->v);
        }
        else if(V[neighbour->v].color == 2){
            neighbour = neighbour->next;
            continue;
        }
        neighbour = neighbour->next;
    }
    V[i].color = 2;
    size++;
}

void DFS(vertex* V, int nv){
    //Vertices are white from main function
    for(int i = 0; i < nv; i++){
        if(V[i].color == 0){
            DFS_Visit(V, i);
            //printf("\n");
        }
    }
}

int main(){
    
    int t;
    scanf("%d", &t);
    
    vertex *V, *V_inv;
    int v1, v2;
    int nv, ne; //Number of vertices and edges
    list *tmp;
    int max_size = 0;

    while(t--){
        scanf("%d", &nv);
        scanf("%d", &ne);

        //vertex G[nv];
        //vertex G_inv[nv];
        //V = G;
        //V_inv = G_inv;
        V = (vertex*)malloc(nv*sizeof(vertex));
        V_inv = (vertex*)malloc(nv*sizeof(vertex));
        
        p = (int*) malloc(nv*sizeof(int));

        for(int i = 0; i < nv; i++){
            V[i].v = i;
            V[i].color = 0;
            V[i].neighbour = NULL;
            
            V_inv[i].v = i;
            V_inv[i].color = 0;
            V_inv[i].neighbour = NULL;
        }

        while(ne--){
            scanf("%d %d", &v1, &v2);
            
            //Making Edge v1 to v2.
            list* ptr = (list*)malloc(sizeof(list));
            ptr->v = v2;

            if(V[v1].neighbour == NULL){
                V[v1].neighbour = ptr;
            }
            else{
                tmp = V[v1].neighbour;
                V[v1].neighbour = ptr;
                ptr->next = tmp;
            }
            
            //Making Edge v2 to v1.
            list* ptr_inv = (list*)malloc(sizeof(list));
            ptr_inv->v = v1;

            if(V_inv[v2].neighbour == NULL){
                V_inv[v2].neighbour = ptr_inv;
            }
            else{
                tmp = V_inv[v2].neighbour;
                V_inv[v2].neighbour = ptr_inv;
                ptr_inv->next = tmp;
            }
        }

        //Remove multiline comment below to print the graph.
        /*
        printf("\nPrinting G:\n");
        for(int i = 0; i < nv; i++){
            list* head = V[i].neighbour;
            printf("%d: ", i);
            while(head != NULL){
                printf("%d ", head->v);
                head = head->next;
            }
            printf("\n");
        }
        
        printf("\nPrinting G_inv:\n");
        for(int i = 0; i < nv; i++){
            list* head = V_inv[i].neighbour;
            printf("%d: ", i);
            while(head != NULL){
                printf("%d ", head->v);
                head = head->next;
            }
            printf("\n");
        }
        */
        DFS(V_inv, nv);
        p--;
        for(int i = 0; i < nv; i++){
            if(V[*p].color != 2)
                DFS_Visit2(V, *p);
            if(size > max_size)
                max_size = size;
            size = 0;
            p--;
        }
        p++;
        printf("%d", max_size);
        
        free_adjList(V, nv);
        free_adjList(V_inv, nv);
        free(V);
        free(V_inv);
        free(p);
    }
    return 0;
}