查询以检查图形中是否存在只访问了一次边的循环 [英] Query to check if there is a cycle in a graph with edges visited only once

查看:98
本文介绍了查询以检查图形中是否存在只访问了一次边的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的图上运行查询,如果没有路径仅遍历每个边并返回起点,则输出"false".

How to write an query runs on my graph, that outputs 'false' if there is NO path traversing through each edge only once and return to the starting point.

推荐答案

我正在使用以下以下查询返回第一个可能的路径:

The following query returns the first possible path:

gremlin> g.V().sideEffect(outE("bridge").aggregate("bridges")).barrier().
......1>   repeat(bothE().or(__.not(select('e')),
......2>                     __.not(filter(__.as('x').select(all, 'e').unfold().where(eq('x'))))).as('e').otherV()).
......3>     until(select(all, 'e').count(local).as("c").select("bridges").count(local).where(eq("c"))).limit(1).
......4>   path().by(id).by(constant(" -> ")).map {String.join("", it.get().objects())}
==>orange -> blue -> white -> orange -> red -> white -> red -> orange -> blue

如果您只需要一个布尔值,则只需追加.hasNext():

If all you need is a boolean value, then just append .hasNext():

g.V().sideEffect(outE("bridge").aggregate("bridges")).barrier().
  repeat(bothE().or(__.not(select('e')),
                    __.not(filter(__.as('x').select(all, 'e').unfold().where(eq('x'))))).as('e').otherV()).
    until(select(all, 'e').count(local).as("c").select("bridges").count(local).where(eq("c"))).hasNext()

这篇关于查询以检查图形中是否存在只访问了一次边的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