# 第六章学习小结

 1 //邻接矩阵
2 typedef struct
3 {
4     VerTexType vexs[MAX];
5     ArcType arcs[MAX][MAX];
6     int vexnum, arcnum;
7 }AMGraph;
8
9 //邻接表
10 typedef struct ArcNode
11 {
13     struct ArcNode *next;
14     OtherInfo info;
15 }ArcNode;
16 typedef struct VNode
17 {
18     VerTexType data;
19     ArcNode *first;
21 typedef struct
22 {
24     int vexnum, arcnum;
25 }ALGraph;

 1 void DFS(graph& g, int v)//深度优先
2 {
3     cout << v << " ";
4     visited[v] = true;
5     for (int i = 0; i < g.vexnum; i++)
6     {
7         if (visited[i] == false && g.arcs[v][i] == 1) DFS(g, i);
8     }
9 }
10
11 void BFS(graph& g, int v)//广度优先
12 {
13     cout << v << " ";
14     visited[v] = true;
15     queue<int> q;
16     q.push(v);
17     while (!q.empty())
18     {
19         int k = q.front();
20         q.pop();
21         for(int i=0; i<g.vexnum; i++)
22             if (visited[i] == false && g.arcs[k][i] == 1)
23             {
24                 cout << i << " ";
25                 visited[i] = true;
26                 q.push(i);
27             }
28     }
29 }

求最短路径主要是用Dijkstra算法，过程和最小生成树有点相似，刚开始学的时候不是很懂，之后重新看才理解

posted @ 2020-06-14 18:22  1907陈航  阅读(269)  评论(0编辑  收藏  举报