随笔 - 145  文章 - 0  评论 - 6  阅读 - 18万

图的遍历

采用图的邻接表存储结构实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
 
using namespace std;
 
#define MAX_NUM 100
 
//边节点
typedef struct ArcNode
{
    int adjvex; //边的终点编号
    int weight; //边权重
    struct ArcNode* next; //指向下一条边
}ArcNode;
 
//顶点节点
typedef struct VNode
{
    int data; //顶点编号
    ArcNode* first; //顶点指向的第一条边
}VNode;
 
//图
typedef struct Graph
{
    VNode adjList[MAX_NUM]; //顶点集合
    int vexnum; //顶点个数
    int arcnum; //边的条数
}ALGraph;
 
void init(ALGraph &graph)
{
    memset(graph.adjList, 0, sizeof(graph.adjList));
    graph.arcnum = 0;
    graph.vexnum = 0;
}
 
//构建图
void buildGraph(ALGraph &graph, int edgeInfo[], int n)
{
    bool vist[MAX_NUM];
    memset(vist, 0, sizeof(vist));
    for(int i = 0; i < n; i = i + 2)
    {
        //新建边节点
        ArcNode* arcNode = (ArcNode*)malloc(sizeof(ArcNode));
        arcNode->adjvex = edgeInfo[i + 1];
        arcNode->next = NULL;
        graph.arcnum++;
 
        //顶点
        if(!vist[edgeInfo[i]])
        {
            graph.vexnum++; vist[edgeInfo[i]] = true;
            graph.adjList[edgeInfo[i]].data = edgeInfo[i];
        }
        if(!vist[edgeInfo[i + 1]])
        {
            graph.vexnum++; vist[edgeInfo[i + 1]] = true;
            graph.adjList[edgeInfo[i + 1]].data = edgeInfo[i + 1];
        }
 
        int vnum = edgeInfo[i];
        if(graph.adjList[vnum].first == NULL)
        {
            graph.adjList[vnum].first = arcNode;
        }
        else
        {
            ArcNode* temp = graph.adjList[vnum].first;
            while(temp->next != NULL)
                temp = temp->next;
            temp->next = arcNode;
        }
    }
}
 
//bfs遍历图
void bfs(ALGraph graph, int v, bool visit[])
{
    queue<int> q;
    q.push(v);
    visit[v] = true;
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        cout<<graph.adjList[cur].data<<"  ";
        for(ArcNode* edge = graph.adjList[cur].first; edge != NULL; edge = edge->next)
        {
            if(!visit[edge->adjvex])
            {
                q.push(edge->adjvex);
                visit[edge->adjvex] = true;
            }
        }
    }
}
 
void bfsTraverse(ALGraph graph)
{
    //初始化访问标记
    bool visit[MAX_NUM];
    memset(visit, 0, sizeof(visit));
    for(int i = 1; i <= graph.vexnum; i++)
        if(!visit[i])
            bfs(graph, i, visit);
    cout<<endl;
}
 
void dfs(ALGraph graph, int v, bool visit[])
{
    cout<<graph.adjList[v].data<<"  "; //v==data
    visit[v] = true;
    for(ArcNode* edge = graph.adjList[v].first; edge != NULL; edge = edge->next)
    {
        if(!visit[edge->adjvex])
        {
            dfs(graph, edge->adjvex, visit);
        }
    }
}
 
void dfsTraverse(ALGraph graph)
{
    bool visit[MAX_NUM];
    memset(visit, 0, sizeof(visit));
    for(int i = 1; i <= graph.vexnum; i++)
        if(!visit[i])
            dfs(graph, i, visit);
    cout<<endl;
}
 
int main()
{
    int edgeInfo[] = {1,2,1,4,2,5,3,1,3,6,4,2,4,6,6,5};
    ALGraph graph;
    init(graph);
 
    buildGraph(graph, edgeInfo, 16);
    cout<<"图的顶点个数: "<<graph.vexnum<<endl;
    cout<<"图的边条数: "<<graph.arcnum<<endl;
    cout<<"广度优先遍历序列: "<<endl;
    bfsTraverse(graph);
    cout<<"深度优先遍历序列: "<<endl;
    dfsTraverse(graph);
    return 0;
}

  

posted on   wastonl  阅读(170)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示