Esempio n. 1
0
        private void onLabelCorrecting(object sender, EventArgs e)
        {
            if (CurrentGraphPanel == null)
            {
                return;
            }

            //Make sure one vertex is selected
            List <ISelectable> selection = CurrentGraphPanel.GetSelection();
            GUIVertex          vert;

            if (selection.Count <= 0 || selection.Count > 1)
            {
                return;
            }
            else if (selection[0] as GUIVertex == null)
            {
                return;
            }
            else
            {
                vert = selection[0] as GUIVertex;
            }

            List <GUIVertex> verts = CurrentGraphPanel.Vertices;
            Graph            g     = CurrentGraphPanel.Graph;

            GraphAlgorithms.LabelCorrecting(g, g.GetVertices().IndexOf(vert.Vertex));
        }
Esempio n. 2
0
        public void GetConnectedVerticesReturnsAllVertices()
        {
            var graph             = CreateTestGraph();
            var connectedVertices = GraphAlgorithms.GetConnectedSubgraph(graph, graph.Vertices.First()).Vertices;

            Assert.That(connectedVertices, Is.EquivalentTo(graph.Vertices));
        }
Esempio n. 3
0
        private static void TestBFS()
        {
            Graph  g  = new Graph();
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();
            Vertex v7 = new Vertex();

            g.AddVertex(v1);
            g.AddVertex(v2);
            g.AddVertex(v3);
            g.AddVertex(v4);
            g.AddVertex(v5);
            g.AddVertex(v6);
            g.AddVertex(v7);

            g.AddEdge(new Edge(v1, v2));
            g.AddEdge(new Edge(v1, v3));
            g.AddEdge(new Edge(v2, v6));
            g.AddEdge(new Edge(v2, v4));
            g.AddEdge(new Edge(v6, v4));
            g.AddEdge(new Edge(v4, v5));

            List <Edge> path = GraphAlgorithms.BreadthFirstSearch(g, 0, 4);
        }
Esempio n. 4
0
        private static void TestResidualNetwork()
        {
            //Create graph from Figure 6.10
            FlowNetwork fn = new FlowNetwork();
            Vertex      v1 = new Vertex();
            Vertex      v2 = new Vertex();
            Vertex      v3 = new Vertex();
            Vertex      v4 = new Vertex();

            fn.AddVertex(v1);
            fn.AddVertex(v2);
            fn.AddVertex(v3);
            fn.AddVertex(v4);

            fn.SetSource(v1);
            fn.SetSink(v4);

            fn.AddEdge(new FlowEdge(v1, v3, 3, 4));
            fn.AddEdge(new FlowEdge(v1, v2, 2, 2));
            fn.AddEdge(new FlowEdge(v2, v3, 2, 3));
            fn.AddEdge(new FlowEdge(v3, v4, 5, 5));
            fn.AddEdge(new FlowEdge(v2, v4, 0, 1));

            Graph g = GraphAlgorithms.CreateResidualNetwork(fn);
        }
Esempio n. 5
0
        public void Undirected()
        {
            var graph = new Graph <int>(false);

            var vertexList = new List <Vertex <int> >();

            for (var i = 1; i < 7; i++)
            {
                vertexList.Add(graph.AddVertex(i));
            }

            /*
             *
             * a = 1
             * b = 2
             * c = 3
             * d = 4
             * e = 5
             * f = 6
             *
             */

            // a
            AddEdge(graph, vertexList, 1, 4, 1);
            AddEdge(graph, vertexList, 1, 2, 13);
            AddEdge(graph, vertexList, 1, 3, 8);

            // b
            AddEdge(graph, vertexList, 2, 3, 15);

            // c
            AddEdge(graph, vertexList, 3, 4, 5);
            AddEdge(graph, vertexList, 3, 5, 3);

            // d
            AddEdge(graph, vertexList, 4, 5, 4);
            AddEdge(graph, vertexList, 4, 6, 5);

            // e
            AddEdge(graph, vertexList, 5, 6, 2);

            var resultGraph = GraphAlgorithms.KruskalsAlgorithm(graph);

            Assert.AreEqual(resultGraph.ContainsEdge(1, 2), true);
            Assert.AreEqual(resultGraph.ContainsEdge(1, 4), true);
            Assert.AreEqual(resultGraph.ContainsEdge(3, 5), true);
            Assert.AreEqual(resultGraph.ContainsEdge(4, 5), true);
            Assert.AreEqual(resultGraph.ContainsEdge(5, 6), true);

            Assert.AreEqual(resultGraph.Edges.Count, 5);

            double totalCost = 0;

            foreach (var edge in resultGraph.Edges)
            {
                totalCost += edge.Weight;
            }

            Assert.AreEqual(totalCost, 23);
        }
Esempio n. 6
0
        public void SubgraphConstructedCorrectly()
        {
            var vertices = new List <Vertex <object> >
            {
                new Vertex <object>(1),
                new Vertex <object>(2),
                new Vertex <object>(3),
                new Vertex <object>(4),
                new Vertex <object>(5),
                new Vertex <object>(6),
                new Vertex <object>(7)
            };
            var edges = new List <Edge <object> >
            {
                new Edge <object>(1, 1, 3),
                new Edge <object>(2, 2, 3),
                new Edge <object>(3, 3, 4),
                new Edge <object>(4, 3, 5),
                new Edge <object>(5, 4, 5),
                new Edge <object>(6, 5, 6),
                new Edge <object>(7, 5, 7)
            };
            var testGraph        = new Graph <object, object>(vertices, edges);
            var subgraphVertices = new uint[] { 3, 4, 5 };
            var subGraph         = GraphAlgorithms.GetSubgraph(testGraph, subgraphVertices);

            Assert.That(subGraph.Vertices.Select(v => v.Id), Is.EquivalentTo(subgraphVertices));
            Assert.That(subGraph.Edges.Select(v => v.Id), Is.EquivalentTo(new ulong[] { 3, 4, 5 }));
            Assert.That(subGraph.GetVertexFromId(3).EdgeIds, Is.EquivalentTo(new ulong[] { 3, 4 }));
            Assert.That(subGraph.GetVertexFromId(4).EdgeIds, Is.EquivalentTo(new ulong[] { 3, 5 }));
            Assert.That(subGraph.GetVertexFromId(5).EdgeIds, Is.EquivalentTo(new ulong[] { 4, 5 }));
        }
Esempio n. 7
0
        private static void TestDijkstra()
        {
            //Graph for figure 4.15 part A
            Graph  g  = new Graph();
            Vertex u1 = new Vertex();
            Vertex u2 = new Vertex();
            Vertex u3 = new Vertex();
            Vertex u4 = new Vertex();
            Vertex u5 = new Vertex();
            Vertex u6 = new Vertex();

            g.AddEdge(new Edge(u1, u2, 2));
            g.AddEdge(new Edge(u1, u3, 8));
            g.AddEdge(new Edge(u2, u3, 5));
            g.AddEdge(new Edge(u2, u4, 3));
            g.AddEdge(new Edge(u3, u2, 6));
            g.AddEdge(new Edge(u3, u5, 0));
            g.AddEdge(new Edge(u4, u3, 1));
            g.AddEdge(new Edge(u4, u5, 7));
            g.AddEdge(new Edge(u4, u6, 6));
            g.AddEdge(new Edge(u5, u4, 4));
            g.AddEdge(new Edge(u6, u5, 2));

            Console.WriteLine("Dijkstra Output:");
            Graph tree = GraphAlgorithms.Dijkstra(g, 0, true);
        }
        public void DFSTest1()
        {
            Graph SomeGraph = new Graph();

            int[] correctDFS  = { 1, 2, 3, 4 };
            int[] correctDFS2 = { 1, 3, 4, 2 };
            int[] tryDFS      = new int[4];

            SomeGraph.AddNode(1);
            SomeGraph.AddNode(2);
            SomeGraph.AddNode(3);
            SomeGraph.AddNode(4);
            SomeGraph.AddEdge(1, 2);
            SomeGraph.AddEdge(1, 3);
            SomeGraph.AddEdge(3, 4);

            int i = 0;

            foreach (int n in GraphAlgorithms.DFS(SomeGraph, 1))
            {
                tryDFS[i++] = n;
            }

            try
            {
                CollectionAssert.AreEqual(correctDFS, tryDFS);
            }
            catch
            {
                CollectionAssert.AreEqual(correctDFS2, tryDFS);
            }
        }
