<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Coding Lad</title>
    <description>The latest articles on DEV Community by Coding Lad (@coding_lad_3f0f2ebabdfa9e).</description>
    <link>https://dev.to/coding_lad_3f0f2ebabdfa9e</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3277787%2Fadf261e4-095d-43db-8a17-0467b6692d60.png</url>
      <title>DEV Community: Coding Lad</title>
      <link>https://dev.to/coding_lad_3f0f2ebabdfa9e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coding_lad_3f0f2ebabdfa9e"/>
    <language>en</language>
    <item>
      <title>Monte Carlo Tree Search (MCTS) Tutorial – Complete Guide with Examples</title>
      <dc:creator>Coding Lad</dc:creator>
      <pubDate>Thu, 19 Jun 2025 21:19:45 +0000</pubDate>
      <link>https://dev.to/coding_lad_3f0f2ebabdfa9e/monte-carlo-tree-search-mcts-tutorial-complete-guide-with-examples-3l45</link>
      <guid>https://dev.to/coding_lad_3f0f2ebabdfa9e/monte-carlo-tree-search-mcts-tutorial-complete-guide-with-examples-3l45</guid>
      <description>&lt;h1&gt;
  
  
  Monte Carlo Tree Search (MCTS) Tutorial – Complete Guide with Examples
&lt;/h1&gt;

&lt;p&gt;Master Monte Carlo Tree Search (MCTS) with this beginner-friendly guide. Learn the theory, step-by-step algorithm, and practical examples used in AI and game-playing systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 What is MCTS?
&lt;/h2&gt;

&lt;p&gt;Monte Carlo Tree Search (MCTS) is a powerful decision-making algorithm used in games and simulations. It balances:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exploration&lt;/strong&gt;: trying new or unknown moves&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exploitation&lt;/strong&gt;: selecting moves that are known to work well&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ⚙️ Four Steps of MCTS
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Selection&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Traverse the tree from the root using the &lt;strong&gt;UCB1&lt;/strong&gt; formula to select the most promising child:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
UCB1(S\_i) = (t\_i / n\_i) + C \* sqrt(ln(N) / n\_i)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;t_i&lt;/code&gt;: total value of node &lt;code&gt;S_i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n_i&lt;/code&gt;: visit count of node &lt;code&gt;S_i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;N&lt;/code&gt;: total visits of the parent&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;C&lt;/code&gt;: exploration constant (usually √2 or 2)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Expansion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once a node is selected, generate one or more child nodes (if any unvisited children exist).&lt;/p&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Rollout (Simulation)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run a &lt;strong&gt;random simulation&lt;/strong&gt; (or using a default policy) from the newly expanded node until a &lt;strong&gt;terminal state&lt;/strong&gt; is reached.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. &lt;strong&gt;Backpropagation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Propagate the result of the simulation &lt;strong&gt;up the tree&lt;/strong&gt;, updating value and visit count for each node on the path.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;🔁 This 4-step process is repeated X times (number of simulations/iterations).&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧩 MCTS Example – Step-by-Step
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztccfxg8jo6qr13lq3yo.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztccfxg8jo6qr13lq3yo.webp" alt="Image description" width="800" height="832"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given a snapshot of an MCTS tree, we perform &lt;strong&gt;2 rollouts&lt;/strong&gt; with return values &lt;code&gt;15&lt;/code&gt; and &lt;code&gt;75&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;💡 Always pick the &lt;strong&gt;right node&lt;/strong&gt; when there's a tie/random choice.&lt;/p&gt;




&lt;h3&gt;
  
  
  ▶️ &lt;strong&gt;Rollout 1&lt;/strong&gt; (return value = 15)
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step 1: Selection
&lt;/h4&gt;

&lt;p&gt;Compute UCB1 for &lt;code&gt;S1&lt;/code&gt; and &lt;code&gt;S2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;S1: t=60, n=2
S2: t=18, n=1
Parent (S0): n=3, ln(3) ≈ 1.0986, C = 2

UCB1(S1) ≈ 30 + 2 * sqrt(1.0986/2) ≈ 31.48  
UCB1(S2) ≈ 18 + 2 * sqrt(1.0986/1) ≈ 20.09  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Choose S1&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Expansion
&lt;/h4&gt;

&lt;p&gt;S1 has an unvisited child &lt;code&gt;S3&lt;/code&gt;. We expand the tree by adding &lt;code&gt;S3&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Rollout
&lt;/h4&gt;

&lt;p&gt;Run a random simulation from &lt;code&gt;S3&lt;/code&gt; → terminal value = &lt;code&gt;15&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: Backpropagation
&lt;/h4&gt;

&lt;p&gt;Update values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
S3: t=15, n=1&lt;br&gt;
S1: t=75, n=3&lt;br&gt;
S0: t=98, n=4&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  ▶️ &lt;strong&gt;Rollout 2&lt;/strong&gt; (return value = 75)
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step 1: Selection from S0
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
S1: t=75, n=3 → UCB1 ≈ 26.36  &lt;br&gt;
S2: t=18, n=1 → UCB1 ≈ 20.35  &lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Choose S1&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Selection within S1
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
S3: t=15, n=1 → UCB1 ≈ 17.09  &lt;br&gt;
S4: t=40, n=1 → UCB1 ≈ 42.09  &lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Choose S4&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Expansion
&lt;/h4&gt;

&lt;p&gt;S4 has an unvisited child → expand to &lt;code&gt;S6&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: Rollout
&lt;/h4&gt;

&lt;p&gt;Simulation from &lt;code&gt;S6&lt;/code&gt; returns &lt;code&gt;75&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5: Backpropagation
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
S6: t=75, n=1&lt;br&gt;
S4: t=115, n=2&lt;br&gt;
S1: t=150, n=4&lt;br&gt;
S0: t=173, n=5&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;📖 &lt;strong&gt;Original blog with diagrams + YouTube video here&lt;/strong&gt;:&lt;br&gt;
👉 &lt;a href="https://www.codinglad.com/blogs/monte-carlo-tree-search-complete-guide-with-examples" rel="noopener noreferrer"&gt;Monte Carlo Tree Search (MCTS) Guide&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;✍️ Written by &lt;a href="https://codinglad.com" rel="noopener noreferrer"&gt;Dipto Mistry&lt;/a&gt; — Full-stack developer &amp;amp; AI enthusiast.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
