题目描述
已知邻接表存储的无向图的部分代码如下,请补充完成深度优先、广度优先遍历算法。
//graphmain.cpp
#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;
}
const int MaxSize=20; // 顶点个数的最大值
int visited[MaxSize]; //访问标志数组(0表示未访问,1表示已访问)
//定义边表结点
struct ArcNode
{
int adjvex; //邻接点的序号
ArcNode next; //指向下一个边结点的指针
};
//定义顶点表结点
template
struct VertexNode
{
T vertex; //顶点的名称
ArcNode
};
//邻接表类
template
class ALGraph
{
public:
ALGraph(T a[ ], int n, int e); //构造函数,初始化一个有n个顶点e条边的图
~ALGraph(); //析构函数,释放邻接表中各边表结点的存储空间
void DispALGraph(); //输出邻接表
int Count( ); //计算连通分量个数,返回值为连通分量的个数(请大家自己测试)
void DFSTraverse(int v); //深度优先遍历图
void BFSTraverse(int v); //深度优先遍历图
private:
VertexNode
int vertexNum, arcNum; //图的顶点数和边数
};
/
前置条件:图不存在
输 入:无
功 能:图的初始化
输 出:无
后置条件:得到一个无向图
/
template
ALGraph
{
arcNum = e; //边数
vertexNum=n; //顶点数
int i,j;
for (i=0; i
adjlist[i].vertex = a[i];
adjlist[i].firstedge = NULL;
}
for (int k=0; k
cin>>i>>j; //输入边所依附的两个顶点的序号
ArcNode
s->adjvex=j; //生成一个边表结点s
s->next=adjlist[i].firstedge; //将结点s插入到i号表的头结点之后
adjlist[i].firstedge=s;
s=new ArcNode;
s->adjvex=i; //生成一个边表结点s
s->next=adjlist[j].firstedge; //将结点s插入到j号表的头结点之后
adjlist[j].firstedge=s;
}
}
/ 前置条件:图已存在
输 入:无
功 能:销毁图
输 出:无
后置条件:释放图所占用的存储空间
/
template
ALGraph
{
for (int i=0; i
ArcNode p=adjlist[i].firstedge;
while (p!=NULL) //循环删除
{
adjlist[i].firstedge=p->next;
delete p; //释放结点空间
p=adjlist[i].firstedge;
}
}
}
/
前置条件:图已存在
输 入:无
功 能:输出图中所有顶点及边的数据信息
输 出:图中所有顶点及边的数据信息
后置条件:图保持不变
/
template
void ALGraph
{
int i;
ArcNode *p;
cout<<”图的邻接表:\n”;
for(i=0;i
cout<
cout<
}
template
int ALGraph
{
int i,n=0;
for(i=0;i
n++;
BFSTraverse(i); //利用深度优先或广度优先遍历算法均可
}
}
return n;
}
//在下面完成深度优先、广度优先遍历算法
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; //输入顶点个数和边个数
ALGraph
G.DispALGraph();
for(i=0;i
cout<<”DFS:”;
G.DFSTraverse(0);
cout<
cout<<”BFS:”;
G.BFSTraverse(0);
// cout<<”\n连通分量个数:”;
// cout<
}
输入
第一行为顶点个数和边的条数:6 7
第二行为各条边所对应的顶点序号:0 1 0 3 1 2 1 5 2 4 3 5 4 5
输出
因为构造函数中各条边所对应的边表结点采用头插法插入到表头结点之后,因此输入0 1 0 3,0号链表后为3 1序列,其它亦如此。
图的邻接表:
0 A 3 1
1 B 5 2 0
2 C 4 1
3 D 5 0
4 E 5 2
5 F 4 3 1
DFS:A D F E C B
BFS:A D B F C E
样例输入
6 7 0 1 0 3 1 2 1 5 2 4 3 5 4 5
样例输出
Graph adjlist: 0 A 3 1 1 B 5 2 0 2 C 4 1 3 D 5 0 4 E 5 2 5 F 4 3 1 DFS:A D F E C B BFS:A D B F C E
提示
来源
提交
package Q1026;import java.util.Scanner;class CirQueue{Object[] queueElem;int front;int rear;int QueueSize=5;public CirQueue(){front = rear = 0;queueElem = new Object[QueueSize];}public void clear(){front = rear = 0;}public boolean isEmpty(){return front == rear;}public int length(){return (rear-front+queueElem.length)% queueElem.length;}public boolean isFull(){return (rear+1) % QueueSize ==front;}public void EnQueue(Object x) throws Exception{if((rear+1)%queueElem.length==front){throw new Exception(" Overflow");}else{queueElem[rear] = x;rear = (rear+1)%queueElem.length;}}public Object DeQueue() throws Exception {if(front==rear){throw new Exception("Downflow");}else{Object t = queueElem[front];front = (front+1)%queueElem.length;return t;}}public Object GetQueue() throws Exception {if(front==rear){throw new Exception("Downflow");}else{Object t = queueElem[front];return t;}}}//图的接口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 ArcNode {public int adjVex;public int value;public ArcNode nextArc;public ArcNode() {this(-1, 0, null);}public ArcNode(int adjVex) {this(adjVex, 0, null);}public ArcNode(int adjVex, int value) {this(adjVex, value, null);}public ArcNode(int adjVex, int value, ArcNode nextArc) {this.value = value;this.adjVex = adjVex;this.nextArc = nextArc;}}class VNode {public Object data;public ArcNode firstArc;public VNode() {this(null, null);}public VNode(Object data) {this(data, null);}public VNode(Object data, ArcNode firstArc) {this.data = data;this.firstArc = firstArc;}}class ALGraph implements IGraph {private int vexNum, arcNum;private VNode[] vexs;private static boolean[] visited;// 访问标志数组public ALGraph() {this(0, 0, null);}public ALGraph(int vexNum, int arcNum, VNode[] vexs) {this.vexNum = vexNum;this.arcNum = arcNum;this.vexs = vexs;}public void createGraph() {Scanner sc = new Scanner(System.in);vexNum = sc.nextInt();arcNum = sc.nextInt();vexs = new VNode[vexNum];String Node[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N"}; //顶点信息for (int v = 0; v < vexNum; v++)vexs[v] = new VNode(Node[v]);for (int k = 0; k < arcNum; k++) {int v = sc.nextInt();// 弧尾int u = sc.nextInt();// 弧头int value = 1;addArc(v, u, value);addArc(u, v, value);}}public void addArc(int v, int u, int value) {ArcNode arc = new ArcNode(u, value);arc.nextArc=vexs[v].firstArc;vexs[v].firstArc=arc;}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].data.equals(vex))return v;return -1;}public VNode[] getVexs() {return vexs;}public Object getVex(int v) throws Exception {if (v < 0 && v >= vexNum)throw new Exception("第" + v + "个顶点不存在!");return vexs[v].data;}public int firstAdjVex(int v) throws Exception {if (v < 0 && v >= vexNum)throw new Exception("第" + v + "个顶点不存在!");VNode vex = vexs[v];if (vex.firstArc != null)return vex.firstArc.adjVex;elsereturn -1;}public int nextAdjVex(int v, int w) throws Exception {if (v < 0 && v >= vexNum)throw new Exception("第" + v + "个顶点不存在!");VNode vex = vexs[v];ArcNode arcvw = null;for (ArcNode arc = vex.firstArc; arc != null; arc = arc.nextArc)if (arc.adjVex == w) {arcvw = arc;break;}if (arcvw != null && arcvw.nextArc != null)return arcvw.nextArc.adjVex;elsereturn -1;}public void setArcNum(int arcNum) {this.arcNum = arcNum;}public void setVexNum(int vexNum) {this.vexNum = vexNum;}public void setVexs(VNode[] vexs) {this.vexs = vexs;}public void DFSTraverse() throws Exception {visited = new boolean[getVexNum()];for (int v = 0; v < getVexNum(); v++)// 访问标志数组初始化visited[v] = false;System.out.print("DFS:");for (int v = 0; v < getVexNum(); v++)if (!visited[v])// 对尚未访问的顶点调用DFSDFS(v);}private void DFS(int v) throws Exception {visited[v] = true;// 访问第v个顶点System.out.print(getVex(v).toString() + " ");for (int w = firstAdjVex(v); w >= 0; w = nextAdjVex(v, w))if (!visited[w])// 对v的尚未访问的邻接顶点w递归调用DFSDFS(w);}public void BFSTraverse() throws Exception {visited = new boolean[getVexNum()];// 访问标志数组for (int v = 0; v < getVexNum(); v++)// 访问标志数组初始化visited[v] = false;System.out.print("BFS:");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() + " ");CirQueue Q = new CirQueue();// 辅助队列QQ.EnQueue(v);// v入队列while (!Q.isEmpty()) {int u = (Integer) Q.DeQueue();// 队头元素出队列并赋值给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.EnQueue(w);}}}void DispMGraph(){int i;ArcNode p;System.out.println("Graph adjlist:");for(i=0;i<vexNum;i++){System.out.print(i + " " +vexs[i].data + " ");p = vexs[i].firstArc;while(p!=null){System.out.print(p.adjVex + " ");p= p.nextArc;}System.out.println("");}}}public class Main {public static void main(String[] args) throws Exception {ALGraph a = new ALGraph();a.createGraph();a.DispMGraph();a.DFSTraverse();System.out.println("");a.BFSTraverse();}}
