SlideShare a Scribd company logo
1 of 18
Barnes-Hut Algorithm (TREECODE)
The Barnes–Hut simulation (Josh Barnes and Piet Hut) is an 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(n2).
Berkembang dan sudah banyak digunakan dikasus N-body dan SPH
karena bisa di paralelisasi.
NOTE
The crucial idea in speeding up the brute force algorithm is to group nearby bodies and
approximate them as a single body. If the group is sufficiently far away, we can
approximate its gravitational effects by using its center of mass.
If two bodies have positions (x1 , y1) and (x2, y2), and masses m1 and m2, center of mass (x,
y) are given by:
m = m1 + m2
x = (x1.m1 + x2.m2) / m
y = (y1.m1 + y2.m2) / m
It recursively divides the set of bodies into
groups by storing them in a quad-tree (2D) or
Octree (3D),
recursively subdivided into quadrants until
each subdivision contains 0 or 1 bodies
To calculate the net force on a particular body, traverse the nodes of the tree, starting
from the root. If the center-of-mass of an internal node is sufficiently far from the body,
approximate the bodies contained in that part of the tree as a single body, whose
position is the group’s center of mass and whose mass is the group’s total mass. The
algorithm is fast because we don’t need to individually examine any of the bodies in the
group.
If the internal node is not sufficiently far from the body, recursively traverse each of its
subtrees.
To determine if a node is sufficiently far away, compute the quotient s / d,
s = width of the region represented by the internal node,
d = the distance between the body and the node’s center-of-mass.
Then, compare this ratio against a threshold value θ.
If s / d < θ, then the internal node is sufficiently far away.
By adjusting the θ parameter, we can change the speed and accuracy of the
simulation. We will always use θ = 0.5, a value commonly used in practice. Note that if
θ = 0, then no internal node is treated as a single body, and the algorithm degenerates
to brute force.
CONSTRUCTING THE BARNES-HUT TREE
To construct the Barnes-Hut tree, insert the bodies one after another. To insert a body
b into the tree rooted at node x, use the following recursive procedure:
1. If node x does not contain a body, put the new body b here.
2. If node x is an internal node, update the center-of-mass and total mass of x.
Recursively insert the body b in the appropriate quadrant.
3. If node x is an external node, say containing a body named c, then there are two
bodies b and c in the same region. Subdivide the region further by creating four
children. Then, recursively insert both b and c into the appropriate quadrant(s). Since
b and c may still end up in the same quadrant, there may be several subdivisions
during a single insertion. Finally, update the center-of-mass and total mass of x.
Example:
Every internal node contain the center of mass and total mass of their
bodies.
CALCULATING THE FORCE ACTING ON A BODY
To calculate the net force acting on body b, use the following recursive procedure,
starting with the root of the quad-tree:
1. If the current node is an external node (and it is not body b), calculate the force
exerted by the current node on b, and add this amount to b’s net force.
2. Otherwise, calculate the ratio s/d. If s/d < θ, treat this internal node as a single
body, and calculate the force it exerts on body b, and add this amount to b’s net
force.
3. Otherwise, run the procedure recursively on each of the current node’s children.
As an example, to calculate the net force acting on body a, we start at the root node,
which is an internal node. It represents the center-of-mass of the six bodies a, b, c, d,
e, and f, which have masses 1, 2, 3, 4, 5, and 6 kg, respectively.
Jika A terlalu jauh dari pusat massa sistem total (kriteria opening
angle terpenuhi) A akan di anggap terkena “gaya” dari sistem itu
sendiri (tidak perlu lagi menghitung per node internal).
Calculate Quadrapole moment (for b, c and d) if
it is included.
Referensi:
http://www.cs.princeton.edu/courses/archive/fall03/cs126/assignments/barnes-hut.html
http://arborjs.org/docs/barnes-hut
http://www.ifa.hawaii.edu/~barnes/treecode/treeguide.html
http://en.wikipedia.org/wiki/Barnes%E2%80%93Hut_simulation

More Related Content

What's hot

Post_Number Systems_7
Post_Number Systems_7Post_Number Systems_7
Post_Number Systems_7
Marc King
 
Vector fields and equipotentials
Vector fields and equipotentialsVector fields and equipotentials
Vector fields and equipotentials
Tarun Gehlot
 
Quantum Computation and the Stabilizer Formalism for Error Correction
Quantum Computation and the Stabilizer Formalism for Error CorrectionQuantum Computation and the Stabilizer Formalism for Error Correction
Quantum Computation and the Stabilizer Formalism for Error Correction
Daniel Bulhosa Solórzano
 
Scribe Post 6
Scribe Post 6Scribe Post 6
Scribe Post 6
MrSiwWy
 

What's hot (20)

Post_Number Systems_7
Post_Number Systems_7Post_Number Systems_7
Post_Number Systems_7
 
Physics Assignment Help
Physics Assignment Help Physics Assignment Help
Physics Assignment Help
 
Vector fields and equipotentials
Vector fields and equipotentialsVector fields and equipotentials
Vector fields and equipotentials
 
