comp.lang.ada
 help / color / mirror / Atom feed
From: jtg <jtg77@poczta.onet.pl>
Subject: Re: [OT] Hints for an algorithm - avoiding O(n^2)
Date: Wed, 17 Mar 2004 03:24:39 +0100
Date: 2004-03-17T03:24:39+01:00	[thread overview]
Message-ID: <c38cpo$ri5$1@korweta.task.gda.pl> (raw)
In-Reply-To: <5d6fdb61.0403120115.7c102e3c@posting.google.com>

Jano wrote:

> Sorry for the off-topic, but this is the only group I read frequently
> that I'm sure is packed with knowledgeable people.
> 
> The problem is a simple one: I'm planning to write a simulator of
> sideral-like objects using the Newton laws (precision is not a top
> priority). The obvious way is to compute the force interactions
> between each pair of objects, sum them all, and iterate over... but
> this is O(n^2). To be precise, there are n(n-1)/2 forces to be
> computed if I'm right (n the number of objects).
> 
> I was wondering if you know of another approach more efficient. A
> simple hint is enough, I'll look further as necessary once in the
> right path.
> 
> I'm having some thoughts about making a discrete division of the space
> and compute the potential field, but the idea is not mature enough...
> I'm not sure if the loss of precision will be too big, or if the
> results will not be similar in time (I'm planning to manage about
> 1000-2000 objects simultaneously).
> 

Two ideas

1. You can assign groups of objects. For every group you can calculate
its mass and the center of the mass. Then you calculate acceleration
between all the objects in the same group, and then acceleration
between all the groups. Of course you apply group acceleration to
every object in that group. For instance you can assign sqrt(N) groups
of sqrt(N) objects, and then you have
    O(sqrt(N)^2) + sqrt(N)*O(sqrt(N)^2) = O(N*sqrt(N))
But there are still some problems to solve for instance group
boundary can divide two masses which are close to each other.
You can use for instance more intelligent clustering algs,
skip group interaction between adjacent groups (calculating
object interactions instead) or even apply more levels
of complexity (objects, small groups, bigger groups etc.)

2. You are going to make a simulation, but you think only
about static calculation of gravity forces. Wrong! It is possible
to do the calculations between objects much less frequently,
while mantaining accuracy and fluent object movement, if you
use some tricks. For instance you can adaptively change
time step, making it small when needed (if two masses come very
close, you calculate their paths more precisely). This will protect
your simulation when two masses go very close. You can also
make some asynchronous calculations (not in time steps),
i.e. calculate gravitational pull for every object, in every time
  step just integrate it, and from time to time update it - more
frequently for close objects, less frequently for distant objects.
However it is memory intensive.

Connecting these two ideas would be VERY effective. Of course
you can calculate group interactions less frequently than
object-in-group interactions. And since you can treat each group
as single object, asynchronous calculations do not demand
much memory in this case.

If you find these ideas helpful, please e-mail me, I'm curious
about the results. :-)



      parent reply	other threads:[~2004-03-17  2:24 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-12  9:15 [OT] Hints for an algorithm - avoiding O(n^2) Jano
2004-03-12 11:06 ` Jean-Pierre Rosen
2004-03-12 12:53   ` Stuart Palin
2004-03-12 12:55   ` Stuart Palin
2004-03-12 13:48     ` Dmitry A. Kazakov
2004-03-17  1:16       ` jtg
2004-03-12 13:30   ` Björn Persson
2004-03-12 13:42     ` James Rogers
2004-03-12 14:29     ` Robert I. Eachus
2004-03-12 23:44       ` Björn Persson
2004-03-13 15:21         ` Robert I. Eachus
2004-03-14 17:27           ` Jano
2004-03-15  5:34             ` Robert I. Eachus
2004-03-14 17:27   ` Jano
2004-03-13  0:12 ` Wes Groleau
2004-03-17  2:24 ` jtg [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox