YaccConstructor / QuickGraph

Generic Graph Data Structures and Algorithms for .NET
http://yaccconstructor.github.io/QuickGraph/
Microsoft Public License
523 stars 198 forks source link

Is it possible not to use recorders to access edge list in Prim Algorithm? #187

Open petrasvestartas opened 5 years ago

petrasvestartas commented 5 years ago

Is it possible not to use recorders to access edge list in Prim Algorithm?

I took example file and in the last line I call compute Method prim.Compute(); How can I access the minimal spanning tree edges after prim.Compute method is called? The initial code use recorders, which results in bigger code, is it possible to directly get a list of edges after prim.Compute is called?

    static void Main(string[] args) {
        GetUndirectedFullGraph(10);
        Prim10();
    }

    private static UndirectedGraph<string, TaggedEdge<string, double>> GetUndirectedFullGraph(int vert)  {
        //Print("Start");
        var usedEdge = new List<KeyValuePair<int, int>>();
        var random = new Random();
        var graph = new UndirectedGraph<string, TaggedEdge<string, double>>();
        var trueGraph = new UndirectedGraph<string, TaggedEdge<string, double>>();
        var ds = new ForestDisjointSet<string>(vert);
        for (int i = 0; i < vert; i++)
        {
            graph.AddVertex(i.ToString());
            trueGraph.AddVertex(i.ToString());
            ds.MakeSet(i.ToString());
            //Print("Loop v: " + i.ToString());
        }
        for (int i = 0; i < vert; i++)
            for (int j = i + 1; j < vert; j++)
            {
                graph.AddEdge(new TaggedEdge<string, double>(i.ToString(), j.ToString(), random.Next(100)));
                //Print("Add Edge " + i.ToString() + " " + j.ToString());
            }

        return graph;
    }

    public static void Prim10()   {
        UndirectedGraph<string, TaggedEdge<string, double>> graph = GetUndirectedFullGraph(10);
        MyPrim(graph, x => x.Tag);

    }

    public static void MyPrim<TVertex, TEdge>(IUndirectedGraph<TVertex, TEdge> g, Func<TEdge, double> edgeWeights) where TEdge : IEdge<TVertex>   {

        List<TEdge> ed = g.Edges.ToList();
        Dictionary<TEdge, double> distances = new Dictionary<TEdge, double>();

        foreach (TEdge e in g.Edges)
        {
            distances[e] = edgeWeights(e);
        }

        PrimMinimumSpanningTreeAlgorithm<TVertex, TEdge> prim = new PrimMinimumSpanningTreeAlgorithm<TVertex, TEdge>(g, e => distances[e]);

        prim.Compute();//---------------------------How can I access edge list of spanning tree?

        }