Swift Playground 错误 - BAD_INSTRUCTION

4

我有以下贪心算法,但是它给了我以下错误:

Playground 执行中止:错误:执行被中断,原因:EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP,subcode=0x0)。进程已停留在中断点处,请使用“thread return -x”返回表达式评估之前的状态。

类:

// 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没有给出像 “下标越界” 这样的明确错误?我的Debug控制台没有任何输出...是什么导致了我的代码错误呢?

enter image description here

2个回答

2
在此代码片段中:
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)
}

完美,有没有更好的方法来调试playgrounds? - SaifDeen
我的日志打印出了“致命错误:索引超出范围”,你呢? - chengsam
我的Xcode只显示我发布的错误,而不在日志中显示。 - SaifDeen
你打开了调试控制台吗?尝试按下 ⇧ + ⌘ + C。 - chengsam
如果我将我的图形代码从单独的类切换到主播放区,调试控制台会显示错误(索引超出范围)。 - SaifDeen
@SaifDeen 我在主要的编程环境中也测试了你的代码,也许这就是为什么你之前看不到错误的原因 :) - chengsam

1

只需编写:

import PlaygroundSupport

它帮助了我


似乎这是游乐场错误的正确解决方案,无论您拥有什么代码。 - DionizB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接