Ejemplo n.º 1
0
        public Dijkstra(Graph graph, int source)
        {
            this.graph = graph;
            this.source = source;

            distance = new int[graph.Vertices.Count];
        }
 public static Graph<int> FromFile(string path, bool isDirected)
 {
     var g = new Graph<int>();
     using (var stream = File.OpenRead(path))
     {
         using (var reader = new StreamReader(stream))
         {
             string line;
             while ((line = reader.ReadLine() ) != null)
             {
                 var vertices = line.Split(new[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
                 if (vertices.Any())
                 {
                     var v = g.GetOrCreateVertex(int.Parse(vertices[0]));
                     foreach (var adj in vertices.Skip(1))
                     {
                         var tokens = adj.Split(',');
                         if (tokens.Length == 2)
                         {
                             int i2 = int.Parse(tokens[0]);
                             int weight = int.Parse(tokens[1]);
                             var v2 = g.GetOrCreateVertex(i2);
                             v.AddEdge(v2.Key, weight);
                         }
                     }
                 }
             }
         }
     }
     g.IsDirected = isDirected;
     return g;
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            const string path = @"D:\Projects\Algorithms\ShortestPath\dijkstraData.txt";
            //const string path = @"D:\Projects\Algorithms\ShortestPath\temp.txt";
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var graph = new Graph(path);
            var loadTime = sw.Elapsed;
            sw.Restart();

            //var route = new Route();
            //Vertex currentNode = graph.Vertices[0];
            //route.Add(currentNode);
            //for (int i = 0; i < 5; i++)
            //{
            //    route.Add(currentNode.AdjacentEdges[0].Tail);
            //    currentNode = currentNode.AdjacentEdges[0].Tail;
            //}

            foreach (var i in new[] { 7, 37, 59, 82, 99, 115, 133, 165, 188, 197 })
            {
                var cost = graph.GetShortestPath(1, i).Cost;
            }
            sw.Stop();
            var workTime = sw.Elapsed;

            Console.WriteLine("Load time: {0}ms, work time: {1}ms, total: {2}ms", loadTime.Milliseconds, workTime.Milliseconds, (loadTime+workTime).Milliseconds);
            //result.Clear();

            //foreach (var i in new[] { 7, 37, 59, 82, 99, 115, 133, 165, 188, 197 })
            //{
            //    result.Add(graph.GetShortestPath(i, 1).Cost);
            //    //Console.WriteLine("from 1 to {0} {1}", i, );
            //}

            //Console.WriteLine(String.Join(",", result));

            //for (int i = 2; i < 31; i++)
            //{
            //    var route = graph.GetShortestPath(1, i);
            //    result.Add(route == null ? 1000000 : route.Cost);
            //}

            //Console.WriteLine(String.Join(",", result));

            //result.Clear();

            //for (int i = 2; i < 31; i++)
            //{
            //    var route = graph.GetShortestPath(i, 1);
            //    result.Add(route == null ? 1000000 : route.Cost);
            //}

            //Console.WriteLine(String.Join(",", result));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parallel Dijkstra algorithm
        /// </summary>
        /// 
        /// <param name="graph">
        /// <see cref="Graph"/> object to check the path for
        /// </param>
        /// 
        /// <param name="start">
        /// Start
        /// </param>
        /// 
        /// <returns>
        /// List of <see cref="Link"/> objects
        /// </returns>
        private static List<Link> Dijkstra_p(Graph graph, int start)
        {
            List<Vertex> P =
                (from v in graph.Vertices
                 select new Vertex(v, graph.Links)).ToList();

            var qT =
                from vertex in P
                where vertex.ID != start
                orderby vertex.ID ascending
                select vertex;

            Queue<Vertex> Q = new Queue<Vertex>(qT);

            Vertex curr = P[start];
            curr.closest_distance = 0.0f;

            while (Q.Count > 0)
            {
                Barrier barrier = new Barrier(curr.neighbors.Count + 1);

                foreach (Tuple<int, double> n in curr.neighbors)
                {
                    var tuple = n;
                    ThreadPool.QueueUserWorkItem(
                        obj =>
                        {
                            Vertex neighbor = P[tuple.Item1];
                            double dist = tuple.Item2 + curr.closest_distance;

                            bool contains = (from v in Q where v.ID == tuple.Item1 select v).Count() > 0;

                            if (contains && (dist < neighbor.closest_distance))
                            {
                                neighbor.closest = curr;
                                neighbor.closest_distance = dist;
                            }

                            barrier.SignalAndWait();
                        },
                        null);
                }

                barrier.SignalAndWait();

                barrier.Dispose();

                curr = Q.Dequeue();
            }

            return
                (from v in P
                 where v.ID != start
                 select new Link(v.ID, v.closest.ID, v.closest_distance)).ToList();
        }
        public void TestSmallGraph()
        {
            var g = new Graph<int>();
            for (int i = 0; i < 4; ++i) g.GetOrCreateVertex(i);

            g.Get(0).AddEdge(1, 1);
            g.Get(0).AddEdge(2, 3);
            g.Get(1).AddEdge(2, 1);
            g.Get(1).AddEdge(3, 3);
            g.Get(2).AddEdge(3, 1);

            var algorithm = new Dijkstra<int>(g, 0);
            algorithm.Run();

            Assert.AreEqual(0, g.Get(0).Score);
            Assert.AreEqual(1, g.Get(1).Score);
            Assert.AreEqual(2, g.Get(2).Score);
            Assert.AreEqual(3, g.Get(3).Score);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sequential version
        /// </summary>
        /// 
        /// <param name="graph">
        /// <see cref="Graph"/> object to check the path for
        /// </param>
        /// 
        /// <param name="start">
        /// Start
        /// </param>
        /// 
        /// <returns>
        /// List of <see cref="Link"/> objects
        /// </returns>
        private static List<Link> Dijkstra(Graph graph, int start)
        {
            List<Vertex> P =
                (from v in graph.Vertices
                 select new Vertex(v, graph.Links)).ToList();

            var qT =
                from vertex in P
                where vertex.ID != start
                orderby vertex.ID ascending
                select vertex;

            Queue<Vertex> Q = new Queue<Vertex>(qT);

            Vertex curr = P[start];
            curr.closest_distance = 0.0f;

            while (Q.Count > 0)
            {
                foreach (Tuple<int, double> n in curr.neighbors)
                {
                    Vertex neighbor = P[n.Item1];
                    double dist = n.Item2 + curr.closest_distance;

                    bool contains = (from v in Q where v.ID == n.Item1 select v).Count() > 0;

                    if (contains && (dist < neighbor.closest_distance))
                    {
                        neighbor.closest = curr;
                        neighbor.closest_distance = dist;
                    }
                }

                curr = Q.Dequeue();
            }

            return
                (from v in P
                 where v.ID != start
                 select new Link(v.ID, v.closest.ID, v.closest_distance)).ToList();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Graph graph = new Graph();
            graph.AddEdge(0, 1, 7);
            graph.AddEdge(0, 2, 9);
            graph.AddEdge(0, 5, 14);
            graph.AddEdge(1, 2, 10);
            graph.AddEdge(1, 3, 15);
            graph.AddEdge(2, 3, 11);
            graph.AddEdge(2, 5, 2);
            graph.AddEdge(3, 4, 6);
            graph.AddEdge(4, 5, 9);

            Dijkstra dijkstra = new Dijkstra(graph, graph.Vertices.First());
            dijkstra.Run();

            Console.WriteLine(dijkstra.Output());
            Console.WriteLine("Press any key..");
            Console.ReadKey();
        }
Ejemplo n.º 8
0
 public EditNode(Graph graph)
 {
     InitializeComponent();
     this.CurrentGraph = graph.Copy();
 }
Ejemplo n.º 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="graph"></param>
 public DijkstraModule(Graph graph)
 {
     this.graph = graph;
 }