Comparison <int> Comparison(Dictionary <int, int> inverseToOrder) { return(delegate(int node1, int node2) { Debug.Assert(properLayeredGraph.IsVirtualNode(node1) && properLayeredGraph.IsVirtualNode(node2)); int succ1 = properLayeredGraph.Succ(node1).ElementAt(0); int succ2 = properLayeredGraph.Succ(node2).ElementAt(0); int pred1 = properLayeredGraph.Pred(node1).ElementAt(0); int pred2 = properLayeredGraph.Pred(node2).ElementAt(0); Point succPoint1 = nodePositions[succ1]; Point succPoint2 = nodePositions[succ2]; Point predPoint1 = nodePositions[pred1]; Point predPoint2 = nodePositions[pred2]; if (succPoint1 != succPoint2) { if (predPoint1 != predPoint2) { return predPoint1.CompareTo(predPoint2); } return succPoint1.CompareTo(succPoint2); } if (properLayeredGraph.IsVirtualNode(succ1)) { if (predPoint1 != predPoint2) { return predPoint1.CompareTo(predPoint2); } int o1 = inverseToOrder[succ1]; int o2 = inverseToOrder[succ2]; Debug.Assert(o1 != -1 && o2 != -1); return (o1.CompareTo(o2)); } while (nodePositions[pred1] == nodePositions[pred2] && properLayeredGraph.IsVirtualNode(pred1)) { pred1 = properLayeredGraph.Pred(pred1).ElementAt(0); pred2 = properLayeredGraph.Pred(pred2).ElementAt(0); } if (nodePositions[pred1] == nodePositions[pred2]) { return node1.CompareTo(node2); } return nodePositions[pred1].CompareTo(nodePositions[pred2]); }); }
/// <summary> /// Sweep layer from left to right and fill S,P arrays as we go. /// The arrays P and S will be sorted according to X. Note that we will not keep them sorted /// as we doing adjacent swaps. Initial sorting only needed to calculate initial clr,crl values. /// </summary> /// <param name="layer"></param> void InitPSArraysForLayer(int[] layer) { foreach (int l in layer) { foreach (int p in properLayeredGraph.Pred(l)) { Dictionary <int, int> so = SOrder[p]; if (so.ContainsKey(l)) { continue; } int sHasNow = so.Count; S[p].Add(l); //l takes the first available slot in S[p] so[l] = sHasNow; } foreach (int s in properLayeredGraph.Succ(l)) { Dictionary <int, int> po = POrder[s]; if (po.ContainsKey(l)) { continue; } int pHasNow = po.Count; P[s].Add(l); //l take the first available slot in P[s] po[l] = pHasNow; } } }
/// <summary> /// Just depth search and assign the index saying when the node was visited /// </summary> void Init() { var counts = new int[nOfLayers]; //the initial layers are set by following the order of the //depth first traversal inside one layer var q = new Stack <int>(); //enqueue all sources of the graph for (int i = 0; i < properLayeredGraph.NodeCount; i++) { if (properLayeredGraph.InEdgesCount(i) == 0) { q.Push(i); } } var visited = new bool[properLayeredGraph.NodeCount]; while (q.Count > 0) { int u = q.Pop(); int l = layerArrays.Y[u]; layerArrays.Layers[l][counts[l]] = u; layerArrays.X[u] = counts[l]; counts[l]++; foreach (int v in properLayeredGraph.Succ(u)) { if (!visited[v]) { visited[v] = true; q.Push(v); } } } X = layerArrays.X; if (balanceVirtAndOrigNodes) { InitOptimalGroupSizes(); } }