/// <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)); }
/// <summary> /// Creates a miminum spanning tree of the given list of vertices, where the vertices form a complete graph. /// </summary> /// <param name="a_vertices"></param> /// <returns>A graph representing the minimum spanning tree</returns> public static IGraph MinimumSpanningTree(IEnumerable <Vertex> a_vertices) { var graph = new AdjacencyListGraph(a_vertices); graph.MakeComplete(); return(MinimumSpanningTree(graph)); }
/// <summary> /// Tries to find a perfect matching with minimum weight of the given vertices, where the vertices form a complete graph /// </summary> /// <param name="a_vertices"></param> /// <returns>Collection of matching edges.</returns> public static IEnumerable <Edge> MinimumWeightPerfectMatching(List <Vertex> a_vertices) { // first, create a complete graph of the vertices var result = new AdjacencyListGraph(a_vertices); result.MakeComplete(); return(GreedyMinimumWeightPerfectMatching(result)); }
/// <summary> /// Creates a t-spanner using a greedy algorithm trying the shortest edges first. /// </summary> /// <param name="a_vertices"> the vertices on which we want to construct a t-spanner</param> /// <param name="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(List <Vertex> a_vertices, float a_t) { var completeGraph = new AdjacencyListGraph(a_vertices); completeGraph.MakeComplete(); // return t-spanner of complete graph return(GreedySpanner(completeGraph, a_t)); }
public void GreedySpannerCompleteTest() { Assert.True(m_complete4.Equals(Spanner.GreedySpanner(m_complete4pos, 1))); //large test case var pos = new List <Vertex> { new Vertex(9, 3), new Vertex(5, 6), new Vertex(4, 7), new Vertex(-2, 5), new Vertex(6, -3), new Vertex(23, 3), new Vertex(22, 4), new Vertex(9.5f, -3.4f), new Vertex(5.5f, -6.3f), new Vertex(-4.5f, 7.4f), new Vertex(-2.5f, -5.3f), new Vertex(6.5f, -3.3f), new Vertex(23.5f, -4.3f), new Vertex(22.5f, -5.3f) }; var completegraph = new AdjacencyListGraph(pos); completegraph.MakeComplete(); var spanner = Spanner.GreedySpanner(pos, 1); Assert.True(completegraph.Equals(spanner)); }
public void EqualGraphTest() { var otherGraph = new AdjacencyListGraph(); var lv2complete = new AdjacencyListGraph(m_level2pos); lv2complete.MakeComplete(); Assert.True(m_complete4.Equals(m_complete4)); Assert.False(otherGraph.Equals(m_complete4)); // Graph on the same vertices Assert.False(lv2complete.Equals(otherGraph)); //Same graphs on different vertex set Assert.True(lv2complete.Equals(lv2complete)); }
public TSPTest() { m_points = new List <Vertex>() { new Vertex(0, 0), new Vertex(2, 0), new Vertex(0, 1), new Vertex(1.5f, -1) }; m_graph = new AdjacencyListGraph(m_points); m_graph.AddEdge(m_points[0], m_points[2]); m_graph.AddEdge(m_points[2], m_points[1]); m_graph.AddEdge(m_points[1], m_points[3]); m_graph.AddEdge(m_points[3], m_points[0]); m_complete4 = new AdjacencyListGraph(m_points); m_complete4.MakeComplete(); expTSP = 1 + Mathf.Sqrt(5) + Mathf.Sqrt(1.25f) + Mathf.Sqrt(3.25f); }