2 * Introduction to graphs::
3 * Functions and Variables for graphs::
6 @node Introduction to graphs, Functions and Variables for graphs, graphs-pkg, graphs-pkg
7 @section Introduction to graphs
9 The @code{graphs} package provides graph and digraph data structure for
10 Maxima. Graphs and digraphs are simple (have no multiple edges nor
11 loops), although digraphs can have a directed edge from @var{u} to
12 @var{v} and a directed edge from @var{v} to @var{u}.
14 Internally graphs are represented by adjacency lists and implemented as
15 a lisp structures. Vertices are identified by their ids (an id is an
16 integer). Edges/arcs are represented by lists of length 2. Labels can be
17 assigned to vertices of graphs/digraphs and weights can be assigned to
18 edges/arcs of graphs/digraphs.
20 There is a @code{draw_graph} function for drawing graphs. Graphs are
21 drawn using a force based vertex positioning
22 algorithm. @code{draw_graph} can also use graphviz programs available
23 from @url{https://www.graphviz.org}. @code{draw_graph} is based on the maxima
26 To use the @code{graphs} package, first load it with @code{load("graphs")}.
28 @opencatbox{Categories:}
29 @category{Share packages}
30 @category{Package graphs}
33 @node Functions and Variables for graphs, , Introduction to graphs, graphs-pkg
34 @section Functions and Variables for graphs
36 @subsection Building graphs
39 @deffn {Function} create_graph @
40 @fname{create_graph} (@var{v_list}, @var{e_list}) @
41 @fname{create_graph} (@var{n}, @var{e_list}) @
42 @fname{create_graph} (@var{v_list}, @var{e_list}, @var{directed})
44 Creates a new graph on the set of vertices @var{v_list} and with edges @var{e_list}.
46 @var{v_list} is a list of vertices (@code{[v1, v2,..., vn]}) or a
47 list of vertices together with vertex labels (@code{[[v1,l1], [v2,l2],..., [vn,ln]]}).
49 @var{n} is the number of vertices. Vertices will be identified by integers from 0 to n-1.
51 @var{e_list} is a list of edges (@code{[e1, e2,..., em]}) or a list of
52 edges together with edge-weights (@code{[[e1, w1], ..., [em, wm]]}).
54 If @var{directed} is not @code{false}, a directed graph will be returned.
56 Example 1: create a cycle on 3 vertices:
59 @c g : create_graph([1,2,3], [[1,2], [2,3], [1,3]])$
63 (%i1) load ("graphs")$
64 (%i2) g : create_graph([1,2,3], [[1,2], [2,3], [1,3]])$
66 Graph on 3 vertices with 3 edges.
73 Example 2: create a cycle on 3 vertices with edge weights:
76 @c g : create_graph([1,2,3], [[[1,2], 1.0], [[2,3], 2.0],
81 (%i1) load ("graphs")$
82 (%i2) g : create_graph([1,2,3], [[[1,2], 1.0], [[2,3], 2.0],
85 Graph on 3 vertices with 3 edges.
92 Example 3: create a directed graph:
101 @c 'directed = true)$
105 (%i1) load ("graphs")$
106 (%i2) d : create_graph(
113 (%i3) print_graph(d)$
114 Digraph on 4 vertices with 4 arcs.
122 @opencatbox{Categories:}
123 @category{Package graphs}
128 @deffn {Function} copy_graph (@var{g})
129 Returns a copy of the graph @var{g}.
131 @opencatbox{Categories:}
132 @category{Package graphs}
133 @category{Package graphs - constructions}
137 @anchor{circulant_graph}
138 @deffn {Function} circulant_graph (@var{n}, @var{d})
139 Returns the circulant graph with parameters @var{n} and @var{d}.
144 @c g : circulant_graph(10, [1,3])$
148 (%i1) load ("graphs")$
149 (%i2) g : circulant_graph(10, [1,3])$
150 (%i3) print_graph(g)$
151 Graph on 10 vertices with 20 edges.
165 @opencatbox{Categories:}
166 @category{Package graphs}
167 @category{Package graphs - constructions}
171 @anchor{clebsch_graph}
172 @deffn {Function} clebsch_graph ()
173 Returns the Clebsch graph.
175 @opencatbox{Categories:}
176 @category{Package graphs}
177 @category{Package graphs - constructions}
181 @anchor{complement_graph}
182 @deffn {Function} complement_graph (@var{g})
183 Returns the complement of the graph @var{g}.
185 @opencatbox{Categories:}
186 @category{Package graphs}
187 @category{Package graphs - constructions}
191 @anchor{complete_bipartite_graph}
192 @deffn {Function} complete_bipartite_graph (@var{n}, @var{m})
193 Returns the complete bipartite graph on @var{n+m} vertices.
195 @opencatbox{Categories:}
196 @category{Package graphs}
197 @category{Package graphs - constructions}
201 @anchor{complete_graph}
202 @deffn {Function} complete_graph (@var{n})
203 Returns the complete graph on @var{n} vertices.
205 @opencatbox{Categories:}
206 @category{Package graphs}
207 @category{Package graphs - constructions}
211 @anchor{cycle_digraph}
212 @deffn {Function} cycle_digraph (@var{n})
213 Returns the directed cycle on @var{n} vertices.
215 @opencatbox{Categories:}
216 @category{Package graphs}
217 @category{Package graphs - constructions}
222 @deffn {Function} cycle_graph (@var{n})
223 Returns the cycle on @var{n} vertices.
225 @opencatbox{Categories:}
226 @category{Package graphs}
227 @category{Package graphs - constructions}
231 @anchor{cuboctahedron_graph}
232 @deffn {Function} cuboctahedron_graph (@var{n})
233 Returns the cuboctahedron graph.
235 @opencatbox{Categories:}
236 @category{Package graphs}
237 @category{Package graphs - constructions}
242 @deffn {Function} cube_graph (@var{n})
243 Returns the @var{n}-dimensional cube.
245 @opencatbox{Categories:}
246 @category{Package graphs}
247 @category{Package graphs - constructions}
251 @anchor{dodecahedron_graph}
252 @deffn {Function} dodecahedron_graph ()
253 Returns the dodecahedron graph.
255 @opencatbox{Categories:}
256 @category{Package graphs}
257 @category{Package graphs - constructions}
262 @deffn {Function} empty_graph (@var{n})
263 Returns the empty graph on @var{n} vertices.
265 @opencatbox{Categories:}
266 @category{Package graphs}
267 @category{Package graphs - constructions}
271 @anchor{flower_snark}
272 @deffn {Function} flower_snark (@var{n})
273 Returns the flower graph on @var{4n} vertices.
278 @c f5 : flower_snark(5)$
279 @c chromatic_index(f5);
282 (%i1) load ("graphs")$
283 (%i2) f5 : flower_snark(5)$
284 (%i3) chromatic_index(f5);
288 @opencatbox{Categories:}
289 @category{Package graphs}
290 @category{Package graphs - constructions}
294 @anchor{from_adjacency_matrix}
295 @deffn {Function} from_adjacency_matrix (@var{A})
296 Returns the graph represented by its adjacency matrix @var{A}.
298 @opencatbox{Categories:}
299 @category{Package graphs}
300 @category{Package graphs - constructions}
304 @anchor{frucht_graph}
305 @deffn {Function} frucht_graph ()
306 Returns the Frucht graph.
308 @opencatbox{Categories:}
309 @category{Package graphs}
310 @category{Package graphs - constructions}
314 @anchor{graph_product}
315 @deffn {Function} graph_product (@var{g1}, @var{g1})
316 Returns the direct product of graphs @var{g1} and @var{g2}.
321 @c grid : graph_product(path_graph(3), path_graph(4))$
325 (%i1) load ("graphs")$
326 (%i2) grid : graph_product(path_graph(3), path_graph(4))$
327 (%i3) draw_graph(grid)$
330 @opencatbox{Categories:}
331 @category{Package graphs}
332 @category{Package graphs - constructions}
337 @image{figures/graphs01,6cm}
341 @deffn {Function} graph_union (@var{g1}, @var{g1})
342 Returns the union (sum) of graphs @var{g1} and @var{g2}.
344 @opencatbox{Categories:}
345 @category{Package graphs}
346 @category{Package graphs - constructions}
351 @deffn {Function} grid_graph (@var{n}, @var{m})
352 Returns the @var{n x m} grid.
354 @opencatbox{Categories:}
355 @category{Package graphs}
356 @category{Package graphs - constructions}
360 @anchor{great_rhombicosidodecahedron_graph}
361 @deffn {Function} great_rhombicosidodecahedron_graph ()
362 Returns the great rhombicosidodecahedron graph.
364 @opencatbox{Categories:}
365 @category{Package graphs}
366 @category{Package graphs - constructions}
370 @anchor{great_rhombicuboctahedron_graph}
371 @deffn {Function} great_rhombicuboctahedron_graph ()
372 Returns the great rhombicuboctahedron graph.
374 @opencatbox{Categories:}
375 @category{Package graphs}
376 @category{Package graphs - constructions}
380 @anchor{grotzch_graph}
381 @deffn {Function} grotzch_graph ()
382 Returns the Grotzch graph.
384 @opencatbox{Categories:}
385 @category{Package graphs}
386 @category{Package graphs - constructions}
390 @anchor{heawood_graph}
391 @deffn {Function} heawood_graph ()
392 Returns the Heawood graph.
394 @opencatbox{Categories:}
395 @category{Package graphs}
396 @category{Package graphs - constructions}
400 @anchor{icosahedron_graph}
401 @deffn {Function} icosahedron_graph ()
402 Returns the icosahedron graph.
404 @opencatbox{Categories:}
405 @category{Package graphs}
406 @category{Package graphs - constructions}
410 @anchor{icosidodecahedron_graph}
411 @deffn {Function} icosidodecahedron_graph ()
412 Returns the icosidodecahedron graph.
414 @opencatbox{Categories:}
415 @category{Package graphs}
416 @category{Package graphs - constructions}
420 @anchor{induced_subgraph}
421 @deffn {Function} induced_subgraph (@var{V}, @var{g})
422 Returns the graph induced on the subset @var{V} of vertices of the graph
428 @c p : petersen_graph()$
430 @c g : induced_subgraph(V, p)$
434 (%i1) load ("graphs")$
435 (%i2) p : petersen_graph()$
436 (%i3) V : [0,1,2,3,4]$
437 (%i4) g : induced_subgraph(V, p)$
438 (%i5) print_graph(g)$
439 Graph on 5 vertices with 5 edges.
448 @opencatbox{Categories:}
449 @category{Package graphs}
450 @category{Package graphs - constructions}
455 @deffn {Function} line_graph (@var{g})
456 Returns the line graph of the graph @var{g}.
458 @opencatbox{Categories:}
459 @category{Package graphs}
460 @category{Package graphs - constructions}
465 @deffn {Function} make_graph @
466 @fname{make_graph} (@var{vrt}, @var{f}) @
467 @fname{make_graph} (@var{vrt}, @var{f}, @var{oriented})
469 Creates a graph using a predicate function @var{f}.
471 @var{vrt} is a list/set of vertices or an integer. If @var{vrt} is an
472 integer, then vertices of the graph will be integers from 1 to
475 @var{f} is a predicate function. Two vertices @var{a} and @var{b} will
476 be connected if @code{f(a,b)=true}.
478 If @var{directed} is not @var{false}, then the graph will be directed.
483 @c g : make_graph(powerset({1,2,3,4,5}, 2), disjointp)$
484 @c is_isomorphic(g, petersen_graph());
485 @c get_vertex_label(1, g);
488 (%i1) load("graphs")$
489 (%i2) g : make_graph(powerset(@{1,2,3,4,5@}, 2), disjointp)$
490 (%i3) is_isomorphic(g, petersen_graph());
492 (%i4) get_vertex_label(1, g);
499 @c f(i, j) := is (mod(j, i)=0)$
500 @c g : make_graph(20, f, directed=true)$
501 @c out_neighbors(4, g);
502 @c in_neighbors(18, g);
505 (%i1) load("graphs")$
506 (%i2) f(i, j) := is (mod(j, i)=0)$
507 (%i3) g : make_graph(20, f, directed=true)$
508 (%i4) out_neighbors(4, g);
509 (%o4) [8, 12, 16, 20]
510 (%i5) in_neighbors(18, g);
511 (%o5) [1, 2, 3, 6, 9]
514 @opencatbox{Categories:}
515 @category{Package graphs}
516 @category{Package graphs - constructions}
520 @anchor{mycielski_graph}
521 @deffn {Function} mycielski_graph (@var{g})
522 Returns the mycielskian graph of the graph @var{g}.
524 @opencatbox{Categories:}
525 @category{Package graphs}
526 @category{Package graphs - constructions}
531 @deffn {Function} new_graph ()
532 Returns the graph with no vertices and no edges.
534 @opencatbox{Categories:}
535 @category{Package graphs}
536 @category{Package graphs - constructions}
540 @anchor{path_digraph}
541 @deffn {Function} path_digraph (@var{n})
542 Returns the directed path on @var{n} vertices.
544 @opencatbox{Categories:}
545 @category{Package graphs}
546 @category{Package graphs - constructions}
551 @deffn {Function} path_graph (@var{n})
552 Returns the path on @var{n} vertices.
554 @opencatbox{Categories:}
555 @category{Package graphs}
556 @category{Package graphs - constructions}
560 @anchor{petersen_graph}
561 @deffn {Function} petersen_graph @
562 @fname{petersen_graph} () @
563 @fname{petersen_graph} (@var{n}, @var{d})
565 Returns the petersen graph @var{P_@{n,d@}}. The default values for
566 @var{n} and @var{d} are @code{n=5} and @code{d=2}.
568 @opencatbox{Categories:}
569 @category{Package graphs}
570 @category{Package graphs - constructions}
574 @anchor{random_bipartite_graph}
575 @deffn {Function} random_bipartite_graph (@var{a}, @var{b}, @var{p})
576 Returns a random bipartite graph on @code{a+b} vertices. Each edge is
577 present with probability @var{p}.
579 @opencatbox{Categories:}
580 @category{Package graphs}
581 @category{Package graphs - constructions}
585 @anchor{random_digraph}
586 @deffn {Function} random_digraph (@var{n}, @var{p})
587 Returns a random directed graph on @var{n} vertices. Each arc is present
588 with probability @var{p}.
590 @opencatbox{Categories:}
591 @category{Package graphs}
592 @category{Package graphs - constructions}
596 @anchor{random_regular_graph}
597 @deffn {Function} random_regular_graph @
598 @fname{random_regular_graph} (@var{n}) @
599 @fname{random_regular_graph} (@var{n}, @var{d})
601 Returns a random @var{d}-regular graph on @var{n} vertices. The default
602 value for @var{d} is @code{d=3}.
604 @opencatbox{Categories:}
605 @category{Package graphs}
606 @category{Package graphs - constructions}
610 @anchor{random_graph}
611 @deffn {Function} random_graph (@var{n}, @var{p})
612 Returns a random graph on @var{n} vertices. Each edge is present with
615 @opencatbox{Categories:}
616 @category{Package graphs}
617 @category{Package graphs - constructions}
621 @anchor{random_graph1}
622 @deffn {Function} random_graph1 (@var{n}, @var{m})
623 Returns a random graph on @var{n} vertices and random @var{m} edges.
625 @opencatbox{Categories:}
626 @category{Package graphs}
627 @category{Package graphs - constructions}
631 @anchor{random_network}
632 @deffn {Function} random_network (@var{n}, @var{p}, @var{w})
633 Returns a random network on @var{n} vertices. Each arc is present with
634 probability @var{p} and has a weight in the range @code{[0,w]}. The
635 function returns a list @code{[network, source, sink]}.
640 @c [net, s, t] : random_network(50, 0.2, 10.0);
641 @c max_flow(net, s, t)$
645 (%i1) load ("graphs")$
646 (%i2) [net, s, t] : random_network(50, 0.2, 10.0);
647 (%o2) [DIGRAPH, 50, 51]
648 (%i3) max_flow(net, s, t)$
650 (%o4) 27.65981397932507
653 @opencatbox{Categories:}
654 @category{Package graphs}
655 @category{Package graphs - constructions}
659 @anchor{random_tournament}
660 @deffn {Function} random_tournament (@var{n})
661 Returns a random tournament on @var{n} vertices.
663 @opencatbox{Categories:}
664 @category{Package graphs}
665 @category{Package graphs - constructions}
670 @deffn {Function} random_tree (@var{n})
671 Returns a random tree on @var{n} vertices.
673 @opencatbox{Categories:}
674 @category{Package graphs}
675 @category{Package graphs - constructions}
679 @anchor{small_rhombicosidodecahedron_graph}
680 @deffn {Function} small_rhombicosidodecahedron_graph ()
681 Returns the small rhombicosidodecahedron graph.
683 @opencatbox{Categories:}
684 @category{Package graphs}
685 @category{Package graphs - constructions}
689 @anchor{small_rhombicuboctahedron_graph}
690 @deffn {Function} small_rhombicuboctahedron_graph ()
691 Returns the small rhombicuboctahedron graph.
693 @opencatbox{Categories:}
694 @category{Package graphs}
695 @category{Package graphs - constructions}
699 @anchor{snub_cube_graph}
700 @deffn {Function} snub_cube_graph ()
701 Returns the snub cube graph.
703 @opencatbox{Categories:}
704 @category{Package graphs}
705 @category{Package graphs - constructions}
709 @anchor{snub_dodecahedron_graph}
710 @deffn {Function} snub_dodecahedron_graph ()
711 Returns the snub dodecahedron graph.
713 @opencatbox{Categories:}
714 @category{Package graphs}
715 @category{Package graphs - constructions}
719 @anchor{truncated_cube_graph}
720 @deffn {Function} truncated_cube_graph ()
721 Returns the truncated cube graph.
723 @opencatbox{Categories:}
724 @category{Package graphs}
725 @category{Package graphs - constructions}
729 @anchor{truncated_dodecahedron_graph}
730 @deffn {Function} truncated_dodecahedron_graph ()
731 Returns the truncated dodecahedron graph.
733 @opencatbox{Categories:}
734 @category{Package graphs}
735 @category{Package graphs - constructions}
740 @anchor{truncated_icosahedron_graph}
741 @deffn {Function} truncated_icosahedron_graph ()
742 Returns the truncated icosahedron graph.
744 @opencatbox{Categories:}
745 @category{Package graphs}
746 @category{Package graphs - constructions}
751 @anchor{truncated_tetrahedron_graph}
752 @deffn {Function} truncated_tetrahedron_graph ()
753 Returns the truncated tetrahedron graph.
755 @opencatbox{Categories:}
756 @category{Package graphs}
757 @category{Package graphs - constructions}
762 @deffn {Function} tutte_graph ()
763 Returns the Tutte graph.
765 @opencatbox{Categories:}
766 @category{Package graphs}
767 @category{Package graphs - constructions}
771 @anchor{underlying_graph}
772 @deffn {Function} underlying_graph (@var{g})
773 Returns the underlying graph of the directed graph @var{g}.
775 @opencatbox{Categories:}
776 @category{Package graphs}
777 @category{Package graphs - constructions}
782 @deffn {Function} wheel_graph (@var{n})
783 Returns the wheel graph on @var{n+1} vertices.
785 @opencatbox{Categories:}
786 @category{Package graphs}
787 @category{Package graphs - constructions}
791 @subsection Graph properties
793 @anchor{adjacency_matrix}
794 @deffn {Function} adjacency_matrix (@var{gr})
795 Returns the adjacency matrix of the graph @var{gr}.
800 @c c5 : cycle_graph(4)$
801 @c adjacency_matrix(c5);
804 (%i1) load ("graphs")$
805 (%i2) c5 : cycle_graph(4)$
806 (%i3) adjacency_matrix(c5);
816 @opencatbox{Categories:}
817 @category{Package graphs}
818 @category{Package graphs - properties}
822 @anchor{average_degree}
823 @deffn {Function} average_degree (@var{gr})
824 Returns the average degree of vertices in the graph @var{gr}.
829 @c average_degree(grotzch_graph());
832 (%i1) load ("graphs")$
833 (%i2) average_degree(grotzch_graph());
839 @opencatbox{Categories:}
840 @category{Package graphs}
841 @category{Package graphs - properties}
845 @anchor{biconnected_components}
846 @deffn {Function} biconnected_components (@var{gr})
847 Returns the (vertex sets of) 2-connected components of the graph
856 @c [1,2],[2,3],[2,4],[3,4],
857 @c [4,5],[5,6],[4,6],[6,7]
859 @c biconnected_components(g);
862 (%i1) load ("graphs")$
863 (%i2) g : create_graph(
866 [1,2],[2,3],[2,4],[3,4],
867 [4,5],[5,6],[4,6],[6,7]
869 (%i3) biconnected_components(g);
870 (%o3) [[6, 7], [4, 5, 6], [1, 2], [2, 3, 4]]
874 @image{figures/graphs13,6cm}
877 @opencatbox{Categories:}
878 @category{Package graphs}
879 @category{Package graphs - properties}
884 @deffn {Function} bipartition (@var{gr})
885 Returns a bipartition of the vertices of the graph @var{gr} or an empty
886 list if @var{gr} is not bipartite.
892 @c h : heawood_graph()$
893 @c [A,B]:bipartition(h);
894 @c draw_graph(h, show_vertices=A, program=circular)$
897 (%i1) load ("graphs")$
898 (%i2) h : heawood_graph()$
899 (%i3) [A,B]:bipartition(h);
900 (%o3) [[8, 12, 6, 10, 0, 2, 4], [13, 5, 11, 7, 9, 1, 3]]
901 (%i4) draw_graph(h, show_vertices=A, program=circular)$
904 @opencatbox{Categories:}
905 @category{Package graphs}
906 @category{Package graphs - properties}
911 @image{figures/graphs02,6cm}
914 @anchor{chromatic_index}
915 @deffn {Function} chromatic_index (@var{gr})
916 Returns the chromatic index of the graph @var{gr}.
921 @c p : petersen_graph()$
922 @c chromatic_index(p);
925 (%i1) load ("graphs")$
926 (%i2) p : petersen_graph()$
927 (%i3) chromatic_index(p);
931 @opencatbox{Categories:}
932 @category{Package graphs}
933 @category{Package graphs - properties}
937 @anchor{chromatic_number}
938 @deffn {Function} chromatic_number (@var{gr})
939 Returns the chromatic number of the graph @var{gr}.
944 @c chromatic_number(cycle_graph(5));
945 @c chromatic_number(cycle_graph(6));
948 (%i1) load ("graphs")$
949 (%i2) chromatic_number(cycle_graph(5));
951 (%i3) chromatic_number(cycle_graph(6));
955 @opencatbox{Categories:}
956 @category{Package graphs}
957 @category{Package graphs - properties}
961 @anchor{clear_edge_weight}
962 @deffn {Function} clear_edge_weight (@var{e}, @var{gr})
963 Removes the weight of the edge @var{e} in the graph @var{gr}.
969 @c g : create_graph(3, [[[0,1], 1.5], [[1,2], 1.3]])$
970 @c get_edge_weight([0,1], g);
971 @c clear_edge_weight([0,1], g)$
972 @c get_edge_weight([0,1], g);
975 (%i1) load ("graphs")$
976 (%i2) g : create_graph(3, [[[0,1], 1.5], [[1,2], 1.3]])$
977 (%i3) get_edge_weight([0,1], g);
979 (%i4) clear_edge_weight([0,1], g)$
980 (%i5) get_edge_weight([0,1], g);
984 @opencatbox{Categories:}
985 @category{Package graphs}
986 @category{Package graphs - properties}
990 @anchor{clear_vertex_label}
991 @deffn {Function} clear_vertex_label (@var{v}, @var{gr})
992 Removes the label of the vertex @var{v} in the graph @var{gr}.
997 @c g : create_graph([[0,"Zero"], [1, "One"]], [[0,1]])$
998 @c get_vertex_label(0, g);
999 @c clear_vertex_label(0, g);
1000 @c get_vertex_label(0, g);
1003 (%i1) load ("graphs")$
1004 (%i2) g : create_graph([[0,"Zero"], [1, "One"]], [[0,1]])$
1005 (%i3) get_vertex_label(0, g);
1007 (%i4) clear_vertex_label(0, g);
1009 (%i5) get_vertex_label(0, g);
1013 @opencatbox{Categories:}
1014 @category{Package graphs}
1015 @category{Package graphs - properties}
1019 @anchor{connected_components}
1020 @deffn {Function} connected_components (@var{gr})
1021 Returns the (vertex sets of) connected components of the graph @var{gr}.
1026 @c g: graph_union(cycle_graph(5), path_graph(4))$
1027 @c connected_components(g);
1030 (%i1) load ("graphs")$
1031 (%i2) g: graph_union(cycle_graph(5), path_graph(4))$
1032 (%i3) connected_components(g);
1033 (%o3) [[1, 2, 3, 4, 0], [8, 7, 6, 5]]
1036 @opencatbox{Categories:}
1037 @category{Package graphs}
1038 @category{Package graphs - properties}
1043 @deffn {Function} diameter (@var{gr})
1044 Returns the diameter of the graph @var{gr}.
1049 @c diameter(dodecahedron_graph());
1052 (%i1) load ("graphs")$
1053 (%i2) diameter(dodecahedron_graph());
1057 @opencatbox{Categories:}
1058 @category{Package graphs}
1059 @category{Package graphs - properties}
1063 @anchor{edge_coloring}
1064 @deffn {Function} edge_coloring (@var{gr})
1065 Returns an optimal coloring of the edges of the graph @var{gr}.
1067 The function returns the chromatic index and a list representing the
1068 coloring of the edges of @var{gr}.
1073 @c p : petersen_graph()$
1074 @c [ch_index, col] : edge_coloring(p);
1075 @c assoc([0,1], col);
1076 @c assoc([0,5], col);
1079 (%i1) load ("graphs")$
1080 (%i2) p : petersen_graph()$
1081 (%i3) [ch_index, col] : edge_coloring(p);
1082 (%o3) [4, [[[0, 5], 3], [[5, 7], 1], [[0, 1], 1], [[1, 6], 2],
1083 [[6, 8], 1], [[1, 2], 3], [[2, 7], 4], [[7, 9], 2], [[2, 3], 2],
1084 [[3, 8], 3], [[5, 8], 2], [[3, 4], 1], [[4, 9], 4], [[6, 9], 3],
1086 (%i4) assoc([0,1], col);
1088 (%i5) assoc([0,5], col);
1092 @opencatbox{Categories:}
1093 @category{Package graphs}
1094 @category{Package graphs - properties}
1098 @anchor{degree_sequence}
1099 @deffn {Function} degree_sequence (@var{gr})
1100 Returns the list of vertex degrees of the graph @var{gr}.
1105 @c degree_sequence(random_graph(10, 0.4));
1108 (%i1) load ("graphs")$
1109 (%i2) degree_sequence(random_graph(10, 0.4));
1110 (%o2) [2, 2, 2, 2, 2, 2, 3, 3, 3, 3]
1113 @opencatbox{Categories:}
1114 @category{Package graphs}
1115 @category{Package graphs - properties}
1119 @anchor{edge_connectivity}
1120 @deffn {Function} edge_connectivity (@var{gr})
1121 Returns the edge-connectivity of the graph @var{gr}.
1123 See also @mref{min_edge_cut}.
1125 @opencatbox{Categories:}
1126 @category{Package graphs}
1127 @category{Package graphs - properties}
1132 @deffn {Function} edges (@var{gr})
1133 Returns the list of edges (arcs) in a (directed) graph @var{gr}.
1138 @c edges(complete_graph(4));
1141 (%i1) load ("graphs")$
1142 (%i2) edges(complete_graph(4));
1143 (%o2) [[2, 3], [1, 3], [1, 2], [0, 3], [0, 2], [0, 1]]
1146 @opencatbox{Categories:}
1147 @category{Package graphs}
1148 @category{Package graphs - properties}
1152 @anchor{get_edge_weight}
1153 @deffn {Function} get_edge_weight @
1154 @fname{get_edge_weight} (@var{e}, @var{gr}) @
1155 @fname{get_edge_weight} (@var{e}, @var{gr}, @var{ifnot})
1157 Returns the weight of the edge @var{e} in the graph @var{gr}.
1159 If there is no weight assigned to the edge, the function returns 1. If
1160 the edge is not present in the graph, the function signals an error or
1161 returns the optional argument @var{ifnot}.
1166 @c c5 : cycle_graph(5)$
1167 @c get_edge_weight([1,2], c5);
1168 @c set_edge_weight([1,2], 2.0, c5);
1169 @c get_edge_weight([1,2], c5);
1172 (%i1) load ("graphs")$
1173 (%i2) c5 : cycle_graph(5)$
1174 (%i3) get_edge_weight([1,2], c5);
1176 (%i4) set_edge_weight([1,2], 2.0, c5);
1178 (%i5) get_edge_weight([1,2], c5);
1182 @opencatbox{Categories:}
1183 @category{Package graphs}
1184 @category{Package graphs - properties}
1188 @anchor{get_vertex_label}
1189 @deffn {Function} get_vertex_label (@var{v}, @var{gr})
1190 Returns the label of the vertex @var{v} in the graph @var{gr}.
1195 @c g : create_graph([[0,"Zero"], [1, "One"]], [[0,1]])$
1196 @c get_vertex_label(0, g);
1199 (%i1) load ("graphs")$
1200 (%i2) g : create_graph([[0,"Zero"], [1, "One"]], [[0,1]])$
1201 (%i3) get_vertex_label(0, g);
1205 @opencatbox{Categories:}
1206 @category{Package graphs}
1207 @category{Package graphs - properties}
1211 @anchor{graph_charpoly}
1212 @deffn {Function} graph_charpoly (@var{gr}, @var{x})
1213 Returns the characteristic polynomial (in variable @var{x}) of the graph
1219 @c p : petersen_graph()$
1220 @c graph_charpoly(p, x), factor;
1223 (%i1) load ("graphs")$
1224 (%i2) p : petersen_graph()$
1225 (%i3) graph_charpoly(p, x), factor;
1227 (%o3) (x - 3) (x - 1) (x + 2)
1230 @opencatbox{Categories:}
1231 @category{Package graphs}
1232 @category{Package graphs - properties}
1236 @anchor{graph_center}
1237 @deffn {Function} graph_center (@var{gr})
1238 Returns the center of the graph @var{gr}.
1243 @c g : grid_graph(5,5)$
1247 (%i1) load ("graphs")$
1248 (%i2) g : grid_graph(5,5)$
1249 (%i3) graph_center(g);
1253 @opencatbox{Categories:}
1254 @category{Package graphs}
1255 @category{Package graphs - properties}
1259 @anchor{graph_eigenvalues}
1260 @deffn {Function} graph_eigenvalues (@var{gr})
1261 Returns the eigenvalues of the graph @var{gr}. The function returns
1262 eigenvalues in the same format as maxima @mref{eigenvalues} function.
1267 @c p : petersen_graph()$
1268 @c graph_eigenvalues(p);
1271 (%i1) load ("graphs")$
1272 (%i2) p : petersen_graph()$
1273 (%i3) graph_eigenvalues(p);
1274 (%o3) [[3, - 2, 1], [1, 4, 5]]
1277 @opencatbox{Categories:}
1278 @category{Package graphs}
1279 @category{Package graphs - properties}
1283 @anchor{graph_periphery}
1284 @deffn {Function} graph_periphery (@var{gr})
1285 Returns the periphery of the graph @var{gr}.
1290 @c g : grid_graph(5,5)$
1291 @c graph_periphery(g);
1294 (%i1) load ("graphs")$
1295 (%i2) g : grid_graph(5,5)$
1296 (%i3) graph_periphery(g);
1297 (%o3) [24, 20, 4, 0]
1300 @opencatbox{Categories:}
1301 @category{Package graphs}
1302 @category{Package graphs - properties}
1307 @deffn {Function} graph_size (@var{gr})
1308 Returns the number of edges in the graph @var{gr}.
1313 @c p : petersen_graph()$
1317 (%i1) load ("graphs")$
1318 (%i2) p : petersen_graph()$
1319 (%i3) graph_size(p);
1323 @opencatbox{Categories:}
1324 @category{Package graphs}
1325 @category{Package graphs - properties}
1329 @anchor{graph_order}
1330 @deffn {Function} graph_order (@var{gr})
1331 Returns the number of vertices in the graph @var{gr}.
1336 @c p : petersen_graph()$
1340 (%i1) load ("graphs")$
1341 (%i2) p : petersen_graph()$
1342 (%i3) graph_order(p);
1346 @opencatbox{Categories:}
1347 @category{Package graphs}
1348 @category{Package graphs - properties}
1353 @deffn {Function} girth (@var{gr})
1354 Returns the length of the shortest cycle in @var{gr}.
1359 @c g : heawood_graph()$
1363 (%i1) load ("graphs")$
1364 (%i2) g : heawood_graph()$
1369 @opencatbox{Categories:}
1370 @category{Package graphs}
1371 @category{Package graphs - properties}
1375 @anchor{hamilton_cycle}
1376 @deffn {Function} hamilton_cycle (@var{gr})
1377 Returns the Hamilton cycle of the graph @var{gr} or an empty list if
1378 @var{gr} is not hamiltonian.
1383 @c c : cube_graph(3)$
1384 @c hc : hamilton_cycle(c);
1385 @c draw_graph(c, show_edges=vertices_to_cycle(hc))$
1388 (%i1) load ("graphs")$
1389 (%i2) c : cube_graph(3)$
1390 (%i3) hc : hamilton_cycle(c);
1391 (%o3) [7, 3, 2, 6, 4, 0, 1, 5, 7]
1392 (%i4) draw_graph(c, show_edges=vertices_to_cycle(hc))$
1395 @opencatbox{Categories:}
1396 @category{Package graphs}
1397 @category{Package graphs - properties}
1402 @image{figures/graphs03,6cm}
1405 @anchor{hamilton_path}
1406 @deffn {Function} hamilton_path (@var{gr})
1407 Returns the Hamilton path of the graph @var{gr} or an empty list if
1408 @var{gr} does not have a Hamilton path.
1413 @c p : petersen_graph()$
1414 @c hp : hamilton_path(p);
1415 @c draw_graph(p, show_edges=vertices_to_path(hp))$
1418 (%i1) load ("graphs")$
1419 (%i2) p : petersen_graph()$
1420 (%i3) hp : hamilton_path(p);
1421 (%o3) [0, 5, 7, 2, 1, 6, 8, 3, 4, 9]
1422 (%i4) draw_graph(p, show_edges=vertices_to_path(hp))$
1425 @opencatbox{Categories:}
1426 @category{Package graphs}
1427 @category{Package graphs - properties}
1432 @image{figures/graphs04,6cm}
1435 @anchor{isomorphism}
1436 @deffn {Function} isomorphism (@var{gr1}, @var{gr2})
1438 Returns a an isomorphism between graphs/digraphs @var{gr1} and
1439 @var{gr2}. If @var{gr1} and @var{gr2} are not isomorphic, it returns
1445 @c clk5:complement_graph(line_graph(complete_graph(5)))$
1446 @c isomorphism(clk5, petersen_graph());
1449 (%i1) load ("graphs")$
1450 (%i2) clk5:complement_graph(line_graph(complete_graph(5)))$
1451 (%i3) isomorphism(clk5, petersen_graph());
1452 (%o3) [9 -> 0, 2 -> 1, 6 -> 2, 5 -> 3, 0 -> 4, 1 -> 5, 3 -> 6,
1453 4 -> 7, 7 -> 8, 8 -> 9]
1456 @opencatbox{Categories:}
1457 @category{Package graphs}
1458 @category{Package graphs - properties}
1462 @anchor{in_neighbors}
1463 @deffn {Function} in_neighbors (@var{v}, @var{gr})
1464 Returns the list of in-neighbors of the vertex @var{v} in the directed
1470 @c p : path_digraph(3)$
1471 @c in_neighbors(2, p);
1472 @c out_neighbors(2, p);
1475 (%i1) load ("graphs")$
1476 (%i2) p : path_digraph(3)$
1477 (%i3) in_neighbors(2, p);
1479 (%i4) out_neighbors(2, p);
1483 @opencatbox{Categories:}
1484 @category{Package graphs}
1485 @category{Package graphs - properties}
1489 @anchor{is_biconnected}
1490 @deffn {Function} is_biconnected (@var{gr})
1491 Returns @code{true} if @var{gr} is 2-connected and @code{false} otherwise.
1496 @c is_biconnected(cycle_graph(5));
1497 @c is_biconnected(path_graph(5));
1500 (%i1) load ("graphs")$
1501 (%i2) is_biconnected(cycle_graph(5));
1503 (%i3) is_biconnected(path_graph(5));
1507 @opencatbox{Categories:}
1508 @category{Package graphs}
1509 @category{Package graphs - properties}
1513 @anchor{is_bipartite}
1514 @deffn {Function} is_bipartite (@var{gr})
1515 Returns @code{true} if @var{gr} is bipartite (2-colorable) and @code{false} otherwise.
1520 @c is_bipartite(petersen_graph());
1521 @c is_bipartite(heawood_graph());
1524 (%i1) load ("graphs")$
1525 (%i2) is_bipartite(petersen_graph());
1527 (%i3) is_bipartite(heawood_graph());
1531 @opencatbox{Categories:}
1532 @category{Package graphs}
1533 @category{Package graphs - properties}
1537 @anchor{is_connected}
1538 @deffn {Function} is_connected (@var{gr})
1539 Returns @code{true} if the graph @var{gr} is connected and @code{false} otherwise.
1544 @c is_connected(graph_union(cycle_graph(4), path_graph(3)));
1547 (%i1) load ("graphs")$
1548 (%i2) is_connected(graph_union(cycle_graph(4), path_graph(3)));
1552 @opencatbox{Categories:}
1553 @category{Package graphs}
1554 @category{Package graphs - properties}
1559 @deffn {Function} is_digraph (@var{gr})
1560 Returns @code{true} if @var{gr} is a directed graph and @code{false} otherwise.
1565 @c is_digraph(path_graph(5));
1566 @c is_digraph(path_digraph(5));
1569 (%i1) load ("graphs")$
1570 (%i2) is_digraph(path_graph(5));
1572 (%i3) is_digraph(path_digraph(5));
1576 @opencatbox{Categories:}
1577 @category{Package graphs}
1578 @category{Package graphs - properties}
1582 @anchor{is_edge_in_graph}
1583 @deffn {Function} is_edge_in_graph (@var{e}, @var{gr})
1584 Returns @code{true} if @var{e} is an edge (arc) in the (directed) graph @var{g}
1585 and @code{false} otherwise.
1590 @c c4 : cycle_graph(4)$
1591 @c is_edge_in_graph([2,3], c4);
1592 @c is_edge_in_graph([3,2], c4);
1593 @c is_edge_in_graph([2,4], c4);
1594 @c is_edge_in_graph([3,2], cycle_digraph(4));
1597 (%i1) load ("graphs")$
1598 (%i2) c4 : cycle_graph(4)$
1599 (%i3) is_edge_in_graph([2,3], c4);
1601 (%i4) is_edge_in_graph([3,2], c4);
1603 (%i5) is_edge_in_graph([2,4], c4);
1605 (%i6) is_edge_in_graph([3,2], cycle_digraph(4));
1609 @opencatbox{Categories:}
1610 @category{Package graphs}
1611 @category{Package graphs - properties}
1616 @deffn {Function} is_graph (@var{gr})
1617 Returns @code{true} if @var{gr} is a graph and @code{false} otherwise.
1622 @c is_graph(path_graph(5));
1623 @c is_graph(path_digraph(5));
1626 (%i1) load ("graphs")$
1627 (%i2) is_graph(path_graph(5));
1629 (%i3) is_graph(path_digraph(5));
1633 @opencatbox{Categories:}
1634 @category{Package graphs}
1635 @category{Package graphs - properties}
1639 @anchor{is_graph_or_digraph}
1640 @deffn {Function} is_graph_or_digraph (@var{gr})
1641 Returns @code{true} if @var{gr} is a graph or a directed graph and @code{false} otherwise.
1646 @c is_graph_or_digraph(path_graph(5));
1647 @c is_graph_or_digraph(path_digraph(5));
1650 (%i1) load ("graphs")$
1651 (%i2) is_graph_or_digraph(path_graph(5));
1653 (%i3) is_graph_or_digraph(path_digraph(5));
1657 @opencatbox{Categories:}
1658 @category{Package graphs}
1659 @category{Package graphs - properties}
1663 @anchor{is_isomorphic}
1664 @deffn {Function} is_isomorphic (@var{gr1}, @var{gr2})
1666 Returns @code{true} if graphs/digraphs @var{gr1} and @var{gr2} are isomorphic
1667 and @code{false} otherwise.
1669 See also @mrefdot{isomorphism}
1674 @c clk5:complement_graph(line_graph(complete_graph(5)))$
1675 @c is_isomorphic(clk5, petersen_graph());
1678 (%i1) load ("graphs")$
1679 (%i2) clk5:complement_graph(line_graph(complete_graph(5)))$
1680 (%i3) is_isomorphic(clk5, petersen_graph());
1684 @opencatbox{Categories:}
1685 @category{Package graphs}
1686 @category{Package graphs - properties}
1691 @deffn {Function} is_planar (@var{gr})
1693 Returns @code{true} if @var{gr} is a planar graph and @code{false} otherwise.
1695 The algorithm used is the Demoucron's algorithm, which is a quadratic time
1701 @c is_planar(dodecahedron_graph());
1702 @c is_planar(petersen_graph());
1703 @c is_planar(petersen_graph(10,2));
1706 (%i1) load ("graphs")$
1707 (%i2) is_planar(dodecahedron_graph());
1709 (%i3) is_planar(petersen_graph());
1711 (%i4) is_planar(petersen_graph(10,2));
1715 @opencatbox{Categories:}
1716 @category{Package graphs}
1717 @category{Package graphs - properties}
1721 @anchor{is_sconnected}
1722 @deffn {Function} is_sconnected (@var{gr})
1723 Returns @code{true} if the directed graph @var{gr} is strongly connected and
1724 @code{false} otherwise.
1729 @c is_sconnected(cycle_digraph(5));
1730 @c is_sconnected(path_digraph(5));
1733 (%i1) load ("graphs")$
1734 (%i2) is_sconnected(cycle_digraph(5));
1736 (%i3) is_sconnected(path_digraph(5));
1740 @opencatbox{Categories:}
1741 @category{Package graphs}
1742 @category{Package graphs - properties}
1746 @anchor{is_vertex_in_graph}
1747 @deffn {Function} is_vertex_in_graph (@var{v}, @var{gr})
1748 Returns @code{true} if @var{v} is a vertex in the graph @var{g} and @code{false} otherwise.
1753 @c c4 : cycle_graph(4)$
1754 @c is_vertex_in_graph(0, c4);
1755 @c is_vertex_in_graph(6, c4);
1758 (%i1) load ("graphs")$
1759 (%i2) c4 : cycle_graph(4)$
1760 (%i3) is_vertex_in_graph(0, c4);
1762 (%i4) is_vertex_in_graph(6, c4);
1766 @opencatbox{Categories:}
1767 @category{Package graphs}
1768 @category{Package graphs - properties}
1773 @deffn {Function} is_tree (@var{gr})
1774 Returns @code{true} if @var{gr} is a tree and @code{false} otherwise.
1779 @c is_tree(random_tree(4));
1780 @c is_tree(graph_union(random_tree(4), random_tree(5)));
1783 (%i1) load ("graphs")$
1784 (%i2) is_tree(random_tree(4));
1786 (%i3) is_tree(graph_union(random_tree(4), random_tree(5)));
1790 @opencatbox{Categories:}
1791 @category{Package graphs}
1792 @category{Package graphs - properties}
1796 @anchor{laplacian_matrix}
1797 @deffn {Function} laplacian_matrix (@var{gr})
1798 Returns the laplacian matrix of the graph @var{gr}.
1803 @c laplacian_matrix(cycle_graph(5));
1806 (%i1) load ("graphs")$
1807 (%i2) laplacian_matrix(cycle_graph(5));
1812 (%o2) [ 0 - 1 2 - 1 0 ]
1819 @opencatbox{Categories:}
1820 @category{Package graphs}
1821 @category{Package graphs - properties}
1826 @deffn {Function} max_clique (@var{gr})
1827 Returns a maximum clique of the graph @var{gr}.
1832 @c g : random_graph(100, 0.5)$
1836 (%i1) load ("graphs")$
1837 (%i2) g : random_graph(100, 0.5)$
1838 (%i3) max_clique(g);
1839 (%o3) [6, 12, 31, 36, 52, 59, 62, 63, 80]
1842 @opencatbox{Categories:}
1843 @category{Package graphs}
1844 @category{Package graphs - properties}
1849 @deffn {Function} max_degree (@var{gr})
1850 Returns the maximal degree of vertices of the graph @var{gr} and a
1851 vertex of maximal degree.
1856 @c g : random_graph(100, 0.02)$
1858 @c vertex_degree(95, g);
1861 (%i1) load ("graphs")$
1862 (%i2) g : random_graph(100, 0.02)$
1863 (%i3) max_degree(g);
1865 (%i4) vertex_degree(95, g);
1869 @opencatbox{Categories:}
1870 @category{Package graphs}
1871 @category{Package graphs - properties}
1876 @deffn {Function} max_flow (@var{net}, @var{s}, @var{t})
1877 Returns a maximum flow through the network @var{net} with the source
1878 @var{s} and the sink @var{t}.
1880 The function returns the value of the maximal flow and a list
1881 representing the weights of the arcs in the optimal flow.
1886 @c net : create_graph(
1897 @c [flow_value, flow] : max_flow(net, 1, 6);
1899 @c for u in out_neighbors(1, net)
1900 @c do fl : fl + assoc([1, u], flow)$
1904 (%i1) load ("graphs")$
1905 (%i2) net : create_graph(
1916 (%i3) [flow_value, flow] : max_flow(net, 1, 6);
1917 (%o3) [0.7, [[[1, 2], 0.5], [[1, 3], 0.2], [[2, 4], 0.2],
1918 [[2, 5], 0.3], [[3, 4], 0.1], [[3, 5], 0.1], [[4, 6], 0.3],
1921 (%i5) for u in out_neighbors(1, net)
1922 do fl : fl + assoc([1, u], flow)$
1927 @opencatbox{Categories:}
1928 @category{Package graphs}
1929 @category{Package graphs - properties}
1933 @anchor{max_independent_set}
1934 @deffn {Function} max_independent_set (@var{gr})
1935 Returns a maximum independent set of the graph @var{gr}.
1940 @c d : dodecahedron_graph()$
1941 @c mi : max_independent_set(d);
1942 @c draw_graph(d, show_vertices=mi)$
1945 (%i1) load ("graphs")$
1946 (%i2) d : dodecahedron_graph()$
1947 (%i3) mi : max_independent_set(d);
1948 (%o3) [0, 3, 5, 9, 10, 11, 18, 19]
1949 (%i4) draw_graph(d, show_vertices=mi)$
1952 @opencatbox{Categories:}
1953 @category{Package graphs}
1954 @category{Package graphs - properties}
1959 @image{figures/graphs05,6cm}
1962 @anchor{max_matching}
1963 @deffn {Function} max_matching (@var{gr})
1964 Returns a maximum matching of the graph @var{gr}.
1969 @c d : dodecahedron_graph()$
1970 @c m : max_matching(d);
1971 @c draw_graph(d, show_edges=m)$
1974 (%i1) load ("graphs")$
1975 (%i2) d : dodecahedron_graph()$
1976 (%i3) m : max_matching(d);
1977 (%o3) [[5, 7], [8, 9], [6, 10], [14, 19], [13, 18], [12, 17],
1978 [11, 16], [0, 15], [3, 4], [1, 2]]
1979 (%i4) draw_graph(d, show_edges=m)$
1982 @opencatbox{Categories:}
1983 @category{Package graphs}
1984 @category{Package graphs - properties}
1989 @image{figures/graphs06,6cm}
1993 @deffn {Function} min_degree (@var{gr})
1994 Returns the minimum degree of vertices of the graph @var{gr} and a
1995 vertex of minimum degree.
2000 @c g : random_graph(100, 0.1)$
2002 @c vertex_degree(21, g);
2005 (%i1) load ("graphs")$
2006 (%i2) g : random_graph(100, 0.1)$
2007 (%i3) min_degree(g);
2009 (%i4) vertex_degree(21, g);
2013 @opencatbox{Categories:}
2014 @category{Package graphs}
2015 @category{Package graphs - properties}
2019 @anchor{min_edge_cut}
2020 @deffn {Function} min_edge_cut (@var{gr})
2021 Returns the minimum edge cut in the graph @var{gr}.
2023 See also @mrefdot{edge_connectivity}
2025 @opencatbox{Categories:}
2026 @category{Package graphs}
2027 @category{Package graphs - properties}
2031 @anchor{min_vertex_cover}
2032 @deffn {Function} min_vertex_cover (@var{gr})
2033 Returns the minimum vertex cover of the graph @var{gr}.
2035 @opencatbox{Categories:}
2036 @category{Package graphs}
2037 @category{Package graphs - properties}
2041 @anchor{min_vertex_cut}
2042 @deffn {Function} min_vertex_cut (@var{gr})
2043 Returns the minimum vertex cut in the graph @var{gr}.
2045 See also @mrefdot{vertex_connectivity}
2047 @opencatbox{Categories:}
2048 @category{Package graphs}
2049 @category{Package graphs - properties}
2053 @anchor{minimum_spanning_tree}
2054 @deffn {Function} minimum_spanning_tree (@var{gr})
2055 Returns the minimum spanning tree of the graph @var{gr}.
2060 @c g : graph_product(path_graph(10), path_graph(10))$
2061 @c t : minimum_spanning_tree(g)$
2062 @c draw_graph(g, show_edges=edges(t))$
2065 (%i1) load ("graphs")$
2066 (%i2) g : graph_product(path_graph(10), path_graph(10))$
2067 (%i3) t : minimum_spanning_tree(g)$
2068 (%i4) draw_graph(g, show_edges=edges(t))$
2071 @opencatbox{Categories:}
2072 @category{Package graphs}
2073 @category{Package graphs - properties}
2078 @image{figures/graphs07,6cm}
2082 @deffn {Function} neighbors (@var{v}, @var{gr})
2083 Returns the list of neighbors of the vertex @var{v} in the graph @var{gr}.
2088 @c p : petersen_graph()$
2092 (%i1) load ("graphs")$
2093 (%i2) p : petersen_graph()$
2094 (%i3) neighbors(3, p);
2098 @opencatbox{Categories:}
2099 @category{Package graphs}
2100 @category{Package graphs - properties}
2105 @deffn {Function} odd_girth (@var{gr})
2106 Returns the length of the shortest odd cycle in the graph @var{gr}.
2111 @c g : graph_product(cycle_graph(4), cycle_graph(7))$
2116 (%i1) load ("graphs")$
2117 (%i2) g : graph_product(cycle_graph(4), cycle_graph(7))$
2124 @opencatbox{Categories:}
2125 @category{Package graphs}
2126 @category{Package graphs - properties}
2130 @anchor{out_neighbors}
2131 @deffn {Function} out_neighbors (@var{v}, @var{gr})
2132 Returns the list of out-neighbors of the vertex @var{v} in the directed
2138 @c p : path_digraph(3)$
2139 @c in_neighbors(2, p);
2140 @c out_neighbors(2, p);
2143 (%i1) load ("graphs")$
2144 (%i2) p : path_digraph(3)$
2145 (%i3) in_neighbors(2, p);
2147 (%i4) out_neighbors(2, p);
2151 @opencatbox{Categories:}
2152 @category{Package graphs}
2153 @category{Package graphs - properties}
2157 @anchor{planar_embedding}
2158 @deffn {Function} planar_embedding (@var{gr})
2160 Returns the list of facial walks in a planar embedding of @var{gr} and
2161 @code{false} if @var{gr} is not a planar graph.
2163 The graph @var{gr} must be biconnected.
2165 The algorithm used is the Demoucron's algorithm, which is a quadratic time
2171 @c planar_embedding(grid_graph(3,3));
2174 (%i1) load ("graphs")$
2175 (%i2) planar_embedding(grid_graph(3,3));
2176 (%o2) [[3, 6, 7, 8, 5, 2, 1, 0], [4, 3, 0, 1], [3, 4, 7, 6],
2177 [8, 7, 4, 5], [1, 2, 5, 4]]
2180 @opencatbox{Categories:}
2181 @category{Package graphs}
2182 @category{Package graphs - properties}
2186 @anchor{print_graph}
2187 @deffn {Function} print_graph (@var{gr})
2188 Prints some information about the graph @var{gr}.
2193 @c c5 : cycle_graph(5)$
2195 @c dc5 : cycle_digraph(5)$
2196 @c print_graph(dc5)$
2197 @c out_neighbors(0, dc5);
2200 (%i1) load ("graphs")$
2201 (%i2) c5 : cycle_graph(5)$
2202 (%i3) print_graph(c5)$
2203 Graph on 5 vertices with 5 edges.
2210 (%i4) dc5 : cycle_digraph(5)$
2211 (%i5) print_graph(dc5)$
2212 Digraph on 5 vertices with 5 arcs.
2219 (%i6) out_neighbors(0, dc5);
2223 @opencatbox{Categories:}
2224 @category{Package graphs}
2229 @deffn {Function} radius (@var{gr})
2230 Returns the radius of the graph @var{gr}.
2235 @c radius(dodecahedron_graph());
2238 (%i1) load ("graphs")$
2239 (%i2) radius(dodecahedron_graph());
2243 @opencatbox{Categories:}
2244 @category{Package graphs}
2245 @category{Package graphs - properties}
2249 @anchor{set_edge_weight}
2250 @deffn {Function} set_edge_weight (@var{e}, @var{w}, @var{gr})
2251 Assigns the weight @var{w} to the edge @var{e} in the graph @var{gr}.
2256 @c g : create_graph([1, 2], [[[1,2], 1.2]])$
2257 @c get_edge_weight([1,2], g);
2258 @c set_edge_weight([1,2], 2.1, g);
2259 @c get_edge_weight([1,2], g);
2262 (%i1) load ("graphs")$
2263 (%i2) g : create_graph([1, 2], [[[1,2], 1.2]])$
2264 (%i3) get_edge_weight([1,2], g);
2266 (%i4) set_edge_weight([1,2], 2.1, g);
2268 (%i5) get_edge_weight([1,2], g);
2272 @opencatbox{Categories:}
2273 @category{Package graphs}
2274 @category{Package graphs - properties}
2278 @anchor{set_vertex_label}
2279 @deffn {Function} set_vertex_label (@var{v}, @var{l}, @var{gr})
2280 Assigns the label @var{l} to the vertex @var{v} in the graph @var{gr}.
2285 @c g : create_graph([[1, "One"], [2, "Two"]], [[1,2]])$
2286 @c get_vertex_label(1, g);
2287 @c set_vertex_label(1, "oNE", g);
2288 @c get_vertex_label(1, g);
2291 (%i1) load ("graphs")$
2292 (%i2) g : create_graph([[1, "One"], [2, "Two"]], [[1,2]])$
2293 (%i3) get_vertex_label(1, g);
2295 (%i4) set_vertex_label(1, "oNE", g);
2297 (%i5) get_vertex_label(1, g);
2301 @opencatbox{Categories:}
2302 @category{Package graphs}
2303 @category{Package graphs - properties}
2307 @anchor{shortest_path}
2308 @deffn {Function} shortest_path (@var{u}, @var{v}, @var{gr})
2309 Returns the shortest path from @var{u} to @var{v} in the graph @var{gr}.
2314 @c d : dodecahedron_graph()$
2315 @c path : shortest_path(0, 7, d);
2316 @c draw_graph(d, show_edges=vertices_to_path(path))$
2319 (%i1) load ("graphs")$
2320 (%i2) d : dodecahedron_graph()$
2321 (%i3) path : shortest_path(0, 7, d);
2322 (%o3) [0, 1, 19, 13, 7]
2323 (%i4) draw_graph(d, show_edges=vertices_to_path(path))$
2326 @opencatbox{Categories:}
2327 @category{Package graphs}
2328 @category{Package graphs - properties}
2333 @image{figures/graphs08,6cm}
2336 @anchor{shortest_weighted_path}
2337 @deffn {Function} shortest_weighted_path (@var{u}, @var{v}, @var{gr})
2338 Returns the length of the shortest weighted path and the shortest
2339 weighted path from @var{u} to @var{v} in the graph @var{gr}.
2341 The length of a weighted path is the sum of edge weights of edges in the
2342 path. If an edge has no weight, then it has a default weight 1.
2348 @c g: petersen_graph(20, 2)$
2349 @c for e in edges(g) do set_edge_weight(e, random(1.0), g)$
2350 @c shortest_weighted_path(0, 10, g);
2353 (%i1) load ("graphs")$
2354 (%i2) g: petersen_graph(20, 2)$
2355 (%i3) for e in edges(g) do set_edge_weight(e, random(1.0), g)$
2356 (%i4) shortest_weighted_path(0, 10, g);
2357 (%o4) [2.575143920268482, [0, 20, 38, 36, 34, 32, 30, 10]]
2360 @opencatbox{Categories:}
2361 @category{Package graphs}
2362 @category{Package graphs - properties}
2366 @anchor{strong_components}
2367 @deffn {Function} strong_components (@var{gr})
2368 Returns the strong components of a directed graph @var{gr}.
2373 @c t : random_tournament(4)$
2374 @c strong_components(t);
2375 @c vertex_out_degree(3, t);
2378 (%i1) load ("graphs")$
2379 (%i2) t : random_tournament(4)$
2380 (%i3) strong_components(t);
2381 (%o3) [[1], [0], [2], [3]]
2382 (%i4) vertex_out_degree(3, t);
2386 @opencatbox{Categories:}
2387 @category{Package graphs}
2388 @category{Package graphs - properties}
2392 @anchor{topological_sort}
2393 @deffn {Function} topological_sort (@var{dag})
2395 Returns a topological sorting of the vertices of a directed graph
2396 @var{dag} or an empty list if @var{dag} is not a directed acyclic graph.
2404 @c [1,2], [2,5], [5,3],
2405 @c [5,4], [3,4], [1,3]
2408 @c topological_sort(g);
2411 (%i1) load ("graphs")$
2412 (%i2) g:create_graph(
2415 [1,2], [2,5], [5,3],
2419 (%i3) topological_sort(g);
2420 (%o3) [1, 2, 5, 3, 4]
2423 @opencatbox{Categories:}
2424 @category{Package graphs}
2425 @category{Package graphs - properties}
2429 @anchor{vertex_connectivity}
2430 @deffn {Function} vertex_connectivity (@var{g})
2431 Returns the vertex connectivity of the graph @var{g}.
2433 See also @mrefdot{min_vertex_cut}
2435 @opencatbox{Categories:}
2436 @category{Package graphs}
2437 @category{Package graphs - properties}
2441 @anchor{vertex_degree}
2442 @deffn {Function} vertex_degree (@var{v}, @var{gr})
2443 Returns the degree of the vertex @var{v} in the graph @var{gr}.
2445 @opencatbox{Categories:}
2446 @category{Package graphs}
2447 @category{Package graphs - properties}
2451 @anchor{vertex_distance}
2452 @deffn {Function} vertex_distance (@var{u}, @var{v}, @var{gr})
2453 Returns the length of the shortest path between @var{u} and @var{v} in
2454 the (directed) graph @var{gr}.
2459 @c d : dodecahedron_graph()$
2460 @c vertex_distance(0, 7, d);
2461 @c shortest_path(0, 7, d);
2464 (%i1) load ("graphs")$
2465 (%i2) d : dodecahedron_graph()$
2466 (%i3) vertex_distance(0, 7, d);
2468 (%i4) shortest_path(0, 7, d);
2469 (%o4) [0, 1, 19, 13, 7]
2472 @opencatbox{Categories:}
2473 @category{Package graphs}
2474 @category{Package graphs - properties}
2478 @anchor{vertex_eccentricity}
2479 @deffn {Function} vertex_eccentricity (@var{v}, @var{gr})
2481 Returns the eccentricity of the vertex @var{v} in the graph @var{gr}.
2486 @c g:cycle_graph(7)$
2487 @c vertex_eccentricity(0, g);
2490 (%i1) load ("graphs")$
2491 (%i2) g:cycle_graph(7)$
2492 (%i3) vertex_eccentricity(0, g);
2496 @opencatbox{Categories:}
2497 @category{Package graphs}
2498 @category{Package graphs - properties}
2502 @anchor{vertex_in_degree}
2503 @deffn {Function} vertex_in_degree (@var{v}, @var{gr})
2504 Returns the in-degree of the vertex @var{v} in the directed graph @var{gr}.
2509 @c p5 : path_digraph(5)$
2511 @c vertex_in_degree(4, p5);
2512 @c in_neighbors(4, p5);
2515 (%i1) load ("graphs")$
2516 (%i2) p5 : path_digraph(5)$
2517 (%i3) print_graph(p5)$
2518 Digraph on 5 vertices with 4 arcs.
2525 (%i4) vertex_in_degree(4, p5);
2527 (%i5) in_neighbors(4, p5);
2531 @opencatbox{Categories:}
2532 @category{Package graphs}
2533 @category{Package graphs - properties}
2537 @anchor{vertex_out_degree}
2538 @deffn {Function} vertex_out_degree (@var{v}, @var{gr})
2539 Returns the out-degree of the vertex @var{v} in the directed graph @var{gr}.
2544 @c t : random_tournament(10)$
2545 @c vertex_out_degree(0, t);
2546 @c out_neighbors(0, t);
2549 (%i1) load ("graphs")$
2550 (%i2) t : random_tournament(10)$
2551 (%i3) vertex_out_degree(0, t);
2553 (%i4) out_neighbors(0, t);
2557 @opencatbox{Categories:}
2558 @category{Package graphs}
2559 @category{Package graphs - properties}
2564 @deffn {Function} vertices (@var{gr})
2565 Returns the list of vertices in the graph @var{gr}.
2570 @c vertices(complete_graph(4));
2573 (%i1) load ("graphs")$
2574 (%i2) vertices(complete_graph(4));
2578 @opencatbox{Categories:}
2579 @category{Package graphs}
2580 @category{Package graphs - properties}
2584 @anchor{vertex_coloring}
2585 @deffn {Function} vertex_coloring (@var{gr})
2586 Returns an optimal coloring of the vertices of the graph @var{gr}.
2588 The function returns the chromatic number and a list representing the
2589 coloring of the vertices of @var{gr}.
2594 @c p:petersen_graph()$
2595 @c vertex_coloring(p);
2598 (%i1) load ("graphs")$
2599 (%i2) p:petersen_graph()$
2600 (%i3) vertex_coloring(p);
2601 (%o3) [3, [[0, 2], [1, 3], [2, 2], [3, 3], [4, 1], [5, 3],
2602 [6, 1], [7, 1], [8, 2], [9, 2]]]
2605 @opencatbox{Categories:}
2606 @category{Package graphs}
2607 @category{Package graphs - properties}
2611 @anchor{wiener_index}
2612 @deffn {Function} wiener_index (@var{gr})
2613 Returns the Wiener index of the graph @var{gr}.
2618 @c wiener_index(dodecahedron_graph());
2621 (%i2) wiener_index(dodecahedron_graph());
2625 @opencatbox{Categories:}
2626 @category{Package graphs}
2627 @category{Package graphs - properties}
2631 @subsection Modifying graphs
2634 @deffn {Function} add_edge (@var{e}, @var{gr})
2635 Adds the edge @var{e} to the graph @var{gr}.
2640 @c p : path_graph(4)$
2642 @c add_edge([0,3], p);
2646 (%i1) load ("graphs")$
2647 (%i2) p : path_graph(4)$
2648 (%i3) neighbors(0, p);
2650 (%i4) add_edge([0,3], p);
2652 (%i5) neighbors(0, p);
2656 @opencatbox{Categories:}
2657 @category{Package graphs}
2658 @category{Package graphs - modifications}
2663 @deffn {Function} add_edges (@var{e_list}, @var{gr})
2664 Adds all edges in the list @var{e_list} to the graph @var{gr}.
2669 @c g : empty_graph(3)$
2670 @c add_edges([[0,1],[1,2]], g)$
2674 (%i1) load ("graphs")$
2675 (%i2) g : empty_graph(3)$
2676 (%i3) add_edges([[0,1],[1,2]], g)$
2677 (%i4) print_graph(g)$
2678 Graph on 3 vertices with 2 edges.
2685 @opencatbox{Categories:}
2686 @category{Package graphs}
2687 @category{Package graphs - modifications}
2692 @deffn {Function} add_vertex (@var{v}, @var{gr})
2693 Adds the vertex @var{v} to the graph @var{gr}.
2698 @c g : path_graph(2)$
2699 @c add_vertex(2, g)$
2703 (%i1) load ("graphs")$
2704 (%i2) g : path_graph(2)$
2705 (%i3) add_vertex(2, g)$
2706 (%i4) print_graph(g)$
2707 Graph on 3 vertices with 1 edges.
2714 @opencatbox{Categories:}
2715 @category{Package graphs}
2716 @category{Package graphs - modifications}
2720 @anchor{add_vertices}
2721 @deffn {Function} add_vertices (@var{v_list}, @var{gr})
2722 Adds all vertices in the list @var{v_list} to the graph @var{gr}.
2724 @opencatbox{Categories:}
2725 @category{Package graphs}
2726 @category{Package graphs - modifications}
2730 @anchor{connect_vertices}
2731 @deffn {Function} connect_vertices (@var{v_list}, @var{u_list}, @var{gr})
2732 Connects all vertices from the list @var{v_list} with the vertices in
2733 the list @var{u_list} in the graph @var{gr}.
2735 @var{v_list} and @var{u_list} can be single vertices or lists of
2741 @c g : empty_graph(4)$
2742 @c connect_vertices(0, [1,2,3], g)$
2746 (%i1) load ("graphs")$
2747 (%i2) g : empty_graph(4)$
2748 (%i3) connect_vertices(0, [1,2,3], g)$
2749 (%i4) print_graph(g)$
2750 Graph on 4 vertices with 3 edges.
2758 @opencatbox{Categories:}
2759 @category{Package graphs}
2760 @category{Package graphs - modifications}
2764 @anchor{contract_edge}
2765 @deffn {Function} contract_edge (@var{e}, @var{gr})
2766 Contracts the edge @var{e} in the graph @var{gr}.
2772 @c 8, [[0,3],[1,3],[2,3],[3,4],[4,5],[4,6],[4,7]])$
2774 @c contract_edge([3,4], g)$
2778 (%i1) load ("graphs")$
2779 (%i2) g: create_graph(
2780 8, [[0,3],[1,3],[2,3],[3,4],[4,5],[4,6],[4,7]])$
2781 (%i3) print_graph(g)$
2782 Graph on 8 vertices with 7 edges.
2792 (%i4) contract_edge([3,4], g)$
2793 (%i5) print_graph(g)$
2794 Graph on 7 vertices with 6 edges.
2805 @opencatbox{Categories:}
2806 @category{Package graphs}
2807 @category{Package graphs - modifications}
2811 @anchor{remove_edge}
2812 @deffn {Function} remove_edge (@var{e}, @var{gr})
2813 Removes the edge @var{e} from the graph @var{gr}.
2818 @c c3 : cycle_graph(3)$
2819 @c remove_edge([0,1], c3)$
2823 (%i1) load ("graphs")$
2824 (%i2) c3 : cycle_graph(3)$
2825 (%i3) remove_edge([0,1], c3)$
2826 (%i4) print_graph(c3)$
2827 Graph on 3 vertices with 2 edges.
2834 @opencatbox{Categories:}
2835 @category{Package graphs}
2836 @category{Package graphs - modifications}
2840 @anchor{remove_vertex}
2841 @deffn {Function} remove_vertex (@var{v}, @var{gr})
2842 Removes the vertex @var{v} from the graph @var{gr}.
2844 @opencatbox{Categories:}
2845 @category{Package graphs}
2849 @subsection Reading and writing to files
2851 @anchor{dimacs_export}
2852 @deffn {Function} dimacs_export @
2853 @fname{dimacs_export} (@var{gr}, @var{fl}) @
2854 @fname{dimacs_export} (@var{gr}, @var{fl}, @var{comment1}, ..., @var{commentn})
2856 Exports the graph into the file @var{fl} in the DIMACS format. Optional
2857 comments will be added to the top of the file.
2859 @opencatbox{Categories:}
2860 @category{Package graphs}
2861 @category{Package graphs - io}
2865 @anchor{dimacs_import}
2866 @deffn {Function} dimacs_import (@var{fl})
2868 Returns the graph from file @var{fl} in the DIMACS format.
2870 @opencatbox{Categories:}
2871 @category{Package graphs}
2872 @category{Package graphs - io}
2876 @anchor{graph6_decode}
2877 @deffn {Function} graph6_decode (@var{str})
2879 Returns the graph encoded in the graph6 format in the string @var{str}.
2881 @opencatbox{Categories:}
2882 @category{Package graphs}
2883 @category{Package graphs - io}
2887 @anchor{graph6_encode}
2888 @deffn {Function} graph6_encode (@var{gr})
2890 Returns a string which encodes the graph @var{gr} in the graph6 format.
2892 @opencatbox{Categories:}
2893 @category{Package graphs}
2894 @category{Package graphs - io}
2898 @anchor{graph6_export}
2899 @deffn {Function} graph6_export (@var{gr_list}, @var{fl})
2901 Exports graphs in the list @var{gr_list} to the file @var{fl} in the
2904 @opencatbox{Categories:}
2905 @category{Package graphs}
2906 @category{Package graphs - io}
2910 @anchor{graph6_import}
2911 @deffn {Function} graph6_import (@var{fl})
2913 Returns a list of graphs from the file @var{fl} in the graph6 format.
2915 @opencatbox{Categories:}
2916 @category{Package graphs}
2917 @category{Package graphs - io}
2921 @anchor{sparse6_decode}
2922 @deffn {Function} sparse6_decode (@var{str})
2924 Returns the graph encoded in the sparse6 format in the string @var{str}.
2926 @opencatbox{Categories:}
2927 @category{Package graphs}
2928 @category{Package graphs - io}
2932 @anchor{sparse6_encode}
2933 @deffn {Function} sparse6_encode (@var{gr})
2935 Returns a string which encodes the graph @var{gr} in the sparse6 format.
2937 @opencatbox{Categories:}
2938 @category{Package graphs}
2939 @category{Package graphs - io}
2943 @anchor{sparse6_export}
2944 @deffn {Function} sparse6_export (@var{gr_list}, @var{fl})
2946 Exports graphs in the list @var{gr_list} to the file @var{fl} in the
2949 @opencatbox{Categories:}
2950 @category{Package graphs}
2951 @category{Package graphs - io}
2955 @anchor{sparse6_import}
2956 @deffn {Function} sparse6_import (@var{fl})
2958 Returns a list of graphs from the file @var{fl} in the sparse6 format.
2960 @opencatbox{Categories:}
2961 @category{Package graphs}
2962 @category{Package graphs - io}
2966 @subsection Visualization
2969 @deffn {Function} draw_graph @
2970 @fname{draw_graph} (@var{graph}) @
2971 @fname{draw_graph} (@var{graph}, @var{option1}, ..., @var{optionk})
2973 Draws the graph using the @ref{draw-pkg} package.
2975 The algorithm used to position vertices is specified by the optional
2976 argument @var{program}. The default value is
2977 @code{program=spring_embedding}. @var{draw_graph} can also use the
2978 graphviz programs for positioning vertices, but graphviz must be
2979 installed separately.
2985 @c g:grid_graph(10,10)$
2986 @c m:max_matching(g)$
2988 @c spring_embedding_depth=100,
2989 @c show_edges=m, edge_type=dots,
2993 (%i1) load ("graphs")$
2994 (%i2) g:grid_graph(10,10)$
2995 (%i3) m:max_matching(g)$
2997 spring_embedding_depth=100,
2998 show_edges=m, edge_type=dots,
3003 @image{figures/graphs09,6cm}
3010 @c g:create_graph(16,
3012 @c [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3013 @c [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3014 @c [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3015 @c [10,14],[15,14],[13,14]
3017 @c t:minimum_spanning_tree(g)$
3020 @c show_edges=edges(t),
3021 @c show_edge_width=4,
3022 @c show_edge_color=green,
3023 @c vertex_type=filled_square,
3028 (%i1) load ("graphs")$
3029 (%i2) g:create_graph(16,
3031 [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3032 [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3033 [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3034 [10,14],[15,14],[13,14]
3036 (%i3) t:minimum_spanning_tree(g)$
3039 show_edges=edges(t),
3041 show_edge_color=green,
3042 vertex_type=filled_square,
3048 @image{figures/graphs10,6cm}
3055 @c g:create_graph(16,
3057 @c [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3058 @c [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3059 @c [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3060 @c [10,14],[15,14],[13,14]
3062 @c mi : max_independent_set(g)$
3065 @c show_vertices=mi,
3066 @c show_vertex_type=filled_up_triangle,
3067 @c show_vertex_size=2,
3075 (%i1) load ("graphs")$
3076 (%i2) g:create_graph(16,
3078 [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3079 [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3080 [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3081 [10,14],[15,14],[13,14]
3083 (%i3) mi : max_independent_set(g)$
3087 show_vertex_type=filled_up_triangle,
3097 @image{figures/graphs11,6cm}
3104 @c net : create_graph(
3107 @c [[0,1], 3], [[0,2], 2],
3108 @c [[1,3], 1], [[1,4], 3],
3109 @c [[2,3], 2], [[2,4], 2],
3110 @c [[4,5], 2], [[3,5], 2]
3116 @c show_weight=true,
3118 @c show_vertices=[0,5],
3119 @c show_vertex_type=filled_square,
3122 @c edge_color="dark-green",
3127 (%i1) load ("graphs")$
3128 (%i2) net : create_graph(
3131 [[0,1], 3], [[0,2], 2],
3132 [[1,3], 1], [[1,4], 3],
3133 [[2,3], 2], [[2,4], 2],
3134 [[4,5], 2], [[3,5], 2]
3142 show_vertices=[0,5],
3143 show_vertex_type=filled_square,
3146 edge_color="dark-green",
3152 @image{figures/graphs12,6cm}
3159 @c g: petersen_graph(20, 2);
3160 @c draw_graph(g, redraw=true, program=planar_embedding);
3163 (%i1) load("graphs")$
3164 (%i2) g: petersen_graph(20, 2);
3166 (%i3) draw_graph(g, redraw=true, program=planar_embedding);
3171 @image{figures/graphs14,6cm}
3178 @c t: tutte_graph();
3179 @c draw_graph(t, redraw=true,
3180 @c fixed_vertices=[1,2,3,4,5,6,7,8,9]);
3183 (%i1) load("graphs")$
3184 (%i2) t: tutte_graph();
3186 (%i3) draw_graph(t, redraw=true,
3187 fixed_vertices=[1,2,3,4,5,6,7,8,9]);
3192 @image{figures/graphs15,6cm}
3195 @opencatbox{Categories:}
3196 @category{Package graphs}
3200 @anchor{draw_graph_program}
3201 @defvr {Option variable} draw_graph_program
3202 Default value: @var{spring_embedding}
3204 The default value for the program used to position vertices in
3205 @code{draw_graph} program.
3207 @opencatbox{Categories:}
3208 @category{Package graphs}
3209 @category{Package graphs - draw_graphs options}
3214 @defvr {draw_graph option} show_id
3215 Default value: @var{false}
3217 If @var{true} then ids of the vertices are displayed.
3219 @opencatbox{Categories:}
3220 @category{Package graphs}
3221 @category{Package graphs - draw_graphs options}
3226 @defvr {draw_graph option} show_label
3227 Default value: @var{false}
3229 If @var{true} then labels of the vertices are displayed.
3231 @opencatbox{Categories:}
3232 @category{Package graphs}
3233 @category{Package graphs - draw_graphs options}
3237 @anchor{label_alignment_graphs}
3238 @defvr {draw_graph option} label_alignment
3239 Default value: @var{center}
3241 Determines how to align the labels/ids of the vertices. Can
3242 be @code{left}, @code{center} or @code{right}.
3244 @opencatbox{Categories:}
3245 @category{Package graphs}
3246 @category{Package graphs - draw_graphs options}
3250 @anchor{show_weight }
3251 @defvr {draw_graph option} show_weight
3252 Default value: @var{false}
3254 If @var{true} then weights of the edges are displayed.
3256 @opencatbox{Categories:}
3257 @category{Package graphs}
3258 @category{Package graphs - draw_graphs options}
3262 @anchor{vertex_type}
3263 @defvr {draw_graph option} vertex_type
3264 Default value: @var{circle}
3266 Defines how vertices are displayed. See the @var{point_type} option for
3267 the @code{draw} package for possible values.
3269 @opencatbox{Categories:}
3270 @category{Package graphs}
3271 @category{Package graphs - draw_graphs options}
3275 @anchor{vertex_size}
3276 @defvr {draw_graph option} vertex_size
3277 The size of vertices.
3279 @opencatbox{Categories:}
3280 @category{Package graphs}
3281 @category{Package graphs - draw_graphs options}
3285 @anchor{vertex_color }
3286 @defvr {draw_graph option} vertex_color
3287 The color used for displaying vertices.
3289 @opencatbox{Categories:}
3290 @category{Package graphs}
3291 @category{Package graphs - draw_graphs options}
3295 @anchor{show_vertices}
3296 @defvr {draw_graph option} show_vertices
3299 Display selected vertices in the using a different color.
3301 @opencatbox{Categories:}
3302 @category{Package graphs}
3303 @category{Package graphs - draw_graphs options}
3307 @anchor{show_vertex_type}
3308 @defvr {draw_graph option} show_vertex_type
3309 Defines how vertices specified in @var{show_vertices} are displayed.
3310 See the @var{point_type} option for the @code{draw} package for possible
3313 @opencatbox{Categories:}
3314 @category{Package graphs}
3315 @category{Package graphs - draw_graphs options}
3319 @anchor{show_vertex_size}
3320 @defvr {draw_graph option} show_vertex_size
3321 The size of vertices in @var{show_vertices}.
3323 @opencatbox{Categories:}
3324 @category{Package graphs}
3325 @category{Package graphs - draw_graphs options}
3329 @anchor{show_vertex_color }
3330 @defvr {draw_graph option} show_vertex_color
3331 The color used for displaying vertices in the @var{show_vertices} list.
3333 @opencatbox{Categories:}
3334 @category{Package graphs}
3335 @category{Package graphs - draw_graphs options}
3339 @anchor{vertex_partition}
3340 @defvr {draw_graph option} vertex_partition
3343 A partition @code{[[v1,v2,...],...,[vk,...,vn]]} of the vertices of the
3344 graph. The vertices of each list in the partition will be drawn in a
3347 @opencatbox{Categories:}
3348 @category{Package graphs}
3349 @category{Package graphs - draw_graphs options}
3353 @anchor{vertex_coloring_variable}
3354 @defvr {draw_graph option} vertex_coloring
3355 Specifies coloring of the vertices. The coloring @var{col} must be
3356 specified in the format as returned by @var{vertex_coloring}.
3358 @opencatbox{Categories:}
3359 @category{Package graphs}
3360 @category{Package graphs - draw_graphs options}
3364 @anchor{edge_color }
3365 @defvr {draw_graph option} edge_color
3366 The color used for displaying edges.
3368 @opencatbox{Categories:}
3369 @category{Package graphs}
3370 @category{Package graphs - draw_graphs options}
3375 @defvr {draw_graph option} edge_width
3378 @opencatbox{Categories:}
3379 @category{Package graphs}
3380 @category{Package graphs - draw_graphs options}
3385 @defvr {draw_graph option} edge_type
3386 Defines how edges are displayed. See the @var{line_type} option for the
3387 @code{draw} package.
3389 @opencatbox{Categories:}
3390 @category{Package graphs}
3391 @category{Package graphs - draw_graphs options}
3396 @defvr {draw_graph option} show_edges
3397 Display edges specified in the list @var{e_list} using a different
3400 @opencatbox{Categories:}
3401 @category{Package graphs}
3402 @category{Package graphs - draw_graphs options}
3406 @anchor{show_edge_color}
3407 @defvr {draw_graph option} show_edge_color
3408 The color used for displaying edges in the @var{show_edges} list.
3410 @opencatbox{Categories:}
3411 @category{Package graphs}
3412 @category{Package graphs - draw_graphs options}
3416 @anchor{show_edge_width}
3417 @defvr {draw_graph option} show_edge_width
3418 The width of edges in @var{show_edges}.
3420 @opencatbox{Categories:}
3421 @category{Package graphs}
3422 @category{Package graphs - draw_graphs options}
3426 @anchor{show_edge_type}
3427 @defvr {draw_graph option} show_edge_type
3428 Defines how edges in @var{show_edges} are displayed. See the
3429 @var{line_type} option for the @code{draw} package.
3431 @opencatbox{Categories:}
3432 @category{Package graphs}
3433 @category{Package graphs - draw_graphs options}
3437 @anchor{edge_partition}
3438 @defvr {draw_graph option} edge_partition
3439 A partition @code{[[e1,e2,...],...,[ek,...,em]]} of edges of the
3440 graph. The edges of each list in the partition will be drawn using a
3443 @opencatbox{Categories:}
3444 @category{Package graphs}
3445 @category{Package graphs - draw_graphs options}
3449 @anchor{edge_coloring_variable}
3450 @defvr {draw_graph option} edge_coloring
3451 The coloring of edges. The coloring must be specified in the
3452 format as returned by the function @var{edge_coloring}.
3454 @opencatbox{Categories:}
3455 @category{Package graphs}
3456 @category{Package graphs - draw_graphs options}
3461 @defvr {draw_graph option} redraw
3462 Default value: @var{false}
3464 If @code{true}, vertex positions are recomputed even if the positions
3465 have been saved from a previous drawing of the graph.
3467 @opencatbox{Categories:}
3468 @category{Package graphs}
3469 @category{Package graphs - draw_graphs options}
3473 @anchor{head_angle_graphs}
3474 @defvr {draw_graph option} head_angle
3477 The angle for the arrows displayed on arcs (in directed graphs).
3479 @opencatbox{Categories:}
3480 @category{Package graphs}
3481 @category{Package graphs - draw_graphs options}
3485 @anchor{head_length_graphs}
3486 @defvr {draw_graph option} head_length
3489 The length for the arrows displayed on arcs (in directed graphs).
3491 @opencatbox{Categories:}
3492 @category{Package graphs}
3493 @category{Package graphs - draw_graphs options}
3497 @anchor{spring_embedding_depth}
3498 @defvr {draw_graph option} spring_embedding_depth
3501 The number of iterations in the spring embedding graph drawing
3504 @opencatbox{Categories:}
3505 @category{Package graphs}
3506 @category{Package graphs - draw_graphs options}
3510 @anchor{terminal_graphs}
3511 @defvr {draw_graph option} terminal
3512 The terminal used for drawing (see the @var{terminal} option in the
3513 @code{draw} package).
3515 @opencatbox{Categories:}
3516 @category{Package graphs}
3517 @category{Package graphs - draw_graphs options}
3521 @anchor{file_name_graphs}
3522 @defvr {draw_graph option} file_name
3523 The filename of the drawing if terminal is not screen.
3525 @opencatbox{Categories:}
3526 @category{Package graphs}
3527 @category{Package graphs - draw_graphs options}
3532 @defvr {draw_graph option} program
3533 Defines the program used for positioning vertices of the graph. Can be
3534 one of the graphviz programs (dot, neato, twopi, circ, fdp),
3535 @var{circular}, @var{spring_embedding} or
3536 @var{planar_embedding}. @var{planar_embedding} is only available for
3537 2-connected planar graphs. When @code{program=spring_embedding}, a set
3538 of vertices with fixed position can be specified with the
3539 @var{fixed_vertices} option.
3541 @opencatbox{Categories:}
3542 @category{Package graphs}
3543 @category{Package graphs - draw_graphs options}
3547 @anchor{fixed_vertices}
3548 @defvr {draw_graph option} fixed_vertices
3549 Specifies a list of vertices which will have positions fixed along a regular polygon.
3550 Can be used when @code{program=spring_embedding}.
3552 @opencatbox{Categories:}
3553 @category{Package graphs}
3554 @category{Package graphs - draw_graphs options}
3558 @anchor{vertices_to_path}
3559 @deffn {Function} vertices_to_path (@var{v_list})
3560 Converts a list @var{v_list} of vertices to a list of edges of the path
3561 defined by @var{v_list}.
3563 @opencatbox{Categories:}
3564 @category{Package graphs}
3568 @anchor{vertices_to_cycle}
3569 @deffn {Function} vertices_to_cycle (@var{v_list})
3570 Converts a list @var{v_list} of vertices to a list of edges of the cycle
3571 defined by @var{v_list}.
3573 @opencatbox{Categories:}
3574 @category{Package graphs}