public static IDictionary <int, double> Get(UndirectedGraph <int, Edge <int> > g)
        {
            var dfs      = new UndirectedDepthFirstSearchAlgorithm <int, Edge <int> >(g);
            var recorder = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edgeWeights => 1.0);

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                return(recorder.Distances);
            }
        }
        public static IDictionary <string, double> Get(UndirectedGraph <string, TaggedEdge <string, string> > g)
        {
            var dfs      = new UndirectedDepthFirstSearchAlgorithm <string, TaggedEdge <string, string> >(g);
            var recorder = new UndirectedVertexDistanceRecorderObserver <string, TaggedEdge <string, string> >(edgeWeights => double.Parse(edgeWeights.Tag));

            using (recorder.Attach(dfs))
            {
                dfs.Compute();
                return(recorder.Distances);
            }
        }
Ejemplo n.º 3
0
        public void Constructor()
        {
            Func <Edge <int>, double> edgeWeights = edge => 1.0;
            var recorder = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edgeWeights);

            Assert.AreSame(edgeWeights, recorder.EdgeWeights);
            Assert.IsNotNull(recorder.DistanceRelaxer);
            Assert.IsNotNull(recorder.Distances);

            var distances = new Dictionary <int, double>();

            recorder = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(
                edgeWeights,
                DistanceRelaxers.ShortestDistance,
                distances);
            Assert.AreSame(edgeWeights, recorder.EdgeWeights);
            Assert.AreSame(DistanceRelaxers.ShortestDistance, recorder.DistanceRelaxer);
            Assert.AreSame(distances, recorder.Distances);
        }
        /// <summary>
        /// Compute
        /// </summary>
        private void Form_Load(object sender, EventArgs e)
        {
            // Crete distance table
            tableDijkstra.Columns.Add();
            tableDijkstra.Rows.Add(tableDijkstra.NewRow());
            for (var i = 0; i < FormMain.nodes.Count; i++)
            {
                tableDijkstra.Columns.Add();
                tableDijkstra.Rows.Add(tableDijkstra.NewRow());
                tableDijkstra.Rows[0][i + 1] = tableDijkstra.Rows[i + 1][0] = i;
            }

            // Create graph
            var graph = new UndirectedGraph <int, Edge <int> >();

            // Add vertices to the graph
            for (var i = 0; i < FormMain.nodes.Count; i++)
            {
                graph.AddVertex(i);
            }

            // Create edges
            for (var i = 0; i < FormMain.edges.Count; i++)
            {
                var quickGraphEdge = new Edge <int>(FormMain.nodes.IndexOf(FormMain.edges[i].nodes[0]), FormMain.nodes.IndexOf(FormMain.edges[i].nodes[1]));
                graph.AddEdge(quickGraphEdge);
            }

            // Compute distances
            for (var source = 0; source < FormMain.nodes.Count; source++)
            {
                // We want to use Dijkstra on this graph
                var dijkstra = new UndirectedDijkstraShortestPathAlgorithm <int, Edge <int> >(graph, edge => 1);

                // attach a distance observer to give us the shortest path distances
                var distObserver = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edge => 1);
                distObserver.Attach(dijkstra);

                // Attach a Vertex Predecessor Recorder Observer to give us the paths
                var predecessorObserver = new UndirectedVertexPredecessorRecorderObserver <int, Edge <int> >();
                predecessorObserver.Attach(dijkstra);

                // Run the algorithm for current Node
                dijkstra.Compute(source);

                // Show distances
                foreach (var target in distObserver.Distances)
                {
                    tableDijkstra.Rows[source + 1][target.Key + 1] = target.Value.ToString();
                    IEnumerable <Edge <int> > edgePath;
                    if (predecessorObserver.TryGetPath(target.Key, out edgePath))
                    {
                        string str = " (";
                        foreach (var nodeID in edgePath.Select(edge => edge.Source).Concat(edgePath.Select(edge => edge.Target)).Distinct().OrderBy(next => distObserver.Distances[next]))
                        {
                            str += string.Format("{0}->", nodeID);
                        }
                        tableDijkstra.Rows[source + 1][target.Key + 1] += str.Substring(0, str.Length - 2) + ")";
                    }
                }
            }
            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
        }
