-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_graph.c
97 lines (74 loc) · 2.16 KB
/
os_graph.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
// SPDX-License-Identifier: BSD-3-Clause
#include "os_graph.h"
#include <stdio.h>
#include <stdlib.h>
/* [ ==== NODE FUNCTIONS === ] */
os_node_t *os_create_node(unsigned int _nodeID, int _nodeInfo)
{
os_node_t *newNode;
newNode = calloc(1, sizeof(os_node_t));
newNode->nodeID = _nodeID;
newNode->nodeInfo = _nodeInfo;
return newNode;
}
/* [ ==== GRAPH FUNCTIONS === ] */
os_graph_t *create_graph_from_data(unsigned int nc, unsigned int ec,
int *values, os_edge_t *edges)
{
int i, isrc, idst;
os_graph_t *graph;
graph = calloc(1, sizeof(os_graph_t));
graph->nCount = nc;
graph->eCount = ec;
graph->nodes = calloc(nc, sizeof(os_node_t *));
for (i = 0; i < nc; ++i) {
graph->nodes[i] = os_create_node(i, values[i]);
graph->nodes[i]->neighbours = calloc(nc, sizeof(unsigned int));
graph->nodes[i]->cNeighbours = 0;
}
for (i = 0; i < ec; ++i) {
isrc = edges[i].src; idst = edges[i].dst;
graph->nodes[isrc]->neighbours[graph->nodes[isrc]->cNeighbours++] = idst;
graph->nodes[idst]->neighbours[graph->nodes[idst]->cNeighbours++] = isrc;
}
graph->visited = calloc(graph->nCount, sizeof(unsigned int));
return graph;
}
os_graph_t *create_graph_from_file(FILE *file)
{
unsigned int nCount, eCount;
int i;
int *values;
os_edge_t *edges;
os_graph_t *graph;
if (fscanf(file, "%d %d", &nCount, &eCount) == 0) {
fprintf(stderr, "[ERROR] Can't read from file\n");
return NULL;
}
values = malloc(nCount * sizeof(int));
for (i = 0; i < nCount; ++i) {
if (fscanf(file, "%d", &values[i]) == 0) {
fprintf(stderr, "[ERROR] Can't read from file\n");
return NULL;
}
}
edges = malloc(eCount * sizeof(os_edge_t));
for (i = 0; i < eCount; ++i) {
if (fscanf(file, "%d %d", &edges[i].src, &edges[i].dst) == 0) {
fprintf(stderr, "[ERROR] Can't read from file\n");
return NULL;
}
}
graph = create_graph_from_data(nCount, eCount, values, edges);
return graph;
}
void printGraph(os_graph_t *graph)
{
int i, j;
for (i = 0; i < graph->nCount; ++i) {
printf("[%d]: ", i);
for (j = 0; j < graph->nodes[i]->cNeighbours; ++j)
printf("%d ", graph->nodes[i]->neighbours[j]);
printf("\n");
}
}