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();
}
}
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();
}
}