题目描述
利用邻接矩阵存储结构,完成深度优先遍历和广度优先遍历算法。已知部分代码如下(勿改动),只需完成深度遍历DFSTraverse和广度遍历BFSTraverse即可。
#include
#include
using namespace std;
//循环队列类
const int QueueSize=100; //循环队列的最大长度
template
class CirQueue
{
public:
CirQueue( ); //构造函数,置空队
~ CirQueue( ); //析构函数
void EnQueue(T x); //将元素x入队
T DeQueue( ); //将队头元素出队
T GetQueue( ); //取队头元素(并不删除)
bool Empty( ); //判断队列是否为空
bool Full();
private:
T data[QueueSize]; //存放队列元素的数组
int front, rear; //队头和队尾指针,分别指向队头元素的前一个位置和队尾元素的位置
};
//功 能:初始化空队列
template
CirQueue
{
front=rear=0;
}
// 功 能:销毁队列
template
CirQueue
{
}
//功 能:元素x入队
template
void CirQueue
{
if (Full()) throw “Overflow”;
rear=(rear+1) % QueueSize; //队尾指针在循环意义下加1
data[rear]=x; //在队尾处插入元素
}
//功 能:队头元素出队,返回值为出队元素
template
T CirQueue
{
if (Empty()) throw “Downflow”;
front=(front+1) % QueueSize; //队头指针在循环意义下加1
return data[front]; //读取并返回出队前的队头元素,注意队头指针
}
//指向队头元素的前一个位置
// 功 能:获取队头元素
template
T CirQueue
{
int i;
if (Empty()) throw “Downflow”;
i=(front+1) % QueueSize; //注意不要给队头指针赋值
return data[i];
}
// 功 能:判断队列是否为空,若空返回true,否则返回false
template
bool CirQueue
{
return front==rear;
}
//功 能:判断队列是否满,若满返回true,否则false
template
bool CirQueue
{
return (rear+1)%QueueSize==front;
}
//Class MGraph
const int MaxSize=20; //图中最多顶点个数
int visited[MaxSize]; //访问标志数组(0表示未访问,1表示已访问)
template
class MGraph
{
public:
MGraph(T a[],int n,int e); //构造函数,初始化具有n个顶点,e条边的图
void DispMGraph(); //输出图中顶点值和矩阵值
void DFSTraverse(int v); // 从v顶点出发深度优先遍历图(一个连通子图)
void BFSTraverse(int v); // 从v顶点出发广度优先遍历图(一个连通子图)
private:
T vertex[MaxSize]; //存放图中顶点信息的数组
int arc[MaxSize][MaxSize]; //存放图中顶点关系(边)的数组
int vertexNum,arcNum; //图的顶点数和边数
};
//构造一个邻接矩阵存储的无向图
template
MGraph
{
vertexNum=n; //顶点数
arcNum=e; //边数
int i,j,k;
for (i=0; i
for (i=0; i
for (k=0; k
cin>>i>>j; //输入各条边依附的两个顶点的序号
arc[i][j]=1; //置有边标志
arc[j][i]=1;
}
}
//输出图中所有顶点信息,和边信息
template
void MGraph
{
int i,j;
cout<<” “;
for(i=0;i
cout<
}
//在此处实现深度优先遍历算法
//在此处实现广度优先遍历算法
int main()
{
string a[]={“A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”}; //顶点信息
int i,n,e;
cin>>n>>e; //输入顶点个数和边个数
MGraph
G.DispMGraph();
for(i=0;i
cout<<”DFS:”;
G.DFSTraverse(0);
cout<
cout<<”BFS:”;
G.BFSTraverse(0);
return 0;
}
输入
第一行为顶点个数和边的条数:6 7
第二行为各条边所对应的顶点序号:0 1 0 3 1 2 1 5 2 4 3 5 4 5
输出
样例输入
6 7 0 1 0 3 1 2 1 5 2 4 3 5 4 5
样例输出
A B C D E F A 0 1 0 1 0 0 B 1 0 1 0 0 1 C 0 1 0 0 1 0 D 1 0 0 0 0 1 E 0 0 1 0 0 1 F 0 1 0 1 1 0 DFS:A B C E F D BFS:A B D C F E
提示
来源
提交
import java.util.Scanner;class Node{Object data;Node next;public Node() {}public Node(Object data) {this.data = data;this.next = null;}public Node(Object data, Node next) {this.data = data;this.next = next;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}class LinkQueue{Node front;Node rear;public LinkQueue(){rear = front = null;}void EnQueue(Object data){Node p =new Node(data);if(front != null){rear.next = p;rear = p;}else{front = rear = p;}}Object DeQueue() throws Exception{if(front != null){Node p = front;front = front.next;if(p==rear){rear = null;}return p.data;}else{throw new Exception("Downflow");}}Object GetQueue() throws Exception{if(front != null){return front.data;}else{throw new Exception("Downflow");}}boolean isEmpty(){return front == null;}public void offer(Object o) {Node p = new Node(o);if (front != null) {rear.next=p;rear = p;} elsefront = rear = p;}public Object peek() {if (front != null)return front.data;elsereturn null;}public Object poll() {if (front != null) {Node p = front;front = front.next;if (p==rear)rear=null;return p.data;} elsereturn null;}}//图的接口interface IGraph {void createGraph();// 创建一个图int getVexNum(); // 返回顶点数int getArcNum();// 返回边数Object getVex(int v) throws Exception;// 返回v表示结点的值, 0 <= v < vexNumint locateVex(Object vex);// 给定顶点的值vex,返回其在图中的位置,如果图中不包含此顶点,则返回-1int firstAdjVex(int v) throws Exception; // 返回v的第一个邻接点,若v没有邻接点,则返回-1,其中0≤v<vexNumint nextAdjVex(int v, int w) throws Exception;// 返回v相对于w的下一个邻接点,若w是v的最后一个邻接点,则返回-1,其中0≤v,// w<vexNum}// 图的实现class MGraph implements IGraph {public final static int INFINITY = Integer.MAX_VALUE;private int vexNum, arcNum;// 图的当前顶点数和边数private Object[] vexs;;// 顶点private int[][] arcs;;// 邻接矩阵private static boolean[] visited;// 访问标志数组public MGraph() {this(0, 0, null, null);}public MGraph(int vexNum, int arcNum, Object[] vexs, int[][] arcs) {this.vexNum = vexNum;this.arcNum = arcNum;this.vexs = vexs;this.arcs = arcs;}public void createGraph() {Scanner sc = new Scanner(System.in);// System.out.println("请分别输入图的顶点数、图的边数:");vexNum = sc.nextInt();arcNum = sc.nextInt();vexs = new Object[vexNum];String _vexs[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N"}; //顶点信息this.vexs = _vexs;// System.out.println("请分别输入图的各个顶点:");// for (int v = 0; v < vexNum; v++)// // 构造顶点向量// vexs[v] = _vexs[v];arcs = new int[vexNum][vexNum];for (int v = 0; v < vexNum; v++)// 初始化邻接矩阵for (int u = 0; u < vexNum; u++)arcs[v][u] = 0;// System.out.println("请输入各个边的顶点:");for (int k = 0; k < arcNum; k++) {int v = sc.nextInt();int u = sc.nextInt();arcs[v][u] = arcs[u][v] = 1;}}void DispMGraph(){int i,j;System.out.print(" ");for(i=0;i<vexNum;i++)System.out.print(vexs[i]+" ");System.out.println();for(i=0;i<vexNum;i++){System.out.print(vexs[i]+" ");for(j=0;j<vexNum;j++)System.out.print(arcs[i][j]+" ");System.out.println();}}public int getVexNum() {return vexNum;}public int getArcNum() {return arcNum;}public int locateVex(Object vex) {for (int v = 0; v < vexNum; v++)if (vexs[v].equals(vex))return v;return -1;}public Object getVex(int v) throws Exception {if (v < 0 && v >= vexNum)throw new Exception("第" + v + "个顶点不存在!");return vexs[v];}public int firstAdjVex(int v) throws Exception {if (v < 0 && v >= vexNum)throw new Exception("第" + v + "个顶点不存在!");for (int j = 0; j < vexNum; j++)if (arcs[v][j] != 0 && arcs[v][j] < INFINITY)return j;return -1;}public int nextAdjVex(int v, int w) throws Exception {if (v < 0 && v >= vexNum)throw new Exception("第" + v + "个顶点不存在!");for (int j = w + 1; j < vexNum; j++)if (arcs[v][j] != 0 && arcs[v][j] < INFINITY)return j;return -1;}public int[][] getArcs() {return arcs;}public Object[] getVexs() {return vexs;}public void setArcNum(int arcNum) {this.arcNum = arcNum;}public void setArcs(int[][] arcs) {this.arcs = arcs;}public void setVexNum(int vexNum) {this.vexNum = vexNum;}public void setVexs(Object[] vexs) {this.vexs = vexs;}// 对图G做深度优先遍历public void DFSTraverse() throws Exception {System.out.print("DFS:");visited = new boolean[getVexNum()];for (int v = 0; v < getVexNum(); v++)// 访问标志数组初始化visited[v] = false;for (int v = 0; v < getVexNum(); v++)if (!visited[v])// 对尚未访问的顶点调用DFSDFS(v);}// 从第v个顶点出发递归地深度优先遍历图Gpublic void DFS(int v) throws Exception {visited[v] = true;System.out.print(getVex(v).toString() + " ");// 访问第v个顶点for (int w = firstAdjVex(v); w >= 0; w = nextAdjVex(v, w))if (!visited[w])// 对v的尚未访问的邻接顶点w递归调用DFSDFS(w);}// 对图G做广度优先遍历public void BFSTraverse() throws Exception {System.out.print("BFS:");visited = new boolean[getVexNum()];// 访问标志数组for (int v = 0; v < getVexNum(); v++)// 访问标志数组初始化visited[v] = false;for (int v = 0; v < getVexNum(); v++)if (!visited[v]) // v尚未访问BFS(v);}private void BFS(int v) throws Exception {visited[v] = true;System.out.print(getVex(v).toString() + " ");LinkQueue Q = new LinkQueue();// 辅助队列QQ.offer(v);// v入队列while (!Q.isEmpty()) {int u = (Integer) Q.poll();// 队头元素出队列并赋值给ufor (int w = firstAdjVex(u); w >= 0; w = nextAdjVex(u, w))if (!visited[w]) {// w为u的尚未访问的邻接顶点visited[w] = true;System.out.print(getVex(w).toString() + " ");Q.offer(w);}}}}public class Main {public static void main(String[] args) throws Exception {MGraph m = new MGraph();m.createGraph();m.DispMGraph();m.DFSTraverse();System.out.println("");m.BFSTraverse();}}
