快速 Playground 错误 BAD_INSTRUCTION

标签 swift xcode swift-playground

所以我有下面的贪心算法,它给了我以下错误:

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.

类:

// 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 ..< 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 ..< V{
            available[cr] = false
        }

        // Assign colors to remaining V-1 vertices
        for i in 1 ..< V{
            //Process all adjacent vertices and flag their colors as unavailable
            for un in 0 ..< adj[i].count{
                if result[un] != -1 {
                    available[result[un]] = true
                }
            }

            //find the first available color
            for cr in 0 ..< V{
                if available[cr] == false{
                    result[i] = cr
                    break
                }
            }

            //Reset the values back to false for the next iteraation
            for un in 0 ..< adj[i].count{
                if result[un] != -1 {
                    available[result[un]] = true
                }
            }
        }

        //Print result
        for r in 0 ..< V{
            print("Vertex \(r) --> Color \(result[r])")
        }
    }
}

我是这样调用它的:

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

所以我之前在某些行中遇到了这个错误,这与我使用数组的方式有关。为什么 playground 不给出诸如 index out of bounds 之类的确切错误? 我的调试控制台什么都不打印...是什么导致了我的代码中的错误?

enter image description here

最佳答案

在这段代码中:

var result = [Int]()

//Assign the first color to first vertex
result[0] = 0

数组 result 为空,因此您无法通过 result[0] 访问第一个元素。

解决方案:

更改自:

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 ..< 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 ..< V{
    available[cr] = false
}

收件人:

var result = [Int]()

//Assign the first color to first vertex
result.append(0)

//Initialize the remaining V-1 vertices as unassigned
for _ in 0 ..< V{
    //No Color is assigned
    result.append(-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 _ in 0 ..< V{
    available.append(false)
}

关于快速 Playground 错误 BAD_INSTRUCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42689381/

相关文章:

swift - 相机跟随下落的球

swift - 我们能否确定路由器是否正在改变其位置?例如。公共(public)汽车、火车等内的路由器

xcode - swift 填充表格 View

ios - 如何从命令行以特定语言启动 iOS 模拟器?

Swift:为什么在这种情况下 CustomStringConvertible 描述运行了太多次?

xcode - 如何使用第三方库: SWXMLHash in the playground ?

ios - 动画期间的 UIView 中心位置

ios - Xcode 中的 iPad 序列错误

ios - SwiftUI Canvas 预览为一个 View 显示多个 View

objective-c - 在 NSObject 的子类上实现什么方法以允许在 swift playground 中进行 pretty-print ?