Java图以及BFS广度优先遍历

package num;
import java.util.*;
import java.io.*;

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 j)
    {
        if(rear == SIZE -1)
            rear = -1;
        queArray[++rear] = j;
    }
    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));
    }
}

class Ver
{
    public char label;
    public boolean wasVisited;
    public Ver(char lab)
    {
        this.label = lab;
        wasVisited = false;
    }
}

class Gra
{
    private final int MAX_VERTS = 20;
    private Ver vertexList[];
    private int adjMat[][];
    private int nVerts;
    private Queue theQueue;
    public Gra()
    {
        vertexList = new Ver[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();
    }
    public void addVertex(char lab)
    {
        vertexList[nVerts++] = new Ver(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 bfs()
    {
        vertexList[0].wasVisited = true;
        displayVertex(0);
        theQueue.insert(0);
        int v2;
        while(! theQueue.isEmpty())           
        {
            int v1 = theQueue.remove();
            while((v2 = getAdjUnvisitedVertex(v1)) != -1)     //注意,这里要用循环了,因为是BFS
            {
                vertexList[v2].wasVisited = true;
                displayVertex(v2);
                theQueue.insert(v2);
            }
        }
        for(int k = 0; k < nVerts; k++)
            vertexList[k].wasVisited = false;
    }
}

public class BFS
{
    public static void main(String args[])
    {
        Gra graph = new Gra();
        graph.addVertex('A');
        graph.addVertex('B');
        graph.addVertex('C');
        graph.addVertex('D');
        graph.addVertex('E');
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(0, 3);
        graph.addEdge(3, 4);
        System.out.print("Visits: ");
        graph.bfs();
        System.out.println();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值