package assignment1.UWG;
public class EdgeNode {
private int weight;
private String fromVertex;
private String toVertex;
public EdgeNode(int weight,String fromVertex,String toVertex){
this.weight = weight;
this.fromVertex = fromVertex;
this.toVertex = toVertex;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getFromVertex() {
return fromVertex;
}
public void setFromVertex(String fromVertex) {
this.fromVertex = fromVertex;
}
public String getToVertex() {
return toVertex;
}
public void setToVertex(String toVertex) {
this.toVertex = toVertex;
}
@Override
public String toString() {
return weight+" "+fromVertex+" "+toVertex;
}
@Override
public boolean equals(Object obj) {
EdgeNode e = (EdgeNode)obj;
if(this.weight != e.getWeight()){
return false;
}else {
if(this.fromVertex.equals(e.getFromVertex())&&this.toVertex.equals(e.getToVertex())
||this.toVertex.equals(e.getToVertex())&&this.fromVertex.equals(e.getFromVertex()))
{
return true;
}
}
return false;
}
}package assignment1.UWG;
public class VertexNode {
private String name;
private int degree;
public VertexNode(String name){
this.name = name;
this.degree = 0;
}
@Override
public boolean equals(Object obj) {
VertexNode v = (VertexNode) obj;
return this.getName().equals(v.getName());
}
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDegree() {
return degree;
}
public void setDegree(int degree) {
this.degree = degree;
}
}package assignment1.UWG;
import java.util.ArrayList;
import java.util.List;
public class UWG {
private static final int DEFAULT_MAX_VERTICES=10;
private static final int DEFAULT_MAX_EDGES = 100;
private VertexNode[] vertexNodes;
private int vertexIndex;
private int vertexNodesLength;//record the number of vertices
private EdgeNode[] edgeNodes;
private int edgeIndex;
private int edgesDynamicLength;//record the number of edges,
//this number will plus one after each insertion
private int[][] graph;
public UWG(){
this(DEFAULT_MAX_VERTICES,DEFAULT_MAX_EDGES);
}
public UWG(int vertexSize,int edgeSize){
this.vertexNodes = new VertexNode[vertexSize];
this.edgeNodes = new EdgeNode[edgeSize];
this.vertexIndex = 0;
this.vertexNodesLength = 0;
this.edgeIndex = 0;
this.edgesDynamicLength = 0;
}
public int getNumVertices(){
return this.vertexNodesLength;
}
public int getNumEdges(){
return this.edgesDynamicLength;
}
/**
* To create a Minimum Spanning Tree
*Step1.Transform the UWG to its adjacency matrix,
*notice it is symmetric, elements in diagonal ,representing there is no circle in MST
*
* @return
*/
public int[][] getGraph(){
int n = this.vertexNodesLength;
this.graph = new int[n][n];
for(int i=0; i
vertexNodes.length){
throw new OutOfSpaceException("Full,cannot add any vertex into the graph any more.");
}
vertexNodes[vertexIndex] = newVertex;
vertexIndex++;
vertexNodesLength++;
}
public void insertEdge(int weight,String fromVertex,String toVertex){
int from = findVertexIndex(fromVertex);
int to = findVertexIndex(toVertex);
if(from == -1){
throw new IllegalArgumentException("Cannot find the vertex "+ fromVertex);
}
if(to == -1){
throw new IllegalArgumentException("Cannot find the vertex "+ toVertex);
}
EdgeNode newEdge = new EdgeNode(weight,fromVertex,toVertex);
if(edgesDynamicLength > edgeNodes.length){
throw new OutOfSpaceException("Full,cannot add any edge into the graph any more.");
}
edgeNodes[edgeIndex] = newEdge;
edgeIndex++;
edgesDynamicLength++;
}
public int degree(String name){
int degree = 0;
for(EdgeNode edge:edgeNodes){
if(edge.getFromVertex().equals(name)){
degree++;
}
if(edge.getToVertex().equals(name)){
degree++;
}
}
getVertexNode(vertexIndex).setDegree(degree);
return degree;
}
public void computeAllVerticesDegree(){
for(VertexNode vertex:vertexNodes){
vertex.setDegree(degree(vertex.getName()));
}
}
/**
* Return the total weight of the path
* @param vertices:vertices in the path
* @throw IllegalArgumentException if there is not a path through vertices
*/
public int totalPathWeight(String[] vertices){
int total = 0;
if(!pathExists(vertices)){
throw new IllegalArgumentException("There is not a path throgh these vertices");
}
for(int i=1; i
checkedVs = new ArrayList
();
EdgeNode rootEdge = input.findMinEdge(checkedVs);
System.out.println("rootEdge "+rootEdge);
String startVertex = rootEdge.getFromVertex();
String nextVertex = rootEdge.getToVertex();
System.out.println("start vertex "+startVertex);
System.out.println("next vertex "+nextVertex);
if(!startVertex.equals(nextVertex)){
checkedVs.add(startVertex);
checkedVs.add(nextVertex);
mst.insertVertex(startVertex);
mst.insertVertex(nextVertex);
mst.insertEdge(rootEdge.getWeight(), startVertex, nextVertex);
}else{
checkedVs.add(startVertex);
mst.insertVertex(startVertex);
}
EdgeNode nextMinEdge = input.findMinEdge(checkedVs);
// System.out.println("nextMinEdge"+nextMinEdge);
while( nextMinEdge!=null){
String fromVertex = nextMinEdge.getFromVertex();
// System.out.print("StartVertex"+fromVertex);
String toVertex = nextMinEdge.getToVertex();
// System.out.print("--->NextVertex"+toVertex);
// System.out.println("\n");
if(!fromVertex.equals(toVertex)){
if(!checkedVs.contains(fromVertex)){
checkedVs.add(fromVertex);
mst.insertVertex(fromVertex);
// System.out.println("***********************");
// for(VertexNode v:mst.getVertexNodes()){
// System.out.println(v);
// }
}
if(!checkedVs.contains(toVertex)){
checkedVs.add(toVertex);
mst.insertVertex(toVertex);
}
mst.insertEdge(nextMinEdge.getWeight(), fromVertex, toVertex);
}else{
if(!checkedVs.contains(fromVertex)){
checkedVs.add(fromVertex);
mst.insertVertex(fromVertex);
}
}
nextMinEdge = input.findMinEdge(checkedVs);
}
return mst;
}
// private static void prim(int verNum, int[][]weight){
// int[] lowerW = new int[verNum];
// boolean[] checked = new boolean[verNum];
// int result = 0;
//
// //set all vertices as unchecked except the first vertex
//
// checked[0]=true;
// int pos=0;
// for(int i=1;i
"+min);
// }
System.out.println("LowerWeight["+j+"]"+lowerW[j]);
// }
//
//
// if(i
"+pos+" min-->"+min); // } // // result += min; // checked[pos] = true; // // // // update lowest weight // for(int j=0; j
ArrayList
toArrayList(T[] array){ // ArrayList
arrayList = new ArrayList
(); // for(int i=0;i
edges){ int min = Integer.MAX_VALUE; EdgeNode minEdge = null; for(int i=0; i
getEdges(String v1,String v2){ List
edges = new ArrayList
(); for(EdgeNode e:edgeNodes){ if( (e.getFromVertex().equals(v1) && e.getToVertex().equals(v2)) || (e.getFromVertex().equals(v2) && e.getToVertex().equals(v1))){ edges.add(e); } } return edges; } @Override public String toString() { StringBuilder strB = new StringBuilder(); strB.append("first vertex ="+vertexNodes[0].toString()+"\n"); strB.append("first edge =["+edgeNodes[0].getWeight()+" "+edgeNodes[0].getFromVertex()+" "+edgeNodes[0].getToVertex()+"]\n\n"); strB.append("Vertices:\n"); for(int i=0;i
checkedVs = new ArrayList
(); //Empty case EdgeNode e1 = graph3.findMinEdge(checkedVs); System.out.println("Empty case "+e1); checkedVs.add("1"); checkedVs.add("4"); EdgeNode e2 = graph3.findMinEdge(checkedVs); System.out.println("Normal case "+e2); //Terminate case checkedVs.add("2"); checkedVs.add("3"); checkedVs.add("7"); checkedVs.add("6"); checkedVs.add("5"); EdgeNode e3 = graph3.findMinEdge(checkedVs); System.out.println("Terminate case "+e3); } private String strPath(String[] vNames){ StringBuilder strB = new StringBuilder(); strB.append("Path :"); for(String vName:vNames){ strB.append(vName+"-"); } strB.deleteCharAt(strB.length()-1); return strB.toString(); } }