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

/*
邻接表
*/
#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 假设我们有以下有向图: ``` 1 -> 2 -> 4 | / | / | v v v v v 3 -> 5 -> 6 ``` 邻接表表示为: ``` 1: [2, 3] 2: [4, 5, 3] 3: [5] 4: [] 5: [4, 6] 6: [] ``` 深度优先遍历的过程如下: 1. 从顶点1开始,将其标记为访问过,输出1 2. 遍历邻接表中与1相邻的未访问过的顶点,选择顶点2,将其标记为访问过,输出2 3. 遍历邻接表中与2相邻的未访问过的顶点,选择顶点4,将其标记为访问过,输出4 4. 遍历邻接表中与4相邻的未访问过的顶点,发现没有,回溯到2 5. 遍历邻接表中与2相邻的未访问过的顶点,选择顶点5,将其标记为访问过,输出5 6. 遍历邻接表中与5相邻的未访问过的顶点,选择顶点6,将其标记为访问过,输出6 7. 遍历邻接表中与6相邻的未访问过的顶点,发现没有,回溯到5 8. 遍历邻接表中与5相邻的未访问过的顶点,发现没有,回溯到2 9. 遍历邻接表中与2相邻的未访问过的顶点,选择顶点3,将其标记为访问过,输出3 10. 遍历邻接表中与3相邻的未访问过的顶点,选择顶点5,发现已经访问过,回溯到1 11. 遍历邻接表中与1相邻的未访问过的顶点,发现没有,遍历结束 深度优先遍历的输出结果为:1 -> 2 -> 4 -> 5 -> 6 -> 3 广度优先遍历的过程如下: 1. 从顶点1开始,将其标记为访问过,加入队列中 2. 队列中弹出顶点1,遍历邻接表中与1相邻的未访问过的顶点,选择顶点2,将其标记为访问过,加入队列中 3. 队列中弹出顶点2,遍历邻接表中与2相邻的未访问过的顶点,选择顶点4和5,将其标记为访问过,加入队列中 4. 队列中弹出顶点4,遍历邻接表中与4相邻的未访问过的顶点,发现没有,回到步骤3 5. 队列中弹出顶点5,遍历邻接表中与5相邻的未访问过的顶点,选择顶点6,将其标记为访问过,加入队列中 6. 队列中弹出顶点6,遍历邻接表中与6相邻的未访问过的顶点,发现没有,回到步骤5 7. 队列中弹出顶点3,遍历邻接表中与3相邻的未访问过的顶点,选择顶点5,将其标记为访问过,加入队列中 8. 队列中弹出顶点5,发现已经访问过,回到步骤2 9. 队列中弹出顶点2,发现已经访问过,回到步骤1 10. 队列中弹出顶点4,发现已经访问过,回到步骤1 11. 遍历结束 广度优先遍历的输出结果为:1 -> 2 -> 3 -> 4 -> 5 -> 6 ### 回答2: 邻接表是一种表示图的数据结构,它通过一个数组来存储各个顶点,每个顶点都维护一个链表,链表中存储了与该顶点相邻的顶点。邻接表可以用来实现图的深度优先遍历广度优先遍历。 以如下图为例: ``` 1 / \ 2 3 / \ / \ 4 5 6 7 ``` 深度优先遍历(DFS):从起始顶点开始,沿着深度优先的方向依次访问图中的顶点。 使用邻接表表示该图时,可以从起始顶点1开始,首先访问与1相邻的顶点2,然后访问顶点2的相邻顶点4,再访问顶点4没有相邻顶点后回溯到顶点2,继续访问顶点2的另一个相邻顶点5,然后回溯到顶点1,访问顶点1的另一个相邻顶点3,接着访问顶点3的相邻顶点6,再访问顶点6的相邻顶点7。 所以,深度优先遍历的访问顺序为:1 -> 2 -> 4 -> 5 -> 3 -> 6 -> 7。 广度优先遍历(BFS):从起始顶点开始,依次访问图中的顶点,先访问与起始顶点直接相邻的顶点,再依次访问与这些顶点相邻的顶点。 使用邻接表表示该图时,可以从起始顶点1开始,先访问与1相邻的顶点2和3,然后继续访问与顶点2和3相邻的顶点,依次访问顶点4、5、6和7。 所以,广度优先遍历的访问顺序为:1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7。 通过邻接表可以方便地实现图的深度优先遍历广度优先遍历,它们在遍历顺序上有所不同,适用于不同的场景和问题。 ### 回答3: 邻接表是一种图的表示方法,它使用一组链表来表示图中每个顶点的邻接顶点。深度优先遍历(Depth First Search,DFS)和广度优先遍历(Breadth First Search,BFS)是常用的图遍历算法。 深度优先遍历是一种沿着图的深度方向进行遍历的算法。它从图的某个顶点开始,沿着一条未访问过的边进入下一个顶点,直到该顶点没有未访问过的边为止,然后返回上一层继续遍历。深度优先遍历使用堆栈(Stack)数据结构来实现。以下是一个深度优先遍历的例子: 假设有以下图的邻接表表示: 顶点1 -> 顶点2 -> 顶点3 顶点2 -> 顶点1 -> 顶点4 -> 顶点5 顶点3 -> 顶点1 -> 顶点6 顶点4 -> 顶点2 顶点5 -> 顶点2 顶点6 -> 顶点3 从顶点1开始进行深度优先遍历的过程如下: 1 -> 2 -> 4 -> 5 -> 3 -> 6 广度优先遍历是一种沿着图的广度方向进行遍历的算法。它从图的某个顶点开始,访问所有与该顶点邻接的顶点,然后再依次访问与这些邻接顶点相邻接的顶点,直到所有顶点都被访问为止。广度优先遍历使用队列(Queue)数据结构来实现。以下是一个广度优先遍历的例子: 假设有以下图的邻接表表示: 顶点1 -> 顶点2 -> 顶点3 顶点2 -> 顶点1 -> 顶点4 -> 顶点5 顶点3 -> 顶点1 -> 顶点6 顶点4 -> 顶点2 顶点5 -> 顶点2 顶点6 -> 顶点3 从顶点1开始进行广度优先遍历的过程如下: 1 -> 2 -> 3 -> 4 -> 5 -> 6 深度优先遍历广度优先遍历在实际应用中有不同的用途。深度优先遍历更适合用于寻找目标节点在图中是否存在,而广度优先遍历更适合用于寻找目标节点与起始节点的最短路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值