source: CIVL/examples/verifyThisProblems/dancingLinks.c@ adcb43a

1.23 2.0 main test-branch
Last change on this file since adcb43a was 41eb9b6, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

format changing

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@3144 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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