Esempio n. 1
0
 /// <summary>
 /// Creates a new instance of <see cref="EdgeWeightedUndirectedGraph"/> from an existing source.
 /// </summary>
 /// <param name="g">the graph being cloned</param>
 public EdgeWeightedUndirectedGraph(EdgeWeightedUndirectedGraph g)
 {
     vertices = new GraphNode[g.vertices.Length];
     Array.Copy(g.vertices, vertices, vertices.Length);
     edges = new WeightedGraphEdge[g.edges.Length];
     Array.Copy(g.edges, edges, edges.Length);
 }
Esempio n. 2
0
        /// <summary>
        /// Computes a shortest paths tree from <tt>s</tt> to every other vertex in the edge-weighted digraph<tt>G</tt>.
        /// </summary>
        /// <param name="graph">the edge-weighted digraph</param>
        /// <param name="source">the source vertex</param>
        public DijkstraUndirectedSearch(EdgeWeightedUndirectedGraph graph, int source)
        {
            for (int i = graph.NumberOfEdges - 1; i >= 0; i--)
            {
                if (graph.GetEdge(i).Cost < 0)
                {
                    throw new RPGException(ErrorMessage.INTERNAL_BAD_ARGUMENT, "edge " + graph.GetEdge(i) + " has negative weight");
                }
            }
            distTo = new double[graph.NumberOfVertices];
            edgeTo = new WeightedGraphEdge[graph.NumberOfVertices];
            for (int v = 0; v < graph.NumberOfVertices; v++)
            {
                distTo[v] = double.PositiveInfinity;
            }
            distTo[source] = 0.0;

            // relax vertices in order of distance from s
            pq = new IndexMinPQ <Double>(graph.NumberOfVertices);
            pq.Insert(source, distTo[source]);
            while (!pq.IsEmpty)
            {
                int v = pq.DelMin();
                WeightedGraphEdge[] adj = graph.GetVertexAdjacencies(v);
                for (int i = adj.Length - 1; i >= 0; i--)
                {
                    Relax(adj[i], v);
                }
            }

            // check optimality conditions
            Debug.Assert(Check(graph, source));
        }
 public EdgeWeightedDirectedGraph(EdgeWeightedUndirectedGraph g)
 {
     vertices = new GraphNode[g.NumberOfVertices];
     for (int i = 0, len = g.NumberOfVertices; i < len; i++)
     {
         vertices[i] = new GraphNode(i);
     }
     edges = new WeightedGraphEdge[0];
 }
Esempio n. 4
0
        // check optimality conditions:
        // (i) for all edges e: distTo[e.to()] <= distTo[e.from()] + e.weight()
        // (ii) for all edge e on the SPT: distTo[e.to()] == distTo[e.from()] +
        // e.weight()
        private bool Check(EdgeWeightedUndirectedGraph graph, int s)
        {
            // check that edge weights are nonnegative
            for (int i = graph.NumberOfEdges - 1; i >= 0; i--)
            {
                WeightedGraphEdge e = graph.GetEdge(i);
                if (e.Cost < 0)
                {
                    Console.WriteLine("negative edge weight detected");
                    return(false);
                }
            }
            // check that distTo[v] and edgeTo[v] are consistent
            if (distTo[s] != 0.0 || edgeTo[s] != null)
            {
                Console.WriteLine("distTo[s] and edgeTo[s] inconsistent");
                return(false);
            }
            for (int v = graph.NumberOfVertices - 1; v >= 0; v--)
            {
                if (v == s)
                {
                    continue;
                }
                if (edgeTo[v] == null && distTo[v] != Double.PositiveInfinity)
                {
                    Console.WriteLine("distTo[] and edgeTo[] inconsistent");
                    return(false);
                }
            }

            // check that all edges e = v->w satisfy distTo[w] <= distTo[v] +
            // e.weight()
            for (int v = graph.NumberOfVertices - 1; v >= 0; v--)
            {
                WeightedGraphEdge[] adj = graph.GetVertexAdjacencies(v);
                for (int i = adj.Length - 1; i >= 0; i--)
                {
                    WeightedGraphEdge e = adj[i];
                    int w;
                    if (v == e.To)
                    {
                        w = e.From;
                    }
                    else
                    {
                        w = e.To;
                    }
                    if (distTo[v] + e.Cost < distTo[w])
                    {
                        Console.WriteLine("edge " + e + " not relaxed");
                        return(false);
                    }
                }
            }

            // check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] +
            // e.weight()
            for (int w = 0; w < graph.NumberOfVertices; w++)
            {
                if (edgeTo[w] == null)
                {
                    continue;
                }
                WeightedGraphEdge e = edgeTo[w];
                int v = e.From;
                if (w != e.To)
                {
                    return(false);
                }
                if (distTo[v] + e.Cost != distTo[w])
                {
                    Console.WriteLine("edge " + e + " on shortest path not tight");
                    return(false);
                }
            }
            return(true);
        }