word_giant.ch
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:4k
源码类别:

通讯编程

开发平台:

Visual C++

  1. % This file is part of the Stanford GraphBase (c) Stanford University 1993
  2. It's a demonstration "change file", which modifies the demonstration program
  3. word_components. To use it on a UNIX system, say
  4.   ctangle word_components word_giant word_giant
  5.   make word_giant
  6.   word_giant
  7. and you should find a file word_giant.gb that contains a useful graph.
  8. (Try testing this graph with "miles_span -gword_giant.gb".)
  9. See queen_wrap.ch for comments on the general form of change files.
  10. @x replace the copyright notice by a change notice
  11. @i boilerplate.w %<< legal stuff: PLEASE READ IT BEFORE MAKING ANY CHANGES!
  12. @y
  13. letmaybe=iffalse % print only sections that change
  14. defprerequisite#1{} defprerequisites#1#2{} % disable boilerplate macros
  15. defbotofcontents{vskip 0pt plus 1filll parskip=0pt
  16.   This program was obtained by modifying {sc WORD_,COMPONENTS} in the
  17.   Stanford GraphBase.par
  18.   Only sections that have changed are listed here.par}
  19. @z
  20. @x change the program title
  21. deftitle{WORD_,COMPONENTS}
  22. @y
  23. deftitle{WORD_,GIANT}
  24. @z
  25. @x here we modify the introductory remarks of section 1
  26. @* Components. kern-.7pt
  27. This simple demonstration program computes the connected
  28. components of the GraphBase graph of five-letter words. It prints the
  29. words in order of decreasing weight, showing the number of edges,
  30. components, and isolated vertices present in the graph defined by the
  31. first $n$ words for all~$n$.
  32. @y
  33. @* Components.
  34. This simple demonstration program computes the largest connected
  35. component of the GraphBase graph of five-letter words, and saves it
  36. in file .{word_giant.gb}. It modifies the edge lengths so that
  37. alphabetic distances are used (as in the .{-a} option of {sc LADDERS}).
  38. @z
  39. @x include additional header files in section 1
  40. #include "gb_graph.h" /* the GraphBase data structures */
  41. @y
  42. #include "gb_graph.h" /* the GraphBase data structures */
  43. #include "gb_save.h" /* the |save_graph| routine */
  44. #include "gb_basic.h" /* the |induced| routine */
  45. @z
  46. @x changes to the code of section 1
  47.   printf("Component analysis of %sn",g->id);
  48.   for (v=g->vertices; v<g->vertices+g->n; v++) {
  49.     n++, printf("%4ld: %5ld %s",n,v->weight,v->name);
  50.     @<Add vertex |v| to the component structure, printing out any
  51.           components it joins@>;
  52.     printf("; c=%ld,i=%ld,m=%ldn", comp, isol, m);
  53.   }
  54.   @<Display all unusual components@>;
  55. @y we suppress printing
  56.   for (v=g->vertices; v<g->vertices+g->n; v++) {
  57.     n++;
  58.     @<Add vertex |v| to the component structure, and change the lengths
  59.           of edges that connect it to previous vertices@>;
  60.   }
  61.   @<Mark all vertices of the giant component@>;
  62.   save_graph(induced(g,"giant",0,0,0),"word_giant.gb");
  63. @z
  64. @x change to the code of section 2
  65. if (!a) printf("[1]"); /* indicate that this word is isolated */
  66. else {@+long c=0; /* the number of merge steps performed because of |v| */
  67.   for (; a; a=a->next) {@+register Vertex *u=a->tip;
  68.     m++;
  69.     @<Merge the components of |u| and |v|, if they differ@>;
  70.   }
  71.   printf(" in %s[%ld]", v->master->name, v->master->size);
  72.    /* show final component */
  73. }
  74. @y
  75. for (; a; a=a->next) {@+register Vertex *u=a->tip;
  76.   register int k=a->loc; /* where the words differ */
  77.   register char *p=v->name+k,*q=u->name+k;
  78.   if (*p<*q) a->len=(a-1)->len=*q-*p;
  79.   else a->len=(a-1)->len=*p-*q; /* alphabetic distance */
  80.   m++;
  81.   @<Merge the components of |u| and |v|, if they differ@>;
  82. }
  83. @z
  84. @x delete printing in section 4
  85.     if (c++>0) printf("%s %s[%ld]", (c==2? " with": ","), u->name, u->size);
  86. @y
  87. @z
  88. @x delete more printing in section 4
  89.     if (c++>0) printf("%s %s[%ld]", (c==2? " with": ","), w->name, w->size);
  90. @y
  91. @z
  92. @x replace section 5
  93. We consider all other components unusual, so we print them out when the
  94. other computation is done.
  95. @<Display all unusual components@>=
  96. printf(
  97.   "nThe following non-isolated words didn't join the giant component:n");
  98. for (v=g->vertices; v<g->vertices+g->n; v++)
  99.   if (v->master==v && v->size>1 && v->size+v->size<g->n) {@+register Vertex *u;
  100.      long c=1; /* count of number printed on current line */
  101.      printf("%s", v->name);
  102.      for (u=v->link; u!=v; u=u->link) {
  103.        if (c++==12) putchar('n'),c=1;
  104.        printf(" %s",u->name);
  105.      }
  106.      putchar('n');
  107.   }
  108. @y
  109. We set the |ind| field to 1 in the giant component, so that the
  110. |induced| routine will retain those vertices.
  111. @<Mark...@>=
  112. for (v=g->vertices; v<g->vertices+g->n; v++)
  113.   if (v->master->size+v->master->size<g->n) v->ind=0;
  114.   else v->ind=1;
  115. @z