|
| 1 | +/******************************************* |
| 2 | +Author : LHearen |
| 3 | + |
| 4 | +Time : 2016-03-04 15:02 |
| 5 | +Description : Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. |
| 6 | +Source : https://leetcode.com/problems/clone-graph/ |
| 7 | +*******************************************/ |
| 8 | +#include <stdlib.h> |
| 9 | +#include <bool.h> |
| 10 | +#define NEIGHBORS_MAX_SIZE 100 |
| 11 | +struct UndirectedGraphNode |
| 12 | +{ |
| 13 | + int label; |
| 14 | + struct UndirectedGraphNode *neighbors[NEIGHBORS_MAX_SIZE]; |
| 15 | + int neighborsCount; |
| 16 | +}; |
| 17 | + |
| 18 | +struct Mapper |
| 19 | +{ |
| 20 | + struct UndirectedGraphNode *key; |
| 21 | + struct UndirectedGraphNode *value; |
| 22 | + struct Mapper *next; |
| 23 | +}; |
| 24 | + |
| 25 | +bool push(struct Mapper* head, struct UndirectedGraphNode* key, struct UndirectedGraphNode* value) |
| 26 | +{ |
| 27 | + struct Mapper* p = head; |
| 28 | + while(p->next) |
| 29 | + { |
| 30 | + if(p->key == key) |
| 31 | + return false; |
| 32 | + p = p->next; |
| 33 | + } |
| 34 | + p->next = (struct Mapper*)malloc(sizeof(struct Mapper)); |
| 35 | + p = p->next; |
| 36 | + p->key = key; |
| 37 | + p->value = value; |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +struct UndirectedGraphNode* pop(struct Mapper* head, struct UndirectedGraphNode* key) |
| 42 | +{ |
| 43 | + while(head) |
| 44 | + { |
| 45 | + if(head->key == key) return head->value; |
| 46 | + head = head->next; |
| 47 | + } |
| 48 | + return NULL; |
| 49 | +} |
| 50 | + |
| 51 | +struct UndirectedGraphNode* copy(struct UndirectedGraphNode* node) |
| 52 | +{ |
| 53 | + struct UndirectedGraphNode *t = (struct UndirectedGraphNode*)malloc(sizeof(struct UndirectedGraphNode)); |
| 54 | + t->label = node->label; |
| 55 | + int count = node->neighborsCount; |
| 56 | + t->neighborsCount = count; |
| 57 | + for(int i = 0; i < count; i++) |
| 58 | + t->neighbors[i] = node->neighbors[i]; |
| 59 | + return t; |
| 60 | +} |
| 61 | + |
| 62 | +struct UndirectedGraphNode* cloneGraph(struct UndirectedGraphNode* graph) |
| 63 | +{ |
| 64 | + if(!graph) return NULL; |
| 65 | + struct UndirectedGraphNode *root = copy(graph); |
| 66 | + struct Mapper *head = (struct Mapper*)malloc(sizeof(struct Mapper)); |
| 67 | + head->next = NULL; |
| 68 | + head->key = 0; |
| 69 | + push(head, graph, root); |
| 70 | + struct UndirectedGraphNode **stack = (struct UndirectedGraphNode**)malloc(sizeof(struct UndirectedGraphNode*)); //recording the address of the original graph; |
| 71 | + int size = 0; |
| 72 | + stack[size++] = graph; |
| 73 | + while(size) |
| 74 | + { |
| 75 | + struct UndirectedGraphNode *cur = stack[--size]; |
| 76 | + printf("size: %d\tcurrent label: %d\t neighborsCount: %d\n", size, cur->label, cur->neighborsCount); |
| 77 | + for(int i = 0; i < cur->neighborsCount; i++) |
| 78 | + { |
| 79 | + struct Mapper *p = head; |
| 80 | + printf("inside map\n"); |
| 81 | + while(p) |
| 82 | + { |
| 83 | + printf("key address: %ld\tvalue: %ld\n", p->key, p->value); |
| 84 | + p = p->next; |
| 85 | + } |
| 86 | + printf("address of neighbor: %ld, pop: %d\n", cur->neighbors[i], pop(head, cur->neighbors[i])); |
| 87 | + if(!pop(head, cur->neighbors[i])) //not visited; |
| 88 | + { |
| 89 | + stack = (struct UndirectedGraphNode**)realloc(stack, sizeof(struct UndirectedGraphNode*)*(size+2)); |
| 90 | + stack[size++] = cur->neighbors[i]; |
| 91 | + struct UndirectedGraphNode *cCur = pop(head, cur); |
| 92 | + struct UndirectedGraphNode *cNeighbor = copy(cur->neighbors[i]); |
| 93 | + cCur->neighbors[i] = cNeighbor; |
| 94 | + push(head, cur->neighbors[i], cNeighbor); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return root; |
| 99 | +} |
0 commit comments