             endCappedIntersections.append(Node(location: endingLocation, pathComponent: component))
         }
         let edges = (1..<endCappedIntersections.count).map {
-            Edge(forwardNode: endCappedIntersections[$0], backwardNode: endCappedIntersections[$0-1])
+            Edge(startingNode: endCappedIntersections[$0-1], endingNode: endCappedIntersections[$0])
         }
         for i in 0..<endCappedIntersections.count {
             if i > 0 {

        let firstPoint = startingNode.pathComponent.point(at: startingNode.componentLocation)
        var points: [CGPoint] = [firstPoint]
        var orders: [Int] = []
        func visit(from node: Node) -> Node? {
            if let edge = node.forwardEdge, edge.inSolution, edge.visited == false {
                let component = edge.component
                visit(component)
                edge.visited = true
                return edge.forwardNode
            }
            if let edge = node.backwardEdge, edge.inSolution, edge.visited == false {
                let component = edge.component.reversed()
                visit(component)
                edge.visited = true
                return edge.backwardNode
            }
            return nil
        }
        func visit(_ component: PathComponent) {
            points += component.points[1..<component.points.count]
            orders += component.orders
        }
        var currentNode = startingNode
        repeat {

private class Edge {
    var visited: Bool = false
    var inSolution: Bool = false
    let endingNode: Node
    let startingNode: Node
    init(startingNode: Node, endingNode: Node) {
        self.startingNode = startingNode
        self.endingNode = endingNode
    }
    var component: PathComponent {
        let parentComponent = self.endingNode.pathComponent
        var nextLocation = endingNode.componentLocation
        if nextLocation == parentComponent.startingIndexedLocation {
            nextLocation = parentComponent.endingIndexedLocation
        }
        return self.endingNode.pathComponent.split(from: startingNode.componentLocation, to: nextLocation)
    }
}

