key)
193 | {
194 | y->left=node;
195 | }
196 | else
197 | {
198 | y->right=node;
199 | }
200 | RBInsertFixup(root,node);
201 | }
202 |
203 |
204 |
205 |
206 | // 练习 13.3-1 那样会破坏性质5.。
207 | //
208 | // 练习 13.3-2 r==red b==black
209 | //
210 | // 38 b
211 | // 31 r 41 b
212 | // 12 b 19 b
213 | // 8r
214 | //
215 | //练习 13.3-4
216 | // 将节点颜色改成红色的只有三处,而且这三处改变的节点都是不可能是nil节点。
217 | //
218 | //练习 13.3-6
219 | // 可以写一个找出父指针的函数代替
220 |
221 |
222 |
223 | void RBTransplant(RBTree **root,RBTree *u,RBTree *v)
224 | {
225 | if(u->p==NULL)
226 | {
227 | *root=v;
228 | }
229 | else if(u==u->p->left)
230 | {
231 | u->p->left=v;
232 | }
233 | else
234 | {
235 | u->p->right=v;
236 | }
237 | if (v != NULL)
238 | v->p = u->p;
239 | }
240 |
241 |
242 | RBTree *RBMinimum(RBTree *x)
243 | {
244 | if(x==NULL)
245 | return NULL;
246 | while(x->left!=NULL)
247 | x=x->right;
248 | return x;
249 | }
250 |
251 |
252 | void RBDeleteFixup(RBTree **root,RBTree *x)
253 | {
254 | RBTree *w=NULL;
255 | while(x!=NULL&&x->color==black)
256 | {
257 | if(x==x->p->left)
258 | {
259 | w=x->p->right;
260 | if(w->color==red)
261 | {
262 | w->color=black;
263 | x->p->color=red;
264 | LeftRotate(root,x->p);
265 | w=x->p->right;
266 | }
267 | if(w->left->color==black&&w->right->color==black)
268 | {
269 | w->color=red;
270 | x=x->p;
271 | }
272 | else if(w->right->color==black)
273 | {
274 | w->left->color=black;
275 | w->color=red;
276 | RightRotate(root,w);
277 | w=x->p->right;
278 | }
279 | w->color=x->p->color;
280 | x->p->color=black;
281 | w->right->color=black;
282 | LeftRotate(root,x->p);
283 | x=*root;
284 | }
285 | else
286 | {
287 |
288 | w=x->p->left;
289 | if(w->color==red)
290 | {
291 | w->color=black;
292 | x->p->color=red;
293 | LeftRotate(root,x->p);
294 | w=x->p->left;
295 | }
296 | if(w->right->color==black&&w->left->color==black)
297 | {
298 | w->color=red;
299 | x=x->p;
300 | }
301 | else if(w->left->color==black)
302 | {
303 | w->right->color=black;
304 | w->color=red;
305 | RightRotate(root,w);
306 | w=x->p->left;
307 | }
308 | w->color=x->p->color;
309 | x->p->color=black;
310 | w->left->color=black;
311 | LeftRotate(root,x->p);
312 | x=*root;
313 | }
314 | }
315 | if (x != NULL)
316 | x->color = black;
317 | }
318 |
319 |
320 | void RBDelete(RBTree **root,int k)
321 | {
322 | RBTree *z = TreeSearch(*root, k);
323 | if (z == NULL)
324 | return;
325 | RBTree *y=z;
326 | RBTree *x=NULL;
327 | enum Color y_color=y->color;
328 | if(z->left==NULL)
329 | {
330 | x=z->right;
331 | RBTransplant(root,z,z->right);
332 | }
333 | else if(z->right==NULL)
334 | {
335 | x=z->left;
336 | RBTransplant(root,z,z->left);
337 | }
338 | else
339 | {
340 | y=RBMinimum(z->right);
341 | y_color=y->color;
342 | x=y->right;
343 | if (y->p==z)
344 | x->p=y;
345 | else
346 | {
347 | RBTransplant(root,y,y->right);
348 | y->right=z->right;
349 | y->right->p=y;
350 | }
351 | RBTransplant(root,z,y);
352 | y->left=z->left;
353 | y->left->p=y;
354 | y->color=z->color;
355 | }
356 | if(y_color==black)
357 | {
358 | RBDeleteFixup(root,x);
359 | }
360 | }
361 |
362 |
363 |
364 |
365 | void test()
366 | {
367 | RBTree *tree = NULL;
368 | RBInsert(&tree, 19);
369 |
370 | RBInsert(&tree, 3);
371 | InOrder(tree);
372 | printf("\n");
373 |
374 | RBInsert(&tree, 5);
375 | InOrder(tree);
376 | printf("\n");
377 |
378 |
379 | RBInsert(&tree, 6);
380 | InOrder(tree);
381 | printf("\n");
382 |
383 | int i;
384 | for (i = 1; i<20; i++)
385 | {
386 | RBInsert(&tree, i);
387 | if (i % 5 == 0)
388 | InOrder(tree);
389 | printf("\n");
390 | }
391 |
392 | RBDelete(&tree, 1);
393 |
394 | InOrder(tree);
395 | }
396 |
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第15章 动态规划/15_1_钢条切割.h:
--------------------------------------------------------------------------------
1 | #include "..//base.h"
2 |
3 | //递归求解
4 | int CutRod(int p[],int n)
5 | {
6 | if(n==0)
7 | return 0;
8 | int q=-1;
9 | int i;
10 | for(i=1;i<=n;i++)
11 | {
12 | q=MAX(q,p[i]+CutRod(p,n-i));
13 | }
14 | return q;
15 | }
16 |
17 | //自顶向上方法 备忘录
18 | int MemorizedCutRodAux(int p[],int n,int r[])
19 | {
20 | if(r[n]>=0)
21 | return r[n];
22 | int q=-1;
23 | if(n==0)
24 | q=0;
25 | else
26 | {
27 | int i;
28 | for(i=1;i<=n;i++)
29 | {
30 | q=MAX(q,p[i]+MemorizedCutRodAux(p,n-i,r));
31 | }
32 | }
33 | r[n]=q;
34 | return q;
35 | }
36 |
37 | int MemoizedCutRod(int p[],int n)
38 | {
39 | int i;
40 | int r[100];
41 | for(i=0;i<=n;i++)
42 | {
43 | r[i]=-1;
44 | }
45 | return MemorizedCutRodAux(p,n,r);
46 | }
47 |
48 |
49 | //自底向上方法
50 | //
51 | int BottomUpRod(int p[],int n)
52 | {
53 | int r[100]={0}; //因不支持变长数组,简单的以合适大小的数组表示。
54 | int i,j,q;
55 | for(i=1;i<=n;i++)
56 | {
57 | q=-1;
58 | for(j=1;j<=i;j++)
59 | {
60 | q=MAX(q,p[j]+r[i-j]);
61 | }
62 | r[i]=q;
63 | }
64 | return r[n];
65 | }
66 |
67 |
68 | //给出对应切割长度
69 | //
70 | int *ExtendedBottomUpCutRod(int p[],int n,int *t)
71 | {
72 | int *s=(int *)malloc(sizeof(int)*(n+1));
73 | int r[100]={0};
74 | int i,j,q;
75 | for(i=1;i<=n;i++)
76 | {
77 | q=-1;
78 | for(j=1;j<=i;j++)
79 | {
80 | if(q0)
98 | {
99 | printf("%d ",*(s+n));
100 | n=n-s[n];
101 | }
102 | printf("\n");
103 | }
104 |
105 |
106 |
107 | void test()
108 | {
109 | int p[] = { 0,1,5,8,9,10,17,17,20,24,30 };
110 | int pi;
111 | pi = CutRod(p,10);
112 | printf("10: price %d\n ",pi);
113 |
114 | pi = MemoizedCutRod(p,10);
115 | printf("10: price %d\n ",pi);
116 |
117 | pi = BottomUpRod(p,10);
118 | printf("10: price %d\n ",pi);
119 |
120 |
121 | PrintCutRodSolution(p,9);
122 | }
123 |
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第15章 动态规划/15_4_最长公共子序列.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第15章 动态规划/15_4_最长公共子序列.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第15章 动态规划/15_5_最优二叉树.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "..//base.h"
3 |
4 | void OptimalBST(double p[], double q[], int n)
5 | {
6 | double e[10][10], w[10][10];
7 | int root[10][10];
8 | for (int i = 1; i <= n + 1; i++)
9 | {
10 | e[i][i - 1] = q[i - 1];
11 | w[i][i - 1] = q[i - 1];
12 | }
13 | for (int l = 1; l <= n; l++)
14 | {
15 | for (int i = 1; i <= n - l + 1; i++)
16 | {
17 | int j = i + l - 1;
18 | e[i][j] = 100;
19 | w[i][j] = w[i][j-1] + q[j] + p[j];
20 | for (int r = i; r <= j; r++)
21 | {
22 | double t = e[i][r - 1] + e[r + 1][j] + w[i][j];
23 | if (t < e[i][j])
24 | {
25 | e[i][j] = t;
26 | root[i][j] = r;
27 | }
28 | }
29 | }
30 | }
31 |
32 | int x;
33 |
34 | }
35 |
36 | void test()
37 | {
38 | double p[] = { 0,0.15,0.10,0.05,0.10,0.20 };
39 | double q[] = { 0.05,0.10,0.05,0.05,0.05,0.10 };
40 | OptimalBST(p, q, 5);
41 | }
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第16章 贪心算法/16_1_活动选择问题.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "..//base.h"
3 |
4 | void RecursizeActivitySelector(int start[], int end[], int k,int n, int ret[], int current)
5 | {
6 | int m = k + 1;
7 | while (m <= n && start[m] < end[k])
8 | {
9 | m++;
10 | }
11 | if (m <= n)
12 | {
13 | ret[current] = m;
14 | RecursizeActivitySelector(start, end, m, n, ret, current + 1);
15 | }
16 | }
17 |
18 | void GreedyActivitySelector(int s[], int f[],int n,int ret[])
19 | {
20 | int cur = 0;
21 | int k = 1;
22 | ret[cur++] = 1;
23 | for (int m = 2; m <= n; m++)
24 | {
25 | if (s[m] > f[k])
26 | {
27 | ret[cur++] = m;
28 | k = m;
29 | }
30 | }
31 | }
32 |
33 |
34 |
35 |
36 | /*
37 | 练习16.1-1
38 |
39 | for i <- 0 to n+1
40 | do c[i,i] <- 0
41 | for l <- 1 to n+1
42 | do for i <- 0 to n+1-l
43 | do j <- i+l
44 | c[i,j] <- 0
45 | for k <- i+1 to j-1
46 | do if f_i <= s_k and f_k <= s_j // test if activity a_k is in S_{ij}
47 | then q <- c[i,k] + c[k,j] + 1
48 | if q > c[i,j] then c[i,j] <- q
49 | a[i,j] <- k
50 | */
51 |
52 |
53 | void test()
54 | {
55 | int s[] = { 0,1,3,0,5,3,5,6,8,8,2,12 };
56 | int f[] = { 0,4,5,6,7,9,9,10,11,12,14,16 };
57 | int ret[10] = { 0 };
58 | RecursizeActivitySelector(s,f,0,11,ret,0);
59 | int ret2[10] = { 0 };
60 | GreedyActivitySelector(s, f, 11, ret2);
61 |
62 |
63 | }
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第16章 贪心算法/16_2_贪心算法原理.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第16章 贪心算法/16_2_贪心算法原理.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第16章 贪心算法/16_3_哈夫曼编码.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第16章 贪心算法/16_3_哈夫曼编码.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第18章 B树/18_2_B树上的基本操作_18_3_删除.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第18章 B树/18_2_B树上的基本操作_18_3_删除.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第19章 斐波那契堆/19_斐波那契堆.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "..//base.h"
3 |
4 |
5 | #define dn 50
6 | typedef struct FIB_NODE
7 | {
8 | int key;
9 | int degree;
10 | int mark;
11 | struct FIB_NODE *left;
12 | struct FIB_NODE *right;
13 | struct FIB_NODE *parent;
14 | struct FIB_NODE *child;
15 | }FibNode;
16 |
17 | typedef struct FIB_HEAP
18 | {
19 | int n;
20 | FibNode *min_node;
21 | }FibHeap;
22 |
23 | FibNode *CreateNode(int x,int ma)
24 | {
25 | FibNode *fib_node = (FibNode *)malloc(sizeof(FibNode));
26 | fib_node->degree = 0;
27 | fib_node->key = x;
28 | fib_node->mark = ma;
29 | fib_node->parent = NULL;
30 | fib_node->child = NULL;
31 | fib_node->left = fib_node;
32 | fib_node->right = fib_node;
33 | return fib_node;
34 | }
35 |
36 | void FibHeapInsert(FibHeap *fibheap, FibNode *fib_node)
37 | {
38 |
39 | if (fibheap->min_node == NULL)
40 | {
41 | fib_node->left = fib_node;
42 | fib_node->right = fib_node;
43 | fibheap->min_node = fib_node;
44 | }
45 | else
46 | {
47 | fib_node->right = fibheap->min_node;
48 | fib_node->left = fibheap->min_node->left;
49 | fibheap->min_node->left->right = fib_node;
50 | fibheap->min_node->left = fib_node;
51 | if (fib_node->key < fibheap->min_node->key)
52 | fibheap->min_node = fib_node;
53 | }
54 | fib_node->parent = NULL;
55 | fibheap->n++;
56 | }
57 |
58 | FibHeap *MakeFibHeap()
59 | {
60 | FibHeap *fib_heap = (FibHeap*)malloc(sizeof(FibHeap));
61 | fib_heap->min_node = NULL;
62 | fib_heap->n = 0;
63 | return fib_heap;
64 | }
65 |
66 | FibHeap *FibHeapUnion(FibHeap *heap1, FibHeap *heap2)
67 | {
68 | FibHeap *heap = MakeFibHeap();
69 | heap->min_node = heap1->min_node;
70 | if ((heap1->min_node == NULL) || ((heap2->min_node != NULL) && (heap2->min_node->key < heap1->min_node->key)))
71 | {
72 | heap->min_node = heap2->min_node;
73 | }
74 | if (heap1->min_node != NULL && heap2->min_node != NULL)
75 | {
76 | heap1->min_node->left->right = heap2->min_node->left;
77 | heap2->min_node->left->right = heap1->min_node->left;
78 | heap1->min_node->left = heap2->min_node;
79 | heap2->min_node->left = heap1->min_node;
80 | }
81 | heap->n = heap1->n + heap2->n;
82 | return heap;
83 | }
84 |
85 | void FibHeapLink(FibHeap *heap, FibNode *y, FibNode *x)
86 | {
87 | y->left->right = y->right;
88 | y->right->left = y->left;
89 | if (x->child == NULL)
90 | {
91 | x->child = y;
92 | y->parent = x;
93 | y->left = y;
94 | y->right = y;
95 | }
96 | else
97 | {
98 | x->child->left->right = y;
99 | y->left = x->child->left;
100 | x->child->left = y;
101 | y->right = x->child;
102 | y->parent = x;
103 | }
104 | x->degree++;
105 | y->mark = 0;
106 | }
107 | void Consolidate(FibHeap *heap)
108 | {
109 | FibNode *a[dn] = { 0 };
110 | FibNode *x = heap->min_node;
111 | do
112 | {
113 | int d = x->degree;
114 | while (a[d]!=NULL&&dmin_node)
117 | {
118 | heap->min_node = heap->min_node->right;
119 | }
120 | FibNode *y = a[d];
121 | if (x->key > y->key)
122 | {
123 | FibNode *temp = x;
124 | x = y;
125 | y = temp;
126 | }
127 | FibHeapLink(heap, y, x);
128 | a[d] = NULL;
129 | d++;
130 | }
131 | a[d] = x;
132 | x = x->right;
133 | } while (x != heap->min_node);
134 | heap->min_node = NULL;
135 |
136 | for (int i = 0; i < dn; i++)
137 | {
138 | if (a[i] != NULL)
139 | {
140 | if (heap->min_node == NULL)
141 | {
142 | a[i]->left = a[i];
143 | a[i]->right = a[i];
144 | heap->min_node = a[i];
145 | }
146 | else
147 | {
148 | a[i]->left = heap->min_node->left;
149 | a[i]->right = heap->min_node;
150 | heap->min_node->left->right = a[i];
151 | heap->min_node->left = a[i];
152 | if (a[i]->key < heap->min_node->key)
153 | {
154 | heap->min_node = a[i];
155 | }
156 | }
157 | }
158 | }
159 |
160 | }
161 |
162 | FibNode *FibHeapExtractMin(FibHeap *heap)
163 | {
164 | FibNode *min = heap->min_node;
165 | if (min != NULL)
166 | {
167 | min->left->right = min->child;
168 | min->right->left = min->child->left;
169 | min->child->left->right = min->right;
170 | min->child->left = min->left;
171 | if (min == min->right)
172 | {
173 | heap->min_node = NULL;
174 | }
175 | else
176 | {
177 | heap->min_node = min->right;
178 | Consolidate(heap);
179 | }
180 | heap->n--;
181 | }
182 | return min;
183 | }
184 |
185 | void Cut(FibHeap *heap, FibNode *x, FibNode *y)
186 | {
187 | if (y->degree == 1)
188 | {
189 | y->child = 0;
190 | }
191 | else
192 | {
193 | if (x == y->child)
194 | {
195 | y->child = x->right;
196 | }
197 | x->left->right = x->right;
198 | x->right->left = x->left;
199 | }
200 | y->degree--;
201 | x->mark = 0;
202 | FibHeapInsert(heap, x);
203 | }
204 |
205 | void CascadingCut(FibHeap *heap, FibNode *y)
206 | {
207 | FibNode *z = y->parent;
208 | if (z != NULL)
209 | {
210 | if (y->mark == 0)
211 | {
212 | y->mark = 1;
213 | }
214 | else
215 | {
216 | Cut(heap, y, z);
217 | CascadingCut(heap, z);
218 | }
219 | }
220 | }
221 |
222 | void FibHeapDecreaseKey(FibHeap *heap, FibNode *x, int k)
223 | {
224 | if (k > x->key)
225 | return;
226 | x->key = k;
227 | FibNode *y = x->parent;
228 | if (y != NULL && x->key < y->key)
229 | {
230 | Cut(heap, x, y);
231 | CascadingCut(heap, y);
232 | }
233 | if (x->key < heap->min_node->key)
234 | {
235 | heap->min_node = x;
236 | }
237 | }
238 |
239 | void FibHeapDelete(FibHeap *heap, FibNode *node)
240 | {
241 | FibHeapDecreaseKey(heap, node, INT_MIN);
242 | FibHeapExtractMin(heap);
243 | }
244 |
245 |
246 | void test()
247 | {
248 | FibHeap *heap = MakeFibHeap();
249 | FibNode *node3 = CreateNode(3, 0);
250 | FibNode *node18 = CreateNode(18, 1);
251 | FibNode *node39 = CreateNode(39, 1);
252 | node18->child = node39;
253 | node39->parent = node18;
254 | FibNode *node52 = CreateNode(52, 0);
255 | FibNode *node38 = CreateNode(38, 0);
256 | FibNode *node41 = CreateNode(41, 0);
257 | node38->child = node41;
258 |
259 | node18->right = node52;
260 | node52->right = node38;
261 | node38->right = node18;
262 | node18->left = node38;
263 | node52->left = node18;
264 | node38->left = node52;
265 | node3->child = node52;
266 | node41->parent = node38;
267 | node39->parent = node18;
268 |
269 | node52->parent = node3;
270 | node38->parent = node3;
271 | node18->parent = node3;
272 |
273 | node3->degree = 3;
274 | node18->degree = 1;
275 | node38->degree = 1;
276 |
277 | FibNode *node17 = CreateNode(17, 0);
278 | FibNode *node30 = CreateNode(30, 0);
279 | node17->child = node30;
280 | node30->parent = node17;
281 | node17->degree = 1;
282 |
283 | FibNode *node46 = CreateNode(46, 0);
284 | FibNode *node24 = CreateNode(24, 0);
285 | FibNode *node26 = CreateNode(26, 1);
286 | FibNode *node35 = CreateNode(35, 0);
287 | node26->child = node35;
288 | node35->parent = node26;
289 | node26->left = node46;
290 | node46->left = node26;
291 | node26->right = node46;
292 | node46->right = node26;
293 | node24->child = node26;
294 | node26->parent = node24;
295 | node46->parent = node24;
296 | node24->degree = 2;
297 | node26->degree=1;
298 |
299 |
300 | FibHeapInsert(heap, node17);
301 | FibHeapInsert(heap, node24);
302 | FibHeapInsert(heap, node3);
303 | FibHeapInsert(heap, CreateNode(23, 0));
304 | FibHeapInsert(heap, CreateNode(7, 0));
305 | FibHeapInsert(heap, CreateNode(21, 0));
306 | heap->n = 15;
307 | FibHeapExtractMin(heap);
308 | FibHeapDecreaseKey(heap, node46, 5);
309 | printf("1234");
310 | }
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第22章 基本的图算法/22_1_图的表示.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_1_图的表示.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第22章 基本的图算法/22_2_广度优先搜索.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_2_广度优先搜索.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第22章 基本的图算法/22_3_深度优先搜索.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_3_深度优先搜索.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第22章 基本的图算法/22_5_强连通分量.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_5_强连通分量.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第23章 最小生成树/23_2_Kruskal算法.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第23章 最小生成树/23_2_Kruskal算法.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第23章 最小生成树/23_2_Prim算法.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第23章 最小生成树/23_2_Prim算法.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第24章 单源最短路径/24_2_Bellman-Ford算法.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第24章 单源最短路径/24_2_Bellman-Ford算法.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第24章 单源最短路径/24_3_Dijkstra算法.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第24章 单源最短路径/24_3_Dijkstra算法.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第24章 单源最短路径/松弛操作.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第24章 单源最短路径/松弛操作.h
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第32章 字符串匹配/32_1_朴素字符串匹配算法.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "..//base.h"
3 |
4 | int NaiveStringMatcher(char t[], int n, char p[], int m)
5 | {
6 | int i, j;
7 | for (i = 0; i < n - m; i++)
8 | {
9 | int flag = 0;
10 | for (j = 0; j < m; j++)
11 | {
12 | if (t[i + j] == p[j])
13 | {
14 | flag++;
15 | }
16 | }
17 | if (flag == m)
18 | break;
19 | }
20 | return i;
21 | }
22 |
23 | void test()
24 | {
25 | char t[] = "acaabc";
26 | char p[] = "aab";
27 | int a = NaiveStringMatcher(t, 6, p, 3);
28 | }
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第32章 字符串匹配/32_2_Rabin-Karp算法.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "..//base.h"
3 |
4 |
5 | int pow(int x, int y)
6 | {
7 | int r = 1;
8 | while (y--)
9 | {
10 | r *= x;
11 | }
12 | return r;
13 | }
14 |
15 |
16 | int RabinKarpMatcher(char st[], int n, char sp[], int m, int d, int q)
17 | {
18 | int i, j;
19 | int p = 0, t = 0;
20 | int h = pow(d, m - 1) % q;
21 | for (i = 0; i < m; i++)
22 | {
23 | p = (d*p +sp[i]) % q;
24 | t = (d*t + st[i]) % q;
25 | }
26 | for (int i = 0; i < n - m; i++)
27 | {
28 | if (p == t)
29 | {
30 | int flag = 0;
31 | for (j = 0; j < m; j++)
32 | {
33 | if (st[i + j] == sp[j])
34 | {
35 | flag++;
36 | }
37 | }
38 | if (flag == m)
39 | break;
40 | }
41 | if (i < n - m)
42 | {
43 | t = (d*(t - st[i] * h) + st[i + m]) % q;
44 | }
45 | }
46 | return i;
47 | }
48 |
49 |
50 | void test()
51 | {
52 | char t[] = "acaabc";
53 | char p[] = "aab";
54 | int a = RabinKarpMatcher(t, 6, p, 3,10,13);
55 | }
--------------------------------------------------------------------------------
/IntroductionToAlgorithms/第32章 字符串匹配/32_4_Hnuth-Morris-Pratt算法.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "..//base.h"
3 |
4 | void ComputePrefixFunction(int pi[], char p[], int m)
5 | {
6 | pi[0] = 0;
7 | int k = 0;
8 | for (int i = 1; i < m; i++)
9 | {
10 | while (k > 0 && p[k] != p[i])
11 | {
12 | k = pi[k];
13 | }
14 | if (p[k] == p[i])
15 | {
16 | k++;
17 | }
18 | pi[i] = k;
19 | }
20 | }
21 |
22 | int KMPMatcher(char st[], int n, char sp[], int m)
23 | {
24 | int *pi = (int *)malloc(sizeof(int)*m);
25 | ComputePrefixFunction(pi, sp, m);
26 | int q = 0;
27 | for (int i = 0; i < n; i ++ )
28 | {
29 | while (q > 0 && sp[q] != st[i]) //not match
30 | {
31 | q = pi[q-1];
32 | }
33 | if (sp[q] == st[i])
34 | {
35 | q++;
36 | }
37 | if (q == m)
38 | {
39 | return i-m+1;
40 | }
41 | }
42 | return -1;
43 | }
44 |
45 | void test()
46 | {
47 | char t[] = "bacbabababacabcbab";
48 | char p[] = "ababaca";
49 | int a = KMPMatcher(t, 15, p, 7);
50 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Introduction-To-Algorithms
2 |
3 | ## 算法导论伪代码的实现及部分习题
4 |
5 | 开始时间 2018-3-3 周六,
6 |
--------------------------------------------------------------------------------