-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs_bonus.c
419 lines (380 loc) · 12.1 KB
/
dfs_bonus.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//
// dfs.c
// CI
//
// Created by Alek on 28/10/2017.
// Copyright © 2017 private. All rights reserved.
// CREATE GRAPH - OK
// QUEUE - OK
// DFS - DO IT!!!
/* Structure:
Graph -> struct Graph. Members: 1) number of nodes 2) p to the 1st node
Node -> struct Node. Members: 1) p to the next node 2) p to the char array of with edges from this node
3) bool if the node has been marked 4) char with the name of the node
Edge -> char array created at run time.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node; struct Graph; struct List;
void copyListElement(struct List* to, struct List* from);
struct Node* findNode(struct Graph* graph, char v);
struct List {
char string[27]; //maximal nr nodes == 27
struct List* next;
// the place in the array of the last element
int placeHolder;
};
void printList(struct List* liste) {
while (liste)
{
printf("%s\n", liste->string);
liste = liste->next;
}
}
// End of stack
struct Graph* createGraph(void);
/* Adds a new Node to the graph at the end */
struct Node* addNode(struct Graph*, char, struct Node* lastNode);
void addEdge(struct Graph*, char*, int length, struct Node* lastNode);
void parseInput(char*, char*, char*);
void printGraph(struct Graph* graph);
void swap(char *x, char *y);
void permute(char *a, int l, int r, char***, int*);
void DFS(struct Graph*, struct Node*, struct List*);
// The vertices included in the Graph - the arrayList
struct Node {
bool marked;
char v;
char* edges;
struct Node* next;
};
// Vertices ordered alphabetically
struct Graph {
int nr_Nodes;
struct Node* firstNode;
char string[27];
};
char* findChildren(struct Graph* graph, char node, struct List* liste) {
if (node == '\0') {
return NULL;
}
struct Node* pCrawl = findNode(graph, node);
char* children = (char *)malloc(sizeof(char) * 27);
int j=0;
while (j<27) {
children[j] = '\0';
j++;
}
// add a feature that recognizes cyrcularity and already marked nodes!!!
// Go through graph and check
int i = 0; j = 0;
char* ret = NULL;
while (pCrawl->edges[i] != '\0') {
ret = memchr(liste->string, pCrawl->edges[i], strlen(liste->string));
if (ret == NULL) {
children[j] = pCrawl->edges[i];
j++;
}
i++;
}
return children;
}
struct List* allListsFull(struct List* liste, int nrNodes) {
while (liste) {
if (strlen(liste->string) == nrNodes) {
liste = liste->next;
}
else
return liste;
}
return NULL;
}
int isListFull(struct List* liste, int nrNodes) {
while (liste)
{
if (strlen(liste->string) == nrNodes)
liste = liste->next;
else
return 0;
}
return 1;
}
int main()
{
char buffer[50] = {0};
struct Graph* graph = createGraph();
for (int i = 0 ; i < 27 ; i++) {
graph->string[i] = '\0';
}
struct Node* lastNode = NULL;
while (scanf(" %s", buffer) == 1) {
char node = '\0', edges[27] = {'\0'};
sscanf(buffer, "%c-%s", &node, edges);
int length = (int)strlen(buffer+2);
lastNode = addNode(graph, node, lastNode);
addEdge(graph, edges, length, lastNode);
}
struct List* liste = (struct List *)malloc(sizeof(struct List));
liste->next = NULL;
liste->placeHolder = 1;
for (int i = 0; i<27; i++) {
liste->string[i] = '\0';
}
liste->string[0] = graph->firstNode->v;
if (graph->firstNode != NULL) {
while (!isListFull(liste, graph->nr_Nodes)) {
DFS(graph, graph->firstNode, liste); // PLACEHOLDER???
}
}
printList(liste);
return 0;
}
struct Node* findNode(struct Graph* graph, char v) {
struct Node* pCrawl = graph->firstNode;
while (pCrawl) {
if (pCrawl->v == v)
return pCrawl;
pCrawl = pCrawl->next;
}
return NULL;
}
bool nodesInList(struct List* liste, char* node) {
for (int j = 0 ; j < strlen(node) ; j++) {
for (int i = 0 ; i < strlen(liste->string) ; i++) {
if (liste->string[i] == node[j]) {
return true;
}
}
}
return false;
}
bool lastNode(struct Graph* graph, char node) {
struct Node* lastNode = findNode(graph, node);
if (strlen(lastNode->edges) == 1 )
return 1;
return 0;
// zyklisch??
}
int fakultaetRec(int x) {
if (x == 1)
return x;
return x*fakultaetRec(x-1);
}
void DFS(struct Graph* graph, struct Node* node, struct List* currentList) {
// IF end reached
char* kids = findChildren(graph, node->v, currentList);
kids = (char*)realloc(kids, strlen(kids));
if (kids[0] == '\0') {
currentList->placeHolder++;
return;
}
else {
unsigned long nrKids = strlen(kids);
if (nrKids == 1) {
// IF NOT ALREADY IN THE STRING!!!
struct Node* foundKid = findNode(graph, kids[0]);
if (!nodesInList(currentList,&foundKid->v)) {
currentList->string[(int)strlen(currentList->string)] = foundKid->v;
if (strlen(currentList->string)<graph->nr_Nodes) {
DFS(graph, foundKid, currentList);
}
}
}
else if(nrKids > 1) {
// nrKids fakultaet == nr Permuts - allocate memory here
int nrPermuts = fakultaetRec((int)nrKids);
//struct List* permuts = NULL;
// find the end of the List????
//currentList->next = permuts;
char** listPermuts = (char**)malloc(sizeof(char*)*nrPermuts);
for (int i=0;i<nrPermuts;i++) {
listPermuts[i] = (char*)malloc(sizeof(char)*nrKids);
}
int counter = 0;
permute(kids, 0, (int)nrKids-1, &listPermuts, &counter);
// the first list element will be also filled, hence nrPermits-1
/* TODO ======= COPYING!!!! EVERYTHING WIRD UEBERSCHRIEBEN */
struct List* pCrawl = currentList; //currentList points to the "first element" of the List
struct List** array = (struct List**)malloc(sizeof(struct List*)*nrPermuts);
for (int i=0; i<nrPermuts; i++) {
array[i] = NULL;
}
array[0] = currentList;
// last element of the list
while (pCrawl->next) {
pCrawl = pCrawl->next;
}
for (int i=0; i<(nrPermuts-1); i++) {
pCrawl->next = (struct List*)malloc(sizeof(struct List));
copyListElement(pCrawl->next, currentList);
array[i+1] = pCrawl->next;
pCrawl = pCrawl->next;
pCrawl->next = NULL;
}
// Now all the memory is prepared. Call DFS on all the new different strings (permuts)
// call DFS on all the kids - different permuts
int j = 0;
pCrawl = array[0]; // set to the first element
// go through the whole array of the List Elements that are relevant
while (j<nrPermuts) {
for (int i=0;i<nrKids;i++) {
struct Node* tempNode = findNode(graph, listPermuts[j][i]);
if (!nodesInList(pCrawl, &tempNode->v)) {
pCrawl->string[(int)strlen(pCrawl->string)] = tempNode->v;
/* MIND THE EDGES!!! IF THEY ALREADY IN THE LIST, DON'T START A NEW SEARCH!!! */
if (strlen(pCrawl->string)<graph->nr_Nodes) {
DFS(graph, tempNode, pCrawl);
}
}
}
j++;
pCrawl = array[j]; // j??
}
}
free(kids);
}
/*
if (strlen(currentList->string) == graph->nr_Nodes) {
return;
}
else if (node->edges[1] == '\0' && (node != graph->firstNode)) {
if (!nodesInList(currentList, &node->v)) {
currentList->string[(int)strlen(currentList->string)] = node->v;
}
return;
} else {
// Even possible??
char* kids = findChildren(graph, node->v, currentList);
if (*kids == '\0') {
if (!nodesInList(currentList, &node->v)) {
currentList->string[(int)strlen(currentList->string)]=node->v;
//currentList->placeHolder++;
}
return;
}
if (strlen(kids) > 1) {
unsigned long int nrKids = strlen(kids);
char* strToBeCopied = (char*)malloc(sizeof(char)*strlen(currentList->string));
memset(strToBeCopied, '\0', sizeof(*strToBeCopied));
strcpy(strToBeCopied, currentList->string);
int nrCycles = 0;
if (!nodesInList(currentList, kids))
permute(kids, 0, (int)nrKids-1, currentList, &nrCycles, strToBeCopied, graph);
}
if (strlen(kids) == 1) {
if (!nodesInList(currentList, &kids[0])) {
currentList->string[(int)strlen(currentList->string)-1] = kids[0];
}
return;
}
//DFS(graph, node, currentList->next);
// PLATZHALTER SHOULD POINT TO WHERE THE LAST DFS WAS CALLED!
// MAKE A BIG WHILE LOOP TO LOOP THROUGH THE NODES UNTILL FULL!
// THE LENGTH OF THE STRING IS OBTAINED WITH strlen()!!!
*/
}
// edges from a single node - char array
// edges from a single node - char array
/* Function to swap values at two pointers */
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int l, int r, char*** listPermuts, int* counter)
{
int i;
if (l == r) {
//printf("%s\n", a); // HERE COMES THE FUNCTIONSAUFRUF OF THE COPY, ADD
strcpy((*listPermuts)[*counter], a);
(*counter)++;
}
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r,listPermuts, counter);
swap((a+l), (a+i)); //backtrack
}
}
}
// TEST!!!
void CopyString(struct List* to, struct List* from)
{
for (int i = 0; i<27; i++) {
to->string[i] = '\0';
}
strcpy(to->string, from->string);
}
void copyListElement(struct List* to, struct List* from) {
memset(to->string, '\0', strlen(to->string));
strcpy(to->string, from->string);
}
/******** GRAPH METHODS ******/
struct Graph* createGraph() {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->firstNode = NULL;
graph->nr_Nodes = 0;
return graph;
}
struct Node* addNode(struct Graph* graph, char v, struct Node* lastNode) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (graph->firstNode == NULL) {
graph->firstNode = newNode;
}
else {
lastNode->next = newNode;
}
newNode->v = v;
newNode->edges = NULL;
newNode->next = NULL;
newNode->marked = false;
graph->nr_Nodes++;
return newNode;
}
void addEdge(struct Graph* graph, char* destination, int len, struct Node* lastNode) {
char* edge = (char*)malloc(sizeof(char)*(len+1));
for (int i = 0; i<=len; i++) {
edge[i] = '\0';
}
lastNode->edges = edge;
int i;
for (i = 0; i < len; i++) {
edge[i] = destination[i];
}
edge[i+1] = '\0';
}
void parseInput(char* buffer, char* edges, char* node) {
*node = buffer[0];
if (buffer[2] == '0') {
return;
}
int i = 0;
do {
i++;
} while (buffer[i]);
}
void printGraph(struct Graph* graph) {
int v;
struct Node* pNode = graph->firstNode;
for (v = 0; v < graph->nr_Nodes; ++v) {
printf("\n Adjacency list of vertex %c\n head ", pNode->v);
int i = 0;
while (pNode->edges[i]!= '\0') {
printf("-> %c", pNode->edges[i]);
i++;
}
printf("\n");
pNode = pNode->next;
}
}