Esempio n. 9
0
        private static void TestDominatingSets()
        {
            Graph g = new Graph();

            g.Directed = false;

            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();
            Vertex v7 = new Vertex();

            g.AddEdge(new Edge(v1, v2));
            g.AddEdge(new Edge(v2, v3));
            g.AddEdge(new Edge(v3, v4));
            g.AddEdge(new Edge(v4, v5));
            g.AddEdge(new Edge(v5, v6));
            g.AddEdge(new Edge(v6, v7));

            //GraphAlgorithms.MinimumDominatingSet(g);

            g.AddEdge(new Edge(v7, v1));
            GraphAlgorithms.MinimumDominatingSet(g);
        }
Esempio n. 10
0
        private void ShortestPath_Click(object sender, RoutedEventArgs e)
        {
            var graph    = GraphArea1.LogicCore.Graph;
            var vertices = graph.Vertices.ToList();

            if (!vertices.Any())
            {
                return;
            }

            var v1 = vertices.First();
            var v2 = vertices.Last();

            GraphArea1.VertexList[v1].Background = new SolidColorBrush(Colors.OrangeRed);
            GraphArea1.VertexList[v2].Background = new SolidColorBrush(Colors.Blue);

            var path = GraphAlgorithms.FindShortestPathUndirected(graph: graph, startVertex: v1, endVertex: v2);

            HighlightUnOrientedPath(path, Colors.DarkOrange);

            path = GraphAlgorithms.FindShortestPath(graph: graph, startVertex: v1, endVertex: v2);
            HighlightPath(path, Colors.LimeGreen);

            GraphArea1.VertexList[v1].Background = new SolidColorBrush(Colors.OrangeRed);
            GraphArea1.VertexList[v2].Background = new SolidColorBrush(Colors.Blue);
        }
Esempio n. 11
0
        public void ShortestPathFoundForGraphWithMultipleEdgesBetweenTwoVertices()
        {
            var vertices = new List <Vertex <object> >
            {
                new Vertex <object>(0),
                new Vertex <object>(1),
                new Vertex <object>(2),
                new Vertex <object>(3),
                new Vertex <object>(4)
            };
            var edges = new List <Edge <object> >
            {
                new Edge <object>(0, 0, 1),
                new Edge <object>(1, 1, 2),
                new Edge <object>(2, 1, 2), // Double edge
                new Edge <object>(3, 2, 3),
                new Edge <object>(4, 1, 4),
                new Edge <object>(5, 2, 4)
            };
            var graph = new Graph <object, object>(vertices, edges);

            Assert.That(() => GraphAlgorithms.ShortestPaths(graph, 0), Throws.Nothing);
            var shortestPathLookup = GraphAlgorithms.ShortestPaths(graph, 0);
            var pathToV3           = shortestPathLookup.PathTo(vertices.Single(v => v.Id == 3));

            Assert.That(pathToV3.PathLength, Is.EqualTo(3));
        }
Esempio n. 12
0
        private void Search_Click(object sender, RoutedEventArgs e)
        {
            var graph = GraphArea1.LogicCore.Graph;
            var path  = GraphAlgorithms.BfSearch(graph: graph).Take(10);

            HighlightVertices(vertices: path, color: Colors.DarkOrange);
        }
