Misplaced Pages

Barnes–Hut simulation

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

The Barnes–Hut simulation (named after Josh Barnes and Piet Hut ) is an approximation algorithm for performing an N-body simulation . It is notable for having order O( n  log  n ) compared to a direct-sum algorithm which would be O( n ).

#762237

33-443: The simulation volume is usually divided up into cubic cells via an octree (in a three-dimensional space), so that only particles from nearby cells need to be treated individually, and particles in distant cells can be treated as a single large particle centered at the cell's center of mass (or as a low-order multipole expansion ). This can dramatically reduce the number of particle pair interactions that must be computed. Some of

66-433: A branching factor greater than one, iterative deepening increases the running time by only a constant factor over the case in which the correct depth limit is known due to the geometric growth of the number of nodes per level. DFS may also be used to collect a sample of graph nodes. However, incomplete DFS, similarly to incomplete BFS , is biased towards nodes of high degree . For the following graph: [REDACTED]

99-587: A topological sorting of any directed acyclic graph . This ordering is also useful in control-flow analysis as it often represents a natural linearization of the control flows. The graph above might represent the flow of control in the code fragment below, and it is natural to consider this code in the order A B C D or A C B D but not natural to use the order A B D C or A C D B. A recursive implementation of DFS: A non-recursive implementation of DFS with worst-case space complexity O ( | E | ) {\displaystyle O(|E|)} , with

132-400: A building block include: The computational complexity of DFS was investigated by John Reif . More precisely, given a graph G {\displaystyle G} , let O = ( v 1 , … , v n ) {\displaystyle O=(v_{1},\dots ,v_{n})} be the ordering computed by the standard recursive DFS algorithm. This ordering

165-433: A depth-first search starting at the node A, assuming that the left edges in the shown graph are chosen before right edges, and assuming the search remembers previously visited nodes and will not repeat them (since this is a small graph), will visit the nodes in the following order: A, B, D, F, E, C, G. The edges traversed in this search form a Trémaux tree , a structure with important applications in graph theory . Performing

198-436: A result, is much smaller than the space needed for searching to the same depth using breadth-first search. For such applications, DFS also lends itself much better to heuristic methods for choosing a likely-looking branch. When an appropriate depth limit is not known a priori, iterative deepening depth-first search applies DFS repeatedly with a sequence of increasing limits. In the artificial intelligence mode of analysis, with

231-435: Is a tree data structure in which each internal node has exactly eight children . Octrees are most often used to partition a three-dimensional space by recursively subdividing it into eight octants . Octrees are the three-dimensional analog of quadtrees . The word is derived from oct (Greek root meaning "eight") + tree . Octrees are often used in 3D graphics and 3D game engines . Each node in an octree subdivides

