/
bpGraphAdjList_BL.c
289 lines (251 loc) · 7.81 KB
/
bpGraphAdjList_BL.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* bpGraphAdjList_BL.c
*
* Double Linked list implementation of an adjacency list representation.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "commonDefs.h"
#include "linkedList.h"
#include "memoryUtil.h"
#include "binaryTree.h"
#include <stddef.h>
#include "bipartiteGraph.h"
struct implBipartGraph_t
{
int vertNum1;
int vertNum2;
binTreeNode_t *vpVertsP1;
binTreeNode_t *vpVertsP2;
};
/* ************************************************************************* */
/* Function implementations */
bpGraph_t* bipartGraphCreate(int part1VertNum, int part2VertNum)
{
/* TODO: Implement me! */
bpGraph_t *pGraph = (bpGraph_t*) safeMalloc(sizeof(bpGraph_t));
pGraph->vertNum1 = part1VertNum;
pGraph->vertNum2 = part2VertNum;
pGraph->vpVertsP1 = NULL;
pGraph->vpVertsP2 = NULL;
int i;
int j;
for (i = 0; i < part1VertNum; i++) {
if(pGraph->vpVertsP1 == NULL) {
pGraph->vpVertsP1 = createTreeNode(i, createList());
} else {
binTreeNode_t *pNewNode = createTreeNode(i, createList());
insertTreeNode(pGraph->vpVertsP1, pNewNode);
}
}
for (j = 0; j < part2VertNum; j++) {
if(pGraph->vpVertsP2 == NULL) {
pGraph->vpVertsP2 = createTreeNode(j, createList());
} else {
binTreeNode_t *pNewNode = createTreeNode(j, createList());
insertTreeNode(pGraph->vpVertsP2, pNewNode);
}
}
return pGraph;
} /* end of bipartGraphDestroy() */
void bipartGraphDestroy(bpGraph_t* pGraph)
{
/* TODO: Implement me! */
destroyTree(pGraph->vpVertsP1);
destroyTree(pGraph->vpVertsP2);
safeFree(pGraph, sizeof(bpGraph_t));
pGraph = NULL;
} /* end of bipartGraphDestroy() */
int bipartGraphInsertVertex(bpGraph_t* pGraph, int vertId, int partite)
{
/* TODO: Implement me! */
binTreeNode_t *currentNode = NULL, *left = NULL, *right = NULL;
if (partite == 1) {
if(pGraph->vpVertsP1 == NULL) {
pGraph->vpVertsP1 = createTreeNode(vertId, createList());
pGraph->vertNum1 += 1;
return NEW_VERTEX;
} else {
binTreeNode_t *pNewNode = createTreeNode(vertId, createList());
if (insertTreeNode(pGraph->vpVertsP1, pNewNode)) {
pGraph->vertNum1 += 1;
return NEW_VERTEX;
}
return EXISTING_VERTEX;
}
}
if (partite == 2) {
if(pGraph->vpVertsP2 == NULL) {
pGraph->vpVertsP2 = createTreeNode(vertId, createList());
pGraph->vertNum2 += 1;
return NEW_VERTEX;
} else {
binTreeNode_t *pNewNode = createTreeNode(vertId, createList());
if (insertTreeNode(pGraph->vpVertsP2, pNewNode)) {
pGraph->vertNum2 += 1;
return NEW_VERTEX;
}
return EXISTING_VERTEX;
}
}
} /* end of bipartGraphInsertVertex() */
int bipartGraphInsertEdge(bpGraph_t* pGraph, int srcVertId, int tarVertId, int srcPartite)
{
/* TODO: Implement me! */
binTreeNode_t *parent = NULL, *pNode1 = NULL, *pNode2 = NULL, *left = NULL, *right = NULL;
int leftChild = 0;
if (srcPartite == 1) {
if ((pNode1 = searchDeleteNode(pGraph->vpVertsP1, srcVertId, &parent, &leftChild)) != NULL && (pNode2 = searchDeleteNode(pGraph->vpVertsP2, tarVertId, &parent, &leftChild)) != NULL) {
if (!findElement(pNode1->value, tarVertId)) {
addNode(pNode1->value, tarVertId);
return NEW_EDGE;
} else {
return EXISTING_EDGE;
}
}
return ERROR_VALUE;
}
else if (srcPartite == 2) {
if ((pNode2 = searchDeleteNode(pGraph->vpVertsP2, srcVertId, &parent, &leftChild)) != NULL && (pNode1 = searchDeleteNode(pGraph->vpVertsP1, tarVertId, &parent, &leftChild)) != NULL) {
if (!findElement(pNode2->value, tarVertId)) {
addNode(pNode2->value, tarVertId);
return NEW_EDGE;
} else {
return EXISTING_EDGE;
}
}
return ERROR_VALUE;
}
return ERROR_VALUE;
/* TODO: Replace placeholder. */
} /* end of bipartGraphInsertEdge() */
void deleteSublistNode (binTreeNode_t *pTree, int tarVertId) {
if (pTree != NULL) {
if (pTree->value) {
if (findElement(pTree->value, tarVertId)) {
deleteNode(pTree->value, tarVertId);
}
}
deleteSublistNode(pTree->left, tarVertId);
deleteSublistNode(pTree->right, tarVertId);
}
}
int bipartGraphDeleteVertex(bpGraph_t* pGraph, int vertId, int partite)
{
binTreeNode_t *parent, *pNode1 = NULL, *pNode2 = NULL;
if (partite == 1) {
parent = NULL;
int leftChild = 0;
if ((pNode1 = searchDeleteNode(pGraph->vpVertsP1, vertId, &parent, &leftChild)) != NULL) {
deleteSublistNode(pGraph->vpVertsP2, vertId);
deleteTreeNode(pGraph->vpVertsP1, pNode1, parent, leftChild);
return SUCCESS;
}
return FAILED;
}
if (partite == 2) {
parent = NULL;
int leftChild = 0;
if ((pNode2 = searchDeleteNode(pGraph->vpVertsP2, vertId, &parent, &leftChild)) != NULL) {
deleteSublistNode(pGraph->vpVertsP1, vertId);
deleteTreeNode(pGraph->vpVertsP2, pNode2, parent, leftChild);
return SUCCESS;
}
return FAILED;
}
} /* end of bipartGraphDeleteVertex() */
int bipartGraphDeleteEdge(bpGraph_t* pGraph, int srcVertId, int tarVertId, int srcPartite)
{
int errorStatus;
binTreeNode_t *pNode = NULL, *left = NULL, *right = NULL, *parent=NULL;
int leftChild = 0;
if (srcPartite == 1) {
if ((pNode = searchDeleteNode(pGraph->vpVertsP1, srcVertId, &parent, &leftChild)) != NULL) {
return deleteNode(pNode->value, tarVertId);
} else {
return NOT_FOUND;
}
}
else if (srcPartite == 2) {
if ((pNode = searchDeleteNode(pGraph->vpVertsP2, srcVertId, &parent, &leftChild)) != NULL) {
return deleteNode(pNode->value, tarVertId);
} else {
return NOT_FOUND;
}
}
return ERROR_VALUE;
} /** end of bipartGraphDeleteEdge() */
int bipartGraphFindVertex(bpGraph_t *pGraph, int vertId, int partite)
{
/* TODO: Implement me! */
/* TODO: Replace placeholder. */
binTreeNode_t *parent = NULL;
int leftChild = 0;
if (partite == 1) {
if (searchDeleteNode(pGraph->vpVertsP1, vertId, &parent, &leftChild) != NULL) {
return FOUND;
} else {
return NOT_FOUND;
}
}
if (partite == 2) {
if (searchDeleteNode(pGraph->vpVertsP2, vertId, &parent, &leftChild) != NULL) {
return FOUND;
} else {
return NOT_FOUND;
}
}
return ERROR_VALUE;
} /* end of bipartGraphFindVertex() */
int bipartGraphFindEdge(bpGraph_t* pGraph, int srcVertId, int tarVertId, int srcPartite)
{
/** TODO: Implement me! */
binTreeNode_t *parent = NULL, *pNode1 = NULL, *pNode2 = NULL;
int leftChild = 0;
if (srcPartite == 1) {
if ((pNode1 = searchDeleteNode(pGraph->vpVertsP1, srcVertId, &parent, &leftChild)) != NULL) {
return findElement(pNode1->value, tarVertId);
}
return NOT_FOUND;
}
if (srcPartite == 2) {
if ((pNode2 = searchDeleteNode(pGraph->vpVertsP2, srcVertId, &parent, &leftChild)) != NULL) {
return findElement(pNode2->value, tarVertId);
}
return NOT_FOUND;
}
return ERROR_VALUE;
} /* end of bipartGraphFindEdge() */
void printLList(binTreeNode_t *pTree);
void printLList(binTreeNode_t *pTree) {
if (pTree != NULL) {
if (pTree->value) {
linkedList_t *currentList = pTree->value;
llNode_t *currentHead = currentList->pHead;
while (currentHead != NULL) {
printf("%d %d\n", pTree->key, currentHead->element);
currentHead = currentHead->pNext;
}
}
printLList(pTree->left);
printLList(pTree->right);
}
}
void bipartGraphPrint(bpGraph_t *pGraph)
{
printf("Vertices:\n");
printf("Part 1:\n");
printTree(pGraph->vpVertsP1);
printf("\n");
printf("Part 2:\n");
printTree(pGraph->vpVertsP2);
printf("\n");
printf("Edges:\n");
/* partite 1 to partite 2 edges. */
printf("Part 1 to 2:\n");
printLList(pGraph->vpVertsP1);
/* partite 2 to partite 1 edges. */
printf("Part 2 to 1:\n");
printLList(pGraph->vpVertsP2);
} /* end of bipartGraphPrint() */