/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cn.iceson.mstapp;
/**
*
* @author lucky_ice
*/
public class StackX {
private final int SIZE=20;
private int[] st;
private int top;
public StackX(){
st=new int[SIZE];
top=-1;
}
public void push(int j){
st[++top]=j;
}
public int pop(){
return st[top--];
}
public int peek(){
return st[top];
}
public boolean isEmpty(){
return top==-1;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cn.iceson.mstapp;
/**
*
* @author lucky_ice
*/
public class Vertex {
public char label;
public boolean wasVistied;
public Vertex(char lab){
this.label=lab;
wasVistied=false;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cn.iceson.mstapp;
/**
*
* @author lucky_ice
*/
public class Graph {
private final int MAX_VERTS=20;
private Vertex[] vertexlist;
private int[][] adjMat;
private int nVerts;
private StackX theStack;
public Graph(){
vertexlist=new Vertex[MAX_VERTS];
adjMat=new int[MAX_VERTS][MAX_VERTS];
nVerts=0;
for(int j=0;j<MAX_VERTS;j++){
for(int k=0;k<MAX_VERTS;k++){
adjMat[j][k]=0;
}
}
theStack=new StackX();
}
public void addVertex(char lab){
vertexlist[nVerts++]=new Vertex(lab);
}
public void addEdge(int start,int end){
adjMat[start][end]=1;
adjMat[end][start]=1;
}
public void displayVertex(int v){
System.out.print(vertexlist[v].label);
}
public void mst(){
vertexlist[0].wasVistied=true;
theStack.push(0);
while(!theStack.isEmpty()){
int currentVertex=theStack.peek();
int v=getAdjUnvisitedVertex(currentVertex);
if(v==-1){
theStack.pop();
}else{
vertexlist[v].wasVistied=true;
theStack.push(v);
displayVertex(currentVertex);
displayVertex(v);
System.out.print(" ");
}
}
for(int i=0;i<nVerts;i++){
vertexlist[i].wasVistied=false;
}
}
public int getAdjUnvisitedVertex(int v){
for(int i=0;i<nVerts;i++)
if(adjMat[v][i]==1&&vertexlist[i].wasVistied==false)
return i;
return -1;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cn.iceson.mstapp;
/**
*
* @author lucky_ice
*/
public class Test {
public static void main(String[] args){
Graph theGraph=new Graph();
theGraph.addVertex('A');
theGraph.addVertex('B');
theGraph.addVertex('C');
theGraph.addVertex('D');
theGraph.addVertex('E');
theGraph.addEdge(0, 1);
theGraph.addEdge(0, 2);
theGraph.addEdge(0, 3);
theGraph.addEdge(0, 4);
theGraph.addEdge(1, 2);
theGraph.addEdge(1, 3);
theGraph.addEdge(1, 4);
theGraph.addEdge(2, 3);
theGraph.addEdge(2, 3);
theGraph.addEdge(3, 4);
System.out.print("Minimun spanning tree:");
theGraph.mst();
System.out.println();
}
}
书山有路勤为径,学海无涯苦作舟。
浙公网安备 33010602011771号