当前位置:网站首页>C#实现图的深度优先遍历--非递归代码
C#实现图的深度优先遍历--非递归代码
2022-07-01 02:58:00 【黄昏和星空】
本文介绍C#实现图的深度优先遍历–非递归代码
1、程序如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 图的应用__深度优先搜索算法
{
using VertexType = System.Char;//顶点数据类型别名声明
using EdgeType = System.Int16;//带权图中边上权值的数据类型别名声明
class Program
{
public const int MAxVertexNum = 100;//顶点数目的最大值
public const int MAXSize = 100;
static void Main(string[] args)
{
MGraph G = new MGraph();
int u;
int[] d = new int[MAxVertexNum];
G.vexnum = 8;
G.arcnum = 8;
G.vex = new VertexType[MAxVertexNum];
G.Edge = new EdgeType[MAxVertexNum, MAxVertexNum];
for (int i = 0; i < MAxVertexNum; ++i)
{
for (int j = 0; j < MAxVertexNum; ++j)
{
G.Edge[i, j] = 0;
}
}
//图的赋值
G.vex[0] = ‘a’; G.vex[1] = ‘b’; G.vex[2] = ‘c’; G.vex[3] = ‘d’; G.vex[4] = ‘e’; G.vex[5] = ‘f’;
G.vex[6] = ‘g’; G.vex[7] = ‘h’;
G.Edge[0, 1] = 1; G.Edge[0, 2] = 1;
G.Edge[1, 0] = 1; G.Edge[1, 3] = 1; G.Edge[1, 4] = 1;
G.Edge[2, 0] = 1; G.Edge[2, 5] = 1; G.Edge[2, 6] = 1;
G.Edge[3, 1] = 1;
G.Edge[4, 1] = 1; G.Edge[4, 7] = 1;
G.Edge[5, 2] = 1;
G.Edge[6, 2] = 1;
G.Edge[7, 4] = 1;
Console.WriteLine(“递归深度优先:”);
DFS_Traverse(G);
Console.ReadLine();
}
/// <summary>
/// 图的定义--邻接矩阵
/// </summary>
public struct MGraph
{
public VertexType[] vex;//顶点表数组
public EdgeType[,] Edge;//临接矩阵、边表
public int vexnum, arcnum;//图的当前顶点数和弧数
}
/// <summary>
/// 图的定义--邻接表法
/// </summary>
public class ArcNode
{//边表节点
public int adjvex;
public ArcNode next;
}
public class VNode
{ //顶点表节点
VertexType data;//顶点信息
ArcNode first;//只想第一条依附改顶点的弧的指针
}
public class ALGraph
{
VNode[] vertices; //邻接表
int vexnum, arcnum;//图的顶点数和弧数
}
/// <summary>
/// 深度优先搜索的递归实现
/// </summary>
/// <param name="G"></param>
/// <param name="v"></param>
/// <returns></returns>
static void DFS_Traverse(MGraph G) {
bool[] visited = new bool[MAxVertexNum];
for (int i=0;i<G.vexnum;++i) {
visited[i] = false; }
for (int v=0;v<G.vexnum;++v) {
if (!visited[v]) {
DFS2(G,v,ref visited);
}
}
}
/// <summary>
/// 递归实现
/// </summary>
/// <param name="G"></param>
/// <param name="v"></param>
static void DFS(MGraph G,int v, ref bool[] visited) {
visit(G,v);
visited[v] = true;
for (int w=FirstNeighbor(G,v); w>=0; w=NextNeighbor(G,v,w)) {
if (!visited[w]) {
DFS(G, w, ref visited);
}
}
}
/// <summary>
/// 非递归实现
/// </summary>
/// <param name="G"></param>
/// <param name="v"></param>
static void DFS2(MGraph G, int v, ref bool[] visited) {
SqStack S = new SqStack();
S.data = new int[MAxVertexNum];
Push(ref S,v);//入栈
while (!StackEmpty(S)) {
PoP(ref S, ref v);
if (!visited[v]) { visit(G, v); }
visited[v] = true;
for (int w=FirstNeighbor(G,v);w>=0;w=NextNeighbor(G,v,w)) {
if (!visited[w]) {
Push(ref S, w);
}
}
}
}
//控制台打印遍历点
static void visit(MGraph G, int v)
{
Console.Write(G.vex[v] + " ");
}
//查找G中,V顶点的首个邻接点
static int FirstNeighbor(MGraph G, int v)
{
int b = -1;
for (int i = 0; i < G.vexnum; ++i)
{
if (G.Edge[v, i] == 1)
{
b = i;
break;
};
}
return b;//返回首个邻接点
}
//查找G中,V顶点的W邻节点后的下一个邻接点
static int NextNeighbor(MGraph G, int v, int w)
{
int b = -1;
for (int i = w + 1; i < G.vexnum; ++i)
{
if (G.Edge[v, i] == 1)
{
b = i;
break;
};
}
return b;//返回下一个邻接点
}
/// <summary>
/// 栈定义
/// </summary>
public struct SqStack
{
public int[] data;
public int top;//栈顶
}
/// <summary>
/// 判断栈是否为空
/// </summary>
/// <param name=""></param>
/// <returns></returns>
static bool StackEmpty(SqStack S)
{
if (S.top == -1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 栈初始化
/// </summary>
/// <param name="S"></param>
static void InitStack(ref SqStack S)
{
S.top = -1;
}
/// <summary>
/// 压栈
/// </summary>
/// <param name="S"></param>
/// <returns></returns>
static bool Push(ref SqStack S, int e)
{
if (S.top >= MAxVertexNum - 1)
{
return false;
}
S.top = S.top + 1;
S.data[S.top] = e;//先加1,再进栈
return true;
}
/// <summary>
/// 出栈
/// </summary>
/// <param name="S"></param>
/// <param name="e"></param>
/// <returns></returns>
static bool PoP(ref SqStack S, ref int e)
{
if (S.top == -1) { return false; }
e = S.data[S.top--];//出栈
return true;
}
/// <summary>
///
///读取栈顶元素
/// </summary>
/// <param name="S"></param>
/// <param name="e"></param>
/// <returns></returns>
bool GetTop(ref SqStack S, ref int e)
{
if (S.top == -1) { return false; }
e = S.data[S.top];//读取元素
return true;
}
}
}
2、测试如下:


边栏推荐
- Mouse over effect 10
- The best learning method in the world: Feynman learning method
- [linear DP] longest common subsequence
- STM32——一线协议之DS18B20温度采样
- 【微信小程序開發】樣式匯總
- An article explaining the publisher subscriber model and the observer model
- If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
- The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
- Restcloud ETL practice data row column conversion
- kubernetes资源对象介绍及常用命令(二)
猜你喜欢

Restcloud ETL practice to realize incremental data synchronization without identification bit

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS

Lenovo x86 server restart management controller (xclarity controller) or TSM method

园区运营效率提升,小程序容器技术加速应用平台化管理

Restcloud ETL data realizes incremental data synchronization through timestamp

8 pits of redis distributed lock
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

So easy deploy program to server

【小程序项目开发-- 京东商城】uni-app之分类导航区域
随机推荐
Redis高效点赞与取消功能
STM32——一线协议之DS18B20温度采样
Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
[wechat applet development] style summary
[applet project development -- JD mall] uni app commodity classification page (Part 2)
鼠标悬停效果十
Mysql知识点
STM32 - DS18B20 temperature sampling of first-line protocol
Chapter 03_ User and authority management
【小程序项目开发-- 京东商城】uni-app之分类导航区域
The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
PTA 1017
世界上最好的学习法:费曼学习法
The best learning method in the world: Feynman learning method
Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
基于Pytorch完整的训练一个神经网络并进行验证
Optimal transport Series 1
Mouse over effect 7
MySQL index --01--- design principle of index
JS to find duplicate elements in two arrays