private void GetDistancesAndWeights(out double[,] d, out double[,] w) { d = new double[VisitedGraph.VertexCount, VisitedGraph.VertexCount]; w = new double[VisitedGraph.VertexCount, VisitedGraph.VertexCount]; double maxCost = 0.0001; var undirected = new UndirectedBidirectionalGraph <TVertex, TEdge>(VisitedGraph); int i = 0; foreach (TVertex source in undirected.Vertices) { var spa = new UndirectedDijkstraShortestPathAlgorithm <TVertex, TEdge>(undirected, edge => edge is IWeightedEdge <TVertex>?Parameters.WeightAdjustment - (((IWeightedEdge <TVertex>)edge).Weight) : 1.0); spa.Compute(source); int j = 0; foreach (TVertex target in undirected.Vertices) { double cost; if (spa.TryGetDistance(target, out cost)) { d[i, j] = cost; if (cost > maxCost) { maxCost = cost; } } else { d[i, j] = double.NaN; } j++; } i++; } double idealEdgeLength = (Math.Min(Parameters.Width, Parameters.Height) / maxCost) * Parameters.LengthFactor; double disconnectedCost = maxCost * Parameters.DisconnectedMultiplier; for (i = 0; i < VisitedGraph.VertexCount; i++) { for (int j = 0; j < VisitedGraph.VertexCount; j++) { if (double.IsNaN(d[i, j])) { d[i, j] = disconnectedCost; } else { d[i, j] *= idealEdgeLength; } w[i, j] = Math.Pow(Math.Max(d[i, j], 0.0001), -Parameters.Alpha); } } }
private static void Verify <TVertex, TEdge>( [NotNull] UndirectedDijkstraShortestPathAlgorithm <TVertex, TEdge> algorithm, [NotNull] UndirectedVertexPredecessorRecorderObserver <TVertex, TEdge> predecessors) where TEdge : IEdge <TVertex> { // Verify the result foreach (TVertex vertex in algorithm.VisitedGraph.Vertices) { if (!predecessors.VerticesPredecessors.TryGetValue(vertex, out TEdge predecessor)) { continue; } if (predecessor.Source.Equals(vertex)) { continue; } Assert.AreEqual( algorithm.TryGetDistance(vertex, out double currentDistance), algorithm.TryGetDistance(predecessor.Source, out double predecessorDistance)); Assert.GreaterOrEqual(currentDistance, predecessorDistance); } }
private static void Verify <TVertex, TEdge>( [NotNull] UndirectedDijkstraShortestPathAlgorithm <TVertex, TEdge> algorithm, [NotNull] UndirectedVertexPredecessorRecorderObserver <TVertex, TEdge> predecessors) where TEdge : IEdge <TVertex> { // Verify the result foreach (TVertex vertex in algorithm.VisitedGraph.Vertices) { if (!predecessors.VertexPredecessors.TryGetValue(vertex, out TEdge predecessor)) { continue; } if (predecessor.Source.Equals(vertex)) { continue; } bool found = algorithm.TryGetDistance(vertex, out double vd); Assert.AreEqual(found, algorithm.TryGetDistance(predecessor.Source, out double vp)); if (found) { Assert.AreEqual(vd, vp + 1); } } }