当前位置:网站首页>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 :


边栏推荐
- Stop saying that you can't solve the "cross domain" problem
- Mouse over effect 10
- servlet【初识】
- MCU firmware packaging Script Software
- Kmeans
- An article explaining the publisher subscriber model and the observer model
- XXL job User Guide
- 【日常训练】1175. 质数排列
- Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
- [reading notes] copywriting realization -- four golden steps for writing effective copywriting
猜你喜欢

【Qt】添加第三方库的知识补充

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

Introduction and installation of Solr

HTB-Lame

Chapter 03_ User and authority management
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

雪崩问题以及sentinel的使用

Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example

Edge Drawing: A combined real-time edge and segment detector 翻译

Huawei operator level router configuration example | BGP VPLS configuration example
随机推荐
最新接口自动化面试题
VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
Clion and C language
Let's just say I can use thousands of expression packs
[linear DP] longest common subsequence
Subnet division and subnet summary
How to verify whether the contents of two files are the same
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
Huawei operator level router configuration example | configuration static VPLS example
Huawei operator level router configuration example | BGP VPLS configuration example
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
XXL job User Guide
如何校验两个文件内容是否相同
力扣-两数之和
Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
第03章_用户与权限管理
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
Lavaweb [first understanding the solution of subsequent problems]
How do I hide div on Google maps- How to float a div over Google Maps?
伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示