📄 construct.w,v
字号:
d219 3a221 1construct(const int n, int *tour, const int heuristic, const long heur_param) {d224 1a225 1 errorif(1,"Unknown heuristic: %d",heuristic);d232 2a233 1construct(const int n, int *tour, const int heuristic, const long heur_param);d245 1a245 1@@ For a warmup, let's implement the canoncial tour, \ie~ordering 0, 1, 2, \etc.d276 1d282 1a282 1 gb_init_rand(heur_param);d284 1a284 1 const int next = gb_unif_rand(n-i);d289 1d304 1a304 5@@ And we must import the interface to the random number generator. We'veused the generator from Knuth's Stanford GraphBase.@@^Stanford GraphBase@@>@@^Knuth, Donald Ervin@@>d308 1a308 1#include "gb_flip.h"d352 6d384 1d386 3a388 1case CONSTRUCT_GREEDY: {d391 1d402 2a403 1@@ We need to export the constant |CONSTRUCT_GREEDY|.d405 2a406 1#define CONSTRUCT_GREEDY 2d478 1d480 1d567 1a567 1is unsaturated, then |unsaturated[inv_unsaturated[i]]==i|.d767 1a767 1 @@<Find a shortest valid edge $e=(x,y)$@@>@@;d786 28a813 2@@ Here's where we pick a shortest remaining valid edge.It is eligible if both ends ared825 2a826 1@@<Find a shortest valid edge $e=(x,y)$@@>=d829 1a829 1 errorif(e==NULL,"Exhausted the priority queue of links.");a834 1 @@<Insert a new link for |x|@@>@@;d836 27d936 2a937 1construct_matching(int n, int *mate, int alg,long alg_param) d955 2a956 1length_t construct_matching(int n, int *mate, int alg,long alg_param);d990 1a991 1 gb_init_rand(alg_param);d993 1a993 1 ui = gb_unif_rand(num_unmated);d995 1a995 1 vi = gb_unif_rand(num_unmated);d1001 1d1016 2a1017 1 { d1019 1a1041 6In the perfect matching case, a node is saturated if it has even a singleedge incident upon it. We maintain the invariant that a city is hiddenin the $k$-d tree if and only if it is saturated.You'll have to agree this is much simpler than the greedy algorithmfor tours.d1048 2a1049 1 pq_edge_t *next_edge = pq_delete_min(pq_edge);d1067 48@1.131log@Use binary heap priority queue in place of heavier weight dictionaries.@text@d1 46a46 1@@i copyrt.wd53 3d179 1a179 1const char *construct_rcs_id = "$Id: construct.w,v 1.130 1998/04/16 16:02:03 neto Exp neto $";@1.130log@webbug fixed: the code for initializing nn work array came after itsfirst use. Doh.@text@d8 4d131 1a131 1const char *construct_rcs_id = "$Id: construct.w,v 1.129 1998/04/16 15:23:20 neto Exp neto $";d379 1a379 2We will use the dictionary routines from module \module{DICT} because they provide an efficient priority queue implementation.d383 6a388 1dict_t *pq_edge = dict_create(cmp_pq_edge,nn_link_prn);d393 1a393 1dict_destroy(pq_edge,NULL);d396 1a396 1dictionary, and to the pool manager.d400 1a400 1#include "dict.h"d412 1a412 1@@ We've had to provide a comparison function for the dictionary routines.d427 1a427 1be able to insert two edges of the same length: the dictionary requiresd459 1a459 1 dict_insert(pq_edge,e);d595 1a595 1 dict_insert(pq_edge,e);d731 1a731 1 e = dict_delete(pq_edge,dict_min(pq_edge),NULL);d759 1a759 1 dict_insert(pq_edge,e);d926 1a926 1 pq_edge_t *next_edge = dict_delete(pq_edge,dict_min(pq_edge),NULL);d956 1a956 1 dict_insert(pq_edge,next_edge);@1.129log@Now it ctangles and compiles with no warnings. (But you ask, does it work?)@text@d8 3d127 1a127 1const char *construct_rcs_id = "$Id: construct.w,v 1.128 1998/04/11 17:39:52 neto Exp neto $";d530 1a604 4@@@@<Build the data structures for Greedy, non-Euclidean case@@>=nn_work = new_arr_of(pq_edge_t,n);@1.128log@Possibly finished coding the non-Euclidean changes.But company is coming, and I must go.@text@d8 4d124 1a124 1const char *construct_rcs_id = "$Id: construct.w,v 1.127 1998/04/11 17:24:40 neto Exp neto $";d321 1a321 1 @@<Build the data structures for Greedy@@>@@;d377 1a377 1dict_t *pq_edge = dict_create(cmp_pq_edge,NULL);d440 1a440 1@@<Build the data structures for Greedy@@>=d468 1a468 1int *farthest_in_queue;d489 1a489 1int *unsaturated, num_unsaturated, *inv_unsaturated;d563 2a564 1 int i,w,num_chosen, farthest_city;d592 6a597 1@@ Array |nn_work| is just an array of at most |n| edges.d599 1a599 1pq_edge_t *nn_work;d883 1d886 1a886 1 @@<Build the data structures for Greedy@@>@@;@1.127log@Added much queue handling for the non-Euclidean case, including refreshingthe list, maintaining the unsaturated cities, and the farthest in queuearray.@text@d8 5d120 1a120 1const char *construct_rcs_id = "$Id: construct.w,v 1.126 1998/04/11 15:43:28 neto Exp neto $";d570 1a570 1 erroif(num_chosen==0,"Bug!");d682 4a685 2 if ( adj[y].count == 2) E2_hide(y); if ( adj[x].count == 2) E2_hide(x);d694 1a694 1E2_unhide_all();d916 4a919 2 E2_hide(u); E2_hide(v);d924 1a924 1E2_unhide_all();d929 4d934 11a944 3next_edge->other_end = E2_nn(u);next_edge->len = cost(u,next_edge->other_end);dict_insert(pq_edge,next_edge);@1.126log@Convert nn link to nn link pool because non-Euclidean case is betterwith multiple possible neighbours in the queue. Still convertingto allow non-Euclidean.@text@d8 5d115 1a115 1const char *construct_rcs_id = "$Id: construct.w,v 1.125 1998/01/23 20:09:22 neto Exp neto $";d443 1a443 5 int x; @@<Initialize the |unsaturated| list@@>@@; for (x=0;x<n;x++) { @@<Get fresh neighbours for |x|@@>@@; }d451 145d673 2a674 1 @@<Find a shortest valid edge $(x,y)$@@>@@;d704 1a704 3@@<Find a shortest valid edge $(x,y)$@@>={pq_edge_t *e;a715 1}d736 1d738 2a739 2 const int i=x; @@<Get fresh neighbours for |x|@@>@@;@1.125log@The mate structure doesn't need to be initialized before it is initialized!@text@d8 3d110 1a110 1const char *construct_rcs_id = "$Id: construct.w,v 1.124 1998/01/23 19:30:56 neto Exp neto $";d288 12d304 1d337 1d341 4a344 2@@ The array of edges in the priority queue is called |nn_link|, and is allocatedat the same time that it is declared. Bentley says ``|nn_link[i]| containsd348 7a354 1inserted into the tour.''d356 1a356 1The entries of |nn_link| serve as the domain over whichd362 1a362 1pq_edge_t *nn_link = the_nn_link = new_arr_of(pq_edge_t,n);d367 1a367 1free_mem(nn_link);d370 2a371 2@@ We need the interface to both the memory allocation routines and to thedictionary.d376 1d414 2a415 1in the |nn_link| array, and then inserting that entry into the priority queue.d422 1d427 2a428 2errorif(!E2_supports(tsp_instance),"Finish the code, David. ");{@@+ int i;d430 5a434 3 nn_link[i].other_end = E2_nn(i); nn_link[i].len = cost(i,nn_link[i].other_end); dict_insert(pq_edge,nn_link+i);d437 6d558 2d561 3a563 3 pq_edge_t *px= dict_delete(pq_edge,dict_min(pq_edge),NULL); errorif(px==NULL,"Exhausted the priority queue of links."); x = px-nn_link; y=px->other_end;d570 9a578 1len += nn_link[x].len;d580 2a581 1@@ In finding a new candidate link for |x|, we must first hide thed583 1d585 1d587 2a588 2 nn_link[x].other_end = E2_nn(x); nn_link[x].len = cost(x,nn_link[x].other_end);d590 7a596 1 dict_insert(pq_edge,nn_link+x);d614 1a614 3pq_edge_t *the_nn_link;int count;void nn_link_prn(void *p);d618 1a618 2 printf(" %d,%d ",np-the_nn_link,np->other_end); count++;d758 1a758 1 u = next_edge - nn_link;d776 3a778 3nn_link[u].other_end = E2_nn(u);nn_link[u].len = cost(u,nn_link[u].other_end);dict_insert(pq_edge,nn_link+u);@1.124log@Fixed to compile. Also match construct is now more appropriatelyconstruct matching.@text@d8 4d107 1a107 1const char *construct_rcs_id = "$Id: construct.w,v 1.123 1997/12/20 22:33:02 neto Exp neto $";d597 1a597 1 errorif(mate==NULL || mate[0]==-1,@1.123log@Fixed another preprocessor booboo.@text@d8 3d103 1a103 1const char *construct_rcs_id = "$Id: construct.w,v 1.122 1997/12/20 22:29:52 neto Exp neto $";d405 1a405 1@@ We need the interface to $k$-d trees.d407 1d567 1a567 1Procedure |match_construct| builds a weighted perfect matching.d587 1a587 1match_construct(const int n, int *mate, int alg,long alg_param) d593 1a593 1 errorif(match==NULL || match[0]==-1,d603 5d649 2@1.122log@Fixed preprocessor booboo.@text@d8 3d100 1a100 1const char *construct_rcs_id = "$Id: construct.w,v 1.121 1997/12/20 22:23:59 neto Exp neto $";d659 1a659 1#endif@1.121log@First crack at greedy matching heuristic.@text@d8 3d97 1a97 1const char *construct_rcs_id = "$Id: construct.w,v 1.120 1997/09/27 18:05:36 neto Exp neto $";d419 1a419 1#if DO_TOURd425 1a425 1#if DO_TOURd431 1a431 1#if DO_TOURd448 1a448 1#if DO_TOURd454 1a454 1#if DO_TOURd460 1a460 1#if DO_TOURd665 1a665 1#if DO_MATCHING@1.120log@Fixed RCS log behaviour.@text@d8 3d94 1a94 1const char *construct_rcs_id = "$Id: construct.w,v 1.119 1997/08/15 20:18:25 neto Exp neto $";d276 1d281 1d382 3d386 1d416 1d418 1d422 1d424 1d428 1d430 1d445 1d447 1d451 3a453 1free_mem(tail);d457 1d459 1d554 152@1.119log@Added Index major section.@text@d6 5a10 1{\obeylines$Log: construct.w,v $d91 1a91 1const char *construct_rcs_id = "$Id: construct.w,v 1.118 1997/06/17 21:23:52 net
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -