You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/modules/ROOT/pages/algorithms/dag/dag-algorithms.adoc
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,11 @@
6
6
Directed Acyclic Graphs (DAGs) are directed graphs that do not contain cycles.
7
7
These kind of graphs are commonly used to model dependencies between entities.
8
8
9
-
The canonical algorithm that goes hand in hand with DAGs is topological sort, for which GDS provides an efficient parallel implementation. Running topological sort is the best way to make sure the graph is a DAG.
9
+
The canonical algorithm that goes hand in hand with DAGs is topological sort, for which GDS provides an efficient parallel implementation.
10
+
Running topological sort is the best way to make sure the graph is a DAG.
10
11
11
-
Some of the problems that are computationally hard to solve in the general case can be solved efficiently when the scope is limited to DAGs. One of these is the longest path problem, for which GDS provides an efficient algorithm.
12
+
Some of the problems that are computationally hard to solve in the general case can be solved efficiently when the scope is limited to DAGs.
13
+
One of these is the longest path problem, for which GDS provides an efficient algorithm.
12
14
13
15
The Neo4j GDS library includes the following DAG algorithms:
Finding the longest path that leads to a node in a graph is possible to do in linear time for the special case of DAGs.
20
20
21
-
GDS implementation for this algorithm is based on topological sort and takes linear time. If the graph is not a DAG, the runtime is still linear, but the results cannot be trusted. You can use xref:algorithms/dag/topological-sort.adoc[topological sort] to make sure the graph is a DAG.
21
+
GDS implementation for this algorithm is based on topological sort and takes linear time.
22
+
If the graph is not a DAG, the runtime is still linear, but the results cannot be trusted.
23
+
You can use xref:algorithms/dag/topological-sort.adoc[topological sort] to make sure the graph is a DAG.
22
24
23
-
The algorithm supports weighted and unweighted graphs. Negative weights are currently unsupported.
25
+
The algorithm supports weighted and unweighted graphs.
26
+
Negative weights are currently unsupported.
24
27
25
28
26
29
=== Usage
27
30
28
-
One example for usage of this algorithm is in the context of a supply chain graph. If edges indicate the time to supply, then the distance of the longest path to a target node is the time required to manufacture the node from decision to completion.
31
+
One example for usage of this algorithm is in the context of a supply chain graph.
32
+
If edges indicate the time to supply, then the distance of the longest path to a target node is the time required to manufacture the node from decision to completion.
| nodeIds | List of Integer | Node ids on the path in traversal order.
77
81
| costs | List of Float | Accumulated costs for each node on the path.
78
82
| path | Path | The path represented as Cypher entity.
79
-
80
83
|===
81
84
82
85
// include-with-stream
@@ -110,9 +113,13 @@ CREATE
110
113
----
111
114
112
115
This graph describes a simple supply chain of constructing a table in the Table Maker workshop.
113
-
In order to have lumber for the table, the workshop processes timber, which takes 1 day to complete. Once the lumber is ready, it is already in the workshop, therefor it takes zero time to ship it. However, the screws take 3 days to be shipped to the workshop. Only after the workshop has all the requirements met, the table can be constructed, a process that takes 1 day.
116
+
In order to have lumber for the table, the workshop processes timber, which takes 1 day to complete.
117
+
Once the lumber is ready, it is already in the workshop, therefor it takes zero time to ship it.
118
+
However, the screws take 3 days to be shipped to the workshop.
119
+
Only after the workshop has all the requirements met, the table can be constructed, a process that takes 1 day.
114
120
115
-
The longest path to the table node starts with the screws, then the workshop and then the table, in total: 4 days. This is the bottleneck path, and total time that takes to manufacture the table.
121
+
The longest path to the table node starts with the screws, then the workshop and then the table, in total: 4 days.
122
+
This is the bottleneck path, and total time that takes to manufacture the table.
116
123
117
124
.The following Cypher statement will project the graph to GDS:
118
125
[source, cypher, role=noplay setup-query]
@@ -147,16 +154,17 @@ RETURN
147
154
nodes(path) as path
148
155
ORDER BY index
149
156
----
157
+
150
158
We use the utility function `asNode` to return the name of node instead of its ID to make results more readable.
A topological sorting of nodes in a graph is an ordering of the nodes in the graph where every node appears only after all the nodes pointing to it have appeared.
18
18
For example, for a graph with 4 nodes and these relations: `a->b`, `a->c`, `b->d`, `c->d`, there are two acceptable topological sorts: `a, b, c, d` and `a, c, b, d`.
19
19
20
-
The topological order of the nodes is defined only for directed acyclic graphs (DAGs). See xref:#topological-sort-cycles[below] for the expected result for graphs with cycles.
20
+
The topological order of the nodes is defined only for directed acyclic graphs (DAGs).
21
+
See xref:#topological-sort-cycles[below] for the expected result for graphs with cycles.
21
22
22
23
GDS provides an efficient parallel implementation for this algorithm.
23
24
24
25
25
26
[[topological-sort-cycles]]
26
27
=== Cycles
27
28
28
-
Running the algorithm on a graph with cycles will cause the omitting of part of the nodes from the sorting. The omitted nodes are:
29
+
Running the algorithm on a graph with cycles will cause the omitting of part of the nodes from the sorting.
30
+
The omitted nodes are:
29
31
30
32
1. Nodes that are part of a cycle (including self cycles)
31
33
@@ -43,11 +45,17 @@ image::example-graphs/{image-file}[Visualization of the example graph,align="cen
43
45
44
46
=== Usage
45
47
46
-
Topological ordering of the nodes is beneficial when you want to guarantee a node will only be processed after its dependencies were processed. This is very useful for dependency related tasks such as scheduling or calculations that derive values from their dependencies.
48
+
Topological ordering of the nodes is beneficial when you want to guarantee a node will only be processed after its dependencies were processed.
49
+
This is very useful for dependency related tasks such as scheduling or calculations that derive values from their dependencies.
50
+
47
51
48
52
==== Cycles detection
49
53
50
-
The algorithm can also be used to determine if the graph contains a cycle or not. If all the nodes in the graph appear in the sorting, there is no cycle in the graph. If some of the nodes are missing from the sorting, there is a cycle. It does not tell which nodes constitute the cycle, but it does give a clue, as described in the xref:#topological-sort-cycles[cycles] section.
54
+
The algorithm can also be used to determine if the graph contains a cycle or not.
55
+
If all the nodes in the graph appear in the sorting, there is no cycle in the graph.
56
+
If some of the nodes are missing from the sorting, there is a cycle.
57
+
It does not tell which nodes constitute the cycle, but it does give a clue, as described in the xref:#topological-sort-cycles[cycles] section.
This graph describes a simplified supply chain of building a house. Each part of the house cannot be worked on before its requirements are met.
145
+
This graph describes a simplified supply chain of building a house.
146
+
Each part of the house cannot be worked on before its requirements are met.
137
147
For example, we cannot build support before getting the steel, the skeleton is not ready until both support and base are ready.
138
148
139
149
.The following Cypher statement will project the graph to GDS:
@@ -145,8 +155,11 @@ WITH gds.graph.project("g", n, target, {}) AS g
145
155
RETURN g
146
156
----
147
157
158
+
148
159
=== Stream
149
-
The stream procedure streams the nodes in the graph ordered by a valid topological order. The nodes can then be processed one by one, guaranteeing that each node is processed only after its dependencies were processed.
160
+
161
+
The stream procedure streams the nodes in the graph ordered by a valid topological order.
162
+
The nodes can then be processed one by one, guaranteeing that each node is processed only after its dependencies were processed.
150
163
151
164
For more details on the stream mode in general, see xref:common-usage/running-algos.adoc#running-algos-stream[Stream].
0 commit comments