DFS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LH.GraphConsole
{
    class Program
    {
        private static bool[] visited;

        static void Main(string[] args)
        {
            DFSTranverse();
        }

        private static void DFSTranverse()
        {
            int vertexNumber = 9;
            int edgeNumber = 15;
            Graph graph = new Graph(vertexNumber, edgeNumber);

            graph.Vertexs[0] = "A";
            graph.Vertexs[1] = "B";
            graph.Vertexs[2] = "C";
            graph.Vertexs[3] = "D";
            graph.Vertexs[4] = "E";
            graph.Vertexs[5] = "F";
            graph.Vertexs[6] = "G";
            graph.Vertexs[7] = "H";
            graph.Vertexs[8] = "I";

            graph.Edges[0, 1] = 1;
            graph.Edges[0, 5] = 1;

            graph.Edges[1, 2] = 1;
            graph.Edges[1, 8] = 1;
            graph.Edges[1, 6] = 1;

            graph.Edges[2, 3] = 1;
            graph.Edges[2, 8] = 1;

            graph.Edges[3, 8] = 1;
            graph.Edges[3, 6] = 1;
            graph.Edges[3, 7] = 1;
            graph.Edges[3, 4] = 1;

            graph.Edges[4, 7] = 1;
            graph.Edges[4, 5] = 1;

            graph.Edges[5, 6] = 1;

            graph.Edges[6, 7] = 1;

            visited = new bool[vertexNumber];
            for (int i = 0; i < vertexNumber; i++)
            {
                if (!visited[i])
                {
                    DFS(graph, i);
                }
            }
        }
        private static void DFS(Graph graph, int vertexIndex)
        {
            visited[vertexIndex] = true;
            Console.WriteLine("Visit vertex index: " + vertexIndex);

            var size = graph.Vertexs.Count();
            for (int i = 0; i < size; i++)
            {
                if (graph.Edges[vertexIndex, i] == 1 && !visited[i])
                {
                    DFS(graph, i);
                }
            }
        }
    }
}

输出结果:
 

数据结构之图 Part3 – 1 遍历的更多相关文章

  1. 数据结构之图 Part3 – 2 遍历

    BFS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  2. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  3. 重新整理数据结构与算法(c#)—— 图的深度遍历和广度遍历[十一]

    参考网址:https://www.cnblogs.com/aoximin/p/13162635.html 前言 简介图: 在数据的逻辑结构D=(KR)中,如果K中结点对于关系R的前趋和后继的个数不加限 ...

  4. 【PHP数据结构】图的遍历:深度优先与广度优先

    在上一篇文章中,我们学习完了图的相关的存储结构,也就是 邻接矩阵 和 邻接表 .它们分别就代表了最典型的 顺序存储 和 链式存储 两种类型.既然数据结构有了,那么我们接下来当然就是学习对这些数据结构的 ...

  5. 数据结构之 图论---图的深度遍历( 输出dfs的先后遍历序列 )

    图的深度遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 请定一个无向图,顶点编号从0到n-1,用深度优先搜索(DFS),遍历并输出.遍历时,先遍历节点编 ...

  6. 数据结构实验之图论二:图的深度遍历(SDUT 2107)(简单DFS)

    题解:图的深度遍历就是顺着一个最初的结点开始,把与它相邻的结点都找到,也就是一直往下搜索直到尽头,然后在顺次找其他的结点. #include <bits/stdc++.h> using n ...

  7. 数据结构——图的深度优先遍历(邻接矩阵表示+java版本)

    ​1.深度优先遍历(DFS) 图的深度优先遍历本质上是一棵树的前序遍历(即先遍历自身,然后遍历其左子树,再遍历右子树),总之图的深度优先遍历是一个递归的过程. 如下图所示,左图是一个图,右图是图的深度 ...

  8. <数据结构>XDOJ324,325图的优先遍历

    XDOJ324.图的广度优先遍历 问题与解答 问题描述 已知无向图的邻接矩阵,以该矩阵为基础,给出广度优先搜索遍历序列,并且给出该无向图的连通分量的个数.在遍历时,当有多个点可选时,优先选择编号小的顶 ...

  9. SDUT-2107_图的深度遍历

    数据结构实验之图论二:图的深度遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请定一个无向图,顶点编号从0到n-1 ...

随机推荐

  1. Extjs 组件共用(单例)问题

    说明: 将store初始化在类定义时便创建, store实例将成为该类的单例 代码: 测试: 说明: 将store初始化放入initComponent函数中.  每次都将创建一个新的实例. 代码: 测 ...

  2. Discovering versions from the identity service failed when creating the password plugin.

    If you are behind the proxy, then you have to set no_proxy environmental variable for your localhost ...

  3. history/location操作 /navigator 操作/ screen操作

    话说mac真的没广告...就凭这点,mac完胜.ei capitan 好牛畅,分屏很好用啊,回不去了. var hist=window.history; hist.go(param); // para ...

  4. 解读Unity中的CG编写Shader系列十 (光滑的镜面反射(冯氏着色))

    前文完成了最基本的镜面反射着色器,单平行光源下的逐顶点着色(per-vertex lighting),又称为古罗着色(Gouraud shading).这篇文章作为后续讨论更光滑的镜面反射方式,逐像素 ...

  5. Icon Font的转换

    Icon Font是用于网页的纯色图标,这里引用一张网络图片: 由于体积小,易维护等特点,IconFont应用非常广泛. 这里推荐一个转换器,通过Upload一个后缀ttf的字体文件,可以反解出文件下 ...

  6. 实现Windows Phone 8中ListBox的分页加载

    功能就是ListBox滚动到最下方的时候,能够自动加载下一页的内容. 解决问题的关键就是如何判断ListBox已经加载到了最底部. 网上找了两个解决方法: 1 http://googlers.itey ...

  7. OKhttp基本使用介绍

    MainActivity.class package com.example.administrator.okhttp3; import android.support.v7.app.AppCompa ...

  8. 【Python升级录】--基础知识

    创建角色成功! 正在载入python........ [python介绍] python是一门动态解释性的强类型定义语言. python的创始人为吉多·范罗苏姆(Guido van Rossum).1 ...

  9. php数据访问(查询)

    查询:常用关键字查询 和 准确查询 单条件查询 创建添加查询元素 <br /> <form action="main.php" method="post ...

  10. August 14th, Week 34th Sunday, 2016

    To live is to function, that is all there is in living. 活着就要发挥作用,这就是生活的全部内容. I often joke that my dr ...