Esempio n. 13
0
        private static void TestLabelCorrecting()
        {
            //Graph for figure 5.10 part A
            Graph g = new Graph();

            g.Directed = true;
            Vertex v1 = new Vertex();
            Vertex v2 = new Vertex();
            Vertex v3 = new Vertex();
            Vertex v4 = new Vertex();
            Vertex v5 = new Vertex();
            Vertex v6 = new Vertex();

            g.AddEdge(new Edge(v1, v2, 10));
            g.AddEdge(new Edge(v1, v3, 15));
            g.AddEdge(new Edge(v2, v3, 25));
            g.AddEdge(new Edge(v3, v2, -20));
            g.AddEdge(new Edge(v2, v4, 0));
            g.AddEdge(new Edge(v2, v5, 5));
            g.AddEdge(new Edge(v4, v5, -5));
            g.AddEdge(new Edge(v5, v4, 10));
            g.AddEdge(new Edge(v5, v3, 30));

            Console.WriteLine("Label Correcting Output:");
            Graph labelCorrect = GraphAlgorithms.LabelCorrecting(g, 0);
        }
        public void DijkstraTest1()
        {
            Graph SomeGraph = new Graph();

            SortedDictionary <int, int> correctDijkstraAnswer = new SortedDictionary <int, int>
            {
                { 1, 0 },
                { 2, 8 },
                { 3, 3 },
                { 4, 9 }
            };

            SomeGraph.AddNode(1);
            SomeGraph.AddNode(2);
            SomeGraph.AddNode(3);
            SomeGraph.AddNode(4);
            SomeGraph.AddEdge(1, 2, 8);
            SomeGraph.AddEdge(1, 3, 3);
            SomeGraph.AddEdge(2, 4, 1);
            SomeGraph.AddEdge(3, 4, 10);
            SomeGraph.AddEdge(1, 4, 100);

            SortedDictionary <int, int> tryDijkstra = GraphAlgorithms.Dijkstra(SomeGraph, 1);

            CollectionAssert.AreEqual(correctDijkstraAnswer, tryDijkstra);
        }
Esempio n. 15
0
        public void GetShortestPathTest()
        {
            Graph g = CreateGraph();

            List <int> path;

            GraphAlgorithms.GetShortestPath(g, 0, 5, out path);
        }
Esempio n. 16
0
        public void Directed()
        {
            var graph = new Graph <int>(true);

            var vertexList = new List <Vertex <int> >();

            for (var i = 1; i < 15; i++)
            {
                vertexList.Add(graph.AddVertex(i));
            }

            AddEdge(graph, vertexList, 1, 2, 5);
            AddEdge(graph, vertexList, 1, 5, 4);
            AddEdge(graph, vertexList, 2, 4, 10);
            AddEdge(graph, vertexList, 2, 3, 6);
            AddEdge(graph, vertexList, 3, 4, 2);
            AddEdge(graph, vertexList, 3, 5, 6);
            AddEdge(graph, vertexList, 3, 7, 4);
            AddEdge(graph, vertexList, 5, 6, 1);
            AddEdge(graph, vertexList, 6, 7, 1);
            AddEdge(graph, vertexList, 6, 8, 9);
            AddEdge(graph, vertexList, 6, 9, 5);
            AddEdge(graph, vertexList, 7, 9, 3);
            AddEdge(graph, vertexList, 7, 10, 4);
            AddEdge(graph, vertexList, 9, 10, 6);
            AddEdge(graph, vertexList, 9, 12, 2);
            AddEdge(graph, vertexList, 11, 12, 9);
            AddEdge(graph, vertexList, 11, 13, 8);
            AddEdge(graph, vertexList, 13, 14, 6);

            var resultGraph = GraphAlgorithms.KruskalsAlgorithm(graph);


            Assert.AreEqual(resultGraph.ContainsEdge(1, 2), true);
            Assert.AreEqual(resultGraph.ContainsEdge(1, 5), true);
            Assert.AreEqual(resultGraph.ContainsEdge(5, 6), true);
            Assert.AreEqual(resultGraph.ContainsEdge(6, 8), true);
            Assert.AreEqual(resultGraph.ContainsEdge(6, 7), true);
            Assert.AreEqual(resultGraph.ContainsEdge(7, 3), true);
            Assert.AreEqual(resultGraph.ContainsEdge(3, 4), true);
            Assert.AreEqual(resultGraph.ContainsEdge(7, 9), true);
            Assert.AreEqual(resultGraph.ContainsEdge(7, 10), true);
            Assert.AreEqual(resultGraph.ContainsEdge(9, 12), true);
            Assert.AreEqual(resultGraph.ContainsEdge(12, 11), true);
            Assert.AreEqual(resultGraph.ContainsEdge(11, 13), true);
            Assert.AreEqual(resultGraph.ContainsEdge(13, 14), true);

            Assert.AreEqual(resultGraph.Edges.Count, 13);

            double totalCost = 0;

            foreach (var edge in resultGraph.Edges)
            {
                totalCost += edge.Weight;
            }

            Assert.AreEqual(totalCost, 58);
        }
