private Dictionary <T, T?> ComputePaths(T source, MyGraphAdj <T> graph) { var vertexes = graph.GetAllVertexes().ToList(); var edges = graph.GetAllEdges().ToList(); Dictionary <T, int> distances = new Dictionary <T, int>(); Dictionary <T, T?> parent = new Dictionary <T, T?>(); Dictionary <T, int> heapMinDistances = new Dictionary <T, int>(); var heap = new MyBinaryHeap <MyBinaryHeapKeyNode <T, int> >(MyBinaryHeapType.MinHeap); foreach (var vertex in vertexes) { heap.Insert(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Key.CompareTo(source) == 0 ? 0 : Int32.MaxValue)); heapMinDistances.Add(vertex.Key, Int32.MaxValue); } parent[source] = null; distances[source] = 0; while (heap.Count > 0) { var current = heap.ExtractTop(); var nodes = edges.Where(x => x[0].Equals( vertexes.First(y => y.Key.CompareTo(current.Key) == 0).Value)).ToList(); if (nodes.Count == 0) { break; } var parentNode = vertexes.First(x => x.Key.CompareTo(current.Key) == 0); foreach (var node in nodes) { var vertex = vertexes.First(x => x.Value == node[1]); if (!heap.Contains(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Value))) { continue; } var currentDistance = edges.First(x => x[0] == parentNode.Value && x[1] == vertex.Value)[2]; distances[vertex.Key] = distances[current.Key] + currentDistance; if (heapMinDistances[vertex.Key] >= distances[vertex.Key]) { parent[vertex.Key] = current.Key; heap.Decrease(new MyBinaryHeapKeyNode <T, int>(vertex.Key, Int32.MaxValue), new MyBinaryHeapKeyNode <T, int>(vertex.Key, currentDistance)); heapMinDistances[vertex.Key] = currentDistance; } } } return(parent); }
public void Should_Check_Contains_True() { //arrange var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap); heap.Insert(17); heap.Insert(15); heap.Insert(10); heap.Insert(6); heap.Insert(10); heap.Insert(7); //act var result = heap.Contains(10); //assert result.ShouldBeEquivalentTo(true); heap.Count.ShouldBeEquivalentTo(6); heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap); }