当前位置:网站首页>Depth first traversal of C implementation Diagram -- non recursive code
Depth first traversal of C implementation Diagram -- non recursive code
2022-07-01 03:18:00 【Dusk and starry sky】
In this paper, C# Realize the depth first traversal of graph – Non recursive code
1、 The procedure is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace The application of graph __ Depth first search algorithm
{
using VertexType = System.Char;// Vertex data type alias declaration
using EdgeType = System.Int16;// Data type alias declaration of edge weight in weighted graph
class Program
{
public const int MAxVertexNum = 100;// The maximum number of vertices
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;
}
}
// Assignment of Graphs
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(“ Recursion depth first :”);
DFS_Traverse(G);
Console.ReadLine();
}
/// <summary>
/// Definition of graph -- Adjacency matrix
/// </summary>
public struct MGraph
{
public VertexType[] vex;// Vertex table array
public EdgeType[,] Edge;// Adjacency matrix 、 Side table
public int vexnum, arcnum;// The current vertex and arc number of a graph
}
/// <summary>
/// Definition of graph -- Adjacency table method
/// </summary>
public class ArcNode
{// Edge table node
public int adjvex;
public ArcNode next;
}
public class VNode
{ // Vertex table node
VertexType data;// Vertex information
ArcNode first;// Just think of the pointer to the first arc attached to the vertex
}
public class ALGraph
{
VNode[] vertices; // Adjacency list
int vexnum, arcnum;// The number of vertices and arcs of a graph
}
/// <summary>
/// Recursive implementation of depth first search
/// </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>
/// Recursive implementation
/// </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>
/// Non recursive implementation
/// </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);// Push
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);
}
}
}
}
// The console prints traversal points
static void visit(MGraph G, int v)
{
Console.Write(G.vex[v] + " ");
}
// lookup G in ,V The first adjacent node of a vertex
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;// Return to the first adjacent contact
}
// lookup G in ,V Vertex W The next adjacent node after the adjacent node
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;// Return to the next adjacent contact
}
/// <summary>
/// Stack definition
/// </summary>
public struct SqStack
{
public int[] data;
public int top;// To the top of the stack
}
/// <summary>
/// Judge whether the stack is empty
/// </summary>
/// <param name=""></param>
/// <returns></returns>
static bool StackEmpty(SqStack S)
{
if (S.top == -1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Stack initialization
/// </summary>
/// <param name="S"></param>
static void InitStack(ref SqStack S)
{
S.top = -1;
}
/// <summary>
/// Pressing stack
/// </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;// First plus 1, Go back to the stack
return true;
}
/// <summary>
/// Out of the stack
/// </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--];// Out of the stack
return true;
}
/// <summary>
///
/// Read the top element of the stack
/// </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];// Read elements
return true;
}
}
}
2、 Test the following :


边栏推荐
- split(),splice(),slice()傻傻分不清楚?
- An article explaining the publisher subscriber model and the observer model
- How do spark tasks of 10W workers run? (Distributed Computing)
- 手把手带你了解一块电路板,从设计到制作(干货)
- How to open a stock account? Also, is it safe to open an account online?
- ctfshow爆破wp
- Druid monitoring statistics source
- Golang多图生成gif
- C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
- 调试定位导航遇到的问题总结
猜你喜欢

Introduction and basic knowledge of machine learning

Restcloud ETL practice data row column conversion

第03章_用戶與權限管理

Redis efficient like and cancel function

Restcloud ETL WebService data synchronization to local

几行事务代码,让我赔了16万
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

MySQL index --01--- design principle of index

How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet

终极套娃 2.0 | 云原生交付的封装
随机推荐
C#实现图的深度优先遍历--非递归代码
JUC学习
Network address translation (NAT) technology
[linear DP] longest common subsequence
Huawei operator level router configuration example | BGP VPLS configuration example
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
Mybati SQL statement printing
If a parent class defines a parameterless constructor, is it necessary to call super ()?
通信协议——分类及其特征介绍
Servlet [first introduction]
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
[exsi] transfer files between hosts
Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
Poj-3486-computers[dynamic planning]
POI导出excel,按照父子节点进行分级显示
Nacos
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
xxl-job使用指南
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)