예제 #1
0
        /// <summary>
        /// Verifies if the given graph is a t-spanner for the given ratio a_t.
        /// </summary>
        /// <param name="a_graph"></param>
        /// <param name="a_t"></param>
        /// <returns>A vertification struct which holds the ratio and possibly a falsification pair. </returns>
        public static SpannerVerification VerifySpanner(IGraph a_graph, float a_t)
        {
            //first determine the possible edges
            var completeGraph = new AdjacencyListGraph(new List <Vertex>(a_graph.Vertices));

            completeGraph.MakeComplete();
            var edges = completeGraph.Edges.ToList();

            edges.Sort();

            Vertex Start = null;
            Vertex End   = null;
            var    ratio = 1f; // best possible ratio

            foreach (var edge in edges)
            {
                // find distance in given graph
                // TODO all-pair shortest path
                var dist = ShortestPath.ShorthestDistance(a_graph, edge.Start, edge.End);

                // compare ratios
                float edgeratio = dist / edge.Weight;
                if (edgeratio > a_t)
                {
                    Start = edge.Start;
                    End   = edge.End;
                }
                if (ratio <= edgeratio)
                {
                    ratio = edgeratio;
                }
            }
            return(new SpannerVerification(Start, End, ratio));
        }
예제 #2
0
        /// <summary>
        /// Creates a t-spanner using a greedy algorithm trying the shortest edges first.
        /// </summary>
        /// <param name="a_graph"> the graph on which we want to construct a t-spanner</param>
        /// <param name="a_t"> parameter t in the definition of t-spanner. Each pair of vertices should have a path
        ///  of at most length t*eucledian distance</param>
        /// <returns></returns>
        public static IGraph GreedySpanner(IGraph a_graph, float a_t)
        {
            var result = new AdjacencyListGraph(a_graph.Vertices);

            var edges = a_graph.Edges.ToList();

            edges.Sort();   // by default sorts on weight

            foreach (var edge in edges)
            {
                // add edge if t-spanner criteria not satisfied
                if (ShortestPath.ShorthestDistance(result, edge.Start, edge.End) > a_t * edge.Weight)
                {
                    result.AddEdge(edge);
                }
            }
            return(result);
        }