private AdjacencyGraph <TVertex, TaggedEquatableEdge <TVertex, double> > RemoveEdge( AdjacencyGraph <TVertex, TaggedEquatableEdge <TVertex, double> > old, TaggedEquatableEdge <TVertex, double> edgeRemoving) { // get copy of the grapth using Serialization and Deserialization var copyGraph = old.Clone(); // remove the edge foreach (var edge in copyGraph.Edges) { if (edge == edgeRemoving) { copyGraph.RemoveEdge(edge); break; } } // get all edges but the removing one var oldEdges = new List <TaggedEquatableEdge <TVertex, double> >(); foreach (var edge in old.Edges) { if (edge != edgeRemoving) { oldEdges.Add(edge); } } return(copyGraph); }
public void TagChanged() { var edge = new TaggedEquatableEdge <int, TestObject>(1, 2, null); int changeCount = 0; edge.TagChanged += (sender, args) => ++ changeCount; edge.Tag = null; Assert.AreEqual(0, changeCount); var tag1 = new TestObject(1); edge.Tag = tag1; Assert.AreEqual(1, changeCount); edge.Tag = tag1; Assert.AreEqual(1, changeCount); var tag2 = new TestObject(2); edge.Tag = tag2; Assert.AreEqual(2, changeCount); edge.Tag = tag1; Assert.AreEqual(3, changeCount); }
/// <summary> /// Runs the algorithm. /// </summary> /// <returns>Found paths.</returns> public IEnumerable <SortedPath> Execute() { var shortestWays = new List <SortedPath>(); // Find the first shortest way SortedPath?shortestWay = GetShortestPathInGraph(_graph); // In case of Dijkstra’s algorithm couldn't find any ways if (shortestWay is null) { throw new NoPathFoundException(); } shortestWays.Add(shortestWay.Value); for (int i = 0; i < _k - 1; ++i) { double minDistance = double.MaxValue; SortedPath?pathSlot = null; TaggedEquatableEdge <TVertex, double> removedEdge = null; foreach (TaggedEquatableEdge <TVertex, double> edge in shortestWay.Value) { _graph.RemoveEdge(edge); // Find shortest way in the graph without this edge SortedPath?newPath = GetShortestPathInGraph(_graph); _graph.AddEdge(edge); if (newPath is null) { continue; } double pathWeight = GetPathDistance(newPath.Value); if (pathWeight >= minDistance) { continue; } minDistance = pathWeight; pathSlot = newPath; removedEdge = edge; } if (pathSlot is null) { break; } shortestWays.Add(pathSlot.Value); _removedEdges.Add(removedEdge); shortestWay = pathSlot; _graph.RemoveEdge(removedEdge); } return(_filter(shortestWays)); }
public IEnumerable <IEnumerable <TaggedEquatableEdge <TVertex, double> > > Execute() { var listShortestWays = new List <IEnumerable <TaggedEquatableEdge <TVertex, double> > >(); // find the first shortest way var shortestWay = GetShortestPathInGraph(_graph); listShortestWays.Add(shortestWay); /* * in case of Dijkstra’s algorithm couldn't find any ways */ if (shortestWay == null) { throw new NoPathFoundException(); } for (var i = 0; i < _k - 1; i++) { var minDistance = double.MaxValue; IEnumerable <TaggedEquatableEdge <TVertex, double> > pathSlot = null; TaggedEquatableEdge <TVertex, double> removedEdge = null; foreach (var edge in shortestWay) { _graph.RemoveEdge(edge); //find shortest way in the graph without this edge var newPath = GetShortestPathInGraph(_graph); _graph.AddEdge(edge); if (newPath == null) { continue; } var pathWeight = GetPathDistance(newPath); if (pathWeight >= minDistance) { continue; } minDistance = pathWeight; pathSlot = newPath; removedEdge = edge; } if (pathSlot == null) { break; } listShortestWays.Add(pathSlot); _removedEdges.Add(removedEdge); shortestWay = pathSlot; _graph.RemoveEdge(removedEdge); } return(_filter(listShortestWays)); }
public void Equals() { var tag1 = new TestObject(1); var tag2 = new TestObject(2); var edge1 = new TaggedEquatableEdge <int, TestObject>(1, 2, tag1); var edge2 = new TaggedEquatableEdge <int, TestObject>(1, 2, tag1); var edge3 = new TaggedEquatableEdge <int, TestObject>(1, 2, tag2); var edge4 = new TaggedEquatableEdge <int, TestObject>(1, 2, null); var edge5 = new TaggedEquatableEdge <int, TestObject>(1, 2, null); Assert.AreEqual(edge1, edge1); Assert.AreEqual(edge1, edge2); Assert.AreEqual(edge1, edge3); // Tag is not taken into account for equality Assert.AreEqual(edge1, edge4); // Tag is not taken into account for equality Assert.AreEqual(edge4, edge5); Assert.AreNotEqual(edge1, null); }
private double DefaultGetWeights(TaggedEquatableEdge <TVertex, double> edge) { return(edge.Tag); }
private static double DefaultGetWeights([NotNull] TaggedEquatableEdge <TVertex, double> edge) { return(edge.Tag); }