题目描述

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

给定图中的两个结点的指针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. 使用velodyne16线激光雷达跑loam-velodyne

    一.velodyne-VLP16使用教程 推荐网址: http://blog.csdn.net/littlethunder/article/details/51920681 https://www.c ...

  2. Vue开发环境搭建

    Nodejs与包管理工具安装: Windows安装: https://nodejs.org/en/ 下载安装包安装就可以. Ubuntu安装: //安装nodejs sudo apt-get inst ...

  3. Browser Page Parsing Details

    Browser Work: 1.输入网址.  2.浏览器查找域名的IP地址.  3. 浏览器给web服务器发送一个HTTP请求  4. 网站服务的永久重定向响应  5. 浏览器跟踪重定向地址 现在,浏 ...

  4. 转 Ubuntu16.04+QT4.8.7开发环境搭建

    Qt安装步骤1.安装g++以及依赖库 sudo apt-get install g++  sudo apt-get install g++-multilib libx11-dev libxext-de ...

  5. EntityFramework Inner Exception Catch

    在保存时加入这一段,就可以查看error具体是哪里出错了.正式发布需要删除这段,try catch毕竟会影响性能 try { entity.SaveChanges(); } catch (DbEnti ...

  6. bzoj5109: [CodePlus 2017]大吉大利,晚上吃鸡!

    Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏中,皮皮 和毛毛最喜欢做的事情就是堵桥,每每有一个好时机都能收到不少的快 ...

  7. FuzzScanner 信息收集小工具

    前言: 该工具集成了各种大牛的工具,比如子域名发现,目录扫描,nmap端口扫描,c段地址查询,端口指纹,以及waf查询 00X1: 安装不推荐git安装,首先直接githup脱下来:git clone ...

  8. Android中刷新Invalidate和postInvalidate的区别

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用.Android提供了Inval ...

  9. RMAN-06900 RMAN-06901 ORA-19921

    转自http://blog.itpub.net/12778571/viewspace-700360/ 1.连接到rman中$ rman target/Recovery Manager: Release ...

  10. windows10下 MySQL5.7.18版本安装过程及遇到的问题

    windows10下 MySQL5.7.18版本安装过程及遇到的问题           mysql-5.7.18-winx64 安装           1.解压 此次将MySQL装在H盘,依个人喜 ...