题目描述

对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径。

给定图中的两个结点的指针DirectedGraphNode* a, DirectedGraphNode* b(请不要在意数据类型,图是有向图),请返回一个bool,代表两点之间是否存在一条路径(a到b或b到a)。

代码如下:

 package com.yzh.hehe;

 import java.util.ArrayList;
import java.util.Stack; public class DirGraCheckPath { public static void main(String[] args) {
// TODO Auto-generated method stub } public boolean checkPath(UndirectedGraphNode a, UndirectedGraphNode b) {
return checkSingle(a, b)||checkSingle(b, a);
} private boolean checkSingle(UndirectedGraphNode a, UndirectedGraphNode b) {
if (a.label==b.label) {
return true;
}
//深度优先遍历用堆栈实现(广度优先遍历用队列实现)
Stack<UndirectedGraphNode> stack= new Stack<UndirectedGraphNode>();
//已遍历的点
ArrayList<UndirectedGraphNode> list=new ArrayList<UndirectedGraphNode>();
list.add(a);
stack.addAll(a.neighbors);
UndirectedGraphNode temp = null;
//深度遍历一个点,看是否目标点,是结束,否则再将此点的连接点放入栈中等待遍历重复(入栈将连接点中已经在栈中和已遍历过的点去掉)。
while (!stack.isEmpty()) {
temp = stack.pop();
if (temp.label==b.label) {
return true;
}
for (UndirectedGraphNode undirectedGraphNode : temp.neighbors) {
if (!stack.contains(undirectedGraphNode)&&!list.contains(undirectedGraphNode)) {
stack.push(undirectedGraphNode);
}
}
list.add(temp);
}
return false;
} }
class UndirectedGraphNode {
int label = 0;
UndirectedGraphNode left = null;
UndirectedGraphNode right = null;
ArrayList<UndirectedGraphNode> neighbors = new ArrayList<UndirectedGraphNode>(); public UndirectedGraphNode(int label) {
this.label = label;
}
}

解题(DirGraCheckPath--有向图的遍历(深度搜索))的更多相关文章

  1. F - 蜘蛛牌(深度搜索)

    Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...

  2. [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 ...

  3. c/c++连通图的遍历(深度遍历/广度遍历)

    连通图的遍历(深度遍历/广度遍历) 概念:图中的所有节点都要遍历到,并且只能遍历一次. 深度遍历 广度遍历 深度遍历 概念:从一个给定的顶点开始,找到一条边,沿着这条边一直遍历. 广度遍历 概念:从一 ...

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

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

  5. DS图遍历--深度优先搜索

    DS图遍历--深度优先搜索 题目描述 给出一个图的邻接矩阵,对图进行深度优先搜索,从顶点0开始 注意:图n个顶点编号从0到n-1 代码框架如下: 输入 第一行输入t,表示有t个测试实例 第二行输入n, ...

  6. #C++初学记录(深度搜索#递归)

    深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  7. 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)

    #include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...

  8. [LeetCode] Populating Next Right Pointers in Each Node 深度搜索

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  10. [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 ...

随机推荐

  1. 6.824 LAB1 环境搭建

    MIT 6.824 LAB1 环境搭建 vmware 虚拟机 linux ubuntu server   安装 go 官方安装步骤: 下载此压缩包并提取到 /usr/local 目录,在 /usr/l ...

  2. Elasticsearch-6.7.0系列(一)9200端口 .tar.gz版本centos7环境--下载安装运行

    https://www.elastic.co/guide/index.html(推荐)        ES官方英文原版文档,一般会更新到最新版本 https://www.elastic.co/cn/d ...

  3. cefsharp插入自定义JS

       string script_1 = "document.getElementsByTagName('head')[0].appendChild(document.createEleme ...

  4. 【转录组入门】6:reads计数

    作业要求: 实现这个功能的软件也很多,还是烦请大家先自己搜索几个教程,入门请统一用htseq-count,对每个样本都会输出一个表达量文件. 需要用脚本合并所有的样本为表达矩阵.参考:生信编程直播第四 ...

  5. ESP32 做Web服务器 http Server步骤

    资料不多.多是国外网站的. 百度搜基本出来的是这个网站https://www.dfrobot.com/blog-922.html 出来的代码是: #include <WiFi.h>#inc ...

  6. sql server top 10 IO性能查询

    use master go ), ((case qs.statement_end_offset then datalength(qt.text) else qs.statement_end_offse ...

  7. php查询mysql数据库

    1.连接数据库,写成一个php,其他文件直接include <?php $connect = mysql_connect("ip地址","用户",&quo ...

  8. MIUI6系统详细卡刷开发版获得root权限的经验

    小米的手机不同手机型号通常情况miui论坛都提供两个不同的版本,分别为稳定版和开发版,稳定版没有提供ROOT权限管理,开发版中就开启了ROOT权限,很多情况下我们需要使用的一些功能强大的App,都需要 ...

  9. Linux在VirtualBox的网络设置

    一.Linux系统版本:Centos7.4. 二.访问外网:在设置-网络-网卡1处选择 “网络地址置换(NAT)”即可.默认情况下,自动获取IP(DHCP),但要在配置中把开机启动选上. 文件位置: ...

  10. DatetimeHelper,时间帮助类

    public static class DateTimeEx { /// <summary> /// 得到中文形式的日期 /// </summary> /// <para ...