/// <summary> /// Gets all loops /// </summary> /// <param name="list">List of loops</param> public void GetLoops(List <DigraphLoop> list) { pathLoops.Clear(); index = 0; newVertices.Add(this); while (step()) { } for (int i = 0; i < pathLoops.Count; i++) { DigraphPath path1 = pathLoops[i] as DigraphPath; for (int j = i + 1; j < pathLoops.Count; j++) { DigraphPath path2 = pathLoops[j] as DigraphPath; if ((path1.Source != path2.Source) | (path1.Target != path2.Target)) { continue; } DigraphLoop loop = new DigraphLoop(new DigraphPath[] { path1, path2 }); loop.Reduce(); if (list.Contains(loop)) { continue; } list.Add(loop); } } }
/// <summary> /// Adds new path /// </summary> /// <param name="path">The path to add</param> /// <returns>False if path have been already added and true otherwise</returns> public bool AddPath(DigraphPath path) { foreach (object o in pathLoops) { if (path.Equals(o)) { return(false); } } pathLoops.Add(path); return(true); }
/// <summary> /// Step for closed loops finding /// </summary> /// <returns>True if process is not finished and false otherwise</returns> private bool stepClosedLoop() { oldVertices.Clear(); oldVertices.AddRange(newVertices); newVertices.Clear(); foreach (DigraphVertex v in oldVertices) { foreach (DigraphEdge e in v.outcomingEdges) { DigraphVertex t = e.Target; if (t == this) { List <DigraphEdge> l = new List <DigraphEdge>(); e.Source.backwardPath(e.Source.index - 1, l); l.Add(e); DigraphPath path = new DigraphPath(l, true); AddPath(path); continue; } if (t.index == -1) { t.index = v.index + 1; newVertices.Add(t); continue; } List <DigraphEdge> list = new List <DigraphEdge>(); List <DigraphEdge> incoming = t.IncomingEdges; foreach (DigraphEdge et in incoming) { if (et == e) { continue; } if (et.Source.index == t.index - 1) { list.Add(et); et.Source.backwardPath(et.Source.index - 1, list); break; } } //DigraphPath path = new DigraphPath(list, true); //AddPath(path); //ArrayList newList = new ArrayList(); //newList.Add(e); //v.backwardPath(v.index - 1, newList); //DigraphPath newPath = new DigraphPath(newList, true); //AddPath(newPath); } } return(newVertices.Count > 0); }
/// <summary> /// Overriden "Equals" function /// </summary> /// <param name="o">Object to compare</param> /// <returns>True if o equals this object and false otherwise</returns> public override bool Equals(object o) { if (!(o is DigraphPath)) { throw new Exception("You cannot compare digraph with another type object"); } DigraphPath p = o as DigraphPath; if (p.edges.Count != edges.Count) { return(false); } for (int i = 0; i < edges.Count; i++) { if (p.edges[i] != edges[i]) { return(false); } } return(true); }
/// <summary> /// Reduces begin of loop /// </summary> protected void reduceBegin() { int i = 0; for (; i < paths[0].Count & i < paths[1].Count; i++) { if (paths[0][i] != paths[1][i]) { break; } } for (int j = 0; j < 2; j++) { DigraphPath path = paths[j]; List <DigraphEdge> l = new List <DigraphEdge>(); for (int k = i; k < path.Count; k++) { l.Add(path[k]); } paths[j] = new DigraphPath(l, false); } }