public void HashcodeDefaultEdge_ReferenceTypeExtremities() { var edge1 = default(SEquatableEdge <TestVertex>); var edge2 = new SEquatableEdge <TestVertex>(); Assert.AreEqual(edge1.GetHashCode(), edge2.GetHashCode()); }
private void FillJData( [NotNull, ItemNotNull] IEnumerable <TVertex> vertices, [NotNull] TVertex vi, [NotNull] TVertex vk, VertexData pathIk) { foreach (TVertex vj in vertices) { var kj = new SEquatableEdge <TVertex>(vk, vj); if (_data.TryGetValue(kj, out VertexData pathKj)) { double combined = _distanceRelaxer.Combine( pathIk.Distance, pathKj.Distance); var ij = new SEquatableEdge <TVertex>(vi, vj); if (_data.TryGetValue(ij, out VertexData pathIj)) { if (_distanceRelaxer.Compare(combined, pathIj.Distance) < 0) { _data[ij] = new VertexData(combined, vk); } } else { _data[ij] = new VertexData(combined, vk); } } } }
public void ObjectToString() { var edge1 = new SEquatableEdge <int>(1, 2); var edge2 = new SEquatableEdge <int>(2, 1); Assert.AreEqual("1 -> 2", edge1.ToString()); Assert.AreEqual("2 -> 1", edge2.ToString()); }
public void Hashcode() { var edge1 = new SEquatableEdge <int>(1, 2); var edge2 = new SEquatableEdge <int>(1, 2); var edge3 = new SEquatableEdge <int>(2, 1); Assert.AreEqual(edge1.GetHashCode(), edge2.GetHashCode()); Assert.AreNotEqual(edge1.GetHashCode(), edge3.GetHashCode()); }
private bool TryGetPathInternal( [NotNull] TVertex source, [NotNull] TVertex target, out IEnumerable <TEdge> path) { #if DEBUG && !NET20 var set = new HashSet <TVertex> { source, target }; #endif var edges = new EdgeList <TVertex, TEdge>(); var todo = new Stack <SEquatableEdge <TVertex> >(); todo.Push(new SEquatableEdge <TVertex>(source, target)); while (todo.Count > 0) { SEquatableEdge <TVertex> current = todo.Pop(); Debug.Assert(!current.Source.Equals(current.Target)); if (_data.TryGetValue(current, out VertexData data)) { if (data.TryGetEdge(out TEdge edge)) { edges.Add(edge); } else { if (data.TryGetPredecessor(out TVertex intermediate)) { #if DEBUG && !NET20 Debug.Assert(set.Add(intermediate)); #endif todo.Push(new SEquatableEdge <TVertex>(intermediate, current.Target)); todo.Push(new SEquatableEdge <TVertex>(current.Source, intermediate)); } else { throw new InvalidOperationException("Cannot find predecessor."); } } } else { // No path found path = null; return(false); } } Debug.Assert(todo.Count == 0); Debug.Assert(edges.Count > 0); path = edges; return(true); }
public void EqualsDefaultEdge_ReferenceTypeExtremities() { var edge1 = default(SEquatableEdge <TestVertex>); var edge2 = new SEquatableEdge <TestVertex>(); Assert.AreEqual(edge1, edge2); Assert.AreEqual(edge2, edge1); Assert.IsTrue(edge1.Equals(edge2)); Assert.IsTrue(edge2.Equals(edge1)); }
private void FillIData([NotNull, ItemNotNull] TVertex[] vertices, [NotNull] TVertex vk) { foreach (TVertex vi in vertices) { var ik = new SEquatableEdge <TVertex>(vi, vk); if (_data.TryGetValue(ik, out VertexData pathIk)) { FillJData(vertices, vi, vk, pathIk); } } }
private void CheckNegativeCycles([NotNull, ItemNotNull] IEnumerable <TVertex> vertices) { foreach (TVertex vi in vertices) { var ii = new SEquatableEdge <TVertex>(vi, vi); if (_data.TryGetValue(ii, out VertexData data) && data.Distance < 0) { throw new NegativeCycleGraphException(); } } }
public void Equals() { var edge1 = new SEquatableEdge <int>(1, 2); var edge2 = new SEquatableEdge <int>(1, 2); var edge3 = new SEquatableEdge <int>(2, 1); Assert.AreEqual(edge1, edge1); Assert.AreEqual(edge1, edge2); Assert.AreNotEqual(edge1, edge3); Assert.AreNotEqual(edge1, null); }
public void ToBidirectionalGraph05325() { BidirectionalGraph<int, SEquatableEdge<int>> bidirectionalGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[1]; bidirectionalGraph = this.ToBidirectionalGraph05<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)bidirectionalGraph); Assert.AreEqual<int>(-1, bidirectionalGraph.EdgeCapacity); Assert.AreEqual<bool>(true, bidirectionalGraph.IsDirected); Assert.AreEqual<bool>(true, bidirectionalGraph.AllowParallelEdges); Assert.AreEqual<bool>(false, bidirectionalGraph.IsEdgesEmpty); Assert.AreEqual<int>(1, bidirectionalGraph.EdgeCount); }
public void ToUndirectedGraph02438() { UndirectedGraph<int, SEquatableEdge<int>> undirectedGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[2]; undirectedGraph = this.ToUndirectedGraph02<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)undirectedGraph); Assert.IsNotNull(undirectedGraph.EdgeEqualityComparer); Assert.AreEqual<int>(4, undirectedGraph.EdgeCapacity); Assert.AreEqual<bool>(false, undirectedGraph.IsDirected); Assert.AreEqual<bool>(true, undirectedGraph.AllowParallelEdges); Assert.AreEqual<int>(2, undirectedGraph.EdgeCount); }
public void ToAdjacencyGraph04438() { AdjacencyGraph<int, SEquatableEdge<int>> adjacencyGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[2]; adjacencyGraph = this.ToAdjacencyGraph04<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)adjacencyGraph); Assert.AreEqual<bool>(true, adjacencyGraph.IsDirected); Assert.AreEqual<bool>(true, adjacencyGraph.AllowParallelEdges); Assert.AreEqual<int>(-1, adjacencyGraph.EdgeCapacity); Assert.AreEqual<bool>(false, adjacencyGraph.IsEdgesEmpty); Assert.AreEqual<int>(2, adjacencyGraph.EdgeCount); }
public void Equals() { var edge1 = new SEquatableEdge <int>(1, 2); var edge2 = new SEquatableEdge <int>(1, 2); var edge3 = new SEquatableEdge <int>(2, 1); Assert.AreEqual(edge1, edge1); Assert.AreEqual(edge1, edge2); Assert.IsTrue(edge1.Equals((object)edge2)); Assert.AreNotEqual(edge1, edge3); Assert.IsFalse(edge1.Equals(null)); Assert.AreNotEqual(edge1, null); }
public void ToAdjacencyGraph04940() { AdjacencyGraph<int, SEquatableEdge<int>> adjacencyGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[1]; SEquatableEdge<int> s0 = new SEquatableEdge<int>(default(int), int.MinValue); sEquatableEdges[0] = s0; adjacencyGraph = this.ToAdjacencyGraph04<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)adjacencyGraph); Assert.AreEqual<bool>(true, adjacencyGraph.IsDirected); Assert.AreEqual<bool>(true, adjacencyGraph.AllowParallelEdges); Assert.AreEqual<int>(-1, adjacencyGraph.EdgeCapacity); Assert.AreEqual<bool>(false, adjacencyGraph.IsEdgesEmpty); Assert.AreEqual<int>(1, adjacencyGraph.EdgeCount); }
public void ToUndirectedGraph02940() { UndirectedGraph<int, SEquatableEdge<int>> undirectedGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[1]; SEquatableEdge<int> s0 = new SEquatableEdge<int>(default(int), int.MinValue); sEquatableEdges[0] = s0; undirectedGraph = this.ToUndirectedGraph02<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)undirectedGraph); Assert.IsNotNull(undirectedGraph.EdgeEqualityComparer); Assert.AreEqual<int>(4, undirectedGraph.EdgeCapacity); Assert.AreEqual<bool>(false, undirectedGraph.IsDirected); Assert.AreEqual<bool>(true, undirectedGraph.AllowParallelEdges); Assert.AreEqual<int>(1, undirectedGraph.EdgeCount); }
/// <inheritdoc /> protected override void InternalCompute() { if (!TryGetRootVertex(out TVertex root)) { throw new InvalidOperationException("Root vertex not set."); } if (_pairs is null) { throw new InvalidProgramException("Pairs not set."); } var graph = _pairs.ToAdjacencyGraph(); var disjointSet = new ForestDisjointSet <TVertex>(); var verticesAncestors = new Dictionary <TVertex, TVertex>(); var dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>( this, VisitedGraph, new Dictionary <TVertex, GraphColor>(VisitedGraph.VertexCount)); dfs.InitializeVertex += vertex => disjointSet.MakeSet(vertex); dfs.DiscoverVertex += vertex => verticesAncestors[vertex] = vertex; dfs.TreeEdge += edge => { disjointSet.Union(edge.Source, edge.Target); // ReSharper disable once AssignNullToNotNullAttribute // Justification: must be in the set because unioned just before. verticesAncestors[disjointSet.FindSet(edge.Source)] = edge.Source; }; dfs.FinishVertex += vertex => { foreach (SEquatableEdge <TVertex> edge in graph.OutEdges(vertex)) { if (dfs.VerticesColors[edge.Target] == GraphColor.Black) { SEquatableEdge <TVertex> pair = edge.ToVertexPair(); // ReSharper disable once AssignNullToNotNullAttribute // Justification: must be present in the set. Ancestors[pair] = verticesAncestors[disjointSet.FindSet(edge.Target)]; } } }; // Run DFS dfs.Compute(root); }
public void SReversedEdgeTestEverything() { var originalEdge1 = new SEquatableEdge <int>(0, 1); var reversedEdge1 = new SReversedEdge <int, SEquatableEdge <int> >(originalEdge1); Assert.AreEqual(1, reversedEdge1.Source); Assert.AreEqual(0, reversedEdge1.Target); Assert.AreEqual(String.Format("R({0}->{1})", 0, 1), reversedEdge1.ToString()); //equatable parts var originalEdge2 = new SEquatableEdge <int>(2, 3); var reversedEdge2 = new SReversedEdge <int, SEquatableEdge <int> >(originalEdge2); Assert.IsFalse(reversedEdge1.Equals(reversedEdge2)); Assert.IsFalse(reversedEdge1.Equals(1)); Assert.IsFalse(reversedEdge1 == reversedEdge2); Assert.IsTrue(reversedEdge1 != reversedEdge2); Assert.IsFalse(reversedEdge1.GetHashCode() == reversedEdge2.GetHashCode()); }
/// <inheritdoc /> protected override void Initialize() { base.Initialize(); // Matrix i,j -> path _data.Clear(); // Prepare the matrix with initial costs // Walk each edge and add entry in cost dictionary foreach (TEdge edge in VisitedGraph.Edges) { SEquatableEdge <TVertex> ij = edge.ToVertexPair(); double cost = _weights(edge); if (!_data.TryGetValue(ij, out VertexData data)) { _data[ij] = new VertexData(cost, edge); } else if (cost < data.Distance) { _data[ij] = new VertexData(cost, edge); } } }
protected static void ContainsEdge_ImmutableGraph_Test( [NotNull] IMutableVertexAndEdgeSet <int, Edge <int> > wrappedGraph, [NotNull, InstantHandle] Func <IEdgeSet <int, SEquatableEdge <int> > > createGraph) { IEdgeSet <int, SEquatableEdge <int> > graph = createGraph(); var edge1 = new Edge <int>(1, 2); var equatableEdge1 = new SEquatableEdge <int>(edge1.Source, edge1.Target); var edge2 = new Edge <int>(1, 3); var equatableEdge2 = new SEquatableEdge <int>(edge2.Source, edge2.Target); var edge3 = new Edge <int>(2, 1); var equatableEdge3 = new SEquatableEdge <int>(edge3.Source, edge3.Target); var edge4 = new Edge <int>(2, 2); var equatableEdge4 = new SEquatableEdge <int>(edge4.Source, edge4.Target); var otherEdge1 = new Edge <int>(1, 2); var equatableOtherEdge1 = new SEquatableEdge <int>(otherEdge1.Source, otherEdge1.Target); Assert.IsFalse(graph.ContainsEdge(equatableEdge1)); Assert.IsFalse(graph.ContainsEdge(equatableEdge2)); Assert.IsFalse(graph.ContainsEdge(equatableEdge3)); Assert.IsFalse(graph.ContainsEdge(equatableEdge4)); Assert.IsFalse(graph.ContainsEdge(equatableOtherEdge1)); wrappedGraph.AddVerticesAndEdge(edge1); graph = createGraph(); Assert.IsTrue(graph.ContainsEdge(equatableEdge1)); Assert.IsFalse(graph.ContainsEdge(equatableEdge2)); Assert.IsFalse(graph.ContainsEdge(equatableEdge3)); Assert.IsFalse(graph.ContainsEdge(equatableEdge4)); Assert.IsTrue(graph.ContainsEdge(equatableOtherEdge1)); wrappedGraph.AddVerticesAndEdge(edge2); graph = createGraph(); Assert.IsTrue(graph.ContainsEdge(equatableEdge1)); Assert.IsTrue(graph.ContainsEdge(equatableEdge2)); Assert.IsFalse(graph.ContainsEdge(equatableEdge3)); Assert.IsFalse(graph.ContainsEdge(equatableEdge4)); Assert.IsTrue(graph.ContainsEdge(equatableOtherEdge1)); wrappedGraph.AddVerticesAndEdge(edge3); graph = createGraph(); Assert.IsTrue(graph.ContainsEdge(equatableEdge1)); Assert.IsTrue(graph.ContainsEdge(equatableEdge2)); Assert.IsTrue(graph.ContainsEdge(equatableEdge3)); Assert.IsFalse(graph.ContainsEdge(equatableEdge4)); Assert.IsTrue(graph.ContainsEdge(equatableOtherEdge1)); wrappedGraph.AddVerticesAndEdge(edge4); graph = createGraph(); Assert.IsTrue(graph.ContainsEdge(equatableEdge1)); Assert.IsTrue(graph.ContainsEdge(equatableEdge2)); Assert.IsTrue(graph.ContainsEdge(equatableEdge3)); Assert.IsTrue(graph.ContainsEdge(equatableEdge4)); Assert.IsTrue(graph.ContainsEdge(equatableOtherEdge1)); wrappedGraph.AddVerticesAndEdge(otherEdge1); graph = createGraph(); Assert.IsTrue(graph.ContainsEdge(equatableEdge1)); Assert.IsTrue(graph.ContainsEdge(equatableEdge2)); Assert.IsTrue(graph.ContainsEdge(equatableEdge3)); Assert.IsTrue(graph.ContainsEdge(equatableEdge4)); Assert.IsTrue(graph.ContainsEdge(equatableOtherEdge1)); // Both vertices not in graph Assert.IsFalse(graph.ContainsEdge(new SEquatableEdge <int>(0, 10))); // Source not in graph Assert.IsFalse(graph.ContainsEdge(new SEquatableEdge <int>(0, 1))); // Target not in graph Assert.IsFalse(graph.ContainsEdge(new SEquatableEdge <int>(1, 0))); }
public void ToUndirectedGraph02162() { UndirectedGraph<int, SEquatableEdge<int>> undirectedGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[3]; SEquatableEdge<int> s0 = new SEquatableEdge<int>(1, 2); sEquatableEdges[0] = s0; SEquatableEdge<int> s1 = new SEquatableEdge<int>(172, 172); sEquatableEdges[1] = s1; undirectedGraph = this.ToUndirectedGraph02<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)undirectedGraph); Assert.IsNotNull(undirectedGraph.EdgeEqualityComparer); Assert.AreEqual<int>(4, undirectedGraph.EdgeCapacity); Assert.AreEqual<bool>(false, undirectedGraph.IsDirected); Assert.AreEqual<bool>(true, undirectedGraph.AllowParallelEdges); Assert.AreEqual<int>(3, undirectedGraph.EdgeCount); }
public void ToAdjacencyGraph04423() { AdjacencyGraph<int, SEquatableEdge<int>> adjacencyGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[3]; SEquatableEdge<int> s0 = new SEquatableEdge<int>(37, 782); sEquatableEdges[0] = s0; SEquatableEdge<int> s1 = new SEquatableEdge<int>(326, 326); sEquatableEdges[1] = s1; adjacencyGraph = this.ToAdjacencyGraph04<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)adjacencyGraph); Assert.AreEqual<bool>(true, adjacencyGraph.IsDirected); Assert.AreEqual<bool>(true, adjacencyGraph.AllowParallelEdges); Assert.AreEqual<int>(-1, adjacencyGraph.EdgeCapacity); Assert.AreEqual<bool>(false, adjacencyGraph.IsEdgesEmpty); Assert.AreEqual<int>(3, adjacencyGraph.EdgeCount); }
protected override void InternalCompute() { var cancelManager = this.Services.CancelManager; // matrix i,j -> path this.data.Clear(); var vertices = this.VisitedGraph.Vertices; var edges = this.VisitedGraph.Edges; // prepare the matrix with initial costs // walk each edge and add entry in cost dictionary foreach (var edge in edges) { var ij = EdgeExtensions.ToVertexPair <TVertex, TEdge>(edge); var cost = this.weights(edge); VertexData value; if (!data.TryGetValue(ij, out value)) { data[ij] = new VertexData(cost, edge); } else if (cost < value.Distance) { data[ij] = new VertexData(cost, edge); } } if (cancelManager.IsCancelling) { return; } // walk each vertices and make sure cost self-cost 0 foreach (var v in vertices) { data[new SEquatableEdge <TVertex>(v, v)] = new VertexData(0, default(TEdge)); } if (cancelManager.IsCancelling) { return; } // iterate k, i, j foreach (var vk in vertices) { if (cancelManager.IsCancelling) { return; } foreach (var vi in vertices) { var ik = new SEquatableEdge <TVertex>(vi, vk); VertexData pathik; if (data.TryGetValue(ik, out pathik)) { foreach (var vj in vertices) { var kj = new SEquatableEdge <TVertex>(vk, vj); VertexData pathkj; if (data.TryGetValue(kj, out pathkj)) { double combined = this.distanceRelaxer.Combine(pathik.Distance, pathkj.Distance); var ij = new SEquatableEdge <TVertex>(vi, vj); VertexData pathij; if (data.TryGetValue(ij, out pathij)) { if (this.distanceRelaxer.Compare(combined, pathij.Distance) < 0) { data[ij] = new VertexData(combined, vk); } } else { data[ij] = new VertexData(combined, vk); } } } } } } // check negative cycles foreach (var vi in vertices) { var ii = new SEquatableEdge <TVertex>(vi, vi); VertexData value; if (data.TryGetValue(ii, out value) && value.Distance < 0) { throw new NegativeCycleGraphException(); } } }
private static void OnCyclingEdgeFound(IEnumerable <IEdge <Identifier> > examinedEdges, ICollection <IReadOnlyCollection <Identifier> > cycles, SEquatableEdge <Identifier> e) { var startingNode = e.Target; var nextNode = e.Source; var knownNodes = new List <Identifier> { startingNode, nextNode }; var edges = examinedEdges.Reverse().Skip(1); // skipping first edge because that's the back edge foreach (var edge in edges) { if (edge.Target != nextNode) { continue; } if (!knownNodes.Contains(edge.Source)) { knownNodes.Add(edge.Source); nextNode = edge.Source; } else { knownNodes.Reverse(); if (!ContainsCycle(cycles, knownNodes)) { cycles.Add(knownNodes); } return; } } }
public void ToBidirectionalGraph05933() { BidirectionalGraph<int, SEquatableEdge<int>> bidirectionalGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[3]; SEquatableEdge<int> s0 = new SEquatableEdge<int>(704643108, 97); sEquatableEdges[0] = s0; SEquatableEdge<int> s1 = new SEquatableEdge<int>(235929636, 97); sEquatableEdges[1] = s1; bidirectionalGraph = this.ToBidirectionalGraph05<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)bidirectionalGraph); Assert.AreEqual<int>(-1, bidirectionalGraph.EdgeCapacity); Assert.AreEqual<bool>(true, bidirectionalGraph.IsDirected); Assert.AreEqual<bool>(true, bidirectionalGraph.AllowParallelEdges); Assert.AreEqual<bool>(false, bidirectionalGraph.IsEdgesEmpty); Assert.AreEqual<int>(3, bidirectionalGraph.EdgeCount); }
public void ToUndirectedGraph02166() { UndirectedGraph<int, SEquatableEdge<int>> undirectedGraph; SEquatableEdge<int>[] sEquatableEdges = new SEquatableEdge<int>[3]; SEquatableEdge<int> s0 = new SEquatableEdge<int>(1623335945, 1623335945); sEquatableEdges[0] = s0; SEquatableEdge<int> s1 = new SEquatableEdge<int>(1887439544, default(int)); sEquatableEdges[1] = s1; SEquatableEdge<int> s2 = new SEquatableEdge<int>(2017597721, -260044104); sEquatableEdges[2] = s2; undirectedGraph = this.ToUndirectedGraph02<int> ((IEnumerable<SEquatableEdge<int>>)sEquatableEdges); Assert.IsNotNull((object)undirectedGraph); Assert.IsNotNull(undirectedGraph.EdgeEqualityComparer); Assert.AreEqual<int>(4, undirectedGraph.EdgeCapacity); Assert.AreEqual<bool>(false, undirectedGraph.IsDirected); Assert.AreEqual<bool>(true, undirectedGraph.AllowParallelEdges); Assert.AreEqual<int>(3, undirectedGraph.EdgeCount); }