Beispiel #1
0
        public void DirectedWeightedEdge_EqualsWithSameVertexOrderTest()
        {
            // Arrange.
            const int expectedWeight = 2;
            DirectedWeightedEdge <Vertex> edge1;
            DirectedWeightedEdge <Vertex> edge2;
            Vertex SourceVertex;
            Vertex DestinationVertex;
            Vertex vertex3;
            Vertex vertex4;
            string expectedEdge1SourceVertexLabel      = "My label a";
            string expectedEdge1DestinationVertexLabel = "My label b";
            string expectedEdge2SourceVertexLabel      = "My label a";
            string expectedEdge2DestinationVertexLabel = "My label b";

            // Act.
            SourceVertex      = new Vertex(expectedEdge1SourceVertexLabel);
            DestinationVertex = new Vertex(expectedEdge1DestinationVertexLabel);
            vertex3           = new Vertex(expectedEdge2SourceVertexLabel);
            vertex4           = new Vertex(expectedEdge2DestinationVertexLabel);
            edge1             = new DirectedWeightedEdge <Vertex>(SourceVertex, DestinationVertex, expectedWeight);
            edge2             = new DirectedWeightedEdge <Vertex>(vertex3, vertex4, expectedWeight);

            // Assert.
            Assert.AreNotSame(edge1, edge2);
            Assert.AreEqual(edge1, edge2);
            Assert.IsTrue(edge1 == edge2);
            Assert.IsFalse(edge1 != edge2);
        }
Beispiel #2
0
        private void Relax(DirectedWeightedEdge edge)
        {
            int from = edge.From();
            int to   = edge.To();

            if (_distTo[to] > _distTo[from] + edge.Weight())
            {
                _distTo[to] = _distTo[from] + edge.Weight();
                _edgeTo[to] = edge;
            }
        }
Beispiel #3
0
        public void DirectedWeightedEdge_IsSelfLoopTest()
        {
            // Arrange.
            const int expectedWeight = 2;
            DirectedWeightedEdge <Vertex> edge;
            Vertex SourceVertex;
            Vertex DestinationVertex;
            string expectedSourceVertexLabel = "My label 1";

            // Act.
            SourceVertex      = new Vertex(expectedSourceVertexLabel);
            DestinationVertex = SourceVertex;
            edge = new DirectedWeightedEdge <Vertex>(SourceVertex, DestinationVertex, expectedWeight);

            // Assert.
            Assert.IsTrue(edge.IsSelfLoop);
        }
Beispiel #4
0
        private void Relax(DirectedWeightedEdge edge, int target)
        {
            var u = edge.To();
            var v = edge.From();

            if (distTo[u] > distTo[v] + edge.Weight())
            {
                distTo[u] = distTo[v] + edge.Weight();
                edgeTo[u] = edge;

                if (pq.Contains(u))
                {
                    pq.DecreaseKey(u, heuristic(target, u) + distTo[u]);
                }
                else
                {
                    pq.Insert(u, heuristic(target, u) + distTo[u]);
                }
            }
        }
Beispiel #5
0
        public void DirectedWeightedEdge_InitializationWithValuesTest()
        {
            // Arrange.
            const int expectedWeight = 2;
            DirectedWeightedEdge <Vertex> edge;
            Vertex SourceVertex;
            Vertex DestinationVertex;
            string expectedSourceVertexLabel      = "My label 1";
            string expectedDestinationVertexLabel = "My label 2";

            // Act.
            SourceVertex      = new Vertex(expectedSourceVertexLabel);
            DestinationVertex = new Vertex(expectedDestinationVertexLabel);
            edge = new DirectedWeightedEdge <Vertex>(SourceVertex, DestinationVertex, expectedWeight);

            // Assert.
            Assert.IsNotNull(edge.SourceVertex);
            Assert.AreSame(SourceVertex, edge.SourceVertex);
            Assert.IsNotNull(edge.DestinationVertex);
            Assert.AreSame(DestinationVertex, edge.DestinationVertex);
            Assert.AreEqual(expectedWeight, edge.Weight);
        }
Beispiel #6
0
        private void ReleaseEdge(object sender, MouseButtonEventArgs args)
        {
            Contract.Assert(!args.Handled);
            Contract.Assert(sender != null);
            Contract.Assert(_source != null);

            var vertex = sender as Vertex;

            if (vertex != null)
            {
                String sourceName = (String)_source.GetValue(NameProperty),
                       sinkName   = (String)vertex.GetValue(NameProperty);

                if (GetValue(GraphProperty) is DirectedWeightedGraph)
                {
                    var           g      = (DirectedWeightedGraph)GetValue(GraphProperty);
                    Graphs.Vertex source = g.Vertices.Single(v => v.Name == sourceName),
                                  sink   = g.Vertices.Single(v => v.Name == sinkName);
                    var e  = new DirectedWeightedEdge(source, sink, 0); // нужно сделать вызов диалога задания веса
                    var ed = g[source, sink];
                    if (ed == null)
                    {
                        g.AddEdge(e);
                    }
                    else
                    {
                        g.RemoveEdge(ed);
                    }
                }

                if (GetValue(GraphProperty) is DirectedGraph)
                {
                    var           g      = (DirectedGraph)GetValue(GraphProperty);
                    Graphs.Vertex source = g.Vertices.Single(v => v.Name == sourceName),
                                  sink   = g.Vertices.Single(v => v.Name == sinkName);
                    var e  = new DirectedEdge(source, sink);
                    var ed = g[source, sink];
                    if (ed == null)
                    {
                        g.AddEdge(e);
                    }
                    else
                    {
                        g.RemoveEdge(ed);
                    }
                }

                if (GetValue(GraphProperty) is UndirectedGraph)
                {
                    var           g      = (UndirectedGraph)GetValue(GraphProperty);
                    Graphs.Vertex source = g.Vertices.Single(v => v.Name == sourceName),
                                  sink   = g.Vertices.Single(v => v.Name == sinkName);
                    var e  = new UndirectedEdge(source, sink);
                    var ed = g[source, sink];
                    if (ed == null)
                    {
                        g.AddEdge(e);
                    }
                    else
                    {
                        g.RemoveEdge(ed);
                    }
                }
            }

            LayoutRoot.Children.Remove(_arrow);
            _arrow = null;
            LayoutRoot.MouseMove -= DirectEdge;
            foreach (var v in LayoutRoot.Children)
            {
                v.MouseLeftButtonUp -= ReleaseEdge;
            }
            LayoutRoot.ReleaseMouseCapture();
        }