1 ! See http://factorcode.org/license.txt for BSD licence.
2 USING: help.markup help.syntax ;
6 ARTICLE: "graph-protocol" "Graph protocol"
7 "All graphs must be instances of the graph mixin:"
9 "All graphs must implement a method on the following generic word:"
10 { $subsection vertices }
11 "At least one of the following two generic words must have a method; the " { $link graph } " mixin has default definitions which are mutually recursive:"
12 { $subsection adjlist }
14 "All mutable graphs must implement a method on the following generic word:"
15 { $subsection add-blank-vertex }
16 "All mutable undirected graphs must implement a method on the following generic word:"
17 { $subsection add-edge }
18 "Mutable directed graphs should not implement the above word, as it has a default definition defined in terms of the following generic word:"
19 { $subsection add-edge* }
20 "The following two words have default definitions, but are available as generics to allow implementations to optimize them:"
21 { $subsection num-vertices }
22 { $subsection num-edges } ;
25 { $class-description "A mixin class whose instances are graphs. Custom implementations of the graph protocol should be declared as instances of this mixin for all graph functionality to work correctly:"
26 { $code "INSTANCE: hex-board graph" }
29 { vertices num-vertices num-edges } related-words
32 { $values { "graph" graph } { "seq" "The vertices" } }
33 { $description "Returns the vertices of the graph." } ;
36 { $values { "graph" graph } { "n" "The number of vertices" } }
37 { $description "Returns the number of vertices in the graph." } ;
40 { $values { "graph" "A graph" } { "n" "The number of edges" } }
41 { $description "Returns the number of edges in the graph." } ;
43 { adjlist adj? } related-words
47 { "from" "The index of a vertex" }
48 { "graph" "The graph to be examined" }
49 { "seq" "The adjacency list" } }
50 { $description "Returns a sequence of vertices that this vertex links to" } ;
54 { "from" "The index of a vertex" }
55 { "to" "The index of a vertex" }
58 { $description "Returns a boolean describing whether there is an edge in the graph between from and to." } ;
60 { add-blank-vertex add-blank-vertices add-edge add-edge* } related-words
62 HELP: add-blank-vertex
64 { "index" "A vertex index" }
65 { "graph" "A graph" } }
66 { $description "Adds a vertex to the graph." } ;
68 HELP: add-blank-vertices
70 { "seq" "A sequence of vertex indices" }
71 { "graph" "A graph" } }
72 { $description "Adds vertices with indices in seq to the graph." } ;
76 { "from" "The index of a vertex" }
77 { "to" "The index of another vertex" }
78 { "graph" "A graph" } }
79 { $description "Adds a one-way edge to the graph, between " { $snippet "from" } " and " { $snippet "to" } "."
81 "If you want to add a two-way edge, use " { $link add-edge } " instead." } ;
85 { "u" "The index of a vertex" }
86 { "v" "The index of another vertex" }
87 { "graph" "A graph" } }
88 { $description "Adds a two-way edge to the graph, between " { $snippet "u" } " and " { $snippet "v" } "."
90 "If you want to add a one-way edge, use " { $link add-edge* } " instead." } ;
92 { depth-first full-depth-first dag? topological-sort } related-words
96 { "v" "The vertex to start the search at" }
97 { "graph" "The graph to search" }
98 { "pre" "A quotation of the form ( n -- )" }
99 { "post" "A quotation of the form ( n -- )" }
100 { "?list" "A list of booleans describing the vertices visited in the search" }
101 { "?" "A boolean describing whether or not the end-search error was thrown" } }
102 { $description "Performs a depth-first search on " { $emphasis "graph" } ". The variable " { $emphasis "graph" } " can be accessed in both quotations."
104 "The " { $emphasis "pre" } " quotation is run before the recursive application of depth-first."
106 "The " { $emphasis "post" } " quotation is run after the recursive application of depth-first."
108 { $emphasis "?list" } " is a list of booleans, " { $link t } " for every vertex visted during the search, and " { $link f } " for every vertex not visited." } ;
110 HELP: full-depth-first
112 { "graph" "The graph to search" }
113 { "pre" "A quotation of the form ( n -- )" }
114 { "post" "A quotation of the form ( n -- )" }
115 { "tail" "A quotation of the form ( -- )" }
116 { "?" "A boolean describing whether or not the end-search error was thrown" } }
117 { $description "Performs a depth-first search on " { $emphasis "graph" } ". The variable " { $emphasis "graph" } "can be accessed in both quotations."
119 "The " { $emphasis "pre" } " quotation is run before the recursive application of depth-first."
121 "The " { $emphasis "post" } " quotation is run after the recursive application of depth-first."
123 "The " { $emphasis "tail" } " quotation is run after each time the depth-first search runs out of nodes. On an undirected graph this will be each connected subgroup but on a directed graph it can be more complex." } ;
128 { "?" "A boolean indicating if the graph is acyclic" } }
129 { $description "Using a depth-first search, determines if the specified directed graph is a directed acyclic graph. An undirected graph will produce a false result, as the algorithm does not eliminate cycles of length 2, which will include any edge that goes both ways." } ;
131 HELP: topological-sort
134 { "seq/f" "Either a sequence of values or f" } }
135 { $description "Using a depth-first search, topologically sorts the specified directed graph. Returns f if the graph contains any cycles, and a topologically sorted sequence otherwise." } ;