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


边栏推荐
猜你喜欢

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)

STM32——一线协议之DS18B20温度采样

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

Latest interface automation interview questions

C语言多线程编程入门学习笔记

So easy deploy program to server
![[applet project development -- JD mall] uni app commodity classification page (first)](/img/6c/5b92fc1f18d58e0fdf6f1896188fcd.png)
[applet project development -- JD mall] uni app commodity classification page (first)

Share Creators萌芽人才培養計劃來了!

Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example

Completely solve the lost connection to MySQL server at 'reading initial communication packet
随机推荐
力扣-两数之和
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
PHP batch Excel to word
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
[linear DP] shortest editing distance
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
Redis efficient like and cancel function
第03章_用戶與權限管理
【小程序项目开发-- 京东商城】uni-app之分类导航区域
An article explaining the publisher subscriber model and the observer model
[applet project development -- JD mall] uni app commodity classification page (Part 2)
Introduction to core functions of webrtc -- an article on understanding SDP PlanB unifiedplan (migrating from PlanB to unifiedplan)
xxl-job使用指南
咱就是说 随便整几千个表情包为我所用一下
Summary of problems encountered in debugging positioning and navigation
C language EXECL function
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
So easy deploy program to server
Magnetic manometer and measurement of foreign coins
How to verify whether the contents of two files are the same