Ejemplo n.º 5
0
        public static string ShortestWayDijsktraAlgorithmUnDirected(GraphVertex vertexD, List <GraphEdge> listEdge, List <GraphVertex> listVertex)
        {
            string s = "";
            UndirectedGraph <GraphVertex, UndirectedEdge <GraphVertex> > graph = new UndirectedGraph <GraphVertex, UndirectedEdge <GraphVertex> >();

            foreach (var vert in listVertex)
            {
                graph.AddVertex(vert);
            }
            foreach (var edge in listEdge)
            {
                graph.AddEdge(new UndirectedEdge <GraphVertex>(edge.StartVertex, edge.EndVertex));
            }
            Dictionary <UndirectedEdge <GraphVertex>, double> edgeCost = new Dictionary <UndirectedEdge <GraphVertex>, double>();

            int i = 0;

            foreach (var edge in graph.Edges)
            {
                double eCost = EdgeCostSearching(edge.Source, edge.Target, listEdge);
                edgeCost.Add(edge, eCost);
                i++;
            }

            IEnumerable <UndirectedEdge <GraphVertex> > pathh;
            Func <UndirectedEdge <GraphVertex>, double> getW = edge => edgeCost[edge];
            UndirectedDijkstraShortestPathAlgorithm <GraphVertex, UndirectedEdge <GraphVertex> >  diijkstra = new UndirectedDijkstraShortestPathAlgorithm <GraphVertex, UndirectedEdge <GraphVertex> >(graph, getW);
            UndirectedVertexDistanceRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> > distObs   = new UndirectedVertexDistanceRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> >(getW);

            using (distObs.Attach(diijkstra))
            {
                UndirectedVertexPredecessorRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> > predObs = new UndirectedVertexPredecessorRecorderObserver <GraphVertex, UndirectedEdge <GraphVertex> >();
                using (predObs.Attach(diijkstra))
                {
                    diijkstra.Compute(vertexD);

                    foreach (KeyValuePair <GraphVertex, double> kvp in distObs.Distances)
                    {
                        s += "From " + vertexD.Name + " to " + kvp.Key.Name + " is " + kvp.Value + "         by ";
                        if (predObs.TryGetPath(kvp.Key, out pathh))
                        {
                            foreach (var t in pathh)
                            {
                                s += "edge " + t.Source.Name + "<->" + t.Target.Name + " ";
                            }
                        }
                        s += System.Environment.NewLine;
                    }
                }
            }
            return(s);
        }
Ejemplo n.º 6
0
        public void Attach()
        {
            // Undirected DFS is used for tests but result may change if using another search algorithm
            // or another starting point
            {
                var recorder = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edge => 1.0);

                var graph = new UndirectedGraph <int, Edge <int> >();

                var dfs = new UndirectedDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.IsEmpty(recorder.Distances);
                }
            }

            {
                var recorder = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edge => 1.0);

                var graph = new UndirectedGraph <int, Edge <int> >();
                graph.AddVertexRange(new[] { 1, 2 });

                var dfs = new UndirectedDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.IsEmpty(recorder.Distances);
                }
            }

            {
                var recorder = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edge => 1.0);

                var edge12 = new Edge <int>(1, 2);
                var edge14 = new Edge <int>(1, 4);
                var edge31 = new Edge <int>(3, 1);
                var edge33 = new Edge <int>(3, 3);
                var edge34 = new Edge <int>(3, 4);
                var edge42 = new Edge <int>(4, 2);
                var graph  = new UndirectedGraph <int, Edge <int> >();
                graph.AddVerticesAndEdgeRange(new[]
                {
                    edge12, edge14, edge31, edge33, edge34, edge42
                });

                var dfs = new UndirectedDepthFirstSearchAlgorithm <int, Edge <int> >(graph);
                using (recorder.Attach(dfs))
                {
                    dfs.Compute();

                    CollectionAssert.AreEqual(
                        new Dictionary <int, double>
                    {
                        [1] = 0,
                        [2] = 1,
                        [3] = 3,
                        [4] = 2
                    },
                        recorder.Distances);
                }
            }
        }
