                      :characters characters})))
 
 
-(def SparseGraph->FlatGraph (comp CountedAndMarkedGraph->FlatGraph
+(def SparseGraph->FlatGraph (comp OrderedGraph->FlatGraph
                                   best-ordering
                                   SparseGraph->ordered-graphs))

                          order-subnodes)))))))


(defn OrderedGraph->CountedAndMarkedGraph
  "Takes an OrderedGraph, counts edge crossings and marks edges that should not
  be drawn straight, and returns a new graph with this information"
  [ordered-graph]
  (let [{:keys [minus-ps minus-qs preds succs characters]} ordered-graph
        [crossings marked] (->> (map #(count-and-mark-crossings
                                       %1 %2 preds succs characters)
                                     minus-ps minus-qs)
                                (apply map vector))]
    (map->CountedAndMarkedGraph (assoc ordered-graph
                                       :crossings (reduce + crossings)
                                       :marked (reduce set/union marked)))))


(defn best-ordering
  "Chooses the graph with the fewest crossings from a collection of OrderedGraphs"
  [ordered-graphs]
  (->> (map OrderedGraph->CountedAndMarkedGraph ordered-graphs)
       (sort-by (fn [g]
                  [(:crossings g)
                   (reduce #(+ %1 (:weight %2)) 0 (:marked g))]))
       first))



               0)))


(defn count-and-mark-crossings-layer
  "Step 5 of ESK, counts the number of crossings that result from a bi-layer
  ordering. Implements the algorithm found in Bilayer Cross Counting, by
  Wilhelm Barth, Petra Mutzel and Michael Jünger
