/** Does not handle Graphs of 1 or fewer nodes */ public Graph kruskal(Graph g) { boolean add = false; Graph mst = new Graph(); // initialize graph Vector edges = g.getEdges(); // get edges Collections.sort(edges); // sort edges // find mst for (int i = 0; i < edges.size(); ++i) { Edge edge = (Edge) edges.get(i); // get edge KNode[] ends = edge.getEnds(); // get ends if (!mst.containsNode(ends[0])) { mst.addNode(ends[0]); // check for first end add = true; } if (!mst.containsNode(ends[1])) { mst.addNode(ends[1]); // check other end add = true; } if (add) // if either end is new add edge { mst.addEdge(edge); add = false; } else // ensure trees are connected if nodes are contained { if (!mst.pathBetween(ends)) mst.addEdge(edge); } } return mst; }
public Graph createGraph2() { Graph graph = new Graph(); KNode n1, n2; n1 = new KNode("A"); graph.addNode(n1); n2 = new KNode("B"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 4)); n1 = new KNode("C"); graph.addNode(n1); n2 = new KNode("D"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 7)); n1 = new KNode("E"); graph.addNode(n1); n2 = new KNode("F"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 10)); n1 = new KNode("G"); graph.addNode(n1); n2 = new KNode("H"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 1)); n1 = new KNode("I"); graph.addNode(n1); graph.addEdge(new Edge((KNode) graph.getNode("B"), (KNode) graph.getNode("C"), 8)); graph.addEdge(new Edge((KNode) graph.getNode("D"), (KNode) graph.getNode("E"), 9)); graph.addEdge(new Edge((KNode) graph.getNode("D"), (KNode) graph.getNode("F"), 14)); graph.addEdge(new Edge((KNode) graph.getNode("C"), (KNode) graph.getNode("F"), 4)); graph.addEdge(new Edge((KNode) graph.getNode("F"), (KNode) graph.getNode("G"), 2)); graph.addEdge(new Edge((KNode) graph.getNode("B"), (KNode) graph.getNode("H"), 11)); graph.addEdge(new Edge((KNode) graph.getNode("H"), (KNode) graph.getNode("A"), 8)); graph.addEdge(new Edge((KNode) graph.getNode("G"), (KNode) graph.getNode("I"), 6)); graph.addEdge(new Edge((KNode) graph.getNode("H"), (KNode) graph.getNode("I"), 7)); graph.addEdge(new Edge((KNode) graph.getNode("C"), (KNode) graph.getNode("I"), 2)); /* GRAHP: (B)---8---(C)---7---(D) / | / \ | \ 4 | 2 \ | 9 / | / \ | \ (A) 11 (I) 4 14 (E) \ | / \ \ | / 8 | 7 6 \ | 10 \ | / \ \ | / (H)---1---(G)---2---(F) MST: (B)---8---(C)---7---(D) / / \ \ 4 2 \ 9 / / \ \ (A) (I) 4 (E) \ \ \ (H)---1---(G)---2---(F) */ return graph; }
public Graph createGraph() { Graph graph = new Graph(); KNode n1, n2; n1 = new KNode("A"); graph.addNode(n1); n2 = new KNode("B"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 1)); n1 = new KNode("C"); graph.addNode(n1); n2 = new KNode("D"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 2)); n1 = new KNode("E"); graph.addNode(n1); n2 = new KNode("F"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 3)); n1 = new KNode("G"); graph.addNode(n1); n2 = new KNode("H"); graph.addNode(n2); graph.addEdge(new Edge(n1, n2, 4)); // box graph.addEdge(new Edge((KNode) graph.getNode("B"), (KNode) graph.getNode("C"), 5)); graph.addEdge(new Edge((KNode) graph.getNode("D"), (KNode) graph.getNode("E"), 6)); graph.addEdge(new Edge((KNode) graph.getNode("F"), (KNode) graph.getNode("G"), 7)); graph.addEdge(new Edge((KNode) graph.getNode("H"), (KNode) graph.getNode("A"), 8)); // cross graph.addEdge(new Edge((KNode) graph.getNode("A"), (KNode) graph.getNode("G"), 9)); graph.addEdge(new Edge((KNode) graph.getNode("C"), (KNode) graph.getNode("G"), 10)); graph.addEdge(new Edge((KNode) graph.getNode("C"), (KNode) graph.getNode("E"), 11)); /* GRAPH: (A)---1---(B)---5---(C)---2---(D) | \ / \ | | \ / \ | | \ / \ | 8 9 10 11 6 | \ / \ | | \ / \ | | \ / \ | (H)---4---(G)---7---(F)---3---(E) MST: (A)---1---(B)---5---(C)---2---(D) | | | 6 | | | (H)---4---(G)---7---(F)---3---(E) */ return graph; }