Java写无向图的基于DFS的最小生成树算法

最小生成树一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。无向图的最小生成树比较简单。代码如下,注意:基于DFS的最小生成树算法,是用到栈这一数据结构的。

import java.util.*;
import java.io.*;


class S                    //栈类,先进后出
{
private final int SIZE = 20;        //栈的大小是20
private int[] st;                   //栈内存储的是int类型的数字
private int top;                    //栈的头指针
public S()                      //构造方法
{
st = new int[SIZE];
top = -1;                       //注意,初始的时候,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);
}
}




class Vertex      //图中用此类来表示一个顶点
{
public char label;              
public boolean wasVisited;
public Vertex(char lab)
{
label = lab;
wasVisited = false;
}
}


class G
{
private final int MAX_VERTS = 20;
private Vertex[] vertexList;
private int adjMat[][];         //邻接矩阵
private int nVerts;             
//private char sortedArray[];
private S theStack;             //栈,用来存储每一个图的节点
public G()
{
vertexList = new Vertex[MAX_VERTS];    //数组
adjMat = new int[MAX_VERTS][MAX_VERTS];   //二维数组模拟邻接矩阵
nVerts = 0;
for(int i = 0; i < MAX_VERTS; i++)    //初始化邻接矩阵
{
for(int j = 0; j < MAX_VERTS; j++)
{
adjMat[i][j] = 0;
}
//sortedArray = new char[MAX_VERTS];     
}
theStack = new S();
}
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 int getAdjUnvisitedVertex(int v)
{
for(int i = 0; i < nVerts; i++)        
if(adjMat[v][i] == 1 && vertexList[i].wasVisited == false)  //某一个顶点有邻接节点并且是未访问过的
return i;
return -1;
}

public void mst()            //用DFS来模拟最小生成树
{
vertexList[0].wasVisited = true;
theStack.push(0);
while(! theStack.isEmpty())
{
int currentVertex = theStack.peek();
int v = getAdjUnvisitedVertex(currentVertex);
if(v == -1)
theStack.pop();
else
{
vertexList[v].wasVisited = true;
theStack.push(v);
displayVertex(currentVertex);
displayVertex(v);
System.out.print(" ");
}
}
for(int j = 0; j < nVerts; j++)              //最后,当栈内的所有节点都为空时,则将节点矩阵置为已访问过
vertexList[j].wasVisited = false;
}

/*public void dfs()
{
vertexList[0].wasVisited = true;
displayVertex(0);
theStack.push(0);
while(! theStack.isEmpty())
{
int v = getAdjUnvisitedVertex(theStack.peek());
if(v == -1)                   //表明栈顶的头元素没有邻接节点
theStack.pop();
else
{
vertexList[v].wasVisited = true;
displayVertex(v);
theStack.push(v);
}
}
for(int j = 0; j < nVerts; j++)
vertexList[j].wasVisited = false;
}*/
}






public class Graph 
{
public static void main(String args[])
{
G theGraph = new G();
theGraph.addVertex('A');   //0
theGraph.addVertex('B');   //1
theGraph.addVertex('C');   //2
theGraph.addVertex('D');   //3
theGraph.addVertex('E');   //4
theGraph.addEdge(0, 1);    //AB
theGraph.addEdge(0, 2);    //BC
theGraph.addEdge(0, 3);    //AD
theGraph.addEdge(0, 4);    //DE
theGraph.addEdge(1, 2);    //BC
theGraph.addEdge(1, 3);    //BD
theGraph.addEdge(1, 4);    //BE
theGraph.addEdge(2, 3);    //CD
theGraph.addEdge(2, 4);    //CE
theGraph.addEdge(3, 4);    //DE
System.out.print("Minimum spanning tree: ");
theGraph.mst();
System.out.println();
}

输入的是类似于一个五角星的图案,然后要求这一图形的最小生成树。输出结果是:AB BC CD DE 

你可能感兴趣的:(Java与数据结构)