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
An integral part of storing, manipulating, and retrieving numerical data are *data structures* or as they are called in Dart: [collections].
9
9
Arguably the most common data structure is the *list*. It enables efficient storage and retrieval of sequential data that can be associated with an index.
10
10
11
-
A more general (non-linear) data structure where an element may be connected to one, several, or none of the other elements is called a **graph**.
11
+
A more general (non-linear) data structure where an element may be connected to one, several, or none of the other elements is called a *graph*.
12
12
13
13
Graphs are useful when keeping track of elements that are linked to or are dependent on other elements.
14
-
Examples include: network connections, links in a document pointing to other paragraphs or documents, foreign keys in a relational database, file dependencies in a build system, etc.
14
+
Examples include: network connections, links in a document pointing to other paragraphs or documents,
15
+
foreign keys in a relational database, file dependencies in a build system, etc.
15
16
16
17
The package [`directed_graph`][directed_graph] contains an implementation of a Dart graph that follows the
17
18
recommendations found in [graphs-examples] and is compatible with the algorithms provided by [`graphs`][graphs].
@@ -26,36 +27,42 @@ for finding:
26
27
* all paths connecting two vertices,
27
28
* the shortest paths from a vertex to all connected vertices,
28
29
* cycles,
29
-
* a topological ordering of the graph vertices.
30
+
* a topological ordering of the graph vertices,
31
+
* a reverse topological ordering of the graph vertices.
30
32
31
33
The class [`GraphCrawler`][GraphCrawler] can be used to retrieve *paths* or *walks* connecting two vertices.
32
34
33
35
## Terminology
34
36
35
-
Elements of a graph are called **vertices** (or nodes) and neighbouring vertices are connected by **edges**.
36
-
The figure below shows a **directed graph** with unidirectional edges depicted as arrows.
37
-
Graph edges are emanating from a vertex and ending at a vertex. In a **weighted directed graph** each
37
+
Elements of a graph are called *vertices* (or nodes) and neighbouring vertices are connected by *edges*.
38
+
The figure below shows a *directed graph* with unidirectional edges depicted as arrows.
39
+
Graph edges are emanating from a vertex and ending at a vertex. In a *weighted directed graph* each
-**In-degree** of a vertex: Number of edges ending at this vertex. For example, vertex H has in-degree 3.
43
-
-**Out-degree** of a vertex: Number of edges starting at this vertex. For example, vertex F has out-degree 1.
44
-
-**Source**: A vertex with in-degree zero is called (local) source. Vertices A and D in the graph above are local sources.
45
-
-**Directed Edge**: An ordered pair of connected vertices (v<sub>i</sub>, v<sub>j</sub>). For example, the edge (A, C) starts at vertex A and ends at vertex C.
46
-
-**Path**: A path \[v<sub>i</sub>, ..., v<sub>n</sub>\] is an ordered list of at least two connected vertices where each **inner** vertex is **distinct**.
44
+
-*In-degree* of a vertex: Number of edges ending at this vertex. For example, vertex H has in-degree 3.
45
+
-*Out-degree* of a vertex: Number of edges starting at this vertex. For example, vertex F has out-degree 1.
46
+
-*Source*: A vertex with in-degree zero is called (local) source. Vertices A and D in the graph above are local sources.
47
+
-*Directed Edge*: An ordered pair of connected vertices (v<sub>i</sub>, v<sub>j</sub>). For example, the edge (A, C) starts at vertex A and ends at vertex C.
48
+
-*Path*: A path \[v<sub>i</sub>, ..., v<sub>n</sub>\] is an ordered list of at least two connected vertices where each *inner* vertex is *distinct*.
47
49
The path \[A, E, G\] starts at vertex A and ends at vertex G.
48
-
-**Cycle**: A cycle is an ordered *list* of connected vertices where each inner vertex is distinct and the
50
+
-*Cycle*: A cycle is an ordered *list* of connected vertices where each inner vertex is distinct and the
49
51
first and last vertices are identical. The sequence \[F, I, K, F\] completes a cycle.
50
-
-**Walk**: A walk is an ordered *list* of at least two connected vertices.
52
+
-*Walk*: A walk is an ordered *list* of at least two connected vertices.
51
53
\[D, F, I, K, F\] is a walk but not a path since the vertex F is listed twice.
52
-
-**DAG**: An acronym for **Directed Acyclic Graph**, a directed graph without cycles.
53
-
-**Topological ordering**: An ordered *set* of all vertices in a graph such that v<sub>i</sub>
54
+
-*DAG*: An acronym for *Directed Acyclic Graph*, a directed graph without cycles.
55
+
-*Topological ordering*: An ordered *set* of all vertices in a graph such that v<sub>i</sub>
54
56
occurs before v<sub>j</sub> if there is a directed edge (v<sub>i</sub>, v<sub>j</sub>).
55
57
A topological ordering of the graph above is: \{A, D, B, C, E, K, F, G, H, I, L\}.
56
58
Hereby, dashed edges were disregarded since a cyclic graph does not have a topological ordering.
59
+
-*Quasi-Topological ordering*: An ordered *sub-set* of graph vertices
60
+
such that v<sub>i</sub>
61
+
occurs before v<sub>j</sub> if there is a directed edge (v<sub>i</sub>, v<sub>j</sub>).
62
+
For a quasi-topological ordering to exist, any two vertices belonging to the *sub-set* must not have mutually connecting edges.
63
+
57
64
58
-
**Note**: In the context of this package the definition of *edge* might be more lax compared to a
65
+
*Note*: In the context of this package the definition of *edge* might be more lax compared to a
59
66
rigorous mathematical definition.
60
67
For example, self-loops, that is edges connecting a vertex to itself are explicitly allowed.
61
68
@@ -67,38 +74,40 @@ example below shows how to construct an object of type [`DirectedGraph`][Directe
67
74
The graph classes provided by this library are generic with type argument
68
75
`T extends Object`, that is `T` must be non-nullable.
69
76
Graph vertices can be sorted if `T is Comparable` or
70
-
if a custom comparator function is provided. In the example below, a custom
71
-
comparator is used to sort vertices in inverse lexicographical order.
77
+
if a custom comparator function is provided.
78
+
79
+
Note: If `T is Comparable` and no comparator is provided, then
80
+
the following default comparator is added:
81
+
```Dart
82
+
(T left, T right) => (left as Comparable<T>).compareTo(right);
83
+
```
84
+
Compared to an explicit comparator this function contains a cast and
85
+
the benchmarks show that is approximatly 3 × slower.
86
+
For large graphs it is advisable to follow the example below and
87
+
explicitly provide a comparator.
88
+
89
+
In the example below, a custom
90
+
comparator is used to sort vertices in lexicographical order.
0 commit comments