图-无权图-广度优先查询(BFS)

package k_graph.A_Graph.B_BFS;
/**
 * 无向图
 * 广度优先查询
 * @author Administrator
 *
 */
public class BFSApp {
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.addVertex('F');
theGraph.addVertex('G');
theGraph.addVertex('H');
theGraph.addVertex('I');


theGraph.addEdge(0, 1);
theGraph.addEdge(0, 2);
theGraph.addEdge(0, 3);
theGraph.addEdge(1, 4);
theGraph.addEdge(1, 5);
theGraph.addEdge(2, 5);
theGraph.addEdge(2, 6);
theGraph.addEdge(6, 7);
theGraph.addEdge(3, 8);
theGraph.bfs();
}
}package k_graph.A_Graph.B_BFS;


public class Graph {
private final int MAX_VERTS = 20;
private Vertex[] vertexList;// 顶点数组
private int adjMat[][];// 链接矩形
private int nVerts;// 顶点个数
private Queue theQueue;// 队列


public Graph() {
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;
theQueue = new Queue();
}


/**
* 添加顶点

* @param lab
*/
public void addVertex(char lab) {
vertexList[nVerts++] = new Vertex(lab);
}


/**
* 添加邻接矩阵

* @param start
* @param end
*/
public void addEdge(int start, int end) {
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}


/**
* 显示顶点

* @param v
*/
public void displayVertex(int v) {
System.out.println(vertexList[v].label);
}


/**
* 显示途径的
*/
public void bfs() { // breadth-frist-search 广度优先搜索
vertexList[0].warVisited = true;
displayVertex(0);
theQueue.insert(0);
int v2 = 0;
while (!theQueue.isEmpty()) {
int v1 = theQueue.remove();
while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {
vertexList[v2].warVisited = true;
theQueue.insert(v2);
displayVertex(v2);
}
}
for (int i = 0; i < nVerts; i++)
vertexList[i].warVisited = false;


}


/**
* 返回邻接顶点

* @param v1
* @return
*/
public int getAdjUnvisitedVertex(int v1) {
int i = -1;
for (int j = 0; j < nVerts; j++) {
if (adjMat[v1][j] == 1 && vertexList[j].warVisited == false) {
i = j;
break;
}
}
return i;
}
}package k_graph.A_Graph.B_BFS;


/**
 * 队列
 * 
 * @author Administrator
 * 
 */
public class Queue {
private final int SIZE = 20;
private int[] queArray;
private int front;
private int rear;


public Queue() {
queArray = new int[SIZE];
front = 0;
rear = -1;
}


public void insert(int key) {
if (rear == SIZE - 1)
rear = -1;
queArray[++rear] = key;
}


public int remove() {
int temp = queArray[front++];
if (front == SIZE)
front = 0;
return temp;
}


public boolean isEmpty() {
// 前面的表示前面和中间空,后面的表示最后末尾空
return (rear + 1 == front) || (front + SIZE - 1 == rear);
}
}package k_graph.A_Graph.B_BFS;


/**
 * 顶点
 * 
 * @author Administrator
 * 
 */
public class Vertex {
public char label;
public boolean warVisited;


public Vertex(char label) {
this.label = label;
warVisited = false;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值