- Timestamp:
- 07/18/14 16:40:21 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeDistance/BottomUpDistanceCalculator.cs
r11197 r11209 51 51 } 52 52 53 private static IArc AddArc(IVertex source, IVertex target) { 54 var arc = new Arc(source, target); 55 source.AddForwardArc(arc); 56 target.AddReverseArc(arc); 57 return arc; 58 } 59 53 60 /// <summary> 54 61 /// Creates a compact representation of the two trees as a directed acyclic graph … … 58 65 /// <returns>The compacted DAG representing the two trees</returns> 59 66 private static Dictionary<ISymbolicExpressionTreeNode, IVertex> Compact(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) { 60 var G = new DirectedGraph();61 67 var K = new Dictionary<ISymbolicExpressionTreeNode, IVertex>(); 62 68 var F = new DisjointUnion(t1, t2); 63 69 var L = new Dictionary<string, IVertex>(); 64 70 var Children = new Dictionary<ISymbolicExpressionTreeNode, int>(); 71 var G = new List<IVertex>(); 65 72 66 73 var nodes = F.Nodes.ToList(); … … 71 78 var z = new Vertex { Content = l, Label = label }; 72 79 L[z.Label] = z; 73 G.Add Vertex(z);80 G.Add(z); 74 81 } 75 82 … … 86 93 while (Q.Any()) { 87 94 var v = Q.Dequeue(); 95 var label = Label(v); 88 96 if (v.SubtreeCount == 0) { 89 K[v] = L[ Label(v)]; // 1897 K[v] = L[label]; // 18 90 98 } else { 91 99 bool found = false; 92 100 // for all nodes w in G in reverse order 93 var vertices = G.Vertices.Reverse();94 foreach (var w in vertices) {95 if (Height(v) != Height(w) || v.SubtreeCount != w.OutDegree || Label(v)!= w.Label)101 for (int i = G.Count - 1; i >= 0; --i) { 102 var w = G[i]; 103 if (Height(v) != Height(w) || v.SubtreeCount != w.OutDegree || label != w.Label) 96 104 break; 97 105 … … 107 115 108 116 if (!found) { 109 var w = new Vertex { Content = v, Label = Label(v)};110 G.Add Vertex(w);117 var w = new Vertex { Content = v, Label = label }; 118 G.Add(w); 111 119 K[v] = w; 112 120 113 121 foreach (var u in v.Subtrees) { 114 G.AddArc(w, K[u]);122 AddArc(w, K[u]); 115 123 } // 40: end for 116 124 } // 41: end if … … 131 139 var M = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t1 => nodes of t2 132 140 var M_ = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t2 => nodes of t1 133 var plist2 = t2.IterateNodesPrefix().ToList(); 134 foreach (var n in t1.IterateNodesBreadth()) { 135 ISymbolicExpressionTreeNode v = n; 136 if (!M.ContainsKey(v)) { 137 var w = plist2.Last(); 138 var pw = plist2.IndexOf(w); // preorder index of node w 139 foreach (var u in plist2.Where(x => K[x] == K[v])) { 140 if (!M_.ContainsKey(u)) { 141 w = u; 142 // commented out test below since we are dealing with unordered trees 143 // if (plist2.IndexOf(u) < pw) { 144 // w = u; 145 // } 146 } 147 } 148 if (K[v] == K[w]) { 149 // simultaneous preorder traversal of the two subtrees 150 var nodesV = v.IterateNodesPrefix().ToList(); 151 var nodesW = w.IterateNodesPrefix().ToList(); 152 for (int i = 0; i < Math.Min(nodesV.Count, nodesW.Count); ++i) { 153 var s = nodesV[i]; 154 var t = nodesW[i]; 155 M[s] = t; 156 M_[t] = s; 157 } 158 } 141 142 var nodes2 = t2.IterateNodesPrefix().Where(x => !M_.ContainsKey(x)).ToList(); 143 foreach (var v in t1.IterateNodesBreadth().Where(x => !M.ContainsKey(x))) { 144 var kv = K[v]; 145 var w = nodes2.LastOrDefault(x => K[x] == kv); 146 if (w == null) continue; 147 148 // simultaneous preorder traversal of the two subtrees 149 var eV = v.IterateNodesPrefix().GetEnumerator(); 150 var eW = w.IterateNodesPrefix().GetEnumerator(); 151 152 while (eV.MoveNext() && eW.MoveNext()) { 153 var s = eV.Current; 154 var t = eW.Current; 155 M[s] = t; 156 M_[t] = s; 159 157 } 160 158 } … … 218 216 219 217 return nodes.OrderByDescending(x => x.Height).Select(x => x.Node); 220 221 //222 // var p1 = r1.Parent;223 // var p2 = r2.Parent;224 //225 // var virtualRootSymbol = new StartSymbol();226 // var virtualRootNode = virtualRootSymbol.CreateTreeNode();227 //228 // virtualRootNode.AddSubtree(r1);229 // virtualRootNode.AddSubtree(r2);230 //231 // var nodes = virtualRootNode.IterateNodesPostfix().ToList();232 //233 // virtualRootNode.RemoveSubtree(0);234 // virtualRootNode.RemoveSubtree(0);235 //236 // r1.Parent = p1;237 // r2.Parent = p2;238 //239 // for (int i = 0; i < nodes.Count - 1; ++i) { yield return nodes[i]; }240 218 } 241 219 }
Note: See TracChangeset
for help on using the changeset viewer.