题目描述

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

给定图中的两个结点的指针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. RAMOS测速

    WIN7X64-DDR31600-RAMOS测速,连续读取17GB/S,连续写入10GB/S,4K读写都是1GB/S左右.不错哦.

  2. WIN10X64_LTSB2016极限精简版by双心

    WIN10X64LTSB2016极限精简版by双心http://www.cnblogs.com/liuzhaoyzz/p/9162113.html 一.前言:关于极限精简版的说明 本系统为极限精简版, ...

  3. 使用R的注意事项

    换源,将源切换到中国 具体方法是: options(repos=structure(c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/&qu ...

  4. Elasticsearch-6.7.0系列(四)Metricbeat仪表盘。本身无端口,依赖kibana

    前提 centos7环境 https://www.cnblogs.com/zhuwenjoyce/p/10629320.html         elasticsearch搜索引擎 https://w ...

  5. OkGo3.0 --真实项目使用和二次封装(转)

    转载:https://blog.csdn.net/jiushiwo12340/article/details/79011480  11.OkGo3.0真实项目使用和二次封装: ====  11.OkG ...

  6. mysql 下的update select from的两种方式比较

    工作中遇到需要将一个表中的数据按照对应规则填入别的表中的情况 例如 表1 a a1    a2 11     90889 32     31241 12     52123 表2 b b1     b ...

  7. .net core2 单元测试

    1.下载   https://marketplace.visualstudio.com/items?itemName=RandomEngy.UnitTestBoilerplateGenerator 2 ...

  8. java封装的概念学习笔记

      继承.封装.多态.抽象是面向对象编程的四大基本概念,其中封装装为重要,因为从我们学习JAVA开始,就基本上接触了封装,因为JAVA中的所有程序都是写在类中的,类也能当做一种封装. 在面向对象中封装 ...

  9. JPA相关知识

    这篇文章是摘自Patrick Linskey的一篇文章,主要是关于JPA相关内容的问答,相信JPA面试会碰到很多这里面的问题 问题:EJB专家团队是如何摆脱事务描述符的? 回答:在会话bean和消息驱 ...

  10. php curl请求页面数据

    /** * * [curl_post post方式请求] * * @param [type] $url [description] * * @param string $data [descripti ...