Esempio n. 17
0
        private static Dictionary <Orbital, Vector3D> CalculateLonePairRepulsion(Graph <Atom, SimpleBond> graph,
                                                                                 IDictionary <uint, Vector3D> forceLookup)
        {
            var lonePairForceLookup = graph.Vertices
                                      .Select(v => (AtomWithOrbitals)v.Object)
                                      .SelectMany(atom => atom.LonePairs)
                                      .ToDictionary(o => o, o => new Vector3D(0, 0, 0));

            foreach (var vertex in graph.Vertices)
            {
                var adjacentVertices         = GraphAlgorithms.GetAdjacentVertices(graph, vertex).Cast <IVertex <Atom> >().ToList();
                var currentAtom              = (AtomWithOrbitals)vertex.Object;
                var filledOuterOrbitals      = currentAtom.OuterOrbitals.Where(o => o.IsFull).ToList();
                var orbitalNeighborVertexMap = MapBondOrbitalToNeighborVertex(
                    filledOuterOrbitals, currentAtom, adjacentVertices);
                for (var orbitalIdx1 = 0; orbitalIdx1 < filledOuterOrbitals.Count; orbitalIdx1++)
                {
                    var orbital1 = filledOuterOrbitals[orbitalIdx1];
                    for (var orbitalIdx2 = orbitalIdx1 + 1; orbitalIdx2 < filledOuterOrbitals.Count; orbitalIdx2++)
                    {
                        var orbital2       = filledOuterOrbitals[orbitalIdx2];
                        var repulsiveForce = CalculateRepulsiveForce(orbital1, orbital2);

                        var orbtial1Vector = currentAtom.Position
                                             .VectorTo(orbital1.MaximumElectronDensityPosition)
                                             .Normalize();
                        var orbital1ParallelForce  = repulsiveForce.ProjectOnto(orbtial1Vector);
                        var orbital1RepulsiveForce = (repulsiveForce - orbital1ParallelForce).ToVector3D();

                        var orbtial2Vector = currentAtom.Position
                                             .VectorTo(orbital2.MaximumElectronDensityPosition)
                                             .Normalize();
                        var orbital2ParallelForce  = repulsiveForce.ProjectOnto(orbtial2Vector);
                        var orbital2RepulsiveForce = (repulsiveForce - orbital2ParallelForce).ToVector3D();

                        if (orbital1.IsPartOfBond)
                        {
                            var neighborVertex = orbitalNeighborVertexMap[orbital1];
                            forceLookup[neighborVertex.Id] += orbital1RepulsiveForce;
                        }
                        else
                        {
                            lonePairForceLookup[orbital1] += orbital1RepulsiveForce;
                        }
                        if (orbital2.IsPartOfBond)
                        {
                            var neighborVertex = orbitalNeighborVertexMap[orbital2];
                            forceLookup[neighborVertex.Id] += -orbital2RepulsiveForce;
                        }
                        else
                        {
                            lonePairForceLookup[orbital2] += -orbital2RepulsiveForce;
                        }
                    }
                }
            }
            return(lonePairForceLookup);
        }
Esempio n. 18
0
        /**********************************/
        /* Constructor                    */
        /**********************************/

        /*
         *  Initialize the form, it makes the
         *  textBox read-only and empty.
         */
        public NeatOffice()
        {
            // Initialization.
            InitializeComponent();
            textBoxCalcScreen.ReadOnly = true;
            textBoxCalcScreen.Text     = String.Empty;
            graphAlObj = new GraphAlgorithms(toolStripProgressBar, toolStripStatusLabelReady, statusStripProgressBar);
            //toolStripStatusLabelGoodDay.Text = "Good Day! Today is " + DateTime.Now.ToString();
        }
