I have a p开发者_如何转开发roblem that I am really struggling with. I have a set of points with weighed edges and I need to create a minimum spanning tree to find the shortest amount of edges needed. I need to do it in java. Right now I have it creating an adjacency matrix and thats the point im stuck. I really have no idea where to go next. Any help would be awesome.
Take a look on Kruskal and Prim algorithms, I really like Prim because it is very simple to implement and understand: http://en.wikipedia.org/wiki/Prim%27s_algorithm
About your question, what do next (Resumed Prim's algorithm): Choose one random vertex and get the edge with smaller cost, insert it into your MST. While you do not have all vertex at your MST: Choose the edge with smaller cost frm the edges of your MST and insert it at your MST.
If you are trying to find a minimum spanning tree, you will always have the same number of edges, the weights will just be different. The method that I recommend using to solve this problem is Prim's algorithm. It works best when you have distinct weighted edges. Even though someone else has discussed it as a possibility, I will explain it in full here to solve the minimum spanning tree problem with an undirected, connected graph.
The first step to Prim's is starting with any vertex V and add it to a (currently) empty set of vertices called "Discovered". From there, you will look at all of the edges that are adjacent to V (using your adjacency matrix) and are connected to vertices that are not in "Discovered" (using your Discovered set), and add them to a minimum heap structure. The heap will allow you to take the minimum edge and add it to a new tree structure. The other end of that edge is your next new starting vertex. Repeat this process until you have your minimum spanning tree.
精彩评论