-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
143 lines (115 loc) · 2.38 KB
/
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
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
#include <stdio.h>
#include <stdlib.h>
#include "util_config.h"
#define GRAPH_INCREMENT 16
typedef struct vertex
{
/**
* Whatever you want.
*/
void *data;
/**
* Size of the data stored in data.
*/
int alloc;
/**
* The index identifying the vertex.
*/
int id;
/**
* The number of neighbors.
*/
int degree;
/**
* Flag set to 1 if the back pointer is set, 0 otherwise.
*/
char bflag;
/**
* Use when doing DFS or BFS; indicates the path taken to current vertex.
*/
struct vertex *back;
/**
* List of neighboring vertices.
*/
struct vertex *neighbors;
} vertex_t;
typedef struct graph
{
/**
* The total number of vertices.
*/
int nvert;
/**
* The total number of edges.
*/
int nedge;
/**
* The number of bytes to allocate for each vertex's data.
*/
int step;
/**
* Pointer to the first vertex.
*/
vertex_t *vertex; /* the pointer to the first vertex */
} graph_t;
/**
* Initializes the vertex_t object with the given identification
* number and the specified data size.
* @param obj_in Input vertex_t object
* @param id_in The id number to assign to the vertex
* @param alloc_in The size of the data stored in data
*/
int vertex_init( vertex_t *obj_in, int id_in, int alloc_in )
{
obj_in->data = (void*) malloc( alloc_in );
if( obj_in->data == NULL )
return -1;
obj_in->alloc = alloc_in;
obj_in->id = 0;
obj_in->degree = 0;
obj_in->bflag = 0;
obj_in->back = NULL;
obj_in->neighbors = NULL;
return 0;
}
int vertex_reset( vertex_t *obj_in )
{
obj_in->bflag = 0;
obj_in->back = NULL;
return 0;
}
int vertex_free( vertex_t *obj_in )
{
free( obj_in->data );
return 0;
}
/**
* Initializes the graph object to the specified number of vertices
* using assumed data size of alloc_in bytes.
* @param obj_in This is the graph_t object
* @param nvtx_in The number of vertices to initialize
* @param alloc_in The number of bytes to allocate for data in each vertex
*/
int graph_init( graph_t *obj_in, int nvtx_in, int alloc_in )
{
int i;
obj_in->vertex = (vertex_t*) malloc( nvtx_in * sizeof(vertex_t) );
obj_in->nvert = nvtx_in;
obj_in->nedge = 0;
obj_in->step = alloc_in;
for(i=0;i<nvtx_in;i++)
vertex_init( &(obj_in->vertex[i]), i, alloc_in );
}
int graph_add_vertex( graph_t *obj_in, void *data_in )
{
}
int graph_add_edge( )
{
}
int graph_free( graph_t *obj_in )
{
int i;
for(i=0;i<obj_in->nvert;i++)
{
}
}