public static ITraversable SpanningTreeKruskal(ITraversable graph) { if (graph.IsDirected) { throw new NotImplementedException("Spanning tree is not available on directed graph."); } var vertices = graph.GetVertices(); var unionFind = new UnionSet(vertices.Max() + 1); var spanningTree = new Graph { IsDirected = false }; spanningTree.AddVertexRange(vertices.Max() + 1); var orderedEdges = vertices .SelectMany(v => graph.GetAdjacentEdges(v)) .ToList(); orderedEdges.Sort(new EdgeWeightComparer()); // sort based on weight // keep adding edges till all components are connected. foreach (var e in orderedEdges) { if (!unionFind.SameComponent(e.Start, e.Stop)) { unionFind.UnionSets(e.Start, e.Stop); spanningTree.AddDoubleEdge(e); } } return(spanningTree); }
public Node(int x, int y, ITraversable tile) { X = x; Y = y; IsTraversable = tile.IsTraversable; tile.OnTraversabilityChange += HandleTraversabilityChange; }
public void PreChildrenVisit(ITraversable traversedElement) { if (traversedElement is EMInclude) { includesStack.Push(traversedElement as EMInclude); } }
public static void BFS( ITraversable traversable, IBFSVisitor visitor, int startVertex) { var toVisit = new Queue <int>(); var enqueued = new HashSet <int>(); toVisit.Enqueue(startVertex); enqueued.Add(startVertex); while (toVisit.Any()) { var v = toVisit.Dequeue(); visitor.EarlyVisit(v); var adj = traversable.GetAdjacentEdges(v); foreach (var a in adj) { if (!enqueued.Contains(a.Stop)) { visitor.Visit(a); toVisit.Enqueue(a.Stop); enqueued.Add(a.Stop); } } visitor.LateVisit(v); } }
private static void ThreeDeepTest() { ITraversable demoTree = ThreeDeepTree(); MaxHeap maxHeap = new MaxHeap(demoTree); Assert.True(maxHeap.Count + 1 == maxHeap.InsertionIndex); Assert.True(maxHeap.Count == 30); // Debug.Log(maxHeap.Count); int counter = 1; int score = maxHeap.Pop().HeapScore; while (maxHeap.HasTop()) { int nextScore = maxHeap.Pop().HeapScore; Assert.True(score >= nextScore); Debug.Log(nextScore); score = nextScore; counter++; } Debug.Log("Insertion Index: " + maxHeap.InsertionIndex); Debug.Log("incrementor counter: " + counter); Debug.Log("maxHeap.Count: " + maxHeap.Count); Assert.True(maxHeap.Count + 1 == maxHeap.InsertionIndex); }
private void AddNodeToHeap(ITraversable node) { int childIndex = InsertionIndex; int parentIndex = InsertionIndex / 2; heap[InsertionIndex] = node; while (parentIndex > 0) { if (heap[childIndex].HeapScore > heap[parentIndex].HeapScore)//if you are larger than parent, swap { Swap(childIndex, parentIndex); childIndex = parentIndex; parentIndex = childIndex / 2; } else//else you are smaller, so { parentIndex = 0;//break out of the loop; } } Count++; InsertionIndex++; }
public void HeapTestSimplePasses() { ITraversable demoT = StarterTree(true); MaxHeap maxHeap = new MaxHeap(demoT); //Debug.Log(maxHeap.InsertionIndex); Assert.True(maxHeap.HasTop()); int count = demoT.ToNodes().Count; Assert.True(maxHeap.Count == count); Assert.True(maxHeap.Count + 1 == maxHeap.InsertionIndex); //Debug.Log(count); int score = maxHeap.Pop().HeapScore; while (maxHeap.HasTop()) { int nextScore = maxHeap.Pop().HeapScore; Debug.Log(nextScore); Assert.True(score >= nextScore); score = nextScore; } ThreeDeepTest(); }
public DemoTraversable(bool isLeaf, int heapScore, ITraversable from, ITraversable[] to) { IsLeaf = isLeaf; HeapScore = heapScore; this.from = from; this.to = to; }
public DemoTraversable(int heapScore, DemoTraversable from, bool isLeaf) { IsLeaf = isLeaf; HeapScore = heapScore; this.from = from; to = null; }
public DemoTraversable(int heapScore) { IsLeaf = false; HeapScore = heapScore; from = null; to = null; }
public static void DFS( ITraversable traversable, IDFSVisitor visitor, int startVertex) { var toVisit = new Stack <int>(); var visited = new HashSet <int>(); toVisit.Push(startVertex); while (toVisit.Any()) { var v = toVisit.Pop(); visitor.EarlyVisit(v); visited.Add(v); var adj = traversable.GetAdjacentEdges(v); foreach (var a in adj) { visitor.Visit(a); if (!visited.Contains(a.Stop)) { toVisit.Push(a.Stop); } } visitor.LateVisit(v); } }
public void PreChildrenVisit(ITraversable traversedElement) { if (traversedElement is EMInclude) { includesStack.Push(traversedElement as EMInclude); } }
public void Reset() { this.stage = !this.filterByPinPosition || this.filteredPinPosition != PinnedRowPosition.Bottom ? (!this.filterByPinPosition || this.filteredPinPosition != PinnedRowPosition.None ? ViewInfoTraverser.Stages.TopPinnedSystemRows : ViewInfoTraverser.Stages.TopSystemRows) : ViewInfoTraverser.Stages.BottomPinnedRows; this.SetCollectionForStage(false); this.collection = (ITraversable)null; this.index = -1; this.current = (GridViewRowInfo)null; }
public void PostChildrenVisit(ITraversable traversedElement) { --level; if (level >= minLevel && level <= maxLevel && traversedElement is EMElement) { action(traversedElement as EMElement); } }
public MaxHeap(ITraversable root) { Count = 0; InsertionIndex = 1; heap = new ITraversable[size]; TryAddToHeap(root); }
public void PostChildrenVisit(ITraversable traversedElement) { renderer(traversedElement, builder, includesStack); if (traversedElement is EMInclude) { includesStack.Pop(); } }
public void PostChildrenVisit(ITraversable traversedElement) { renderer(traversedElement, builder, includesStack); if (traversedElement is EMInclude) { includesStack.Pop(); } }
public virtual ICollectionBuilder <A, CC> Append(ITraversable <A> trav) { ICollectionBuilder <A, CC> builder = this; foreach (var item in trav) { builder = builder.Append(item); } return(builder); }
protected override void OnItemException(ITraversable item, Exception e) { if (Debugger.IsAttached) { Debugger.Break(); } Console.WriteLine("-------------------------------------------------------"); Console.WriteLine($"ERROR while working on {item.Value}."); Console.WriteLine(e); }
public static ITraversable Find(this ITraversable item, EpubPath path) { ITraversable result = item; while (path.ContainsKeys && result != null) { result = result.Child(path.FirstKey); path = path.Rest; } return(result); }
public void PostChildrenVisit(ITraversable traversedElement) { if (traversedElement is EMHeader) { action(traversedElement as EMHeader, includesStack); } if (traversedElement is EMInclude) { includesStack.Pop(); } }
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- public static Stack <T> TwinStarT <T>(ITraversable begNode, ITraversable endNode, bool dualSearch = true) where T : ITraversable { object chainLocker = new object(); if (dualSearch) { Thread backwards = new Thread(() => SoloStar <T>(endNode, begNode, chainLocker, false, 0f, 1f)); backwards.Start(); } return(SoloStar <T>(begNode, endNode, chainLocker)); }
public static bool Equals(ITraversable left, ITraversable right) { if (left.GetVertices().Count() != right.GetVertices().Count()) { return(false); } var verticesEquals = left.GetVertices().SequenceEqual(right.GetVertices()); var edgesEquals = !left.GetVertices() .Any(v => !left.GetAdjacentEdges(v).SequenceEqual(right.GetAdjacentEdges(v))); return(verticesEquals && edgesEquals); }
public void PostChildrenVisit(ITraversable traversedElement) { if (traversedElement is EMHeader) { action(traversedElement as EMHeader, includesStack); } if (traversedElement is EMInclude) { includesStack.Pop(); } }
private bool ApplyFilters(ITraversable item) { foreach (var(path, target) in Query.Filters) { // use object.Equals to achieve magic filters if (!Equals(target, item.SafeGet(path))) { return(false); } } return(true); }
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- public static void ReverseOriginChain(ITraversable endNode) { ITraversable currentNode = endNode; ITraversable previousNode = null; ITraversable nextNode = null; do { nextNode = currentNode.origin; currentNode.origin = previousNode; previousNode = currentNode; currentNode = nextNode; } while (currentNode != null); }
//this isn't really recursive. IDK why just don't worry about it private void TryAddToHeap(ITraversable node) { if (node.IsLeaf) { AddNodeToHeap(node); } else { foreach (ITraversable traversable in node.ToNodes()) { TryAddToHeap(traversable); } } }
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- public static Stack <T> TwinStarT <T>(ITraversable begNode, ITraversable endNode, bool dualSearch = true) where T : ITraversable { object chainLocker = new object(); if (dualSearch) { Task backwards = Task.Run(() => SoloStar <T>(endNode, begNode, chainLocker, false, 0f, 1f)); } Task <Stack <T> > forward = Task.Run(() => SoloStar <T>(begNode, endNode, chainLocker)); return(forward.Result); }
public static IEnumerable <IEnumerable <int> > ConnectedComponents(ITraversable g) { var vs = g.GetVertices().ToList(); var comps = new List <List <int> >(); while (vs.Any()) { var vis = new ConnectedComponentsVistor(); BFS(g, vis, vs.First()); vs.RemoveAll(e => vis.Component.Contains(e)); comps.Add(vis.Component); } return(comps); }
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- public static void ClearOriginChain(ITraversable endNode) { foreach (ITraversable tn in endNode.GetConnectedTraversables()) { if (tn.origin == endNode) { ClearOriginChain(tn); } } if (endNode.origin != null) { ClearOriginChain(endNode.origin); endNode.origin = null; } }
public static StageSet GetTraversedStages(ITraversable start) { StageSet stages = new StageSet(); stages.Add((Stage)start); foreach (Stage stage in start.NextStages) { if (!stages.Contains(stage)) { stages.UnionWith(GetTraversedStages((ITraversable)stage)); } } return(stages); }
private static Graph Residuals(ITraversable graph) { var res = new Graph(); var numberOfVertices = graph.GetVertices().Max() + 1; res.AddVertexRange(numberOfVertices); foreach (var v in graph.GetVertices()) { foreach (var e in graph.GetAdjacentEdges(v)) { res.AddEdge(e); } } return(res); }
public static T SafeExecute <T>(this ITraversable item, EpubPath path, Func <ITraversable, T> action) { T safeExecuteImpl(ITraversable current, EpubPath currentPath) { if (currentPath.IsEmpty) { return(action(current)); } using (var child = current.Child(currentPath.FirstKey)) { return(safeExecuteImpl(child, currentPath.Rest)); } } // will ensure that all intermediate traversables are disposed return(safeExecuteImpl(item, path)); }
void Utility.IStage.Leave() { _ResleaseSession(); if (_Traversable != null) { _Binder.Unbind<ITraversable>(_Traversable); _Traversable = null; } _Member.BeginTraversable -= _OnBeginTraver; _Member.EndTraversable -= _OnEndTraver; _UnregisterQuit(_Player); _Unbind(_Player, _Binder); _Scene.ShutdownEvent -= ExitWorldEvent; _Scene.Exit(_Member); _Binder.Unbind<IBelongings>(_Belongings); _Updater.Shutdown(); }
private void _OnEndTraver(ITraversable traversable) { _Traversable = null; _Binder.Unbind<ITraversable>(traversable); _Bind(_Player, _Binder); }
void _OnBeginTraver(ITraversable traversable) { _Traversable = traversable; _Unbind(_Player, _Binder); _Binder.Bind<ITraversable>(traversable); }