-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_digraphs.c
More file actions
184 lines (148 loc) · 5.43 KB
/
Copy pathtutorial_digraphs.c
File metadata and controls
184 lines (148 loc) · 5.43 KB
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
#include "granet.h"
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
/* MACRO used to change the color of the standard output */
#define TXT_RED "\033[0;31m"
#define TXT_NO_COLOR "\033[0m"
#define TXT_GREEN "\033[0;32m"
#define TXT_BLUE "\033[0;34m"
#define TXT_YELLOW "\033[0;33m"
#define TXT_CYAN "\033[0;36m"
#define TXT_PURPLE "\033[0;35m"
#define USAGE "\nUsage: tutorial_digraphs --graph <FILE_NAME> --format <TYPE> --graph-type <GRAPH_TYPE> --output-dir <DIR_NAME>\n\n"\
"\t-f, --graph <FILE_NAME> Read the graph from file <FILE_NAME>.\n"\
"\t-o, --output-dir <DIR_NAME> Write output to dir <DIR_NAME>.\n"\
"\t-t, --format <TYPE> The format of the file containing the graph, admitted types are:\n"\
"\t 0 - (Edges List) The first line of the file must contain the \n"\
"\t number of nodes and edges of the graph in the following format: \n"\
"\t \"# Nodes: <NUMBER> Edges: <NUMBER>\" \n"\
"\t-g, --graph-type <GRAPH_TYPE> Admitted types are:\n"\
"\t D - Directed graph\n"\
"\t U - Undirected graph\n\n"
int main(int argc, char **argv){
int file_type = -1; /* Format of the file containing the graph */
char ch; /* Option read by getopt_long */
char *file_name = NULL; /* Name of the file containing the graph */
char graph_type = 0;
char *output_dir = NULL;
static struct option long_options[] ={
{"graph", required_argument, NULL, 'f'},
{"format", required_argument, NULL, 't'},
{"graph-type", required_argument, NULL, 'g'},
{"output-dir", required_argument, NULL, 'o'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
/* getopt_long() returns the option character when a short option is recognized.
* For a long option, it returns <val> if <flag> is NULL, and 0 otherwise. */
while ((ch = getopt_long(argc, argv, "f:t:g:o:h", long_options, NULL)) != -1){
switch (ch){
case 'f':
file_name = strdup(optarg);
break;
case 'o':
output_dir = strdup(optarg);
if ( mkdir(output_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) != 0 ){
if ( errno != EEXIST){
exit(EXIT_FAILURE);
}
}
break;
case 't':
file_type = atoi(optarg); /* Check that the file format is one of the allowed types */
if ( file_type != EDGE_LIST ){
printf(USAGE);
exit(EXIT_FAILURE);
}
break;
case 'g':
graph_type = optarg[0];
if ( graph_type != 'D' && graph_type != 'U'){
printf(USAGE);
exit(EXIT_FAILURE);
}
break;
case 'h':
default:
printf(USAGE);
exit(EXIT_FAILURE);
break;
}
}
/* Check that all needed information have been provided through the command line */
if ( file_type == -1 || file_name == NULL || graph_type == 0 || output_dir == NULL) {
printf(USAGE);
exit(EXIT_FAILURE);
}
if (graph_type == 'U'){
printf("This program does not support Undirected Graphs\n");
exit(EXIT_FAILURE);
}else if(graph_type != 'D') {
printf(USAGE);
exit(EXIT_FAILURE);
}
digraph_type *dg, *ndg;
/* Read the graph */
fprintf(stderr, "(%s) Read Graph from file: %s\n", get_time_string(), file_name);
dg = get_digraph_from_file( file_name, file_type);
if (dg == NULL){
exit(EXIT_FAILURE);
}
print_digraph(dg, 0);
printf("\n");
/* Write digraph */
char out_file[256] = "digraph.out.edgelist";
const char *path;
path = get_path(output_dir,out_file);
write_digraph_edgelist(dg, path);
fprintf(stderr, "(%s) Write Graph to file: %s\n", get_time_string(), path);
/* Remove Nodes */
vertex_type vts[2] = {1,2};
ndg = delete_nodes_from_digraph ( dg, vts, 2);
print_digraph(ndg, 0);
print_digraph(ndg, 1);
free_digraph(ndg);
printf("\n");
double density, avg_deg, n_nodes, n_edges;
n_nodes = dg->nodes;
n_edges = dg->edges;
/* Number of Nodes and Edges */
fprintf(stderr, "(%s) Nodes: %.0lf Edges: %.0lf\n", get_time_string(), n_nodes, n_edges);
/* Density and Average Degree */
avg_deg = n_edges / n_nodes;
fprintf(stderr, "(%s) IN/OUT Average Degree: %le\n", get_time_string(), avg_deg);
density = n_edges / (n_nodes * (n_nodes-1));
fprintf(stderr, "(%s) Density: %le\n", get_time_string(), density);
printf("\n");
/* IN and OUT degree */
fprintf(stderr, "(%s) Compute IN/OUR Degree\n", get_time_string());
didegree_type *deg;
deg = get_inoutdegree(dg);
print_inoutdegree( deg, dg->nodes, dg->nodes);
printf("\n");
sort_inoutdegree( deg, dg->nodes, OUTDEG, DESC);
print_inoutdegree( deg, dg->nodes, 3);
printf("\n");
sort_inoutdegree( deg, dg->nodes, OUTDEG, ASC);
print_inoutdegree( deg, dg->nodes, 3);
printf("\n");
/* Compute SCC */
fprintf(stderr, "(%s) Compute SCC\n", get_time_string());
cc_type *scc;
scc = get_digraph_scc(dg);
print_cc(scc);
free_cc(scc);
printf("\n");
/* Compute WCC */
fprintf(stderr, "(%s) Compute WCC\n", get_time_string());
cc_type *wcc;
wcc = get_digraph_wcc(dg);
print_cc(wcc);
free_cc(wcc);
printf("\n");
free(file_name);
free(output_dir);
return 0;
}