Calc 4.4b
Calc 4.4bCalc 4.4b
Calc 4.4b
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
 
George Green's Contribution to MRI, Roger Bowley, 21 October 2014
George Green's Contribution to MRI, Roger Bowley, 21 October 2014George Green's Contribution to MRI, Roger Bowley, 21 October 2014
George Green's Contribution to MRI, Roger Bowley, 21 October 2014
 
Faktorisasi aljabar kelas 7
Faktorisasi aljabar kelas 7Faktorisasi aljabar kelas 7
Faktorisasi aljabar kelas 7
 
NAM_final2015
NAM_final2015NAM_final2015
NAM_final2015
 
Teorema de Green-Tao
Teorema de Green-TaoTeorema de Green-Tao
Teorema de Green-Tao
 
Mechanical Engineering Assignment Help
Mechanical Engineering Assignment HelpMechanical Engineering Assignment Help
Mechanical Engineering Assignment Help
 
17 prims-kruskals (1)
17 prims-kruskals (1)17 prims-kruskals (1)
17 prims-kruskals (1)
 
Lar calc10 ch02_sec4
Lar calc10 ch02_sec4Lar calc10 ch02_sec4
Lar calc10 ch02_sec4
 
Binping xiao superconducting surface impedance under radiofrequency field
Binping xiao   superconducting surface impedance under radiofrequency fieldBinping xiao   superconducting surface impedance under radiofrequency field
Binping xiao superconducting surface impedance under radiofrequency field
 
Ewald summation
Ewald summationEwald summation
Ewald summation
 
Serie de dyson
Serie de dysonSerie de dyson
Serie de dyson
 
Quantum Computation and the Stabilizer Formalism for Error Correction
Quantum Computation and the Stabilizer Formalism for Error CorrectionQuantum Computation and the Stabilizer Formalism for Error Correction
Quantum Computation and the Stabilizer Formalism for Error Correction
 
Gauge theory field
Gauge theory fieldGauge theory field
Gauge theory field
 
presentation_chalmers
presentation_chalmerspresentation_chalmers
presentation_chalmers
 
Presentation gauge field theory
Presentation gauge field theoryPresentation gauge field theory
Presentation gauge field theory
 
Scribe Post 6
Scribe Post 6Scribe Post 6
Scribe Post 6
 

Viewers also liked (8)

Binary-Search-Tree (BST) - Inserting a node into a BST
Binary-Search-Tree (BST) - Inserting a node into a BSTBinary-Search-Tree (BST) - Inserting a node into a BST
Binary-Search-Tree (BST) - Inserting a node into a BST
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
 
Trie Data Structure
Trie Data StructureTrie Data Structure
Trie Data Structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 

Similar to Tree Algorithm

11 - 3 Experiment 11 Simple Harmonic Motio.docx
11  -  3       Experiment 11 Simple Harmonic Motio.docx11  -  3       Experiment 11 Simple Harmonic Motio.docx
11 - 3 Experiment 11 Simple Harmonic Motio.docx
tarifarmarie
 
centroid and centre of gravity...
centroid and centre of gravity...centroid and centre of gravity...
centroid and centre of gravity...
Mihir Dixit
 
1VectorAdditionofForcesObjectivesTousethe.docx
 1VectorAdditionofForcesObjectivesTousethe.docx 1VectorAdditionofForcesObjectivesTousethe.docx
1VectorAdditionofForcesObjectivesTousethe.docx
ShiraPrater50
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
chidabdu
 

Similar to Tree Algorithm (20)

Finite element method
Finite element methodFinite element method
Finite element method
 
DOMV No 4 PHYSICAL DYNAMIC MODEL TYPES (1).pdf
DOMV No 4  PHYSICAL DYNAMIC MODEL TYPES (1).pdfDOMV No 4  PHYSICAL DYNAMIC MODEL TYPES (1).pdf
DOMV No 4 PHYSICAL DYNAMIC MODEL TYPES (1).pdf
 
Algorithm Exam Help
Algorithm Exam HelpAlgorithm Exam Help
Algorithm Exam Help
 
Enhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial DatasetEnhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial Dataset
 
Physical Chemistry Homework Help
Physical Chemistry Homework HelpPhysical Chemistry Homework Help
Physical Chemistry Homework Help
 
physics430_lecture06. center of mass, angular momentum
physics430_lecture06. center of mass, angular momentumphysics430_lecture06. center of mass, angular momentum
physics430_lecture06. center of mass, angular momentum
 
Optimization Of Fuzzy Bexa Using Nm
Optimization Of Fuzzy Bexa Using NmOptimization Of Fuzzy Bexa Using Nm
Optimization Of Fuzzy Bexa Using Nm
 
Free Ebooks Download ! Edhole.com
Free Ebooks Download ! Edhole.comFree Ebooks Download ! Edhole.com
Free Ebooks Download ! Edhole.com
 
Project report on Data Clustering
Project report on Data ClusteringProject report on Data Clustering
Project report on Data Clustering
 
Review
ReviewReview
Review
 
