Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public WeightedGraph<V, E> createOstovTreeViaMethodPrim() {
- WeightedGraph<V, E> graph = new WeightedGraph<V, E>();
- for (Object v : this.getVertices())
- graph.addVertex((V)v);
- Set<Integer> tracked_vertices = new HashSet<Integer>();
- tracked_vertices.add((int)this.getVertices().iterator().next());
- Object min_edge = null, edge = new Object();
- while (tracked_vertices.size() != this.getVertexCount()) {
- for (Object track_vertex : tracked_vertices) {
- for (Object neighbor_untrack : this.getIncidentEdges(track_vertex)) {
- if (!graph.containsEdge(neighbor_untrack)) {
- if (min_edge == null || getEdgeWeight(min_edge) > getEdgeWeight(neighbor_untrack)) {
- min_edge = neighbor_untrack;
- }
- }
- }
- }
- graph.addEdge(min_edge, this.getIncidentVertices(min_edge), getEdgeWeight(min_edge));
- tracked_vertices.addAll(this.getIncidentVertices(min_edge));
- min_edge = null;
- }
- return graph;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement