title: swift playground error BAD_INSTRUCTION
body: <p>So I have the following greedy algorithm which gives me the following error:</p>
  
  <blockquote>
    <p>Playground execution aborted: error: Execution was interrupted,
    reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). The
    process has been left at the point where it was interrupted, use
    "thread return -x" to return to the state before expression
    evaluation.</p>
  </blockquote>
  
  <p><strong>Class:</strong></p>
  
  <pre><code>// This class represents an undirected graph using adjacency list
  public class Graph{
      var V: Int // number of vertices
      var adj: [[Int]] = [[]] //Adjacency List
  
      public init(v: Int) {
          V = v
          adj = [[Int]](repeating: [], count: v)
      }
  
      // Function to add an edge into the graph
      public func addEdge(v: Int, w: Int){
          adj[v].append(w)
          adj[w].append(v) // Graph is undirected
      }
  
      // Assigns colors (starting from 0) to all vertices and
      // prints the assignment of colors
      public func greedyColoring() {
          var result = [Int]()
  
          //Assign the first color to first vertex
          result[0] = 0
  
          //Initialize the remaining V-1 vertices as unassigned
          for i in 0 ..&lt; V{
              //No Color is assigned
              result[i] = -1
          }
  
          // A temporary array to store the available colors. True
          // value of available[cr] would mean that the color cr is
          // assigned to one of its adjacent vertices
          var available = [Bool]()
          for cr in 0 ..&lt; V{
              available[cr] = false
          }
  
          // Assign colors to remaining V-1 vertices
          for i in 1 ..&lt; V{
              //Process all adjacent vertices and flag their colors as unavailable
              for un in 0 ..&lt; adj[i].count{
                  if result[un] != -1 {
                      available[result[un]] = true
                  }
              }
  
              //find the first available color
              for cr in 0 ..&lt; V{
                  if available[cr] == false{
                      result[i] = cr
                      break
                  }
              }
  
              //Reset the values back to false for the next iteraation
              for un in 0 ..&lt; adj[i].count{
                  if result[un] != -1 {
                      available[result[un]] = true
                  }
              }
          }
  
          //Print result
          for r in 0 ..&lt; V{
              print("Vertex \(r) --&gt; Color \(result[r])")
          }
      }
  }
  </code></pre>
  
  <p><strong>This how I call it:</strong></p>
  
  <pre><code>import Foundation
  import UIKit
  import XCPlayground
  
  var g1 = Graph(v: 5)
  g1.addEdge(v: 0, w: 1)
  g1.addEdge(v: 0, w: 2)
  g1.addEdge(v: 1, w: 2)
  g1.addEdge(v: 1, w: 3)
  g1.addEdge(v: 2, w: 3)
  g1.addEdge(v: 3, w: 4)
  g1.greedyColoring() // Fails HERE
  </code></pre>
  
  <p>So I had this error in some lines earlier and it was related to how I used the arrays. Why doesn't playground give a exact error like <code>index out of bounds or so?</code> My Debug Console prints nothing... What is causing the error in my code?</p>
  
  <p><a href="https://i.stack.imgur.com/Yxg7C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yxg7C.png" alt="enter image description here"></a></p>