Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| retegraphprocessing:elements:prototyping:simple_algorithm [2023/06/20 10:40] – [Simple Cycle Detection within the Web Graph library] gedbadmin | retegraphprocessing:elements:prototyping:simple_algorithm [2024/01/19 17:41] (current) – removed gedbadmin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Simple Algorithm Prototype ====== | ||
| - | |||
| - | ===== Simple Cycle Detection ===== | ||
| - | |||
| - | ==== Defining a Simple Cycle ==== | ||
| - | |||
| - | |||
| - | The goal of this prototype is to implement a simple algorithm for detecting simple cycles within a graph. | ||
| - | |||
| - | Let //G = (V,E)// be a directed graph (//G//) consists of a finite set of vertices (//V//) and a finite set of edges (// | ||
| - | |||
| - | Within G a cycle of length //l// is a sequence of vertices [v< | ||
| - | |||
| - | The cycle is simple when each vertex in [v< | ||
| - | |||
| - | ==== Simple Cycle Detection using standard algorithms ==== | ||
| - | |||
| - | |||
| - | |||
| - | ==== Simple Cycle Detection within the Web Graph library ==== | ||
| - | |||
| - | The Web Graph library does not include an algorithm for Cycle Detection. | ||
| - | |||
| - | ==== Simple Cycle Detection using a Graph Database | ||
| - | |||
| - | === Cycle Detection within Neo4J === | ||
| - | |||
| - | The Neo4J library provides two methods for detecting cycles: cyphter queries and the APOC library. | ||
| - | |||
| - | Cypther is a declarative query language supported by neo4j. | ||
| - | |||
| - | The following code was provided on Stack Overflow | ||
| - | |||
| - | < | ||
| - | |||
| - | MATCH p=(n)-[*1..15]-> | ||
| - | |||
| - | </ | ||
| - | |||
| - | However, as noted in the stack overflow discussion, this simple example has many limitations: | ||
| - | - The query is expensive for larger graphs, with the cost growing exponentially as the graph size and //l// are increased. | ||
| - | - It will return cycles consisting of a single vertex. | ||
| - | - It detects cycles that include duplicate vertices. | ||
| - | |||
| - | For detecting cycles in a more convenient and better performing way the APOC library provides the '' | ||
| - | |||
| - | The signature for the procedure is as follows: | ||
| - | |||
| - | < | ||
| - | apoc.nodes.cycles(nodes :: LIST? OF NODE?, config = {} :: MAP?) :: (path :: PATH?) | ||
| - | </ | ||
| - | |||
| - | The documentation provides the following example: | ||
| - | |||
| - | < | ||
| - | CREATE (m1:Start {bar: ' | ||
| - | CREATE (m1:Start {bar: ' | ||
| - | CREATE (m1:Start {bar: ' | ||
| - | CREATE (m1:Start {bar: ' | ||
| - | CREATE (m1:Start {bar: ' | ||
| - | |||
| - | MATCH (m1:Start) WITH collect(m1) as nodes CALL apoc.nodes.cycles(nodes) YIELD path RETURN path | ||
| - | </ | ||
| - | |||
| - | |||
| - | === Cycle Detection within Tinkerpop === | ||
| - | |||
| - | The Apache Tinkerpop documentation provides a recipe for Cycle Detection ((https:// | ||
| - | |||
| - | The following code detects cycles of arbitrary length. | ||
| - | |||
| - | < | ||
| - | g.V().as(' | ||
| - | both().where(eq(' | ||
| - | dedup().by(unfold().order().by(id).dedup().fold()) | ||
| - | </ | ||
| - | |||
| - | This code takes advantage of the the following Gremlin steps: | ||
| - | * simplePath() step to ensure that no vertex is repeated within the cycle ((https:// | ||
| - | * loops() step to count the number of times a loop is repeated ((https:// | ||
| - | * dedup() step to remove repeated objects ((https:// | ||
| - | |||
| - | These loops() ad dedup() steps indicate inefficiencies in the recipe. | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | ====== References ====== | ||
| - | |||
| - | * Golumbic, M. C. (2004). Algorithmic Graph Theory and Perfect Graphs (2nd ed., Chapter 1 - Graph Theoretic Foundations). North Holland Publishing Company, a Subsidiary of Eslevier. | ||