以下是老师作为数据结构课的作业的要求,没有什么实际用处和可以探讨和总结的的地方,所以简单代码直接展示。

宽度优先遍历:

 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; #define _clr(x, y) memset(x, y, sizeof(x))
#define N 1010 int head[N], tot;
struct Edge
{
int v, next;
}edge[N];
int Queue[N];
bool used[N]; void Add(int u, int v)
{
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
} void bfs(int s)
{
_clr(Queue, );
_clr(used, );
int front=, rear=;
Queue[rear++] = ;
cout << s <<" ";
used[s] = true;
while(front < rear)
{
int Cur = Queue[front++];
for(int i=head[Cur]; i!=-; i=edge[i].next)
{
int v = edge[i].v;
if(!used[v])
{
used[v] = true;
cout << v << " ";
Queue[rear++] = v;
}
}
}
cout << endl;
}
int main()
{
int n, m, x, y;
cout << "请输入图的顶点数和边数: ";
while(cin >> n >> m && n+m)
{
tot = ;
_clr(head, -);
for(int i=; i<m; i++)
{
scanf("%d%d",&x, &y);
Add(x, y);
}
cout << "广度优先遍历顺序如下:\n";
bfs();
cout<<endl;
cout << "请输入图的顶点数和边数(输入两个0代表结束输入): ";
}
return ;
}

深度优先遍历:

 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; #define _clr(x, y) memset(x, y, sizeof(x))
#define N 1010 int head[N], tot;
struct Edge
{
int v, next;
}edge[N];
int Queue[N];
bool used[N]; void Add(int u, int v)
{
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
} void dfs(int s)
{
cout << s << " ";
for(int i=head[s]; i!=-; i=edge[i].next)
{
int v = edge[i].v;
if(!used[v])
{
used[v] = true;
dfs(v);
}
}
}
int main()
{
int n, m, x, y;
cout << "请输入图的顶点数和边数: ";
while(cin >> n >> m && n+m)
{
tot = ;
_clr(head, -);
for(int i=; i<m; i++)
{
scanf("%d%d",&x, &y);
Add(x, y);
}
cout << "深优先遍历顺序如下:\n";
dfs();
cout<<endl;
cout << "请输入图的顶点数和边数(输入两个0代表结束输入): ";
}
return ;
}

图的广度、深度优先遍历 C语言的更多相关文章

  1. 1047图的深度优先遍历c语言

    描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...

  2. 图的建立(邻接矩阵)+深度优先遍历+广度优先遍历+Prim算法构造最小生成树(Java语言描述)

    主要参考资料:数据结构(C语言版)严蔚敏   ,http://blog.chinaunix.net/uid-25324849-id-2182922.html   代码测试通过. package 图的建 ...

  3. C语言实现邻接矩阵创建无向图&图的深度优先遍历

    /* '邻接矩阵' 实现无向图的创建.深度优先遍历*/ #include <stdio.h> #include <stdlib.h> #define MaxVex 100 // ...

  4. [PHP] 算法-邻接矩阵图的广度和深度优先遍历的PHP实现

    1.图的深度优先遍历类似前序遍历,图的广度优先类似树的层序遍历 2.将图进行变形,根据顶点和边的关系进行层次划分,使用队列来进行遍历 3.广度优先遍历的关键点是使用一个队列来把当前结点的所有下一级关联 ...

  5. 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径1

    import java.util.ArrayList; import java.util.List; // 模块E public class AdjMatrixGraph<E> { pro ...

  6. 图的存储及遍历 深度遍历和广度遍历 C++代码实现

    /*图的存储及遍历*/ #include<iostream> using namespace std; //----------------------------------- //邻接 ...

  7. C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)

    图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...

  8. 【图的遍历】广度优先遍历(DFS)、深度优先遍历(BFS)及其应用

    无向图满足约束条件的路径 •[目的]:掌握深度优先遍历算法在求解图路径搜索问题的应用 [内容]:编写一个程序,设计相关算法,从无向图G中找出满足如下条件的所有路径:  (1)给定起点u和终点v.  ( ...

  9. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

随机推荐

  1. STL set容器添加结构体并排序

    #include <iostream> #include <string> #include <cstring> //strcpy #include <cst ...

  2. c++ 编译期计算 (一)

    编译期就是编译器进行编译,产生.obj文件的所处的那一段时间(如果是广义的编译期,那么一般还包括了链接期,因为现在很多编译器都会自动调用链接器进行链接)执行期就是你执行某个已经链接好的程序的那段时间. ...

  3. pair/sort/find/qsort

    1. pair template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_t ...

  4. web worker使用

    使用postMessage()方法传递信息.来自Worker的数据保存在event.data中.通过message和error事件与页面通信. <script> var data = [4 ...

  5. 关于HTML的使用。

    一丶标签问题 对于初学者来说,无疑用得最多的标签就是div和span了,当然就算只用这2个标签也能写出一个好看的页面,但是W3C为什么会给我们这么多标签来选择呢? 从浏览器的渲染来讲,标签的语义话,更 ...

  6. Choose the best route--hdu2680

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. LeetCode_Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. VS2010的openssl源码编译方法

    http://download.csdn.net/download/soucula/9591308

  9. 关于GC的意见

    转载:http://tieba.baidu.com/p/3171732371?pid=53949564351&cid=#53949564351 0.这里的GC是指“垃圾回收”(garbage ...

  10. Eclipse代码提示功能设置(Java & Eclipse+CDT C/C++)

    http://developer.51cto.com/art/200907/136242.htm http://blog.chinaunix.net/u/21684/showart_462486.ht ...