| 1 | #include <stdlib.h>
|
|---|
| 2 | #include <stdbool.h>
|
|---|
| 3 | #include <assert.h>
|
|---|
| 4 | #include <time.h>
|
|---|
| 5 |
|
|---|
| 6 | #ifdef _CIVL
|
|---|
| 7 | #include <civlc.cvh>
|
|---|
| 8 | $input int N=10;
|
|---|
| 9 | $assume (N >= 3);
|
|---|
| 10 | #else
|
|---|
| 11 | int N=10;
|
|---|
| 12 | #endif
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 |
|
|---|
| 16 | NOTE: still need improvement
|
|---|
| 17 |
|
|---|
| 18 | problem description:
|
|---|
| 19 | Dancing links is a technique introduced in 1979 by Hitotumatu and
|
|---|
| 20 | Noshita and later popularized by Knuth. The technique can be used to
|
|---|
| 21 | efficiently implement a search for all solutions of the exact cover
|
|---|
| 22 | problem, which in its turn can be used to solve Tiling, Sudoku,
|
|---|
| 23 | N-Queens, and other problems.
|
|---|
| 24 |
|
|---|
| 25 | Suppose x points to a node of a doubly linked list; let L[x] and R[x]
|
|---|
| 26 | point to the predecessor and successor of that node. Then the operations
|
|---|
| 27 |
|
|---|
| 28 | L[R[x]] := L[x];
|
|---|
| 29 | R[L[x]] := R[x];
|
|---|
| 30 |
|
|---|
| 31 | remove x from the list. The subsequent operations
|
|---|
| 32 |
|
|---|
| 33 | L[R[x]] := x;
|
|---|
| 34 | R[L[x]] := x;
|
|---|
| 35 |
|
|---|
| 36 | will put x back into the list again.
|
|---|
| 37 |
|
|---|
| 38 | command: civl verify -inputN=10 DancingLinks.c
|
|---|
| 39 |
|
|---|
| 40 | ************TODO*************
|
|---|
| 41 | check the left and right node during iteration
|
|---|
| 42 |
|
|---|
| 43 | store pointers and compare them
|
|---|
| 44 |
|
|---|
| 45 | modify to c program
|
|---|
| 46 |
|
|---|
| 47 | function to check if insert or delete is correct
|
|---|
| 48 | */
|
|---|
| 49 | struct Node{
|
|---|
| 50 | struct Node *left;
|
|---|
| 51 | struct Node *right;
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | void insert(struct Node *x){
|
|---|
| 55 | x->right->left = x;
|
|---|
| 56 | x->left->right = x;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void delete(struct Node *x){
|
|---|
| 60 | x->right->left = x->left;
|
|---|
| 61 | x->left->right = x->right;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | //initilize a linkedlist
|
|---|
| 65 | struct Node* init(int n){
|
|---|
| 66 | struct Node *head = (struct Node *)malloc(sizeof(struct Node));
|
|---|
| 67 | struct Node *temp = head;
|
|---|
| 68 | temp->left = NULL;
|
|---|
| 69 | while(n > 1){
|
|---|
| 70 | struct Node *new = (struct Node *)malloc(sizeof(struct Node));
|
|---|
| 71 | temp->right = new;
|
|---|
| 72 | new->left = temp;
|
|---|
| 73 | temp = temp->right;
|
|---|
| 74 | n--;
|
|---|
| 75 | }
|
|---|
| 76 | temp->right = NULL;
|
|---|
| 77 | return head;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | struct Node* getNode(struct Node* list, int index){
|
|---|
| 81 | struct Node *result = list;
|
|---|
| 82 | while(index >1){
|
|---|
| 83 | result = result->right;
|
|---|
| 84 | index--;
|
|---|
| 85 | }
|
|---|
| 86 | return result;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | void initNodeArray(struct Node** array, struct Node* list, int n){
|
|---|
| 90 | struct Node *cur = list;
|
|---|
| 91 | int index=0;
|
|---|
| 92 | while(n > 0){
|
|---|
| 93 | array[index++] = cur;
|
|---|
| 94 | cur = cur->right;
|
|---|
| 95 | n--;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void verifyDelete(struct Node *list, struct Node **nodeArray, int index, int n){
|
|---|
| 100 | struct Node *temp1 = list;
|
|---|
| 101 | for(int i=0; i<index-1; i++){
|
|---|
| 102 | assert(temp1 == nodeArray[i]);
|
|---|
| 103 | assert(temp1->left == (nodeArray[i])->left);
|
|---|
| 104 | if(i != index-2){
|
|---|
| 105 | assert(temp1->right == (nodeArray[i])->right);
|
|---|
| 106 | }else{
|
|---|
| 107 | if(index == n){
|
|---|
| 108 | assert(temp1->right == NULL);
|
|---|
| 109 | }else{
|
|---|
| 110 | assert(temp1->right == nodeArray[index]);
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | temp1 = temp1->right;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | for(int j=index; j<=n-1; j++){
|
|---|
| 117 | assert(temp1 == nodeArray[j]);
|
|---|
| 118 | assert(temp1->right == (nodeArray[j])->right);
|
|---|
| 119 | if(j == index){
|
|---|
| 120 | if(index == 1){
|
|---|
| 121 | assert(temp1->left == NULL);
|
|---|
| 122 | }else{
|
|---|
| 123 | assert(temp1->left == nodeArray[index-2]);
|
|---|
| 124 | }
|
|---|
| 125 | }else{
|
|---|
| 126 | assert(temp1->left == (nodeArray[j])->left);
|
|---|
| 127 | }
|
|---|
| 128 | temp1 = temp1->right;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void verifyInsert(struct Node *list, struct Node **nodeArray, int index, int n){
|
|---|
| 133 | struct Node *temp1 = list;
|
|---|
| 134 | for(int k=0;k < n; k++){
|
|---|
| 135 | assert(temp1 == nodeArray[k]);
|
|---|
| 136 | assert(temp1->left == (nodeArray[k])->left);
|
|---|
| 137 | assert(temp1->right == (nodeArray[k])->right);
|
|---|
| 138 | temp1 = temp1->right;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | int main(){
|
|---|
| 143 | // size of the linked list
|
|---|
| 144 | #ifdef _CIVL
|
|---|
| 145 | int n = $choose_int(N-2)+3;
|
|---|
| 146 | #else
|
|---|
| 147 | srand(time(NULL));
|
|---|
| 148 | int n = rand()%(N-2)+3;
|
|---|
| 149 | #endif
|
|---|
| 150 |
|
|---|
| 151 | struct Node *list = init(n);
|
|---|
| 152 | // the index of the node who get deleted
|
|---|
| 153 | #ifdef _CIVL
|
|---|
| 154 | int index = $choose_int(n-2)+2;
|
|---|
| 155 | #else
|
|---|
| 156 | int index = rand()%(N-2)+2;
|
|---|
| 157 | #endif
|
|---|
| 158 |
|
|---|
| 159 | struct Node *x = getNode(list, index);
|
|---|
| 160 | struct Node *nodeArray[n];
|
|---|
| 161 | initNodeArray(nodeArray, list, n);
|
|---|
| 162 |
|
|---|
| 163 | delete(x);
|
|---|
| 164 |
|
|---|
| 165 | verifyDelete(list, nodeArray, index, n);
|
|---|
| 166 |
|
|---|
| 167 | insert(x);
|
|---|
| 168 |
|
|---|
| 169 | verifyInsert(list, nodeArray, index, n);
|
|---|
| 170 |
|
|---|
| 171 | int t = n;
|
|---|
| 172 | struct Node *temp1 = list;
|
|---|
| 173 | while(t >0){
|
|---|
| 174 | struct Node *temp2 = temp1;
|
|---|
| 175 | temp1 = temp1->right;
|
|---|
| 176 | free(temp2);
|
|---|
| 177 | t--;
|
|---|
| 178 | }
|
|---|
| 179 | return 0;
|
|---|
| 180 | }
|
|---|