Esempio n. 1
0
        public void PriceUpdate(PriceUpdate update)
        {
            lock (lockTheEntireGraph)
            {
                var    from   = new ExchangeCurrency(update.Exchange, update.SourceCurrency);
                var    to     = new ExchangeCurrency(update.Exchange, update.DestinationCurrency);
                double weight = -Math.Log(update.Factor);

                var oldEdge = graph.Adj(from).Where(e => e.To().CompareTo(to) == 0).SingleOrDefault();
                var newEdge = new Edge <ExchangeCurrency>(from, to, weight, update.Timestamp);
                if (oldEdge != null && oldEdge.TimeStamp < update.Timestamp)
                {
                    graph.RemoveEdge(oldEdge);
                    graph.AddEdge(newEdge);
                }
                else if (oldEdge == null) // add 1<-->1 edges by currency
                {
                    graph.AddEdge(newEdge);
                    foreach (var v in graph.Vertexes()
                             .Where(v => v.Currency == update.SourceCurrency)
                             .Where(v => v.CompareTo(from) != 0))
                    {
                        if (!graph.HasEdgeBetween(v, from))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(v, from, -Math.Log(1)));
                        }
                        if (!graph.HasEdgeBetween(from, v))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(from, v, -Math.Log(1)));
                        }
                    }

                    foreach (var v in graph.Vertexes()
                             .Where(v => v.Currency == update.DestinationCurrency)
                             .Where(v => v.CompareTo(to) != 0))
                    {
                        if (!graph.HasEdgeBetween(v, to))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(v, to, -Math.Log(1)));
                        }
                        if (!graph.HasEdgeBetween(to, v))
                        {
                            graph.AddEdge(new Edge <ExchangeCurrency>(to, v, -Math.Log(1)));
                        }
                    }
                }
            }
        }
        public void WeightedDiGraph_Smoke_Test()
        {
            var graph = new WeightedDiGraph <int, int>();

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);
            graph.AddVertex(5);

            graph.AddEdge(1, 2, 1);
            graph.AddEdge(2, 3, 2);
            graph.AddEdge(3, 4, 3);
            graph.AddEdge(4, 5, 1);
            graph.AddEdge(4, 1, 6);
            graph.AddEdge(3, 5, 4);

            Assert.AreEqual(2, graph.GetAllOutEdges(4).Count);
            Assert.AreEqual(2, graph.GetAllInEdges(5).Count);

            Assert.AreEqual(5, graph.VerticesCount);

            Assert.IsTrue(graph.HasEdge(1, 2));

            graph.RemoveEdge(1, 2);

            Assert.IsFalse(graph.HasEdge(1, 2));

            graph.RemoveEdge(2, 3);
            graph.RemoveEdge(3, 4);
            graph.RemoveEdge(4, 5);
            graph.RemoveEdge(4, 1);

            Assert.IsTrue(graph.HasEdge(3, 5));
            graph.RemoveEdge(3, 5);
            Assert.IsFalse(graph.HasEdge(3, 5));

            graph.RemoveVertex(1);
            graph.RemoveVertex(2);
            graph.RemoveVertex(3);
            graph.RemoveVertex(4);

            graph.AddEdge(5, 5, 5);
            graph.RemoveEdge(5, 5);
            graph.RemoveVertex(5);

            Assert.AreEqual(0, graph.VerticesCount);
        }
        /// <summary>
        /// getting rendom paths of only two-direction vertecies for testing
        /// </summary>
        /// <param name="Paths">empty list of paths to fill</param>
        /// <returns></returns>
        public List <int> GetPathsAdvancedIntersect(ref List <List <string> > Paths)
        {
            List <string>         vertecies = new List <string>();
            List <List <string> > edges     = new List <List <string> >();

            // get all vertecies and edges from DB
            DBservices db = new DBservices();

            vertecies = db.GetAllVertecies("TheMoleConnection", "MoviesVertecies");
            edges     = db.GetEdges("TheMoleConnection", "MoviesEdges");

            //1. create a graph
            var graph = new WeightedDiGraph <string, int>();

            //2. insert vertecies to the graph
            foreach (string vertex in vertecies)
            {
                graph.AddVertex(vertex);
            }
            //3. insert edges to the graph
            foreach (var edge in edges)
            {
                graph.AddEdge(edge[0], edge[1], 1);
            }
            //remove one-direction edges
            List <List <string> > twoDirectionsEdges = new List <List <string> >();

            foreach (var edge in edges)
            {
                if (!graph.HasEdge(edge[1], edge[0]))
                {
                    graph.RemoveEdge(edge[0], edge[1]);
                }
                if (graph.HasEdge(edge[1], edge[0]))
                {
                    twoDirectionsEdges.Add(edge);
                }
            }
            //4. create dijkstra algorithm
            var algorithm = new DijikstraShortestPath <string, int>(new DijikstraShortestPathOperators());


            List <int> pathsCount = new List <int>();

            //var result = algorithm.FindShortestPath(graph, Source, Target);
            //pathsCount.Add(result.Path.Count);
            //Paths.Add(result.Path

            //5.run the algoritm for 110 random vertecies.
            for (int i = 0; i < 200; i++)
            {
                int sourceVertex = random.Next(0, vertecies.Count);
                int targetVertex = random.Next(0, vertecies.Count);
                //if source and target pages are the same DON'T run the algorithm
                if (sourceVertex != targetVertex)
                {
                    var result = algorithm.FindShortestPath(graph, vertecies[sourceVertex], vertecies[targetVertex]);
                    pathsCount.Add(result.Path.Count);
                    Paths.Add(result.Path);
                }
            }

            return(pathsCount);
        }