数据结构-图的深度优先遍历方式(非递归,邻接表存储)

#include<stdio.h>
#include<string.h>

//表结点,采用邻接表存储图
#include<iostream>
#include<string>
#include<stack>
using namespace std;
//表结点
struct ArcNode {
    ArcNode * next; //下一个关联的边
    int adjvex;   //保存弧尾顶点在顶点表中的下标
};
//顶点表
struct Vnode {
    string data; //顶点名称
    ArcNode * firstarc; //第一个依附在该顶点边
};
class Graph_DG {
private:
    int vexnum; //图的顶点数
    int edge;   //图的边数
    int * indegree; //每条边的入度情况
    Vnode * arc; //邻接表
public:
    Graph_DG(int, int);
    ~Graph_DG();
    //检查输入边的顶点是否合法
    bool check_edge_value(int,int);
    //创建一个图
    void createGraph();
    //打印邻接表
    void print();
    //进行拓扑排序,Kahn算法
    bool topological_sort();
    //进行拓扑排序,DFS算法
    bool topological_sort_by_dfs();
    void dfs(int n,bool * & visit, stack<string> & result);
};
//初始化 vexnum:顶点个数,edge:边数
Graph_DG::Graph_DG(int vexnum, int edge)
{
	
	this->vexnum = vexnum;
    this->edge = edge;
	
	this->arc = new Vnode[this->vexnum];
	
	this->indegree = new int[this->vexnum]; //每个顶点的入度数目
	
	for (int i = 0; i < this->vexnum; i++) {
        this->indegree[i] = 0;
        this->arc[i].firstarc = NULL;
        this->arc[i].data = "v" + to_string(i + 1);
    }
}
void Graph_DG::createGraph() {
    int count = 0;
    int start, end;
    cout << "输入每条起点和终点的顶点编号(从1开始编号)" << endl;
    while (count != this->edge) {
        cin >> start;
        cin >> end;
        //检查边是否合法
        while (!this->check_edge_value(start, end)) {
            cout << "输入的顶点不合法,请重新输入" << endl;
            cin >> start;
            cin >> end;
        }
        //声明一个新的表结点
        ArcNode * temp = new ArcNode;
        temp->adjvex = end - 1;
        temp->next = NULL;
        //如果当前顶点的还没有边依附时,
        if (this->arc[start - 1].firstarc == NULL) {
            this->arc[start - 1].firstarc = temp;
        }
    else {
            ArcNode * now = this->arc[start - 1].firstarc;
            while(now->next) {
                now = now->next;
            }//找到该链表的最后一个结点
            now->next = temp;
        }
        ++count;
    }
}
void Graph_DG::print() {
    int count = 0;
    cout << "图的邻接矩阵为:" << endl;
    //遍历链表,输出链表的内容
    while (count != this->vexnum) {
        //输出链表的结点
        cout << this->arc[count].data<<" ";
        ArcNode * temp = this->arc[count].firstarc;
        while (temp) {
            cout<<"<"<< this->arc[count].data<<","<< this->arc[temp->adjvex].data<<"> ";
            temp = temp->next;
        }
        cout << "^" << endl;
        ++count;
    }
}
//非递归的方式去深度优先遍历图

