Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
petrasvestartas opened this issue Dec 25, 2018 · 0 comments

Comments

@petrasvestartas
Copy link

petrasvestartas commented Dec 25, 2018

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?

        }
@petrasvestartas petrasvestartas changed the title Is there at least one fully working example file in the documentation? Is it possible not to use recorders to access edge list in Prim Algorithm? Dec 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant