source: project/wiki/eggref/4/digraph @ 24478

Last change on this file since 24478 was 24478, checked in by Ivan Raikov, 9 years ago

digraph version history update

File size: 7.2 KB
Line 
1[[tags:egg]]
2
3== digraph
4
5Directed graph in adjacency list format.
6
7[[toc:]]
8
9== Usage
10
11(require-extension digraph)
12
13== Documentation
14
15
16The {{digraph}} library is an implementation of a directed graph, where
17the edges are stored as adjacency lists indexed by node number.
18
19The library defines a digraph "object" -- a procedure that takes a
20method name as a symbol, and returns the procedure that implements the
21respective operation.
22
23=== Directed graph procedures
24
25
26The digraph object is created by procedure {{make-digraph}}, which is the only user-visible procedure defined in this egg:
27
28<procedure>make-digraph:: NAME INFO [NODE-LIST [SUCC-LIST [PRED-LIST]]] -> SELECTOR</procedure>
29
30where:
31
32* {{NAME}} is the graph name (string or symbol)
33* {{INFO}} is an optional metadata object of an arbitrary type or {{#f}}
34* {{NODE-LIST}} is an optional list of nodes to be inserted in the graph; each element of the list must be of the form {{(N INFO)}} where {{N}} is a unique node number (integer), and {{INFO}} is an optional metadata object describing the node.
35* {{SUCC-LIST}} and {{PRED-LIST}} can be used to define the graph edges upon graph creation. If supplied, these arguments must be lists in which every element is of the form {{(I J INFO)}}, where {{I}} and {{J}} are node numbers, and {{INFO}} is an optional metadata object.
36
37
38The returned selector procedure can take one of the following arguments:
39
40; {{'name}} : returns the graph name (string or symbol)
41; {{'info}} : returns the graph metadata (arbitrary type)
42; {{'new-id!}} : returns a procedure with no arguments, which returns the lowest available node number
43; {{'add-node!}} : returns a procedure {{LAMBDA N INFO}} which inserts in the graph node with number {{N}} and metadata {{INFO}}; if the node already exists in the graph, it will be overwritten with the new metadata
44; {{'add-edge!}} : returns a procedure {{LAMBDA EDGE}} which inserts in the graph the specifed edge; the edge is given by a list of the form {{(I J INFO)}}, where {{I}} and {{J}} are source and destination nodes, respectively, and {{INFO}} is edge metadata of arbitrary type
45; {{'remove-node!}} : returns a procedure {{LAMBDA N}} which removes node {{N}} and all its edges from the graph
46; {{'nodes}} : returns a procedure with no arguments, which returns a list with the nodes of the graph and their metadata
47; {{'edges}} : returns a procedure with no arguments, which returns a list with the edges of the graph and their metadata
48; {{'roots}} : returns a procedure with no arguments, which returns a list with all nodes in the graph that do not have an predecessor
49; {{'order}} : returns a procedure with no arguments, which returns the number of nodes in the graph
50; {{'size}} : returns a procedure with no arguments, which returns the number of edges in the graph
51; {{'capacity}} : returns a procedure with no arguments, which returns the size of the underlying dynamic vector
52; {{'succ}} : returns a procedure {{LAMBDA N}} which returns a list with the successor nodes of node {{N}}
53; {{'pred}} : returns a procedure {{LAMBDA N}} which returns a list with the predecessor nodes of node {{N}}
54; {{'succ-list}} : returns a procedure with no arguments  which returns a list containing the successor nodes for each node.
55; {{'pred-list}} : returns a procedure with no arguments  which returns a list containing the predecessor nodes for each node.
56; {{'out-edges}} : returns a procedure {{LAMBDA N}} which returns a list with the outgoing edges of node {{N}}
57; {{'in-edges}} : returns a procedure {{LAMBDA N}} which returns a list with the incoming edges of node {{N}}
58; {{'has-edge}} : returns a procedure {{LAMBDA I J}} which returns true if edge {{I -> J}} exists in the graph and false otherwise
59; {{'has-node}} : returns a procedure {{LAMBDA N}} which returns true if node {{N}} exists in the graph and false otherwise
60; {{'node-info}} : returns a procedure {{LAMBDA N}} which returns the metadata for node {{N}}
61; {{'node-info-set!}} : returns a procedure {{LAMBDA N V}} which sets the metadata for node {{N}}
62; {{'foreach-node}} : returns an iterator procedure {{LAMBDA F}} which iterates over the nodes in the graph by invoking function {{F}} on the node number and metadata of each node
63; {{'foreach-edge}} : returns an iterator procedure {{LAMBDA F}} which iterates over the edges in the graph by invoking function {{F}} on each edge
64; {{'debug}} : returns a list with the internal representation of the graph
65
66
67
68== Examples
69
70
71 ;; example adapted from graph example in the Boost library documentation
72 (require-extension srfi-1)
73 (require-extension digraph)
74 (define g (make-digraph 'depgraph "dependency graph"))
75 
76 (define used-by
77    (list
78      (cons 'dax_h 'foo_cpp) (cons 'dax_h 'bar_cpp) (cons 'dax_h 'yow_h)
79      (cons 'yow_h 'bar_cpp) (cons 'yow_h 'zag_cpp) (cons 'boz_h 'bar_cpp)
80      (cons 'boz_h 'zig_cpp) (cons 'boz_h 'zag_cpp) (cons 'zow_h 'foo_cpp)
81      (cons 'foo_cpp 'foo_o) (cons 'foo_o 'libfoobar_a)
82      (cons 'bar_cpp 'bar_o) (cons 'bar_o 'libfoobar_a)
83      (cons 'libfoobar_a 'libzigzag_a) (cons 'zig_cpp 'zig_o)
84      (cons 'zig_o 'libzigzag_a) (cons 'zag_cpp 'zag_o)
85      (cons 'zag_o 'libzigzag_a) (cons 'libzigzag_a 'killerapp)))
86 
87 
88 (define node-list (delete-duplicates
89                   (concatenate (list (map car used-by) (map cdr used-by)))))
90 
91 (define node-ids (list-tabulate (length node-list) values))
92 
93 (for-each (lambda (i n) ((g 'add-node!) i n)) node-ids node-list)
94 (define node-map (zip node-list node-ids))
95 
96 (for-each (lambda (e)
97            (match e ((ni . nj) (let ((i (car (alist-ref ni node-map)))
98                                      (j (car (alist-ref nj node-map))))
99                                  ((g 'add-edge!) (list i j (format "~A->~A" ni nj)))))
100                   (else (error "invalid edge " e))))
101          used-by)
102 (print ((g 'nodes)))
103 (print ((g 'edges)))
104 
105 ((g 'remove-node!) 0)
106 (print ((g 'nodes)))
107 (print ((g 'edges)))
108
109== About this egg
110
111
112=== Author
113
114[[/users/ivan-raikov|Ivan Raikov]]
115
116=== Version history
117
118; 1.15 : Ensure unit test script return proper exit code
119; 1.13 : Added test as a test dependency
120; 1.12 : Converted documentation to wiki format
121; 1.11 : Ported to Chicken 4
122; 1.10 : Now using matchable extension
123; 1.9 : Added procedures pred-list and succ-list
124; 1.8 : Added procedure node-info-set!
125; 1.7 : Build script updated for better cross-platform compatibility
126; 1.6 : Test infrastructure changed to use testbase
127; 1.5 : Bug fixes in set-out-edges! and set-in-edges! [thanks to Andreas Scholta]
128; 1.4 : License upgrade to GPL v3
129; 1.3 : Updated the roots procedure to match the documentation
130; 1.2 : Minor changes to the setup script
131; 1.1 : Added support for chicken-setup -test
132; 1.0 : Initial release
133
134
135=== License
136
137 Copyright 2007-2011 Ivan Raikov and the Okinawa Institute of Science and Technology
138 
139 This program is free software: you can redistribute it and/or modify
140 it under the terms of the GNU General Public License as published by
141 the Free Software Foundation, either version 3 of the License, or (at
142 your option) any later version.
143 
144 This program is distributed in the hope that it will be useful, but
145 WITHOUT ANY WARRANTY; without even the implied warranty of
146 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147 General Public License for more details.
148 
149 A full copy of the GPL license can be found at
150 <http://www.gnu.org/licenses/>.
151
Note: See TracBrowser for help on using the repository browser.