264-425: Is called the lexicographic depth-first search ordering. John Reif considered the complexity of computing the lexicographic depth-first search ordering, given a graph and a source. A decision version of the problem (testing whether some vertex u occurs before some vertex v in this order) is P -complete , meaning that it is "a nightmare for parallel processing ". A depth-first search ordering (not necessarily

297-477: Is either A B D B A C A or A C D C A B A (choosing to first visit B or C from A is up to the algorithm). Note that repeat visits in the form of backtracking to a node, to check if it has still unvisited neighbors, are included here (even if it is found to have none). Thus the possible preorderings are A B D C and A C D B, while the possible postorderings are D B C A and D C B A, and the possible reverse postorderings are A C B D and A B C D. Reverse postordering produces

330-406: Is respectively the center of mass and total mass of the internal node. If the internal node is sufficiently close to the body, the process is repeated for each of its children. Whether a node is or isn't sufficiently far away from a body, depends on the quotient s / d {\displaystyle s/d} , where s is the width of the region represented by the internal node, and d

363-536: Is stopped when a given exit condition is met. Examples of such exit conditions (shown in code below) are: Taking the full list of colors of a 24-bit RGB image as point input to the Octree point decomposition implementation outlined above, the following example show the results of octree color quantization. The first image is the original (532818 distinct colors), while the second is the quantized image (184 distinct colors) using octree decomposition, with each pixel assigned

SECTION 10

#1732783796763

396-443: Is the distance between the body and the node's center of mass. The node is sufficiently far away when this ratio is smaller than a threshold value θ . The parameter θ determines the accuracy of the simulation; larger values of θ increase the speed of the simulation but decreases its accuracy. If θ = 0, no internal node is treated as a single body and the algorithm degenerates to a direct-sum algorithm. Octree An octree

429-405: Is typically used to traverse an entire graph, and takes time O ( | V | + | E | ) {\displaystyle O(|V|+|E|)} , where | V | {\displaystyle |V|} is the number of vertices and | E | {\displaystyle |E|} the number of edges . This is linear in the size of

462-479: The RGB system. The node index to branch out from at the top level is determined by a formula that uses the most significant bits of the red, green, and blue color components, e.g. 4r + 2g + b. The next lower level uses the next bit significance, and so on. Less significant bits are sometimes ignored to reduce the tree size. The algorithm is highly memory efficient because the tree's size can be limited. The bottom level of

495-445: The color at the center of the octree bin in which it falls. Alternatively, final colors could be chosen at the centroid of all colors in each octree bin, however this added computation has very little effect on the visual result. Depth-first search Depth-first search ( DFS ) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as

528-521: The different properties of the vertex orderings the two algorithms produce. For applications of DFS in relation to specific domains, such as searching for solutions in artificial intelligence or web-crawling, the graph to be traversed is often either too large to visit in its entirety or infinite (DFS may suffer from non-termination ). In such cases, search is only performed to a limited depth ; due to limited resources, such as memory or disk space, one typically does not use data structures to keep track of

561-453: The graph. In these applications it also uses space O ( | V | ) {\displaystyle O(|V|)} in the worst case to store the stack of vertices on the current search path as well as the set of already-visited vertices. Thus, in this setting, the time and space bounds are the same as for breadth-first search and the choice of which of these two algorithms to use depends less on their complexity and more on

594-404: The group of bodies beneath it, and stores the center of mass and the total mass of all its children bodies. To calculate the net force on a particular body, the nodes of the tree are traversed, starting from the root. If the center of mass of an internal node is sufficiently far from the body, the bodies contained in that part of the tree are treated as a single particle whose position and mass

627-503: The most demanding high-performance computing projects perform computational astrophysics using the Barnes–Hut treecode algorithm, such as DEGIMA . In a three-dimensional N -body simulation , the Barnes–Hut algorithm recursively divides the n bodies into groups by storing them in an octree (or a quad-tree in a 2D simulation). Each node in this tree represents a region of the three-dimensional space. The topmost node represents

660-428: The nodes from the example graph in the following order: A, B, D, F, E, C, G. The non-recursive implementation will visit the nodes as: A, E, F, B, D, C, G. The non-recursive implementation is similar to breadth-first search but differs from it in two ways: If G is a tree , replacing the queue of the breadth-first search algorithm with a stack will yield a depth-first search algorithm. For general graphs, replacing

693-408: The octree consists of leaf nodes that accrue color data not represented in the tree; these nodes initially contain single bits. If much more than the desired number of palette colors are entered into the octree, its size can be continually reduced by seeking out a bottom-level node and averaging its bit data up into a leaf node, pruning part of the tree. Once sampling is complete, exploring all routes in

SECTION 20

#1732783796763

726-414: The original graph is undirected then all of its edges are tree edges or back edges. It is also possible to use depth-first search to linearly order the vertices of a graph or tree. There are four possible ways of doing this: For binary trees there is additionally in-ordering and reverse in-ordering . For example, when searching the directed graph below beginning at node A, the sequence of traversals

759-413: The possibility of duplicate vertices on the stack: These two variations of DFS visit the neighbors of each vertex in the opposite order from each other: the first neighbor of v visited by the recursive variation is the first one in the list of adjacent edges, while in the iterative variation the first visited neighbor is the last one in the list of adjacent edges. The recursive implementation will visit

792-542: The root node in the case of a graph) and explores as far as possible along each branch before backtracking. Extra memory, usually a stack , is needed to keep track of the nodes discovered so far along a specified branch which helps in backtracking of the graph. A version of depth-first search was investigated in the 19th century by French mathematician Charles Pierre Trémaux as a strategy for solving mazes . The time and space analysis of DFS differs according to its application area. In theoretical computer science, DFS

