/// <summary> /// Compute missing edges on an incomplete MTG at a given scale. /// </summary> /// <param name="tree"></param> /// <param name="scale"></param> /// <param name="edgeTypeProperty"></param> /// <returns> Returns true. </returns> bool ComputeMissingEdges(mtg tree, int scale, Dictionary <int, dynamic> edgeTypeProperty = null) { List <int> roots = tree.Roots(scale); foreach (int vid in roots) { List <int> components = tree.Components(vid); if (components == null || components == new List <int>() { }) { Console.WriteLine("Error ! Missing component for vertex " + vid); continue; } else { int componentId = components[0]; if (tree.Parent(componentId) == null) { continue; } int?parentId = tree.Complex((int)tree.Parent(componentId)); if (parentId == null) { Console.WriteLine("Error ! Missing parent for vertex" + vid); continue; } if (edgeTypeProperty != null) { try { string edgeType = edgeTypeProperty[componentId]; tree.AddChild((int)parentId, new Dictionary <string, dynamic>() { { "Edge_Type", edgeType } }, vid); } catch (KeyNotFoundException) { tree.AddChild((int)parentId, vid); } } else { tree.AddChild((int)parentId, vid); } } } return(true); }
/// <summary> /// Return the vertices from the vertex in the parameters up to the root. /// </summary> /// <param name="g"> The MTG. </param> /// <param name="vertexId"> The vertex identifier. </param> /// <returns> An iterator on the ancestors of the vertexId up to the root. </returns> public IEnumerable <int> FullAncestors(mtg g, int vertexId, string restrictedTo = "NoRestriction", string edgeType = "*", int containedIn = -1) { Dictionary <int, dynamic> edgeT = g.Property("Edge_Type"); int cScale; int v = vertexId; while (v != -1) { if (edgeType != "*" && edgeT.ContainsKey(v)) { if (edgeT[v] != edgeType) { yield break; } } if (restrictedTo == "SameComplex") { if (g.Complex(v) != g.Complex(vertexId)) { yield break; } } else { if (restrictedTo == "SameAxis") { if (edgeT.ContainsKey(v)) { if (edgeT[v] == "+") { yield break; } } } } if (containedIn != -1) { cScale = (int)g.Scale((int)containedIn); if (g.ComplexAtScale(v, cScale) != containedIn) { yield break; } } yield return(v); v = (int)g.Parent(v); } }
void LookForCommonAncestor(mtg tree, List <int> commonAncestors, int currentNode) { while (currentNode != -1) { for (int i = 0; i < commonAncestors.Count; i++) { int node = commonAncestors[i]; if (node == currentNode) { for (int j = 0; j < i; j++) { commonAncestors.RemoveAt(0); } return; } } currentNode = (int)tree.Parent(currentNode); } }
/// <summary> /// Display an MTG. /// </summary> /// <param name="tree"></param> /// <param name="vertexId"></param> /// <returns></returns> public IEnumerable <string> DisplayMtg(mtg tree, int vertexId) { Dictionary <int, dynamic> label = tree.Property("label"); Dictionary <int, dynamic> edgeType = tree.Property("Edge_Type"); traversal t = new traversal(); string edgeT; int currentVertex = vertexId; int tab = 0; foreach (int vertex in t.MtgIterator(tree, vertexId)) { edgeT = "/"; if (vertex != currentVertex) { int scale1 = (int)tree.Scale(currentVertex); int scale2 = (int)tree.Scale(vertex); if (scale1 >= scale2) { try { edgeT = edgeType[vertex]; } catch (KeyNotFoundException) { } if (scale1 == scale2) { if (tree.Parent(vertex) != currentVertex) { tab = -1; edgeT = "^" + edgeT; } else { edgeT = "^" + edgeT; } } else { if (scale1 > scale2) { int v = currentVertex; for (int i = 0; i < scale1 - scale2; i++) { v = (int)tree.Complex(v); } if (tree.Parent(vertex) == v) { edgeT = "^" + edgeT; } else { tab -= 1; edgeT = "^" + edgeT; } } } } else { Debug.Assert(scale2 - scale1 == 1); tab += 1; } string tabs = ""; for (int i = 0; i < tab; i++) { tabs = tabs + "\t"; } string labelVertex; if (label.ContainsKey(vertex)) { labelVertex = label[vertex]; } else { labelVertex = vertex.ToString(); } yield return(tabs + edgeT + labelVertex); } currentVertex = vertex; } }