UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • Running Algorithms
    • Degree Centrality
    • Closeness Centrality
    • Harmonic Centrality
    • Eccentricity Centrality
    • Betweenness Centrality
    • Bridges
    • Articulation Points
    • Eigenvector Centrality
    • Katz Centrality
    • CELF
    • PageRank
    • ArticleRank
    • TextRank
    • HITS
    • SybilRank
    • Jaccard Similarity
    • Overlap Similarity
    • Cosine Similarity
    • Pearson Correlation Coefficient
    • Euclidean Distance
    • KNN
    • Vector Similarity
    • Bipartite Graph
    • HyperANF
    • Weakly Connected Components (WCC)
    • Strongly Connected Components (SCC)
    • k-Edge Connected Components
    • Local Clustering Coefficient
    • Triangle Count
    • Clique Count
    • k-Core
    • k-Truss
    • p-Cohesion
    • Induced Subgraph
    • Topological Sort
    • Breadth-First Search (BFS)
    • Depth-First Search (DFS)
    • Dijkstra's Shortest Path
    • A* Shortest Path
    • Yen's K-Shortest Paths
    • Shortest Path (BFS)
    • Delta-Stepping SSSP
    • Shortest Path Faster Algorithm (SPFA)
    • All-Pairs Shortest Path (APSP)
    • Minimum Spanning Tree (MST)
    • K-Spanning Tree
    • Steiner Tree
    • Prize-Collecting Steiner Tree (PCST)
    • Minimum Cost Flow
    • Maximum Flow
    • K-Hop Fast
    • Longest Path (DAG)
    • Random Walk
    • Adamic-Adar Index
    • Common Neighbors
    • Preferential Attachment
    • Resource Allocation
    • Total Neighbors
    • Same Community
    • Louvain
    • Leiden
    • Modularity Optimization
    • Label Propagation
    • HANP
    • SLPA
    • k-Means
    • HDBSCAN
    • K-1 Coloring
    • Modularity
    • Conductance
    • Max k-Cut
      • Node2Vec
      • Struc2Vec
      • LINE
      • Fast Random Projection
      • Summary of Graph Embedding
      • Gradient Descent
      • Backpropagation
      • Skip-gram
      • Skip-gram Optimization
  1. Docs
  2. /
  3. Graph Algorithms
  4. /
  5. Pathfinding

Depth-First Search (DFS)

Overview

Graph traversal is a search technique used to systematically visit and explore all the nodes in a graph. Its primary goal is to reveal and examine the structure and connections of the graph. There are two common strategies for graph traversal:

  • Breadth-First Search (BFS)
  • Depth-First Search (DFS)

The DFS algorithm is based on the principle of backtracking and proceeds as follows:

  1. Create a stack (last-in, first-out) to keep track of visited nodes.
  2. Start from a selected node, push it into the stack, and mark it as visited.
  3. Push any unvisited neighbor of the node on the top of the stack into the stack, and mark it as visited. If multiple unvisited neighbors exist, select one arbitrarily or according to a predefined order.
  4. Repeat step 3 until there are no more unvisited neighbors to push into the stack.
  5. When there are no new nodes to visit, backtrack to the previous node (the one from which the current node was explored) by popping the top node from the stack.
  6. Repeat steps 3, 4 and 5 until the stack is empty.

Below is an example of traversing the graph using the DFS approach, starting from node A and assuming to visit neighbors in alphabetical order (A~Z):

Considerations

  • Only nodes within the same connected component as the start node will be traversed. Nodes in other connected components are excluded from the traversal results.

Example Graph

GQL
INSERT (A:default {_id: "A"}), (B:default {_id: "B"}),
       (C:default {_id: "C"}), (D:default {_id: "D"}),
       (E:default {_id: "E"}), (F:default {_id: "F"}),
       (G:default {_id: "G"}),
       (A)-[:default]->(B), (A)-[:default]->(D),
       (B)-[:default]->(E), (C)-[:default]->(A),
       (E)-[:default]->(F), (F)-[:default]->(C),
       (G)-[:default]->(D)

Parameters

NameTypeDefaultDescription
startNodeSTRING/Required. Starting node _id.
maxDepthINT-1Maximum depth to traverse (-1 = unlimited).
directionSTRINGoutEdge direction: in, out, or both.

Run Mode

Returns:

ColumnTypeDescription
nodeIdSTRINGNode identifier (_id)
depthINTDepth from start node
parentSTRINGParent node in DFS tree
discoveryOrderINTOrder in which the node was first visited
finishOrderINTOrder in which the node was fully explored
GQL
CALL algo.dfs({
  startNode: "B",
  direction: "in"
}) YIELD nodeId, depth, parent, discoveryOrder, finishOrder

Result:

nodeIddepthparentdiscoveryOrderfinishOrder
B009
A1B18
C2A27
F3C36
E4F45

Stream Mode

Returns the same columns as run mode, streamed for memory efficiency.

GQL
CALL algo.dfs.stream({
  startNode: "A",
  maxDepth: 3
}) YIELD nodeId, depth
RETURN nodeId, depth

Result:

nodeIddepth
A0
D1
B1
E2
F3

Stats Mode

Returns:

ColumnTypeDescription
nodeCountINTTotal number of nodes visited
maxDepthINTMaximum depth reached from start node
GQL
CALL algo.dfs.stats({
  startNode: "A"
}) YIELD nodeCount, maxDepth

Result:

nodeCountmaxDepth
64