825-462: The root node of an MX octree must represent a finite bounded space so that the implicit centers are well-defined. Note that octrees are not the same as k -d trees : k -d trees split along a dimension and octrees split around a point. Also k -d trees are always binary, which is not the case for octrees. By using a depth-first search the nodes are to be traversed and only required surfaces are to be viewed. The use of octrees for 3D computer graphics

858-415: The same search without remembering previously visited nodes results in visiting the nodes in the order A, B, D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E cycle and never reaching C or G. Iterative deepening is one technique to avoid this infinite loop and would reach all nodes. The result of a depth-first search of a graph can be conveniently described in terms of a spanning tree of

891-409: The set of all previously visited vertices. When search is performed to a limited depth, the time is still linear in terms of the number of expanded vertices and edges (although this number is not the same as the size of the entire graph because some vertices may be searched more than once and others not at all) but the space complexity of this variant of DFS is only proportional to the depth limit, and as

924-427: The space it represents into eight octants . In a point region (PR) octree, the node stores an explicit three-dimensional point , which is the "center" of the subdivision for that node; the point defines one of the corners for each of the eight children. In a matrix-based (MX) octree, the subdivision point is implicitly the center of the space the node represents. The root node of a PR octree can represent infinite space;

957-417: The stack of the iterative depth-first search implementation with a queue would also produce a breadth-first search algorithm, although a somewhat nonstandard one. Another possible implementation of iterative depth-first search uses a stack of iterators of the list of neighbors of a node, instead of a stack of nodes. This yields the same traversal as recursive DFS. Algorithms that use depth-first search as

990-406: The tree down to the leaf nodes, taking note of the bits along the way, will yield approximately the required number of colors. The example recursive algorithm outline below ( MATLAB syntax) decomposes an array of 3-dimensional points into octree style bins. The implementation begins with a single bin surrounding all given points, which then recursively subdivides into its 8 octree regions. Recursion

1023-436: The vertices reached during the search. Based on this spanning tree, the edges of the original graph can be divided into three classes: forward edges , which point from a node of the tree to one of its descendants, back edges , which point from a node to one of its ancestors, and cross edges , which do neither. Sometimes tree edges , edges which belong to the spanning tree itself, are classified separately from forward edges. If

Barnes–Hut simulation - Misplaced Pages Continue

1056-423: The whole space, and its eight children represent the eight octants of the space. The space is recursively subdivided into octants until each subdivision contains 0 or 1 bodies (some regions do not have bodies in all of their octants). There are two types of nodes in the octree: internal and external nodes. An external node has no children and is either empty or represents a single body. Each internal node represents

1089-720: Was pioneered by Donald Meagher at Rensselaer Polytechnic Institute , described in a 1980 report "Octree Encoding: A New Technique for the Representation, Manipulation and Display of Arbitrary 3-D Objects by Computer", for which he holds a 1995 patent (with a 1984 priority date ) "High-speed image generation of complex solid objects using octree encoding" The octree color quantization algorithm, invented by Gervautz and Purgathofer in 1988, encodes image color data as an octree up to nine levels deep. Octrees are used because 2 3 = 8 {\displaystyle 2^{3}=8} and there are three color components in

#762237