要求:对有向图进行DFS(深度优先遍历)、BFS(广度优先遍历)、拓扑排序。写出深度优先遍历的递归和非递归算法。
代码如下:
在此非常感谢crazy_27的评论,给我指出错误。
#include
#include
#include
#include
#include
#define MAX 100
using namespace std;
int visited[MAX+1];//记录是否访问过
typedef struct node{
int data;
struct node *next;
}Node,*ArcNode;
//定义邻接表的边类型
typedef struct vnode{
char data;
ArcNode firstarc;
}VNode;
//定义邻接表的结点类型
typedef struct Graph{
VNode AdjList[MAX];
int vexnum,arcnum;
}MGraph;
//定义图类型
int LocateVex(char ch,MGraph G)
{//取该结点在数组中的位置
int i;
for(i=0;i
if(G.AdjList[i].data==ch)
return i;
return -1;
}
void InitGraph(MGraph &G)
{//建立一个有向图
int n,m;
int i,j,k;
char ch1,ch2;
ArcNode p;
printf("请输入结点个数和边个数:");
scanf("%d %d",&G.vexnum,&G.arcnum);
printf("请输入结点:");
getchar();
for(i=0;i
{
scanf(

本文介绍了如何使用Python实现有向图的深度优先遍历(DFS)、广度优先遍历(BFS)以及拓扑排序。通过邻接表结构,展示了递归和非递归的DFS算法,并提供了BFS的实现。最后,通过一个实例展示了拓扑排序的过程。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



