2017-07-25 15:38:00

writer:pprp

在前一篇图基于邻接列表表示法的代码加了一小部分,加了一个DFS函数,visited[N]数组

参考书目:张新华的《算法竞赛宝典》


代码如下:

#include <iostream>

using namespace std;

const int N = ;

int visited[N] = {};    //新引入一个数组,用于标记是否访问过

struct node
{
int vertex;
node*next;
}; node head[N];
void create(int node1,int node2)//通过起点和终点的值创建一个邻接表
{
node * point;
node * New = new node();
if(New!=NULL)
{
New->vertex = node2;
New->next = NULL;
point = &(head[node1]);
while(point->next!=NULL)
point = point->next;
point->next = New;
}
} void DFS(int vertex)
{
node*point;
visited[vertex] = ;
cout <<"["<<vertex<<"]->";
point = head[vertex].next;
while(point!=NULL)
{
if(visited[point->vertex]==)
DFS(point->vertex);
point = point->next;
}
} void print()
{
node*point;
for(int i = ; i < N; i++)
{
point = head[i].next;
cout << "Head["<<i<<"]";
while(point!=NULL)
{
cout <<"-> "<<point->vertex;
point = point->next;
}
cout << endl;
}
} int main()
{
int node1,node2; for(int i = ; i < N; i++)
{
head[i].vertex = i;
head[i].next = NULL;
}
while()
{
cout <<"please enter the start point" << endl; cin >> node1;
if(node1 == -)
break;
cout <<"please enter the end point" << endl;
cin >> node2; if(node1 == node2)
cout <<"自身循环"<<endl;
else if(node1>=N||node2>=N)
cout <<"超出范围"<<endl;
else
create(node1,node2);
} cout << "邻接表为:" << endl;
print(); DFS(); cout <<"\n"<<endl; return ;
}

DFS - 深度搜索 - 基于邻接列表表示法的更多相关文章

  1. [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  2. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. BFS - 广度优先搜索 - 邻接列表表示法

    2017-07-25 21:40:22 writer:pprp 在DFS的基础上加上了一个BFS函数 #include <iostream> #include <queue> ...

  5. [LeetCode] Unique Binary Search Trees II dfs 深度搜索

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  6. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. 题目--oil Deposits(油田) 基础DFS(深度搜索)

    上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...

  8. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...

  9. Python 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法

    图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...

随机推荐

  1. LeetCode 学习

    1.整数反转 题目:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 思路:把最后的一位提取出来,放到新的容器前面,反复进行上面的操作,同时也要判断是否会导致溢出 class ...

  2. 支付宝SDK的使用方法

    前奏 现在随着移动开发的快速发展,越来越多的应用要求在线支付功能.最近做了一个关于支付宝支付功能的应用,在使用支付宝的过程中,遇到一些不必要的弯路,因此,写了这篇文章总结一下关于ios开发如何使用支付 ...

  3. 好难忘又伤心一个非常好的学习js群解散了

    不知道为什么看到web前端之天天向上这个群解散,好难过.应该可以说,这个群是我见过比较靠谱的群,大家都非常热情帮助.那些管理员管理的非常好,突然解散了.觉得好可惜,也不会因为你是菜鸟不让你加,感觉好可 ...

  4. PHP数组遍历详解

    一.PHP数组简介 1.PHP数组的分类 按照下标的不同分为关联数组和索引数组①索引数组:下标从0开始依次增长②关联数组:下标为字符串格式,每个下标字符串与数组的值一一对应,(有点像对象的键值对) 下 ...

  5. php smarty模板引擎

    <?php /* 一.什么是smarty? smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲, 目的就是要使用PHP程序员同美工分离,使用的程序员改 ...

  6. 白话Redis分布式锁

    redis分布式 简单来说就是,操作redis实例时,不是常规(单机)操作一个实例,而是操作两台或多台,也就是基于分布式集群: 而且redis内部是单进程.单线程,是数据安全的(只有自己的线程在操作数 ...

  7. Scrapy(爬虫)基本运行机制

    Scrapy(爬虫)基本运行机制

  8. pip安装lxml报错 Fatal error in launcher: Unable to create process using '"c:\users\administrator\appdata\local\programs\python\python36\python.exe" "C:\Users\Administrator\AppData\L

    pip install lxml 安装报错 E:\apollo\spider_code>Fatal error in launcher: Unable to create process usi ...

  9. 我的Android进阶之旅------>解决错误: java.util.regex.PatternSyntaxException: Incorrect Unicode property

    1.错误描述 今天使用正则表达式验证密码的时候,报了错误 java.util.regex.PatternSyntaxException: Incorrect Unicode property near ...

  10. Leetcode 之 Kth Largest Element in an Array

    636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...