Minimum Spanning Tree Plot

I want to find the smallest spanning tree of the next plot using a quick plot. I went through the provided guide, but I really don't understand how. Here is my code:

 static void Main(string[] args)
        {

        var g = new UndirectedGraph<int, TaggedUndirectedEdge<int,int>>();

        var e1 = new TaggedUndirectedEdge<int, int>(1, 2, 57);
        var e2 = new TaggedUndirectedEdge<int, int>(1, 4, 65);
        var e3 = new TaggedUndirectedEdge<int, int>(2, 3, 500);
        var e4 = new TaggedUndirectedEdge<int, int>(2, 4, 1);
        var e5 = new TaggedUndirectedEdge<int, int>(3, 4, 78);
        var e6 = new TaggedUndirectedEdge<int, int>(3, 5, 200);

        g.AddVerticesAndEdge(e1);
        g.AddVerticesAndEdge(e2);
        g.AddVerticesAndEdge(e3);
        g.AddVerticesAndEdge(e4);
        g.AddVerticesAndEdge(e5);
        g.AddVerticesAndEdge(e6);

        foreach (var v in g.Edges)
            Console.WriteLine(v);
}

      

Thank you for your help.

+3


source to share


2 answers


@daryal beat me to it - my version is:

var mst = g.MinimumSpanningTreePrim(e => e.Tag).ToList();  

      

enter image description here



"Use source Luke" - I looked at MinimumSpanningTreeTest.cs, test Prim12273

(Also you can look at this question on Lambda / Delegates for a better understanding of basic function pointers)

+3


source


Assuming the parameter Tag

(the latter) TaggedUndirectedEdge

implies edgeWeight of the edge, the following might work.



Func<TEdge, double> edgeWeights = (q) => {
g.Edges.SingleOrDefault(m => q == m).Tag;
}

IEnumerable<TEdge> mst = g.MinimumSpanningTreePrim(g, edgeWeights);

      

+1


source







All Articles