邻接表 深度优先遍历 广度优先遍历

/*
邻接表
*/
#include <iostream>
#include <deque>
using namespace std;


#define MAXSIZE 512
#define INVALID -1


struct BaseNode
{
BaseNode()
{
tailIndex = INVALID;
nWeight = INVALID;
Next = NULL;
}


int tailIndex;//弧尾 下标


int nWeight; //权值


BaseNode * Next;
};


struct GraphNode
{
BaseNode *first;


int mIndex;


bool bVisit;
};


class Graph
{
public:
Graph()
{
m_AllNode = NULL;
m_EdgeNum = 0;
m_VertexNum = 0;
}
bool Init(bool boDirection);


BaseNode * InsertNode(BaseNode * CurNode,int tailIndex, int nWeight);


void Print();


void depth();


void depthSearch(int index);


void Breadth();


void BreadthSearch(int index);


public:
GraphNode *m_AllNode;


int m_EdgeNum; //边数


int m_VertexNum; //顶点数


bool  m_boDirection; //有向 无向
};


BaseNode * Graph::InsertNode(BaseNode * CurNode, int tailIndex, int nWeight)
{

if (CurNode->Next != NULL)
{
cout << "Next No Null" << endl;
return CurNode;
}
BaseNode *t = new BaseNode;


if (t == NULL)
{
cout << "New BaseNode Error" << endl;
return CurNode;
}


if ((t)->tailIndex > m_VertexNum)
{
cout << "InsertNode Big Error " << endl;
return CurNode;
}


(t)->tailIndex = tailIndex;
(t)->nWeight = nWeight;


CurNode->Next = t;


return t;
}




bool Graph::Init(bool boDirection)
{
m_boDirection = boDirection;


cout << "请输入顶点个数" << endl;

cin >> m_VertexNum;


if (m_VertexNum >= MAXSIZE || m_VertexNum <= 0)
{
cout << "顶点个数有误" << endl;
return false;
}


m_AllNode = new GraphNode[m_VertexNum+1];


for (int i = 1; i <= m_VertexNum; i++)
{
m_AllNode[i].first = new BaseNode;
m_AllNode[i].mIndex = i;
m_AllNode[i].first->tailIndex = i;
m_AllNode[i].bVisit = false;
}


cout << "请输入边数" << endl;
cin >> m_EdgeNum;
if (m_EdgeNum >= (m_VertexNum - 1)*m_VertexNum / 2 || m_EdgeNum <= 0 || m_EdgeNum >= MAXSIZE)
{
cout << "边数有误" << endl;
for (int i = 1; i <= m_VertexNum; i++)
{
delete m_AllNode[i].first;
m_AllNode[i].first = NULL;
}
delete[] m_AllNode;
m_AllNode = NULL;
return false;
}


cout << "请输入边的信息" << endl;


for (int i = 0; i < m_EdgeNum; i++)
{
int a, b, c;
cin >> a >> b >> c;
if (a <= m_VertexNum)
{
BaseNode * aLast = m_AllNode[a].first;

while (aLast->Next)
{
aLast = aLast->Next;
}


InsertNode(aLast, b, c);
if (!boDirection)
{
BaseNode * bLast = m_AllNode[b].first;


while (bLast->Next)
{
bLast = bLast->Next;
}
InsertNode(bLast, a, c);
}

}
else
{
cout << "a Error" << endl;
}

}
return true;
}




void Graph::Print()
{
for (int i = 1; i <= m_VertexNum; i++)
{
cout << "访问" << m_AllNode[i].mIndex<<"开始"<<endl;
BaseNode * t = m_AllNode[i].first;
while (t)
{
cout << t->tailIndex <<"  "<< t->nWeight << " ";
t = t->Next;
}
cout << "访问" << m_AllNode[i].mIndex << "结束" << endl;
}
}


//深度优先遍历
void Graph::depth()
{
cout << "深度优先遍历开始" << endl;
for (int i = 1; i <= m_VertexNum; i++)
{
depthSearch(i);
}


for (int i = 0; i <= m_VertexNum; i++)
{
m_AllNode[i].bVisit = false;
}
}


void  Graph::depthSearch(int index)
{
if (m_AllNode[index].bVisit == true)
{
return;
}
cout << "访问" << index << endl;


m_AllNode[index].bVisit = true;


BaseNode * t = m_AllNode[index].first->Next;
while (t)
{
depthSearch(t->tailIndex);
t = t->Next;
}
}


deque<int> tQueue;
void Graph::Breadth()
{
cout << "广度优先遍历开始" << endl;
for (int i = 1; i <= m_VertexNum; i++)
{
BreadthSearch(i);
}


for (int i = 0; i <= m_VertexNum; i++)
{
m_AllNode[i].bVisit = false;
}
}


void Graph::BreadthSearch(int index)
{
if (m_AllNode[index].bVisit == true)
{
return;
}
cout << "访问" << index << endl;


m_AllNode[index].bVisit = true;


BaseNode * t = m_AllNode[index].first->Next;


while (t)
{
if (!m_AllNode[t->tailIndex].bVisit)
{
tQueue.push_back(t->tailIndex);
}
t = t->Next;
}


while (tQueue.size() > 0)
{
int m = tQueue.front();
tQueue.pop_front();
BreadthSearch(m);
}
}


int main()
{
while (true)
{
Graph a;
if (a.Init(false))
{
a.Print();
a.depth();
a.Breadth();
}
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/seanbill/article/details/79686917
今日推荐