Ejemplo n.º 7
0
        public static void createTables()
        {
            // Create graph
            var graph = new UndirectedGraph<int, Edge<int>>();

            // Add vertices to the graph
            for (var i = 0; i < FormMain.nodes.Count; i++)
                graph.AddVertex(i);

            // Create edges
            var edgeCost = new Dictionary<Edge<int>, double>();
            for (var i = 0; i < FormMain.edges.Count; i++)
            {
                var quickGraphEdge = new Edge<int>(FormMain.nodes.IndexOf(FormMain.edges[i].nodes[0]), FormMain.nodes.IndexOf(FormMain.edges[i].nodes[1]));
                graph.AddEdge(quickGraphEdge);
                edgeCost.Add(quickGraphEdge, FormMain.edges[i].Weight);
            }

            // Initialize tables
            distanceTable = new double[FormMain.nodes.Count, FormMain.nodes.Count];
            pathTable = new List<int>[FormMain.nodes.Count, FormMain.nodes.Count];
            for (var i = 0; i < FormMain.nodes.Count; i++)
                for (var j = 0; j < FormMain.nodes.Count; j++)
                {
                    distanceTable[i, j] = double.PositiveInfinity;
                    pathTable[i, j] = new List<int>();
                }

            // Create tables
            for (var source = 0; source < FormMain.nodes.Count; source++)
            {
                // We want to use Dijkstra on this graph
                var dijkstra = new UndirectedDijkstraShortestPathAlgorithm<int, Edge<int>>(graph, edge => edgeCost[edge]);

                // attach a distance observer to give us the shortest path distances
                var distObserver = new UndirectedVertexDistanceRecorderObserver<int, Edge<int>>(edge => edgeCost[edge]);
                distObserver.Attach(dijkstra);

                // Attach a Vertex Predecessor Recorder Observer to give us the paths
                var predecessorObserver = new UndirectedVertexPredecessorRecorderObserver<int, Edge<int>>();
                predecessorObserver.Attach(dijkstra);

                // Run the algorithm for current Node
                dijkstra.Compute(source);

                // Add values to table
                foreach (var target in distObserver.Distances)
                {
                    distanceTable[source, target.Key] = target.Value;
                    IEnumerable<Edge<int>> path;
                    if (predecessorObserver.TryGetPath(target.Key, out path))
                        pathTable[source, target.Key].AddRange(path.Select(edge => edge.Source).Concat(path.Select(edge => edge.Target)).Distinct().OrderBy(next => distObserver.Distances[next]));
                }
            }

            // Create route tables
            foreach (var source in FormMain.nodes)
                source.routeTable = FormMain.nodes.ToDictionary(target => target, target =>
                    (
                        from edge in FormMain.edges
                        where edge.nodes.Contains(source)
                        let adjacent = edge.nodes[0] != source ? edge.nodes[0] : edge.nodes[1]
                        where !pathTable[adjacent.ID, target.ID].Contains(source.ID)
                        orderby edge.Weight + distanceTable[adjacent.ID, target.ID] ascending
                        select adjacent
                    ).ToList());

            TablesRequired = false;
        }
Ejemplo n.º 8
0
        public static void createTables()
        {
            // Create graph
            var graph = new UndirectedGraph <int, Edge <int> >();

            // Add vertices to the graph
            for (var i = 0; i < FormMain.nodes.Count; i++)
            {
                graph.AddVertex(i);
            }

            // Create edges
            var edgeCost = new Dictionary <Edge <int>, double>();

            for (var i = 0; i < FormMain.edges.Count; i++)
            {
                var quickGraphEdge = new Edge <int>(FormMain.nodes.IndexOf(FormMain.edges[i].nodes[0]), FormMain.nodes.IndexOf(FormMain.edges[i].nodes[1]));
                graph.AddEdge(quickGraphEdge);
                edgeCost.Add(quickGraphEdge, FormMain.edges[i].Weight);
            }

            // Initialize tables
            distanceTable = new double[FormMain.nodes.Count, FormMain.nodes.Count];
            pathTable     = new List <int> [FormMain.nodes.Count, FormMain.nodes.Count];
            for (var i = 0; i < FormMain.nodes.Count; i++)
            {
                for (var j = 0; j < FormMain.nodes.Count; j++)
                {
                    distanceTable[i, j] = double.PositiveInfinity;
                    pathTable[i, j]     = new List <int>();
                }
            }

            // Create tables
            for (var source = 0; source < FormMain.nodes.Count; source++)
            {
                // We want to use Dijkstra on this graph
                var dijkstra = new UndirectedDijkstraShortestPathAlgorithm <int, Edge <int> >(graph, edge => edgeCost[edge]);

                // attach a distance observer to give us the shortest path distances
                var distObserver = new UndirectedVertexDistanceRecorderObserver <int, Edge <int> >(edge => edgeCost[edge]);
                distObserver.Attach(dijkstra);

                // Attach a Vertex Predecessor Recorder Observer to give us the paths
                var predecessorObserver = new UndirectedVertexPredecessorRecorderObserver <int, Edge <int> >();
                predecessorObserver.Attach(dijkstra);

                // Run the algorithm for current Node
                dijkstra.Compute(source);

                // Add values to table
                foreach (var target in distObserver.Distances)
                {
                    distanceTable[source, target.Key] = target.Value;
                    IEnumerable <Edge <int> > path;
                    if (predecessorObserver.TryGetPath(target.Key, out path))
                    {
                        pathTable[source, target.Key].AddRange(path.Select(edge => edge.Source).Concat(path.Select(edge => edge.Target)).Distinct().OrderBy(next => distObserver.Distances[next]));
                    }
                }
            }

            // Create route tables
            foreach (var source in FormMain.nodes)
            {
                source.routeTable = FormMain.nodes.ToDictionary(target => target, target =>
                                                                (
                                                                    from edge in FormMain.edges
                                                                    where edge.nodes.Contains(source)
                                                                    let adjacent = edge.nodes[0] != source ? edge.nodes[0] : edge.nodes[1]
                                                                                   where !pathTable[adjacent.ID, target.ID].Contains(source.ID)
                                                                                   orderby edge.Weight + distanceTable[adjacent.ID, target.ID] ascending
                                                                                   select adjacent
                                                                ).ToList());
            }

            TablesRequired = false;
        }