⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gb_miles.w

📁 模拟器提供了一个简单易用的平台
💻 W
📖 第 1 页 / 共 2 页
字号:
% This file is part of the Stanford GraphBase (c) Stanford University 1993@i boilerplate.w %<< legal stuff: PLEASE READ IT BEFORE MAKING ANY CHANGES!@i gb_types.w\def\title{GB\_\,MILES}\prerequisites{GB\_\,GRAPH}{GB\_\,IO}@* Introduction. This GraphBase module contains the |miles| subroutine,which creates a family of undirected graphs based on highway mileage databetween North American cities. Examples of the use of this procedure can befound in the demo programs {\sc MILES\_\,SPAN} and {\sc GB\_\,PLANE}.@(gb_miles.h@>=extern Graph *miles();@ The subroutine call {\advance\thinmuskip 0mu plus 4mu|miles(n,north_weight,west_weight,pop_weight,max_distance,max_degree,seed)|}constructs a graph based on the information in \.{miles.dat}.Each vertex of the graph corresponds to one of the 128 cities whosename is alphabetically greater than or equal to `Ravenna, Ohio' inthe 1949 edition of Rand McNally {\char`\&} Company's {\sl Standard HighwayMileage Guide}. Edges between vertices are assigned lengths representingdistances between cities, in miles. In most cases these mileages comefrom the Rand McNally Guide, but several dozen entries needed to be changeddrastically because they were obviously too large or too small; in such casesan educated guess was made. Furthermore, about 5\% of the entries wereadjusted slightly in order toensure that all distances satisfy the ``triangle inequality'': Thegraph generated by |miles| has the property that thedistance from |u| to~|v| plus the distance from |v| to~|w| always exceedsor equals the distance from |u| to~|w|.The constructed graph will have $\min(n,128)$ vertices; the default value|n=128| is substituted if |n=0|. If |n| is lessthan 128, the |n| cities will be selected by assigning a weight toeach city and choosing the |n| with largest weight, using randomnumbers to break ties in case of equal weights. Weights are computedby the formula$$ |north_weight|\cdot|lat|+|west_weight|\cdot|lon|+|pop_weight|\cdot|pop|, $$where |lat| is latitude north of the equator, |lon| is longitudewest of Greenwich, and |pop| is the population in 1980. Both |lat| and |lon|are given in ``centidegrees'' (hundredths of degrees). For example,San Francisco has |lat=3778|, |lon=12242|, and |pop=678974|;this means that, before the recent earthquake, it was located at$37.78^\circ$ north latitude and $122.42^\circ$ west longitude, and that it had678,974 residents in the 1980 census. The weight parameters must satisfy$$ \vert|north_weight|\vert\le100{,}000,\quad   \vert|west_weight|\vert\le100{,}000,\quad   \vert|pop_weight|\vert\le100.$$The constructed graph will be ``complete''---that is, it will haveedges between every pair of vertices---unless special values are given tothe parameters|max_distance| or |max_degree|. If |max_distance!=0|, edges with morethan |max_distance| miles will not appear; if |max_degree!=0|, eachvertex will be limited to at most |max_degree| of its shortest edges.Vertices of the graph will appear in order of decreasing weight.The |seed| parameter defines the pseudo-random numbers used wherevera ``random'' choice between equal-weight vertices or equal-length edgesneeds to be made.@d MAX_N 128@(gb_miles.h@>=#define MAX_N 128 /* maximum and default number of cities */@ Examples: The call |miles(100,0,0,1,0,0,0)| will construct acomplete graph on 100 vertices, representing the 100 most populouscities in the database.  It turns out that San Diego, with apopulation of 875,538, is the winning city by this criterion, followedby San Antonio (population 786,023), San Francisco (678,974), andWashington D.C. (638,432).To get |n| cities in the western United States and Canada, you can say$|miles|(n,0,1,0,\ldots\,)$; to get |n| cities in the Northeast, use acall like $|miles|(n,1,-1,0,\ldots\,)$. A parameter setting like$(50,-500,0,1,\ldots\,)$ produces mostly Southern cities, except for afew large metropolises in the north.If you ask for |miles(n,a,b,c,0,1,0)|, you get an edge between cities ifand only if each city is the nearest to the other, among the |n| citiesselected. (The graph is always undirected: There is an arc from |u| to~|v|if and only if there's an arc of the same length from |v| to~|u|.)A random selection of cities can be obtained by calling|miles(n,0,0,0,m,d,s)|.  Different choices of the seed number |s| willproduce different selections, in a system-independent manner;identical results will be obtained on all computers when identicalparameters have been specified.  Equivalent experiments on algorithmsfor graph manipulation can therefore be performed by researchers indifferent parts of the world. Any value of |s| between 0 and$2^{31}-1$ is permissible.@ If the |miles| routine encounters a problem, it returns |NULL|(\.{NULL}), after putting a code number into the external variable|panic_code|. This code number identifies the type of failure.Otherwise |miles| returns a pointer to the newly created graph, whichwill be represented with the data structures explained in {\sc GB\_\,GRAPH}.(The external variable |panic_code| is itself defined in {\sc GB\_\,GRAPH}.)@d panic(c) @+{@+panic_code=c;@+gb_trouble_code=0;@+return NULL;@+}@ The \CEE/ file \.{gb\_miles.c} has the following overall shape:@p#include "gb_io.h" /* we will use the {\sc GB\_\,IO} routines for input */#include "gb_flip.h" /* we will use the {\sc GB\_\,FLIP} routines for random numbers */#include "gb_graph.h" /* we will use the {\sc GB\_\,GRAPH} data structures */#include "gb_sort.h" /* and the linksort routine */@h@#@<Type declarations@>@;@<Private variables@>@;@#Graph *miles(n,north_weight,west_weight,pop_weight,    max_distance,max_degree,seed)  unsigned long n; /* number of vertices desired */  long north_weight; /* coefficient of latitude in the weight function */  long west_weight; /* coefficient of longitude in the weight function */  long pop_weight; /* coefficient of population in the weight function */  unsigned long max_distance; /* maximum distance in an edge, if nonzero */  unsigned long max_degree;       /* maximum number of edges per vertex, if nonzero */  long seed; /* random number seed */{@+@<Local variables@>@;@#  gb_init_rand(seed);  @<Check that the parameters are valid@>;  @<Set up a graph with |n| vertices@>;  @<Read the data file \.{miles.dat} and compute city weights@>;  @<Determine the |n| cities to use in the graph@>;  @<Put the appropriate edges into the graph@>;  if (gb_trouble_code) {    gb_recycle(new_graph);    panic(alloc_fault); /* oops, we ran out of memory somewhere back there */  }  return new_graph;}@ @<Local var...@>=Graph *new_graph; /* the graph constructed by |miles| */register long j,k; /* all-purpose indices */@ @<Check that the parameters are valid@>=if (n==0 || n>MAX_N) n=MAX_N;if (max_degree==0 || max_degree>=n) max_degree=n-1;if (north_weight>100000 || west_weight>100000 || pop_weight>100 @| || north_weight<-100000 || west_weight<-100000 || pop_weight<-100)  panic(bad_specs); /* the magnitude of at least one weight is too big */@ @<Set up a graph with |n| vertices@>=new_graph=gb_new_graph(n);if (new_graph==NULL)  panic(no_room); /* out of memory before we're even started */sprintf(new_graph->id,"miles(%lu,%ld,%ld,%ld,%lu,%lu,%ld)",  n,north_weight,west_weight,pop_weight,max_distance,max_degree,seed);strcpy(new_graph->util_types,"ZZIIIIZZZZZZZZ");@* Vertices.  As we read in the data, we construct a list of nodes,each of which contains a city's name, latitude, longitude, population,and weight. These nodes conform to the specifications stipulated inthe {\sc GB\_\,SORT} module. After the list has been sorted by weight, thetop |n| entries will be the vertices of the new graph.@<Type decl...@>=typedef struct node_struct { /* records to be sorted by |gb_linksort| */  long key; /* the nonnegative sort key (weight plus $2^{30}$) */  struct node_struct *link; /* pointer to next record */  long kk; /* index of city in the original database */  long lat,lon,pop; /* latitude, longitude, population */  char name[30]; /* |"City Name, ST"| */} node;@ The constants defined here are taken from the specific data in \.{miles.dat},because this routine is not intended to be perfectly general.@<Private...@>=static long min_lat=2672, max_lat=5042, min_lon=7180, max_lon=12312, min_pop=2521, max_pop=875538; /* tight bounds on data entries */static node *node_block; /* array of nodes holding city info */static long *distance; /* array of distances */@ The data in \.{miles.dat} appears in 128 groups of lines, one for eachcity, in reverse alphabetical order. These groups have the general form$$\vcenter{\halign{\tt#\hfil\crCity Name, ST[lat,lon]pop\crd1 d2 d3 d4 d5 d6 ... (possibly several lines' worth)\cr}}$$where \.{City Name} is the name of the city (possibly including spaces);\.{ST} is the two-letter state code; \.{lat} and \.{lon} are latitudeand longitude in hundredths of degrees; \.{pop} is the population; andthe remaining numbers \.{d1}, \.{d2}, \dots\ are distances to thepreviously named cities in reverse order. Each distance is separatedfrom the previous item by either a blank space or a newline character.For example, the line$$\hbox{\tt San Francisco, CA[3778,12242]678974}$$specifies the data about San Francisco that was mentioned earlier.From the first few groups$$\vcenter{\halign{\tt#\hfil\crYoungstown, OH[4110,8065]115436\crYankton, SD[4288,9739]12011\cr966\cr

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -