-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRAPH
160 lines (126 loc) · 3.05 KB
/
GRAPH
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX 100
enum {WHITE, GREY , BLACK};
typedef struct vertex{
int id;
int color;
int dist;
int disc_time;
int endP_time_;
int pred;
int scc;
int *rowAdj;
}vertex;
typedef struct graph{
vertex *g;
int nv;
}graph;
// Function Prototypes
graph *graphLoad (char *filename);
int graphFind (graph *g, int id);
void graphAttributeInit (graph *g);
void graphDispose (graph *g);
// Main Function
int main(){
graph *g = NULL;
g = graphLoad("../input.txt");
// Example: Print the adjacency matrix of the loaded graph
printf("Adjacency Matrix:\n");
for (int i = 0; i < g->nv; i++) {
for (int j = 0; j < g->nv; j++) {
printf("%d ", g->g[i].rowAdj[j]);
}
printf("\n");
}
// Example: Find and print the index of a vertex with ID 2
int index_2 = graphFind(g, 2);
if (index_2 != -1) {
printf("Index of vertex with ID 2: %d\n", index_2);
} else {
printf("Vertex with ID 2 not found.\n");
}
graphDispose(g);
graphDispose(g);
return EXIT_SUCCESS;
}
// Function Declarations
graph *graphLoad (char *filename) {
graph *g;
char line[MAX];
int i, j, weight, dir;
FILE *fp;
g = (struct graph *) malloc( sizeof(graph));
if(g == NULL){
fprintf(stderr,"memory allocation failed\n");
exit(EXIT_FAILURE);
}
fp = fopen(filename, "r");
if(fp == NULL){
fprintf(stderr,"memory allocation failed\n");
exit(EXIT_FAILURE);
}
fgets (line, MAX, fp);
if (sscanf(line, "%d %d", &g->nv, &dir) != 2) {
sscanf(line, "%d", &g->nv);
dir = 1;
}
g->g = (struct vertex *) malloc(g->nv * sizeof(vertex));
if(g->g == NULL){
fprintf(stderr,"memory allocation failed\n");
exit(EXIT_FAILURE);
}
for (i=0; i<g->nv; i++) {
g->g[i].id = i;
g->g[i].color = WHITE;
g->g[i].dist = INT_MAX;
g->g[i].pred = -1;
g->g[i].scc = -1;
g->g[i].disc_time = -1;
g->g[i].rowAdj = (int *) malloc (g->nv * sizeof(int));
}
while (fgets(line, MAX, fp) != NULL) {
if (sscanf(line, "%d%d%d", &i, &j, &weight) != 3) {
sscanf(line, "%d%d", &i, &j);
weight = 1;
}
g->g[i].rowAdj[j] = weight;
if (dir == 0) {
g->g[j].rowAdj[i] = weight;
}
}
fclose(fp);
return g;
}
void graphAttributeInit (graph *g) {
int i;
for (i=0; i<g->nv; i++) {
g->g[i].color = WHITE;
g->g[i].dist = INT_MAX;
g->g[i].disc_time = -1;
g->g[i].endP_time_ = -1;
g->g[i].pred = -1;
g->g[i].scc = -1;
}
return;
}
int graphFind (graph *g, int id){
int i;
for (i=0; i<g->nv; i++) {
if (g->g[i].id == id) {
return i;
}
}
return -1;
}
void graphDispose (graph *g) {
int i;
for (i=0; i< g->nv; i++) {
free(g->g[i].rowAdj);
}
free(g->g);
free(g);
return;
}