Esempio n. 19
0
        public void GetConnectedVerticesReturnsOnlyConnectedVertices()
        {
            var graph = CreateTestGraph();
            var nonConnectedVertex = new Vertex <object>(99);

            graph.AddVertex(nonConnectedVertex);

            var connectedVertices = GraphAlgorithms.GetConnectedSubgraph(graph, graph.Vertices.First()).Vertices;

            Assert.That(connectedVertices, Is.EquivalentTo(graph.Vertices.Except(new[] { nonConnectedVertex })));
        }
Esempio n. 20
0
        private void onAllPairs(object sender, EventArgs e)
        {
            if (CurrentGraphPanel == null)
            {
                return;
            }

            Graph g = CurrentGraphPanel.Graph;

            GraphAlgorithms.AllPairsShortestPath(g);
        }
Esempio n. 21
0
        public void ShortestPathThrowsExceptionIfAnyEdgeHasNegativeWeight()
        {
            // Remove this test if an algorithm with support for
            // negative weights has been implemented.

            var graph = CreateTestGraph();

            graph.Edges.First().Weight = -1;

            Assert.Throws <NotImplementedException>(() => GraphAlgorithms.ShortestPaths(graph, graph.GetVertexFromId(0)));
        }
Esempio n. 22
0
        private void onPrintMatrix(object sender, EventArgs e)
        {
            if (CurrentGraphPanel == null)
            {
                return;
            }

            Graph g = CurrentGraphPanel.Graph;

            g.UpdateMatrix();
            GraphAlgorithms.PrintMatrix(g.GetRawMatrix(), g.GetVertices().Count);
        }
Esempio n. 23
0
        public static void MarkBackbone(this Molecule molecule, MoleculeReference moleculeReference)
        {
            var pathsFromFirstAtom = GraphAlgorithms.ShortestPaths(molecule.MoleculeStructure, moleculeReference.FirstAtomId);
            var pathToLastAtom     = pathsFromFirstAtom.PathTo(molecule.MoleculeStructure.GetVertexFromId(moleculeReference.LastAtomId));
            var pathVertices       = pathToLastAtom.Path
                                     .SelectMany(edge => new[] { edge.Vertex1Id, edge.Vertex2Id })
                                     .Distinct()
                                     .Select(vId => molecule.MoleculeStructure.GetVertexFromId(vId))
                                     .Select(v => v.Object);

            pathVertices.ForEach(atom => atom.IsBackbone = true);
        }
Esempio n. 24
0
        private static void TestAllPairs()
        {
            //Graph for figure 4.15 part B
            Graph  g   = new Graph();
            Vertex v1  = new Vertex();
            Vertex v2  = new Vertex();
            Vertex v3  = new Vertex();
            Vertex v4  = new Vertex();
            Vertex v5  = new Vertex();
            Vertex v6  = new Vertex();
            Vertex v7  = new Vertex();
            Vertex v8  = new Vertex();
            Vertex v9  = new Vertex();
            Vertex v10 = new Vertex();
            Vertex v11 = new Vertex();
            Vertex v12 = new Vertex();

            g.AddVertex(v1);
            g.AddVertex(v2);
            g.AddVertex(v3);
            g.AddVertex(v4);
            g.AddVertex(v5);
            g.AddVertex(v6);
            g.AddVertex(v7);
            g.AddVertex(v8);
            g.AddVertex(v9);
            g.AddVertex(v10);
            g.AddVertex(v11);
            g.AddVertex(v12);

            g.AddEdge(new Edge(v1, v2, 5));
            g.AddEdge(new Edge(v1, v4, 10));
            g.AddEdge(new Edge(v2, v3, 7));
            g.AddEdge(new Edge(v2, v5, 1));
            g.AddEdge(new Edge(v3, v6, 4));
            g.AddEdge(new Edge(v4, v5, 3));
            g.AddEdge(new Edge(v5, v6, 3));
            g.AddEdge(new Edge(v1, v2, 5));
            g.AddEdge(new Edge(v4, v7, 11));
            g.AddEdge(new Edge(v1, v2, 5));
            g.AddEdge(new Edge(v5, v8, 7));
            g.AddEdge(new Edge(v6, v9, 5));
            g.AddEdge(new Edge(v7, v10, 9));
            g.AddEdge(new Edge(v7, v8, 2));
            g.AddEdge(new Edge(v8, v11, 1));
            g.AddEdge(new Edge(v8, v9, 0));
            g.AddEdge(new Edge(v9, v12, 12));
            g.AddEdge(new Edge(v11, v12, 4));

            float[,] d = GraphAlgorithms.AllPairsShortestPath(g);

            GraphAlgorithms.PrintMatrix(d, g.GetVertices().Count);
        }