11 - 3 Experiment 11 Simple Harmonic Motio.docx
11  -  3       Experiment 11 Simple Harmonic Motio.docx11  -  3       Experiment 11 Simple Harmonic Motio.docx
11 - 3 Experiment 11 Simple Harmonic Motio.docx
 
Lec10
Lec10Lec10
Lec10
 
20120140503023
2012014050302320120140503023
20120140503023
 
centroid and centre of gravity...
centroid and centre of gravity...centroid and centre of gravity...
centroid and centre of gravity...
 
ROOT OF NON-LINEAR EQUATIONS
ROOT OF NON-LINEAR EQUATIONSROOT OF NON-LINEAR EQUATIONS
ROOT OF NON-LINEAR EQUATIONS
 
1VectorAdditionofForcesObjectivesTousethe.docx
 1VectorAdditionofForcesObjectivesTousethe.docx 1VectorAdditionofForcesObjectivesTousethe.docx
1VectorAdditionofForcesObjectivesTousethe.docx
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Boundness of a neural network weights using the notion of a limit of a sequence
Boundness of a neural network weights using the notion of a limit of a sequenceBoundness of a neural network weights using the notion of a limit of a sequence
Boundness of a neural network weights using the notion of a limit of a sequence
 
JMVA_Paper_Shibasish
JMVA_Paper_ShibasishJMVA_Paper_Shibasish
JMVA_Paper_Shibasish
 
DiffEqPresent.ppt
DiffEqPresent.pptDiffEqPresent.ppt
DiffEqPresent.ppt
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Tree Algorithm

  • 1. Barnes-Hut Algorithm (TREECODE) The Barnes–Hut simulation (Josh Barnes and Piet Hut) is an 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(n2). Berkembang dan sudah banyak digunakan dikasus N-body dan SPH karena bisa di paralelisasi. NOTE
  • 2. The crucial idea in speeding up the brute force algorithm is to group nearby bodies and approximate them as a single body. If the group is sufficiently far away, we can approximate its gravitational effects by using its center of mass. If two bodies have positions (x1 , y1) and (x2, y2), and masses m1 and m2, center of mass (x, y) are given by: m = m1 + m2 x = (x1.m1 + x2.m2) / m y = (y1.m1 + y2.m2) / m It recursively divides the set of bodies into groups by storing them in a quad-tree (2D) or Octree (3D), recursively subdivided into quadrants until each subdivision contains 0 or 1 bodies
  • 3. To calculate the net force on a particular body, traverse the nodes of the tree, starting from the root. If the center-of-mass of an internal node is sufficiently far from the body, approximate the bodies contained in that part of the tree as a single body, whose position is the group’s center of mass and whose mass is the group’s total mass. The algorithm is fast because we don’t need to individually examine any of the bodies in the group. If the internal node is not sufficiently far from the body, recursively traverse each of its subtrees. To determine if a node is sufficiently far away, compute the quotient s / d, s = width of the region represented by the internal node, d = the distance between the body and the node’s center-of-mass. Then, compare this ratio against a threshold value θ. If s / d < θ, then the internal node is sufficiently far away. By adjusting the θ parameter, we can change the speed and accuracy of the simulation. We will always use θ = 0.5, a value commonly used in practice. Note that if θ = 0, then no internal node is treated as a single body, and the algorithm degenerates to brute force.
  • 4. CONSTRUCTING THE BARNES-HUT TREE To construct the Barnes-Hut tree, insert the bodies one after another. To insert a body b into the tree rooted at node x, use the following recursive procedure: 1. If node x does not contain a body, put the new body b here. 2. If node x is an internal node, update the center-of-mass and total mass of x. Recursively insert the body b in the appropriate quadrant. 3. If node x is an external node, say containing a body named c, then there are two bodies b and c in the same region. Subdivide the region further by creating four children. Then, recursively insert both b and c into the appropriate quadrant(s). Since b and c may still end up in the same quadrant, there may be several subdivisions during a single insertion. Finally, update the center-of-mass and total mass of x.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. Every internal node contain the center of mass and total mass of their bodies.
  • 11. CALCULATING THE FORCE ACTING ON A BODY To calculate the net force acting on body b, use the following recursive procedure, starting with the root of the quad-tree: 1. If the current node is an external node (and it is not body b), calculate the force exerted by the current node on b, and add this amount to b’s net force. 2. Otherwise, calculate the ratio s/d. If s/d < θ, treat this internal node as a single body, and calculate the force it exerts on body b, and add this amount to b’s net force. 3. Otherwise, run the procedure recursively on each of the current node’s children.
  • 12. As an example, to calculate the net force acting on body a, we start at the root node, which is an internal node. It represents the center-of-mass of the six bodies a, b, c, d, e, and f, which have masses 1, 2, 3, 4, 5, and 6 kg, respectively.
  • 13. Jika A terlalu jauh dari pusat massa sistem total (kriteria opening angle terpenuhi) A akan di anggap terkena “gaya” dari sistem itu sendiri (tidak perlu lagi menghitung per node internal).
  • 14.
  • 15. Calculate Quadrapole moment (for b, c and d) if it is included.
  • 16.
  • 17.