void DFS(Graph_DG G,int startnode)
{
	vector<int> visit(vexnum,0);
	
	stack<int> s;

    s.push(startnode);

    visit[startnode] =1;
	
	ArcNode * temp = this->arc[startnode].firstarc;
	
	while(!s.empty())
	{
		temp =this->arc[s.top()].firstarc;  //栈顶
		while(temp)   
		{
			if(visit[temp->adjvex]==0)  
			{
				visit[temp->adjvex]==1;
				printf("2%d");
				
				s.push(temp->adjvex);
				
				temp=this->arc[temp->adjvex].firstarc;
				
			}
			
			else //
			{
				
				temp =temp->next;
			}	
			
		}
		
		if(temp==NULL)
		{
			s.pop();
		}
		
	}
	
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是邻接深度优先的C语言代码: ```c #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 // 最大顶点数 #define MAX_STACK_SIZE 20 // 最大栈深度 typedef int VertexType; // 顶点的数据类型 // 定义边结构体 typedef struct ArcNode { int adjvex; // 邻接点在顶点数组中的下标 struct ArcNode *nextarc; // 指向下一个邻接点的指针 } ArcNode; // 定义顶点结构体 typedef struct { VertexType data; // 顶点的数据 ArcNode *firstarc; // 指向第一个邻接点的指针 } VNode; // 定义邻接结构体 typedef struct { VNode vertex[MAX_VERTEX_NUM]; // 顶点数组 int vexnum, arcnum; // 顶点数和边数 } ALGraph; // 初始化邻接 void init(ALGraph *G) { int i; G->vexnum = G->arcnum = 0; for (i = 0; i < MAX_VERTEX_NUM; i++) { G->vertex[i].firstarc = NULL; } } // 添加边 void addEdge(ALGraph *G, int i, int j) { ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode)); p->adjvex = j; p->nextarc = G->vertex[i].firstarc; G->vertex[i].firstarc = p; G->arcnum++; } // 深度优先 void DFS(ALGraph *G, int v) { int visited[MAX_VERTEX_NUM]; int top = -1; // 栈顶指针 int stack[MAX_STACK_SIZE]; // 栈 ArcNode *p; int i; // 初始化visited数组 for (i = 0; i < MAX_VERTEX_NUM; i++) { visited[i] = 0; } // 将起点v入栈并标记为已访问 stack[++top] = v; visited[v] = 1; // 遍栈中的元素 while (top != -1) { // 取出栈顶元素 i = stack[top--]; printf("%d ", G->vertex[i].data); // 遍邻接点 p = G->vertex[i].firstarc; while (p != NULL) { // 如果邻接点未被访问,则入栈并标记为已访问 if (visited[p->adjvex] == 0) { stack[++top] = p->adjvex; visited[p->adjvex] = 1; } p = p->nextarc; } } } int main() { ALGraph G; init(&G); addEdge(&G, 0, 1); addEdge(&G, 0, 2); addEdge(&G, 1, 2); addEdge(&G, 2, 0); addEdge(&G, 2, 3); addEdge(&G, 3, 3); printf("深度优先结果:"); DFS(&G, 2); return 0; } ``` ### 回答2: 下面是邻接深度优先的C语言代码。首先,我们需要定义一个的结构体,包含顶点数量和邻接数组。 ```c #include <stdio.h> #include <stdlib.h> // 定义的结构体 typedef struct GraphNode{ int vertex; struct GraphNode* next; } GraphNode; typedef struct Graph{ int vertices; GraphNode** adjList; } Graph; // 创建的函数 Graph* createGraph(int vertices){ Graph* graph = (Graph*) malloc(sizeof(Graph)); graph->vertices = vertices; graph->adjList = (GraphNode**) malloc(vertices * sizeof(GraphNode*)); for(int i = 0; i < vertices; i++){ graph->adjList[i] = NULL; } return graph; } // 添加边的函数 void addEdge(Graph* graph, int src, int dest){ GraphNode* newNode = (GraphNode*) malloc(sizeof(GraphNode)); newNode->vertex = dest; newNode->next = graph->adjList[src]; graph->adjList[src] = newNode; // 若为无向,则也需添加反向边 newNode = (GraphNode*) malloc(sizeof(GraphNode)); newNode->vertex = src; newNode->next = graph->adjList[dest]; graph->adjList[dest] = newNode; } // 深度优先函数 void DFS(Graph* graph, int startVertex){ int* visited = (int*) malloc(graph->vertices * sizeof(int)); for(int i = 0; i < graph->vertices; i++){ visited[i] = 0; } // 使用栈来实现归遍 int stack[graph->vertices]; int top = -1; stack[++top] = startVertex; visited[startVertex] = 1; while(top != -1){ int currentVertex = stack[top--]; printf("%d ", currentVertex); GraphNode* temp = graph->adjList[currentVertex]; while(temp){ int adjVertex = temp->vertex; if(!visited[adjVertex]){ stack[++top] = adjVertex; visited[adjVertex] = 1; } temp = temp->next; } } } // 主函数 int main(){ int vertices = 6; Graph* graph = createGraph(vertices); addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 3); addEdge(graph, 1, 4); addEdge(graph, 2, 4); addEdge(graph, 3, 4); addEdge(graph, 3, 5); printf("深度优先结果: "); DFS(graph, 0); return 0; } ``` 代码中的`createGraph`函数用于创建具有指定数量顶点的。`addEdge`函数用于在两个顶点之间添加一条边。`DFS`函数用于执行深度优先。在`main`函数中,我们创建了一个具有6个顶点的,并添加了一些边。最后,我们调用`DFS`函数从第一个顶点开始进行深度优先,并打印结果。 ### 回答3: 深度优先(Depth First Search,简称DFS)是的一种遍算法,其中邻接是一种方式。下面是一个归的深度优先的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 100 // 定义邻接节点 typedef struct ArcNode { int adjvex; // 邻接顶点的下标 struct ArcNode *nextarc; // 指向下一个邻接节点的指针 } ArcNode; // 定义的顶点节点 typedef struct VNode { int data; // 顶点的数据 ArcNode *firstarc; // 指向第一个邻接节点的指针 } VNode, AdjList[MAX_VERTEX_NUM]; // 定义 typedef struct { AdjList vertices; // 顶点数组 int vexnum; // 顶点数 } Graph; // 归DFS遍 void DFS(Graph G, int v) { int visited[MAX_VERTEX_NUM] = {0}; // 记录顶点是否被访问过的数组 ArcNode *stack[MAX_VERTEX_NUM]; // 用数组实现栈 int top = -1; // 栈顶指针 int i; printf("DFS遍顺序: "); stack[++top] = &(G.vertices[v]); // 将起始顶点的邻接节点入栈 visited[v] = 1; // 标记起始顶点已经被访问 while (top != -1) { // 栈不为空时 ArcNode *p = stack[top--]; // 弹出栈顶元素 printf("%d ", p->data); // 访问该顶点 for (ArcNode *q = p->firstarc; q != NULL; q = q->nextarc) { // 遍该顶点的邻接点 if (visited[q->adjvex] == 0) { // 若邻接点未被访问过 stack[++top] = &(G.vertices[q->adjvex]); // 入栈 visited[q->adjvex] = 1; // 标记已访问 } } } } int main() { Graph G; G.vexnum = 4; // 假设有4个顶点 for (int i = 0; i < G.vexnum; i++) { G.vertices[i].data = i; // 顶点数据 G.vertices[i].firstarc = NULL; // 初始化邻接节点指针为空 } // 添加边 ArcNode *arc0 = (ArcNode *)malloc(sizeof(ArcNode)); arc0->adjvex = 1; arc0->nextarc = NULL; G.vertices[0].firstarc = arc0; ArcNode *arc1 = (ArcNode *)malloc(sizeof(ArcNode)); arc1->adjvex = 2; arc1->nextarc = NULL; G.vertices[1].firstarc = arc1; ArcNode *arc2 = (ArcNode *)malloc(sizeof(ArcNode)); arc2->adjvex = 3; arc2->nextarc = NULL; G.vertices[2].firstarc = arc2; // 顶点0的邻接点为1 // 顶点1的邻接点为2 // 顶点2的邻接点为3 DFS(G, 0); return 0; } ``` 此代码采用邻接的形式存储,并使用归的深度优先算法遍中的顶点和边。首先定义了邻接节点和顶点节点的数据结构,然后定义了数据结构,包括顶点数组和顶点数。接下来,使用归的深度优先算法实现了DFS()函数,在DFS()函数中,使用栈来记录待访问的节点,并使用visited数组来标记节点是否被访问过。最后,在主函数中创建了一个具有4个顶点的,并添加了相应的边,然后调用DFS()函数进行遍

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值