 
 (defn- add-edge [graph last-node node characters]
   "Creates an edge and adds it to the graph as a whole and to each participating node"
-  (-> graph
-      (update-in [:succs last-node] (fnil conj #{}) (Edge. node characters))
-      (update-in [:preds node] (fnil conj #{}) (Edge. last-node characters))))
+  (let [forward-edge (Edge. last-node node characters)
+        ;; We record the "true" forward edge here, for marking edges that
+        ;; cross segment containers. The ordering algorithm works through
+        ;; the graph backwards on every second pass, and the cross counting
+        ;; algorithm switches the order of the layers depending on which
+        ;; is smaller, but we always layout and draw the graph forward, so the
+        ;; marked edges need to be the forward ones.
+        edge-1 (-> forward-edge
+                   (assoc :forward-edge forward-edge))
+        edge-2 (-> (Edge. node last-node characters)
+                   (assoc :forward-edge forward-edge))]
+    (-> graph
+        (update-in [:succs last-node] (fnil conj #{}) edge-1)
+        (update-in [:preds node] (fnil conj #{}) edge-2))))
 
 
 (defn- make-node [graph layer-id input]

  adding right siblings, for a total count of edges that cross this one"
  [layer tree orig-index edge]
  (let [weight (count (:characters edge))
        is-seg-c (instance? SegmentContainer edge)]
    (loop [layer layer
           tree tree
           index orig-index



(defrecord Node [id layer-id characters])
(defrecord Edge [src dest characters])
(defrecord Layer [id duration nodes marked])
(defrecord SegmentContainer [segments])
(defrecord Segment [edge])
