I have a problem 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.
Related
I want to determine whether a bipartite graph is separable when there is a vertex whose weight is less than or equal to the threshold. For example, 0.2 is chosen as a threshold.
In figure 1), there is a vertex with red whose weight is less than 0.2. The bipartite graph can be separated into three subgraphs and the red vertex is copied into the three subgraphs respectively.
In figure 2), there is also a vertex with red whose weight is less than 0.2. However, the red edge causes the bipartite graph to not be split into subgraphs.
My idea:
copy the vertex(named lowVer, red) whose weight is less than or equal to the threshold and link the duplicate vertices to associated vertice respectively(green edges). Associated vertice is the vertices connected to the vertex lowVer.
disconnect from the vertex lowVer(yellow edges).
judge whether the bipartite graph is separable by depth-first-search
Is there a better way?
If I understand well, what you want is to know if a given vertex (the one less than the thresold) is an articulation point or not. An articulation point is a vertex that, when removed from the graph, increase the number of connected components.
If I formulated correctly your problem, then there are many algorithms to find articulation points, see for example https://en.wikipedia.org/wiki/Biconnected_component#Other_algorithms or https://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/
There can be many ways to solve this.
Let's pick node with 0.1 weight and put it like the root of the graph.
image
now if the leaf node has degree 1 , its separable
else, it's not separable.
Let me know if i am missing something..
Good morning/afternoon/evening.
So our data structures course gave us an assignment to segment a grayscale image in java using the following algorithm:
Input: A gray-scale image with P pixels and number R
Output: An image segmented into R regions
1. Map the image onto a primal weighted graph.
2. Find an MST of the graph.
3. Cut the MST at the R – 1 most costly edges.
4. Assign the average tree vertex weight to each vertex in each tree in the forest
5. Map the partition onto a segmentation image
The thing is, they just threw us in the dark. They gave us the jgraph package which we had absolutely no experience with (we never studied it) practically saying "go teach yourselves". Nothing new there.
The way I'm going about doing this is by making a class for vertix objects that contains the coordinates of the pixel in addition to its value so that I can add each one both to the graph and a 2D array. Afterwards, I used the array to add weighted edges between adjacent vertices because java can't tell where in the graph a vertix actually is without edges.
Afterwards, I used Kruskal's packaged method for minimum spanning trees and an arraylist to get around the protected status of edge weights in the tree like so:
ArrayList<WeightedEdge> edgeList = new ArrayList<>(height*width*3);
KruskalMinimumSpanningTree mst4 = new KruskalMinimumSpanningTree(map4);
Set<DefaultWeightedEdge> edges = mst4.getSpanningTree().getEdges();
for (DefaultWeightedEdge edge : edges) {
edgeList.add(new WeightedEdge(edge, map4.getEdgeWeight(edge)));
}
edgeList.sort(null);
for (int i = 0; i < n; i++) {
map4.removeEdge(edgeList.get(edgeList.size()-1).getEdge());
}
So now that I cut the (R-1) most costly edges in the graph, I should be left with a forest. And that's where I hit another dead end. How do I get the program to traverse each tree? The way I'm understanding this, I need a general traversal algorithm to visit every tree and assign the average value to each vertix. The problem? There isn't a general traversal algorithm in the package. And there isn't a way to identify individual trees either.
The idea is easy to understand and implement on paper, really. The problems only lie in actually coding all of this in java.
Sorry if this was messy or too long, but I'm just at my wit's end and physical limits. Thank you in advance.
I am a big fan of JGraphT and honestly I think it is pretty good that you're given it for your task assignment. It takes a bit of time to get started, but then it proves to be a very good tool. But you also need to understand the CS behind implemented algorithms, using JGraphT without knowing the theory is somewhat difficult.
From your task assignment I don't really understand step 1 (building the primal weighted graph). The rest should work with JGraphT quite well.
You did step 2 with KruskalMinimumSpanningTree. Now you can sort the edges by weight and remove R-1 top edges from the graph.
I would, however, suggest that you first build a new graph which would represent the calculated MST. And then remove remove R-1 top edges from that graph. Effectively truning it into a forest.
How do I get the program to traverse each tree?
With the forest from the previous step you can use the ConnectivityInspector to get a list of sets of connected vertices. Each set will contain vertices from one of the trees of the forest. Sets of vertices are easy to work with, you don't need any traversal, just iterate over the set.
So I want to make a program to find the shortest path between 2 train stations. What would you suggest is the best way to represent the train lines, where they intersect and search for this? My current thoughts are Adjacency Matrix or List however, I'm not sure as not all adjacent points are linked.
Eg.
The train lines would look like this:
Grey Line stations - Waterloo, Southwark, London Bridge
Black Line stations - Kennington, Oval, Borough, London Bridge
Black line 2 Stations - Kennington, Waterloo, Embankment
Brown Line Stations - Elephant & Castle, Waterloo, Embankment.
if I wanted to to from Oval to Southwark for example, I would go:
Oval to Kennington on Black Line (then switch the Blank line 2)
Kennington to Waterloo (Then switch to Grey line)
Waterloo to Southwark.
This is a shortest paths problem. Since you're interested in shortest time, then just use the durations in place of distances. Dijkstra's algorithm is a common approach for solving these types of problems. It is well documented on the Internet. If you intend to generate a timetable of all trips, then you should look into the Floyd-Warshall algorithm since it can generate solutions for all pairs of stations.
Both of these methods will require you to model the rail system as a graph. The edge weights will be the times to travel between stations. For stations with multiple lines, you could use multiple nodes and edges with a weight of 4 between them.
This is similar to the travelling salesman problem, for which there is no known closed solution, other than trying all possible paths. Note that the number of such paths increases exponentially with the number of stations, so it becomes computationally very expensive quite rapidly.
Also, you have to decide whether you want the shortest distance between the stations, or the shortest travel time between them.
I have a large graph with agents moving from one point to another. the problem is when i want to determine the path between two nodes that are close, the time taken is small. what algorithm can i use to determine the the path between say a node at coordinat 1,1 and a node at coordinate 599,599 for a coordinate system with 600/600 width and height.
Without any further information about the constraints of your specific task, I'd recommend you to have a look at the Dijkstra's algorithm
I am trying to parse an XML file in java, after I have to represent it as a tree using Jframe like this
Trees are generally one of the easier linked constructs to lay out like this, because paths generally don't "merge" or "cross".
You can approach it in a roughly tabular way by traversing the tree "left to right": start at the root, and draw its representation at the top left of the area. Then, traverse its "left" branch one level at a time, drawing those nodes' representations on successively lower "rows", in the same "column" as the root. Then, as you move to the "right"-side nodes, draw that node in the next "column" available to the right on the same level. This will produce a ramp-shaped graph of the tree's structure.
You can add some pre-analysis of the number of levels and nodes at each level, which will allow you to "center" the tree into a rough pyramid shape by knowing the maximum number of levels the graph will require and the number of nodes at each level of that graph. But, that requires traversing the entire graph before you start drawing anything.
As for "arranging" a tree's nodes so they fit in the smallest area without arrows crossing or overlapping, that's a problem with a scope far exceeding the average SO answer.
There are lots of good libraries for visualizing graphs.
Here's a pretty extensive list of options: http://s6ai.livejournal.com/33969.html
Us the standard forms to print the image
http://www.java-forums.org/awt-swing/6763-how-display-image.html
Prefuse could probably create something aesthetically similar. Of course, you could go the primitive route and do the graphics manually. For an XML to graph transform, the TreeMLReader API may be of some use, however you might have to convert the XML to the TreeML format with XSLT first.