Esempio n. 25
0
        public void Directed()
        {
            var graph = new Graph <int>(true);

            var vertexList = new List <Vertex <int> >();

            for (var i = 1; i < 15; i++)
            {
                vertexList.Add(graph.AddVertex(i));
            }

            AddEdge(graph, vertexList, 1, 2, 5);
            AddEdge(graph, vertexList, 1, 5, 4);
            AddEdge(graph, vertexList, 2, 4, 10);
            AddEdge(graph, vertexList, 2, 3, 6);
            AddEdge(graph, vertexList, 3, 4, 2);
            AddEdge(graph, vertexList, 3, 5, 6);
            AddEdge(graph, vertexList, 3, 7, 4);
            AddEdge(graph, vertexList, 5, 6, 1);
            AddEdge(graph, vertexList, 6, 7, 1);
            AddEdge(graph, vertexList, 6, 8, 9);
            AddEdge(graph, vertexList, 6, 9, 5);
            AddEdge(graph, vertexList, 7, 9, 3);
            AddEdge(graph, vertexList, 7, 10, 4);
            AddEdge(graph, vertexList, 9, 10, 6);
            AddEdge(graph, vertexList, 9, 12, 2);
            AddEdge(graph, vertexList, 11, 12, 9);
            AddEdge(graph, vertexList, 11, 13, 8);
            AddEdge(graph, vertexList, 13, 14, 6);

            var resultGraph = GraphAlgorithms.PrimsAlgorithm(graph, vertexList[0]);

            Assert.IsTrue(resultGraph.ContainsEdge(1, 2));
            Assert.IsTrue(resultGraph.ContainsEdge(1, 5));
            Assert.IsTrue(resultGraph.ContainsEdge(5, 6));
            Assert.IsTrue(resultGraph.ContainsEdge(6, 8));
            Assert.IsTrue(resultGraph.ContainsEdge(6, 7));
            Assert.IsTrue(resultGraph.ContainsEdge(7, 3));
            Assert.IsTrue(resultGraph.ContainsEdge(3, 4));
            Assert.IsTrue(resultGraph.ContainsEdge(7, 9));
            Assert.IsTrue(resultGraph.ContainsEdge(7, 10));
            Assert.IsTrue(resultGraph.ContainsEdge(9, 12));
            Assert.IsTrue(resultGraph.ContainsEdge(12, 11));
            Assert.IsTrue(resultGraph.ContainsEdge(11, 13));
            Assert.IsTrue(resultGraph.ContainsEdge(13, 14));

            Assert.AreEqual(13, resultGraph.Edges.Count);

            double totalCost = resultGraph.Edges.Sum(edge => edge.Weight);

            Assert.AreEqual(58, totalCost);
        }
Esempio n. 26
0
        public void BreadthFirstSearchTest()
        {
            Graph g = CreateGraph();

            Dictionary <int, int> distances;

            GraphAlgorithms.BreathFirstSearch(g, 0, out distances);

            Assert.AreEqual(0, distances[0], "Distance from 0");
            Assert.AreEqual(1, distances[11], "Distance from 11");
            Assert.AreEqual(2, distances[6], "Distance from 6");
            Assert.AreEqual(3, distances[5], "Distance from 5");
        }
