Re: Calculating distances in O(1)



On Sat, 14 Jan 2006 00:16:23 -0500, "Chuck F. " <cbfalconer@xxxxxxxxx>
wrote:

>racygirl wrote:
>>
>> I was just wondering, is it possible to write a solution to the
>> following problem with the following criteria:
>>
>> 1. it must be as efficient as possible at run-time
>> 2. be in O(1)
>> 3. and be memory efficient
>>
>> The problem is:
>> Find the total distance between any two cities if:
>> Given
>> source city destination city distance
>> 0 1 5 mi.
>> 1 2 2 mi.
>> 2 3 7 mi.
>> 3 4 8 mi.
>>
>> etc... for N cities.
>> Example: distance(0, 4) = 5+2+7+8 = 22 The distance function is
>> being called millions and millions of times and the list of
>> cities and distances do not change after the program starts.

Create an array of distances from city 0. The distance from city A to
city B is the difference of their distances from city 0. For your
data the array looks like:

city 0 1 2 3 4
distance 0 5 7 14 22

Example: the distance from city 2 to city 4 is 22-7=15.

Something you might want to think about if this is homework (I imagine
it is) is how O(1) solutions have to work, vs how O(log n) solutions
work. For example, a binary tree is almost certainly the wrong
answer. Why is that?


Richard Harter, cri@xxxxxxxx
http://home.tiac.net/~cri, http://www.varinoma.com
The eternal revolution has its needs - its slogans to be chanted,
its demonstrations to be held, its children to eat.
.



Relevant Pages

  • Re: [C++] using arrays in a class
    ... you cannot use your Cities class in a GUI program. ... You do not allocate storage for your array. ... This has undefined behavior since you haven't initialized 'city'. ... Do not use literal constants. ...
    (comp.programming)
  • Re: Calculating distances in O(1)
    ... > Find the total distance between any two cities if: ... the distance between Atlanta to Norfolk would be 2770 ... look up the two positions and perform the distance calculation. ... be able to look up city names which can easily be done in Otime ...
    (comp.lang.c)
  • Re: OT What am I going to do with her?
    ... I don't have a problem with "popping in" for local folks, as long as they don't expect me to be at home waiting for them. ... I'd not be too happy with a close family member traveling a long distance and not giving me a bit of advance warning so I could at least plan to be home when they came. ... And I would not be happy with parents or siblings who traveled a distance to come to the city where I was living without giving me some advance notice, even if they were coming for some other purpose. ...
    (rec.crafts.textiles.quilting)
  • Re: Calculating distances
    ... One way to do this would be to store the precise latitude and longditude of each city, and then use great-circle trig. ... My method would be to produce a temporary table with distance field then use that as the criteria for selecting the report results. ... An Access program would be preferrable, ...
    (microsoft.public.access.reports)
  • Re: Calculating distances
    ... My table has an origin city and state and a destination city and state. ... starting point on a form and the distance to the destination city and state ... Allen Browne - Microsoft MVP. ... city (radius search). ...
    (microsoft.public.access.reports)