基於鄰接表的深度優先搜索

header.h

#include<iostream>
using namespace std;
const int MaxVertexNum=20;
struct EdgeNode{
	int adjvex;
	struct EdgeNode * next;
};
struct VNode{
	int data;
	EdgeNode * firstedge;
};
class ALGraph{
public:
	ALGraph();
	~ALGraph();
	void DFS(int index);
	int FirstAdjVex(int v);
	int NextAdjVex(int v,int w);
	bool DFSTraverse();
	int LocateVex(int u);
	ALGraph & InsertVex(int v);
	ALGraph & DeleteVex(int v);
	ALGraph & InsertArc(int v,int w);
	ALGraph & DeleteArc(int v,int w);
	void DisPlay();
	VNode vertices[MaxVertexNum];
	private:
		bool visited[MaxVertexNum];
	int vexnum;
	int arcnum;
};


header。cpp

#include"header.h"
int ALGraph::LocateVex(int u)
{
	for(int i=0;i<vexnum;i++)
		if(vertices[i].data==u)
			return i;
	return -1;
}
ALGraph::ALGraph()
{
	int i,j,k;
	int v1,v2;
	cout<<"請輸入圖的頂點數,邊數:";
	cin>>vexnum>>arcnum;
	cout<<"請輸入"<<vexnum<<"個頂點的值:";
	for(i=0;i<vexnum;i++)
	{
		cin>>vertices[i].data;
		vertices[i].firstedge=NULL;
	}
	for(k=0;k<arcnum;k++)
	{
		cout<<"請輸入一條弧的弧尾、弧頭:";
		cin>>v1>>v2;
		i=LocateVex(v1);
		j=LocateVex(v2);
		EdgeNode * p=new EdgeNode;
		p->adjvex=j;
		p->next=vertices[i].firstedge;
		vertices[i].firstedge=p;
	}
}
ALGraph & ALGraph::InsertVex(int u)
{
	if(vexnum>MaxVertexNum)
	{
		cout<<"頂點數超出範圍,無法插入!";
		exit(1);
	}
	if(LocateVex(u)>=0)
	{
		cout<<"頂點已存在";
		exit(1);
	}
	vertices[vexnum].data=u;
	vertices[vexnum].firstedge=NULL;
	vexnum++;
	return *this;
}
ALGraph & ALGraph::DeleteVex(int v)
{
	int i,j;
	EdgeNode * p,* q;
	i=LocateVex(v);
	p=vertices[i].firstedge;
	while(p)
	{
		q=p;
		p=p->next;
		delete q;
		arcnum--;
	}
	vexnum--;
	for(j=i;j<vexnum;j++)
		vertices[j]=vertices[j+1];
	for(j=0;j<vexnum;j++)
	{
		p=vertices[j].firstedge;
		while(p)
		{
			if(p->adjvex==i)
			{
				if(p==vertices[j].firstedge)
				{
					vertices[j].firstedge=p->next;
					delete p;
					p=vertices[j].firstedge;
					arcnum--;
				}
				else
				{
					q->next=p->next;
					delete p;
					p=q->next;
					arcnum--;
				}
			}
			else
			{
				if(p->adjvex>i)//由於節點少了一個,故對於》i的節點採取了--的操作。
					p->adjvex--;
				q=p;
				p=p->next;
			}
		}
	}
	return *this;
}
ALGraph & ALGraph::InsertArc(int v,int w)
{
	EdgeNode * p;
	int i,j;
	i=LocateVex(v);
	j=LocateVex(w);
	if(i<0||j<0)
	{
		cout<<"頂點不存在!";
		exit(1);
	}
	arcnum++;
	p=new EdgeNode;
	p->adjvex=j;
	p->next=NULL;
	p->next=vertices[i].firstedge;
	vertices[i].firstedge=p;
	return *this;
}
ALGraph & ALGraph::DeleteArc(int v,int w)//注意此處刪除的是弧,而非邊
{
	EdgeNode * p,* q;
	int i,j;
	i=LocateVex(v);
	j=LocateVex(w);
	if(i<0||j<0||i==j)
	{
		cout<<"邊不存在!";
		exit(1);
	}
	p=vertices[i].firstedge;
	while(p&&p->adjvex!=j)
	{
		q=p;
		p=p->next;
	}
	if(p&&p->adjvex==j)
	{
		if(p==vertices[i].firstedge)
			vertices[i].firstedge=p->next;
		else
			q->next=p->next;
		delete p;
		arcnum--;
	}
	/*if(p&&p->adjvex==i)
	{
		if(p==vertices[j].firstedge)
			vertices[j].firstedge=p->next;
		else
			q->next=p->next;
		delete p;
	}*/
	return *this;
}
void ALGraph::DisPlay()
{
	int i;
	EdgeNode * p;
	cout<<"有向圖的"<<vexnum<<"個頂點:"<<endl;
	for(i=0;i<vexnum;i++)
		cout<<vertices[i].data<<"  ";
	cout<<endl;
	cout<<"有向圖的"<<arcnum<<"條弧:"<<endl;
	 for(i=0;i<vexnum;i++)
	{
		p=vertices[i].firstedge;
		while(p!=NULL)
		{
			cout<<vertices[i].data<<"->"<<vertices[p->adjvex].data<<'\t';
			cout<<endl;
			p=p->next;
		}
	}
}
ALGraph::~ALGraph()
{
	int i;
	EdgeNode * p,* q;
	for(i=0;i<vexnum;i++)
	{
		p=vertices[i].firstedge;
		while(p)
		{

			q=p->next;
			delete p;
			p=q;
		}
	}
	arcnum=0;
	vexnum=0;
}
void ALGraph::DFS(int index)
{
	int v1;
	int i;
	visited[index]=true;
	v1=vertices[index].data;
	cout<<v1<<"  ";
	for(i=FirstAdjVex(v1);i>=0;i=NextAdjVex(v1,vertices[i].data))
		if(!visited[i])
			DFS(i);
}
int ALGraph::FirstAdjVex(int v)
{
	int i=LocateVex(v);
	EdgeNode * p=vertices[i].firstedge;
	if(p)
		return p->adjvex;
	else
		return -1;
}
int ALGraph::NextAdjVex(int v,int w)
{
	EdgeNode * p;
	int i=LocateVex(v);
	int j=LocateVex(w);
	p=vertices[i].firstedge;
	while(p&&(p->adjvex!=j))
	{
		p=p->next;
	}
	if(!p||!p->next)
		return -1;
	else
		return p->next->adjvex;
}
bool ALGraph::DFSTraverse()
{
	int i;
	for(i=0;i<vexnum;i++)
		visited[i]=false;
	for(i=0;i<vexnum;i++)
	{
		if(!visited[i])
			DFS(i);
	}
	cout<<endl;
	return true;
}


main.cpp

#include"header.h"
int main()
{
	ALGraph ag;
	cout<<"深度優先搜索爲:";
	ag.DFSTraverse();
	cout<<endl;
	ag.DisPlay();
	int x=9;
	int y=10;
	ag.InsertVex(x);
	ag.InsertVex(y);
	ag.InsertArc(x,y);
	ag.DisPlay();
	system("pause");
	return 0;
}


算法思想與基於鄰接矩陣的深度優先搜索一致

發佈了47 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章