Esempio n. 27
0
        public void ShortestPathFindsExpectedPathLengts()
        {
            var graph = CreateTestGraph();

            graph.Edges.Single(e => e.Vertex1Id == 1 && e.Vertex2Id == 3).Weight = 0.5;

            var shortestPathLookup = GraphAlgorithms.ShortestPaths(graph, graph.GetVertexFromId(0));

            Assert.That(shortestPathLookup.PathLengthTo(graph.GetVertexFromId(0)), Is.EqualTo(0));
            Assert.That(shortestPathLookup.PathLengthTo(graph.GetVertexFromId(1)), Is.EqualTo(1));
            Assert.That(shortestPathLookup.PathLengthTo(graph.GetVertexFromId(2)), Is.EqualTo(2));
            Assert.That(shortestPathLookup.PathLengthTo(graph.GetVertexFromId(3)), Is.EqualTo(1.5));
        }
Esempio n. 28
0
    // Find a path between two vertexes
    public void FindPath(Vertex <string> FromVertex, Vertex <string> ToVertext)
    {
        Debug.LogWarning("FromVertex Data: " + FromVertex.Data);
        Debug.LogWarning("ToVertex Data: " + ToVertext.Data);
        videoQueue.Clear();
        Graph <string> pathGraph = GraphAlgorithms.DijkstrasAlgorithm <string>(videoGraph, FromVertex);
        var            endVertex = pathGraph.GetVertex(ToVertext.Data);

        pathQueue.Clear();
        pathQueue.Enqueue(ToVertext.Data);
        string fromVertexID = FromVertex.Data;
        string nextVertexID = null;
        var    nextVertex   = endVertex;

        while (nextVertexID != fromVertexID)
        {
            nextVertexID = nextVertex.IncidentEdges[0].FromVertex.Data;
            if (nextVertexID == nextVertex.Data)
            {
                nextVertexID = nextVertex.IncidentEdges[1].FromVertex.Data;
            }
            pathQueue.Enqueue(nextVertexID);
            Debug.LogWarning("next vertex: " + nextVertexID);
            nextVertex = pathGraph.GetVertex(nextVertexID);
        }

        var pathQueueSize = pathQueue.Count;

        List <string>[] tmpList  = new List <string> [pathQueueSize];
        var             pathlist = pathQueue.ToList();

        for (int i = pathlist.Count - 1; i > 0; i--)
        {
            Debug.LogWarning("path list i: " + pathlist[i]);
            var list0 = databaseControl.GetVideoList(pathlist[i]);
            Debug.LogWarning("path list i-1: " + pathlist[i - 1]);
            var list1   = databaseControl.GetVideoList(pathlist[i - 1]);
            var commons = list0.Intersect(list1);
            Debug.LogWarning(commons.FirstOrDefault());
            Debug.LogWarning("videoQueue size: " + videoQueue.Count);
            string playInfo = databaseControl.GetPlayInfo(pathlist[i], pathlist[i - 1], commons.FirstOrDefault());
            videoQueue.Enqueue(playInfo);
        }


        reviewMode = false;
        StopAllCoroutines();
        currentFrameList.Clear();
        StartCoroutine(SetLoadFramesForNavigation(videoQueue));
        playNavigationMode = true;
    }
Esempio n. 29
0
        public void HasCyclesReturnsFalseForDiamondGraph()
        {
            var graph = new Graph <object, object>(
                Enumerable.Range(1, 4).Select(vertexId => new Vertex <object>((uint)vertexId)),
                new []
            {
                new Edge <object>(0, 1, 2, isDirected: true),
                new Edge <object>(1, 1, 3, isDirected: true),
                new Edge <object>(2, 2, 4, isDirected: true),
                new Edge <object>(3, 3, 4, isDirected: true)
            });

            Assert.That(GraphAlgorithms.HasCycles(graph), Is.False);
        }
Esempio n. 30
0
        public void HasCyclesThrowsExceptionForPartiallyUndirectedGraph()
        {
            var graph = new Graph <object, object>(
                Enumerable.Range(1, 4).Select(vertexId => new Vertex <object>((uint)vertexId)),
                new []
            {
                new Edge <object>(0, 1, 2),
                new Edge <object>(1, 1, 3),
                new Edge <object>(2, 2, 4, isDirected: true),
                new Edge <object>(3, 3, 4, isDirected: true)
            });

            Assert.That(() => GraphAlgorithms.HasCycles(graph), Throws.Exception);
        }