2 * Introduction to graphs::
3 * Functions and Variables for graphs::
6 @node Introduction to graphs, Functions and Variables for graphs, Package graphs, Package graphs
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, Package graphs
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]]}.
48 A vertex may be any integer,
49 and @var{v_list} may contain vertices in any order.
50 A label may be any Maxima expression,
51 and two or more vertices may have the same label.
53 @var{n} is the number of vertices. Vertices will be identified by integers from 0 to n-1.
55 @var{e_list} is a list of edges @code{[e1, e2,..., em]} or a list of
56 edges together with edge-weights @code{[[e1, w1], ..., [em, wm]]}.
58 If @var{directed} is not @code{false}, a directed graph will be returned.
60 Example 1: create a cycle on 3 vertices:
63 @c g : create_graph([1,2,3], [[1,2], [2,3], [1,3]])$
67 (%i1) load ("graphs")$
68 (%i2) g : create_graph([1,2,3], [[1,2], [2,3], [1,3]])$
70 Graph on 3 vertices with 3 edges.
77 Example 2: create a cycle on 3 vertices with edge weights:
80 @c g : create_graph([1,2,3], [[[1,2], 1.0], [[2,3], 2.0],
85 (%i1) load ("graphs")$
86 (%i2) g : create_graph([1,2,3], [[[1,2], 1.0], [[2,3], 2.0],
89 Graph on 3 vertices with 3 edges.
96 Example 3: create a directed graph:
105 @c 'directed = true)$
109 (%i1) load ("graphs")$
110 (%i2) d : create_graph(
117 (%i3) print_graph(d)$
118 Digraph on 4 vertices with 4 arcs.
126 @opencatbox{Categories:}
127 @category{Package graphs}
132 @deffn {Function} copy_graph (@var{g})
133 Returns a copy of the graph @var{g}.
135 @opencatbox{Categories:}
136 @category{Package graphs}
137 @category{Package graphs - constructions}
141 @anchor{circulant_graph}
142 @deffn {Function} circulant_graph (@var{n}, @var{d})
143 Returns the circulant graph with parameters @var{n} and @var{d}.
148 @c g : circulant_graph(10, [1,3])$
152 (%i1) load ("graphs")$
153 (%i2) g : circulant_graph(10, [1,3])$
154 (%i3) print_graph(g)$
155 Graph on 10 vertices with 20 edges.
169 @opencatbox{Categories:}
170 @category{Package graphs}
171 @category{Package graphs - constructions}
175 @anchor{clebsch_graph}
176 @deffn {Function} clebsch_graph ()
177 Returns the Clebsch graph.
179 @opencatbox{Categories:}
180 @category{Package graphs}
181 @category{Package graphs - constructions}
185 @anchor{complement_graph}
186 @deffn {Function} complement_graph (@var{g})
187 Returns the complement of the graph @var{g}.
189 @opencatbox{Categories:}
190 @category{Package graphs}
191 @category{Package graphs - constructions}
195 @anchor{complete_bipartite_graph}
196 @deffn {Function} complete_bipartite_graph (@var{n}, @var{m})
197 Returns the complete bipartite graph on @var{n+m} vertices.
199 @opencatbox{Categories:}
200 @category{Package graphs}
201 @category{Package graphs - constructions}
205 @anchor{complete_graph}
206 @deffn {Function} complete_graph (@var{n})
207 Returns the complete graph on @var{n} vertices.
209 @opencatbox{Categories:}
210 @category{Package graphs}
211 @category{Package graphs - constructions}
215 @anchor{cycle_digraph}
216 @deffn {Function} cycle_digraph (@var{n})
217 Returns the directed cycle on @var{n} vertices.
219 @opencatbox{Categories:}
220 @category{Package graphs}
221 @category{Package graphs - constructions}
226 @deffn {Function} cycle_graph (@var{n})
227 Returns the cycle on @var{n} vertices.
229 @opencatbox{Categories:}
230 @category{Package graphs}
231 @category{Package graphs - constructions}
235 @anchor{cuboctahedron_graph}
236 @deffn {Function} cuboctahedron_graph (@var{n})
237 Returns the cuboctahedron graph.
239 @opencatbox{Categories:}
240 @category{Package graphs}
241 @category{Package graphs - constructions}
246 @deffn {Function} cube_graph (@var{n})
247 Returns the @var{n}-dimensional cube.
249 @opencatbox{Categories:}
250 @category{Package graphs}
251 @category{Package graphs - constructions}
255 @anchor{dodecahedron_graph}
256 @deffn {Function} dodecahedron_graph ()
257 Returns the dodecahedron graph.
259 @opencatbox{Categories:}
260 @category{Package graphs}
261 @category{Package graphs - constructions}
266 @deffn {Function} empty_graph (@var{n})
267 Returns the empty graph on @var{n} vertices.
269 @opencatbox{Categories:}
270 @category{Package graphs}
271 @category{Package graphs - constructions}
275 @anchor{flower_snark}
276 @deffn {Function} flower_snark (@var{n})
277 Returns the flower graph on @var{4n} vertices.
282 @c f5 : flower_snark(5)$
283 @c chromatic_index(f5);
286 (%i1) load ("graphs")$
287 (%i2) f5 : flower_snark(5)$
288 (%i3) chromatic_index(f5);
292 @opencatbox{Categories:}
293 @category{Package graphs}
294 @category{Package graphs - constructions}
298 @anchor{from_adjacency_matrix}
299 @deffn {Function} from_adjacency_matrix (@var{A})
300 Returns the graph represented by its adjacency matrix @var{A}.
302 @opencatbox{Categories:}
303 @category{Package graphs}
304 @category{Package graphs - constructions}
308 @anchor{frucht_graph}
309 @deffn {Function} frucht_graph ()
310 Returns the Frucht graph.
312 @opencatbox{Categories:}
313 @category{Package graphs}
314 @category{Package graphs - constructions}
318 @anchor{graph_product}
319 @deffn {Function} graph_product (@var{g1}, @var{g1})
320 Returns the direct product of graphs @var{g1} and @var{g2}.
325 @c grid : graph_product(path_graph(3), path_graph(4))$
329 (%i1) load ("graphs")$
330 (%i2) grid : graph_product(path_graph(3), path_graph(4))$
331 (%i3) draw_graph(grid)$
334 @opencatbox{Categories:}
335 @category{Package graphs}
336 @category{Package graphs - constructions}
341 @image{figures/graphs01,6cm}
345 @deffn {Function} graph_union (@var{g1}, @var{g1})
346 Returns the union (sum) of graphs @var{g1} and @var{g2}.
348 @opencatbox{Categories:}
349 @category{Package graphs}
350 @category{Package graphs - constructions}
355 @deffn {Function} grid_graph (@var{n}, @var{m})
356 Returns the @var{n x m} grid.
358 @opencatbox{Categories:}
359 @category{Package graphs}
360 @category{Package graphs - constructions}
364 @anchor{great_rhombicosidodecahedron_graph}
365 @deffn {Function} great_rhombicosidodecahedron_graph ()
366 Returns the great rhombicosidodecahedron graph.
368 @opencatbox{Categories:}
369 @category{Package graphs}
370 @category{Package graphs - constructions}
374 @anchor{great_rhombicuboctahedron_graph}
375 @deffn {Function} great_rhombicuboctahedron_graph ()
376 Returns the great rhombicuboctahedron graph.
378 @opencatbox{Categories:}
379 @category{Package graphs}
380 @category{Package graphs - constructions}
384 @anchor{grotzch_graph}
385 @deffn {Function} grotzch_graph ()
386 Returns the Grotzch graph.
388 @opencatbox{Categories:}
389 @category{Package graphs}
390 @category{Package graphs - constructions}
394 @anchor{heawood_graph}
395 @deffn {Function} heawood_graph ()
396 Returns the Heawood graph.
398 @opencatbox{Categories:}
399 @category{Package graphs}
400 @category{Package graphs - constructions}
404 @anchor{icosahedron_graph}
405 @deffn {Function} icosahedron_graph ()
406 Returns the icosahedron graph.
408 @opencatbox{Categories:}
409 @category{Package graphs}
410 @category{Package graphs - constructions}
414 @anchor{icosidodecahedron_graph}
415 @deffn {Function} icosidodecahedron_graph ()
416 Returns the icosidodecahedron graph.
418 @opencatbox{Categories:}
419 @category{Package graphs}
420 @category{Package graphs - constructions}
424 @anchor{induced_subgraph}
425 @deffn {Function} induced_subgraph (@var{V}, @var{g})
426 Returns the graph induced on the subset @var{V} of vertices of the graph
432 @c p : petersen_graph()$
434 @c g : induced_subgraph(V, p)$
438 (%i1) load ("graphs")$
439 (%i2) p : petersen_graph()$
440 (%i3) V : [0,1,2,3,4]$
441 (%i4) g : induced_subgraph(V, p)$
442 (%i5) print_graph(g)$
443 Graph on 5 vertices with 5 edges.
452 @opencatbox{Categories:}
453 @category{Package graphs}
454 @category{Package graphs - constructions}
459 @deffn {Function} line_graph (@var{g})
460 Returns the line graph of the graph @var{g}.
462 @opencatbox{Categories:}
463 @category{Package graphs}
464 @category{Package graphs - constructions}
469 @deffn {Function} make_graph @
470 @fname{make_graph} (@var{vrt}, @var{f}) @
471 @fname{make_graph} (@var{vrt}, @var{f}, @var{oriented})
473 Creates a graph using a predicate function @var{f}.
475 @var{vrt} is a list or set of vertices, or an integer.
477 When @var{vrt} is a list or set,
478 its elements may be any integers,
479 and, if a list, may be listed in any order.
481 When @var{vrt} is an integer,
482 vertices of the graph will be integers from 1 to @var{vrt}.
484 @var{f} is a predicate function. Two vertices @var{a} and @var{b} will
485 be connected if @code{f(a,b)=true}.
487 If @var{directed} is not @var{false}, then the graph will be directed.
492 @c g : make_graph(powerset({1,2,3,4,5}, 2), disjointp)$
493 @c is_isomorphic(g, petersen_graph());
494 @c get_vertex_label(1, g);
497 (%i1) load("graphs")$
498 (%i2) g : make_graph(powerset(@{1,2,3,4,5@}, 2), disjointp)$
499 (%i3) is_isomorphic(g, petersen_graph());
501 (%i4) get_vertex_label(1, g);
508 @c f(i, j) := is (mod(j, i)=0)$
509 @c g : make_graph(20, f, directed=true)$
510 @c out_neighbors(4, g);
511 @c in_neighbors(18, g);
514 (%i1) load("graphs")$
515 (%i2) f(i, j) := is (mod(j, i)=0)$
516 (%i3) g : make_graph(20, f, directed=true)$
517 (%i4) out_neighbors(4, g);
518 (%o4) [8, 12, 16, 20]
519 (%i5) in_neighbors(18, g);
520 (%o5) [1, 2, 3, 6, 9]
523 @opencatbox{Categories:}
524 @category{Package graphs}
525 @category{Package graphs - constructions}
529 @anchor{mycielski_graph}
530 @deffn {Function} mycielski_graph (@var{g})
531 Returns the mycielskian graph of the graph @var{g}.
533 @opencatbox{Categories:}
534 @category{Package graphs}
535 @category{Package graphs - constructions}
540 @deffn {Function} new_graph ()
541 Returns the graph with no vertices and no edges.
543 @opencatbox{Categories:}
544 @category{Package graphs}
545 @category{Package graphs - constructions}
549 @anchor{path_digraph}
550 @deffn {Function} path_digraph (@var{n})
551 Returns the directed path on @var{n} vertices.
553 @opencatbox{Categories:}
554 @category{Package graphs}
555 @category{Package graphs - constructions}
560 @deffn {Function} path_graph (@var{n})
561 Returns the path on @var{n} vertices.
563 @opencatbox{Categories:}
564 @category{Package graphs}
565 @category{Package graphs - constructions}
569 @anchor{petersen_graph}
570 @deffn {Function} petersen_graph @
571 @fname{petersen_graph} () @
572 @fname{petersen_graph} (@var{n}, @var{d})
574 Returns the petersen graph @var{P_@{n,d@}}. The default values for
575 @var{n} and @var{d} are @code{n=5} and @code{d=2}.
577 @opencatbox{Categories:}
578 @category{Package graphs}
579 @category{Package graphs - constructions}
583 @anchor{random_bipartite_graph}
584 @deffn {Function} random_bipartite_graph (@var{a}, @var{b}, @var{p})
585 Returns a random bipartite graph on @code{a+b} vertices. Each edge is
586 present with probability @var{p}.
588 @opencatbox{Categories:}
589 @category{Package graphs}
590 @category{Package graphs - constructions}
594 @anchor{random_digraph}
595 @deffn {Function} random_digraph (@var{n}, @var{p})
596 Returns a random directed graph on @var{n} vertices. Each arc is present
597 with probability @var{p}.
599 @opencatbox{Categories:}
600 @category{Package graphs}
601 @category{Package graphs - constructions}
605 @anchor{random_regular_graph}
606 @deffn {Function} random_regular_graph @
607 @fname{random_regular_graph} (@var{n}) @
608 @fname{random_regular_graph} (@var{n}, @var{d})
610 Returns a random @var{d}-regular graph on @var{n} vertices. The default
611 value for @var{d} is @code{d=3}.
613 @opencatbox{Categories:}
614 @category{Package graphs}
615 @category{Package graphs - constructions}
619 @anchor{random_graph}
620 @deffn {Function} random_graph (@var{n}, @var{p})
621 Returns a random graph on @var{n} vertices. Each edge is present with
624 @opencatbox{Categories:}
625 @category{Package graphs}
626 @category{Package graphs - constructions}
630 @anchor{random_graph1}
631 @deffn {Function} random_graph1 (@var{n}, @var{m})
632 Returns a random graph on @var{n} vertices and random @var{m} edges.
634 @opencatbox{Categories:}
635 @category{Package graphs}
636 @category{Package graphs - constructions}
640 @anchor{random_network}
641 @deffn {Function} random_network (@var{n}, @var{p}, @var{w})
642 Returns a random network on @var{n} vertices. Each arc is present with
643 probability @var{p} and has a weight in the range @code{[0,w]}. The
644 function returns a list @code{[network, source, sink]}.
649 @c [net, s, t] : random_network(50, 0.2, 10.0);
650 @c max_flow(net, s, t)$
654 (%i1) load ("graphs")$
655 (%i2) [net, s, t] : random_network(50, 0.2, 10.0);
656 (%o2) [DIGRAPH, 50, 51]
657 (%i3) max_flow(net, s, t)$
659 (%o4) 27.65981397932507
662 @opencatbox{Categories:}
663 @category{Package graphs}
664 @category{Package graphs - constructions}
668 @anchor{random_tournament}
669 @deffn {Function} random_tournament (@var{n})
670 Returns a random tournament on @var{n} vertices.
672 @opencatbox{Categories:}
673 @category{Package graphs}
674 @category{Package graphs - constructions}
679 @deffn {Function} random_tree (@var{n})
680 Returns a random tree on @var{n} vertices.
682 @opencatbox{Categories:}
683 @category{Package graphs}
684 @category{Package graphs - constructions}
688 @anchor{small_rhombicosidodecahedron_graph}
689 @deffn {Function} small_rhombicosidodecahedron_graph ()
690 Returns the small rhombicosidodecahedron graph.
692 @opencatbox{Categories:}
693 @category{Package graphs}
694 @category{Package graphs - constructions}
698 @anchor{small_rhombicuboctahedron_graph}
699 @deffn {Function} small_rhombicuboctahedron_graph ()
700 Returns the small rhombicuboctahedron graph.
702 @opencatbox{Categories:}
703 @category{Package graphs}
704 @category{Package graphs - constructions}
708 @anchor{snub_cube_graph}
709 @deffn {Function} snub_cube_graph ()
710 Returns the snub cube graph.
712 @opencatbox{Categories:}
713 @category{Package graphs}
714 @category{Package graphs - constructions}
718 @anchor{snub_dodecahedron_graph}
719 @deffn {Function} snub_dodecahedron_graph ()
720 Returns the snub dodecahedron graph.
722 @opencatbox{Categories:}
723 @category{Package graphs}
724 @category{Package graphs - constructions}
728 @anchor{truncated_cube_graph}
729 @deffn {Function} truncated_cube_graph ()
730 Returns the truncated cube graph.
732 @opencatbox{Categories:}
733 @category{Package graphs}
734 @category{Package graphs - constructions}
738 @anchor{truncated_dodecahedron_graph}
739 @deffn {Function} truncated_dodecahedron_graph ()
740 Returns the truncated dodecahedron graph.
742 @opencatbox{Categories:}
743 @category{Package graphs}
744 @category{Package graphs - constructions}
749 @anchor{truncated_icosahedron_graph}
750 @deffn {Function} truncated_icosahedron_graph ()
751 Returns the truncated icosahedron graph.
753 @opencatbox{Categories:}
754 @category{Package graphs}
755 @category{Package graphs - constructions}
760 @anchor{truncated_tetrahedron_graph}
761 @deffn {Function} truncated_tetrahedron_graph ()
762 Returns the truncated tetrahedron graph.
764 @opencatbox{Categories:}
765 @category{Package graphs}
766 @category{Package graphs - constructions}
771 @deffn {Function} tutte_graph ()
772 Returns the Tutte graph.
774 @opencatbox{Categories:}
775 @category{Package graphs}
776 @category{Package graphs - constructions}
780 @anchor{underlying_graph}
781 @deffn {Function} underlying_graph (@var{g})
782 Returns the underlying graph of the directed graph @var{g}.
784 @opencatbox{Categories:}
785 @category{Package graphs}
786 @category{Package graphs - constructions}
791 @deffn {Function} wheel_graph (@var{n})
792 Returns the wheel graph on @var{n+1} vertices.
794 @opencatbox{Categories:}
795 @category{Package graphs}
796 @category{Package graphs - constructions}
800 @subsection Graph properties
802 @anchor{adjacency_matrix}
803 @deffn {Function} adjacency_matrix (@var{gr})
804 Returns the adjacency matrix of the graph @var{gr}.
809 @c c5 : cycle_graph(4)$
810 @c adjacency_matrix(c5);
813 (%i1) load ("graphs")$
814 (%i2) c5 : cycle_graph(4)$
815 (%i3) adjacency_matrix(c5);
825 @opencatbox{Categories:}
826 @category{Package graphs}
827 @category{Package graphs - properties}
831 @anchor{average_degree}
832 @deffn {Function} average_degree (@var{gr})
833 Returns the average degree of vertices in the graph @var{gr}.
838 @c average_degree(grotzch_graph());
841 (%i1) load ("graphs")$
842 (%i2) average_degree(grotzch_graph());
848 @opencatbox{Categories:}
849 @category{Package graphs}
850 @category{Package graphs - properties}
854 @anchor{biconnected_components}
855 @deffn {Function} biconnected_components (@var{gr})
856 Returns the (vertex sets of) 2-connected components of the graph
865 @c [1,2],[2,3],[2,4],[3,4],
866 @c [4,5],[5,6],[4,6],[6,7]
868 @c biconnected_components(g);
871 (%i1) load ("graphs")$
872 (%i2) g : create_graph(
875 [1,2],[2,3],[2,4],[3,4],
876 [4,5],[5,6],[4,6],[6,7]
878 (%i3) biconnected_components(g);
879 (%o3) [[6, 7], [4, 5, 6], [1, 2], [2, 3, 4]]
883 @image{figures/graphs13,6cm}
886 @opencatbox{Categories:}
887 @category{Package graphs}
888 @category{Package graphs - properties}
893 @deffn {Function} bipartition (@var{gr})
894 Returns a bipartition of the vertices of the graph @var{gr} or an empty
895 list if @var{gr} is not bipartite.
901 @c h : heawood_graph()$
902 @c [A,B]:bipartition(h);
903 @c draw_graph(h, show_vertices=A, program=circular)$
906 (%i1) load ("graphs")$
907 (%i2) h : heawood_graph()$
908 (%i3) [A,B]:bipartition(h);
909 (%o3) [[8, 12, 6, 10, 0, 2, 4], [13, 5, 11, 7, 9, 1, 3]]
910 (%i4) draw_graph(h, show_vertices=A, program=circular)$
913 @opencatbox{Categories:}
914 @category{Package graphs}
915 @category{Package graphs - properties}
920 @image{figures/graphs02,6cm}
923 @anchor{chromatic_index}
924 @deffn {Function} chromatic_index (@var{gr})
925 Returns the chromatic index of the graph @var{gr}.
930 @c p : petersen_graph()$
931 @c chromatic_index(p);
934 (%i1) load ("graphs")$
935 (%i2) p : petersen_graph()$
936 (%i3) chromatic_index(p);
940 @opencatbox{Categories:}
941 @category{Package graphs}
942 @category{Package graphs - properties}
946 @anchor{chromatic_number}
947 @deffn {Function} chromatic_number (@var{gr})
948 Returns the chromatic number of the graph @var{gr}.
953 @c chromatic_number(cycle_graph(5));
954 @c chromatic_number(cycle_graph(6));
957 (%i1) load ("graphs")$
958 (%i2) chromatic_number(cycle_graph(5));
960 (%i3) chromatic_number(cycle_graph(6));
964 @opencatbox{Categories:}
965 @category{Package graphs}
966 @category{Package graphs - properties}
970 @anchor{clear_edge_weight}
971 @deffn {Function} clear_edge_weight (@var{e}, @var{gr})
972 Removes the weight of the edge @var{e} in the graph @var{gr}.
978 @c g : create_graph(3, [[[0,1], 1.5], [[1,2], 1.3]])$
979 @c get_edge_weight([0,1], g);
980 @c clear_edge_weight([0,1], g)$
981 @c get_edge_weight([0,1], g);
984 (%i1) load ("graphs")$
985 (%i2) g : create_graph(3, [[[0,1], 1.5], [[1,2], 1.3]])$
986 (%i3) get_edge_weight([0,1], g);
988 (%i4) clear_edge_weight([0,1], g)$
989 (%i5) get_edge_weight([0,1], g);
993 @opencatbox{Categories:}
994 @category{Package graphs}
995 @category{Package graphs - properties}
999 @anchor{clear_vertex_label}
1000 @deffn {Function} clear_vertex_label (@var{v}, @var{gr})
1001 Removes the label of the vertex @var{v} in the graph @var{gr}.
1006 @c g : create_graph([[0,"Zero"], [1, "One"]], [[0,1]])$
1007 @c get_vertex_label(0, g);
1008 @c clear_vertex_label(0, g);
1009 @c get_vertex_label(0, g);
1012 (%i1) load ("graphs")$
1013 (%i2) g : create_graph([[0,"Zero"], [1, "One"]], [[0,1]])$
1014 (%i3) get_vertex_label(0, g);
1016 (%i4) clear_vertex_label(0, g);
1018 (%i5) get_vertex_label(0, g);
1022 @opencatbox{Categories:}
1023 @category{Package graphs}
1024 @category{Package graphs - properties}
1028 @anchor{connected_components}
1029 @deffn {Function} connected_components (@var{gr})
1030 Returns the (vertex sets of) connected components of the graph @var{gr}.
1035 @c g: graph_union(cycle_graph(5), path_graph(4))$
1036 @c connected_components(g);
1039 (%i1) load ("graphs")$
1040 (%i2) g: graph_union(cycle_graph(5), path_graph(4))$
1041 (%i3) connected_components(g);
1042 (%o3) [[1, 2, 3, 4, 0], [8, 7, 6, 5]]
1045 @opencatbox{Categories:}
1046 @category{Package graphs}
1047 @category{Package graphs - properties}
1052 @deffn {Function} diameter (@var{gr})
1053 Returns the diameter of the graph @var{gr}.
1058 @c diameter(dodecahedron_graph());
1061 (%i1) load ("graphs")$
1062 (%i2) diameter(dodecahedron_graph());
1066 @opencatbox{Categories:}
1067 @category{Package graphs}
1068 @category{Package graphs - properties}
1072 @anchor{edge_coloring}
1073 @deffn {Function} edge_coloring (@var{gr})
1074 Returns an optimal coloring of the edges of the graph @var{gr}.
1076 The function returns the chromatic index and a list representing the
1077 coloring of the edges of @var{gr}.
1082 @c p : petersen_graph()$
1083 @c [ch_index, col] : edge_coloring(p);
1084 @c assoc([0,1], col);
1085 @c assoc([0,5], col);
1088 (%i1) load ("graphs")$
1089 (%i2) p : petersen_graph()$
1090 (%i3) [ch_index, col] : edge_coloring(p);
1091 (%o3) [4, [[[0, 5], 3], [[5, 7], 1], [[0, 1], 1], [[1, 6], 2],
1092 [[6, 8], 1], [[1, 2], 3], [[2, 7], 4], [[7, 9], 2], [[2, 3], 2],
1093 [[3, 8], 3], [[5, 8], 2], [[3, 4], 1], [[4, 9], 4], [[6, 9], 3],
1095 (%i4) assoc([0,1], col);
1097 (%i5) assoc([0,5], col);
1101 @opencatbox{Categories:}
1102 @category{Package graphs}
1103 @category{Package graphs - properties}
1107 @anchor{degree_sequence}
1108 @deffn {Function} degree_sequence (@var{gr})
1109 Returns the list of vertex degrees of the graph @var{gr}.
1114 @c degree_sequence(random_graph(10, 0.4));
1117 (%i1) load ("graphs")$
1118 (%i2) degree_sequence(random_graph(10, 0.4));
1119 (%o2) [2, 2, 2, 2, 2, 2, 3, 3, 3, 3]
1122 @opencatbox{Categories:}
1123 @category{Package graphs}
1124 @category{Package graphs - properties}
1128 @anchor{edge_connectivity}
1129 @deffn {Function} edge_connectivity (@var{gr})
1130 Returns the edge-connectivity of the graph @var{gr}.
1132 See also @mref{min_edge_cut}.
1134 @opencatbox{Categories:}
1135 @category{Package graphs}
1136 @category{Package graphs - properties}
1141 @deffn {Function} edges (@var{gr})
1142 Returns the list of edges (arcs) in a (directed) graph @var{gr}.
1147 @c edges(complete_graph(4));
1150 (%i1) load ("graphs")$
1151 (%i2) edges(complete_graph(4));
1152 (%o2) [[2, 3], [1, 3], [1, 2], [0, 3], [0, 2], [0, 1]]
1155 @opencatbox{Categories:}
1156 @category{Package graphs}
1157 @category{Package graphs - properties}
1161 @anchor{get_edge_weight}
1162 @deffn {Function} get_edge_weight @
1163 @fname{get_edge_weight} (@var{e}, @var{gr}) @
1164 @fname{get_edge_weight} (@var{e}, @var{gr}, @var{ifnot})
1166 Returns the weight of the edge @var{e} in the graph @var{gr}.
1168 If there is no weight assigned to the edge, the function returns 1. If
1169 the edge is not present in the graph, the function signals an error or
1170 returns the optional argument @var{ifnot}.
1175 @c c5 : cycle_graph(5)$
1176 @c get_edge_weight([1,2], c5);
1177 @c set_edge_weight([1,2], 2.0, c5);
1178 @c get_edge_weight([1,2], c5);
1181 (%i1) load ("graphs")$
1182 (%i2) c5 : cycle_graph(5)$
1183 (%i3) get_edge_weight([1,2], c5);
1185 (%i4) set_edge_weight([1,2], 2.0, c5);
1187 (%i5) get_edge_weight([1,2], c5);
1191 @opencatbox{Categories:}
1192 @category{Package graphs}
1193 @category{Package graphs - properties}
1197 @anchor{get_vertex_label}
1198 @deffn {Function} get_vertex_label (@var{v}, @var{gr})
1199 Returns the label of the vertex @var{v} in the graph @var{gr}.
1201 If no label is assigned to vertex @var{v},
1202 @code{get_vertex_label} returns @code{false}.
1207 @c g: create_graph([[0, "Zero"], [1, "One"], 2, 3], [])$
1208 @c get_vertex_label(0, g);
1209 @c get_vertex_label(2, g);
1212 (%i1) load("graphs")$
1213 (%i2) g: create_graph([[0, "Zero"], [1, "One"], 2, 3], [])$
1214 (%i3) get_vertex_label(0, g);
1216 (%i4) get_vertex_label(2, g);
1220 @opencatbox{Categories:}
1221 @category{Package graphs}
1222 @category{Package graphs - properties}
1226 @anchor{get_unique_vertex_by_label}
1227 @deffn {Function} get_unique_vertex_by_label (@var{l}, @var{gr})
1228 Returns the unique vertex which has the label @var{l} in graph @var{gr}.
1230 If there is no such vertex,
1231 @code{get_unique_vertex_by_label} returns @code{false}.
1233 If there are two or more vertices with label @var{l},
1234 @code{get_unique_vertex_by_label} reports an error.
1239 @c g: create_graph ([[0, "Zero"], [1, "One"], [2, "Other"], [3, "Other"]], []) $
1240 @c get_unique_vertex_by_label ("Zero", g);
1241 @c get_unique_vertex_by_label ("Two", g);
1242 @c errcatch (get_unique_vertex_by_label ("Other", g));
1245 (%i1) load ("graphs")$
1246 (%i2) g: create_graph ([[0, "Zero"], [1, "One"], [2, "Other"], [3, "Other"]], []) $
1247 (%i3) get_unique_vertex_by_label ("Zero", g);
1249 (%i4) get_unique_vertex_by_label ("Two", g);
1251 (%i5) errcatch (get_unique_vertex_by_label ("Other", g));
1252 get_unique_vertex_by_label: two or more vertices have the same label "Other"
1256 @opencatbox{Categories:}
1257 @category{Package graphs}
1258 @category{Package graphs - properties}
1262 @anchor{get_all_vertices_by_label}
1263 @deffn {Function} get_all_vertices_by_label (@var{l}, @var{gr})
1264 Returns all vertices, if any, which have the label @var{l} in graph @var{gr}.
1266 If there are no such vertices,
1267 @code{get_all_vertices_by_label} returns an empty list @code{[]}.
1272 @c g: create_graph ([[0, "Zero"], [1, "One"], [2, "Other"], [3, "Other"]], []) $
1273 @c get_all_vertices_by_label ("Zero", g);
1274 @c get_all_vertices_by_label ("Two", g);
1275 @c get_all_vertices_by_label ("Other", g);
1278 (%i1) load ("graphs")$
1279 (%i2) g: create_graph ([[0, "Zero"], [1, "One"], [2, "Other"], [3, "Other"]], []) $
1280 (%i3) get_all_vertices_by_label ("Zero", g);
1282 (%i4) get_all_vertices_by_label ("Two", g);
1284 (%i5) get_all_vertices_by_label ("Other", g);
1288 @opencatbox{Categories:}
1289 @category{Package graphs}
1290 @category{Package graphs - properties}
1294 @anchor{graph_charpoly}
1295 @deffn {Function} graph_charpoly (@var{gr}, @var{x})
1296 Returns the characteristic polynomial (in variable @var{x}) of the graph
1302 @c p : petersen_graph()$
1303 @c graph_charpoly(p, x), factor;
1306 (%i1) load ("graphs")$
1307 (%i2) p : petersen_graph()$
1308 (%i3) graph_charpoly(p, x), factor;
1310 (%o3) (x - 3) (x - 1) (x + 2)
1313 @opencatbox{Categories:}
1314 @category{Package graphs}
1315 @category{Package graphs - properties}
1319 @anchor{graph_center}
1320 @deffn {Function} graph_center (@var{gr})
1321 Returns the center of the graph @var{gr}.
1326 @c g : grid_graph(5,5)$
1330 (%i1) load ("graphs")$
1331 (%i2) g : grid_graph(5,5)$
1332 (%i3) graph_center(g);
1336 @opencatbox{Categories:}
1337 @category{Package graphs}
1338 @category{Package graphs - properties}
1342 @anchor{graph_eigenvalues}
1343 @deffn {Function} graph_eigenvalues (@var{gr})
1344 Returns the eigenvalues of the graph @var{gr}. The function returns
1345 eigenvalues in the same format as maxima @mref{eigenvalues} function.
1350 @c p : petersen_graph()$
1351 @c graph_eigenvalues(p);
1354 (%i1) load ("graphs")$
1355 (%i2) p : petersen_graph()$
1356 (%i3) graph_eigenvalues(p);
1357 (%o3) [[3, - 2, 1], [1, 4, 5]]
1360 @opencatbox{Categories:}
1361 @category{Package graphs}
1362 @category{Package graphs - properties}
1366 @anchor{graph_periphery}
1367 @deffn {Function} graph_periphery (@var{gr})
1368 Returns the periphery of the graph @var{gr}.
1373 @c g : grid_graph(5,5)$
1374 @c graph_periphery(g);
1377 (%i1) load ("graphs")$
1378 (%i2) g : grid_graph(5,5)$
1379 (%i3) graph_periphery(g);
1380 (%o3) [24, 20, 4, 0]
1383 @opencatbox{Categories:}
1384 @category{Package graphs}
1385 @category{Package graphs - properties}
1390 @deffn {Function} graph_size (@var{gr})
1391 Returns the number of edges in the graph @var{gr}.
1396 @c p : petersen_graph()$
1400 (%i1) load ("graphs")$
1401 (%i2) p : petersen_graph()$
1402 (%i3) graph_size(p);
1406 @opencatbox{Categories:}
1407 @category{Package graphs}
1408 @category{Package graphs - properties}
1412 @anchor{graph_order}
1413 @deffn {Function} graph_order (@var{gr})
1414 Returns the number of vertices in the graph @var{gr}.
1419 @c p : petersen_graph()$
1423 (%i1) load ("graphs")$
1424 (%i2) p : petersen_graph()$
1425 (%i3) graph_order(p);
1429 @opencatbox{Categories:}
1430 @category{Package graphs}
1431 @category{Package graphs - properties}
1436 @deffn {Function} girth (@var{gr})
1437 Returns the length of the shortest cycle in @var{gr}.
1442 @c g : heawood_graph()$
1446 (%i1) load ("graphs")$
1447 (%i2) g : heawood_graph()$
1452 @opencatbox{Categories:}
1453 @category{Package graphs}
1454 @category{Package graphs - properties}
1458 @anchor{hamilton_cycle}
1459 @deffn {Function} hamilton_cycle (@var{gr})
1460 Returns the Hamilton cycle of the graph @var{gr} or an empty list if
1461 @var{gr} is not hamiltonian.
1466 @c c : cube_graph(3)$
1467 @c hc : hamilton_cycle(c);
1468 @c draw_graph(c, show_edges=vertices_to_cycle(hc))$
1471 (%i1) load ("graphs")$
1472 (%i2) c : cube_graph(3)$
1473 (%i3) hc : hamilton_cycle(c);
1474 (%o3) [7, 3, 2, 6, 4, 0, 1, 5, 7]
1475 (%i4) draw_graph(c, show_edges=vertices_to_cycle(hc))$
1478 @opencatbox{Categories:}
1479 @category{Package graphs}
1480 @category{Package graphs - properties}
1485 @image{figures/graphs03,6cm}
1488 @anchor{hamilton_path}
1489 @deffn {Function} hamilton_path (@var{gr})
1490 Returns the Hamilton path of the graph @var{gr} or an empty list if
1491 @var{gr} does not have a Hamilton path.
1496 @c p : petersen_graph()$
1497 @c hp : hamilton_path(p);
1498 @c draw_graph(p, show_edges=vertices_to_path(hp))$
1501 (%i1) load ("graphs")$
1502 (%i2) p : petersen_graph()$
1503 (%i3) hp : hamilton_path(p);
1504 (%o3) [0, 5, 7, 2, 1, 6, 8, 3, 4, 9]
1505 (%i4) draw_graph(p, show_edges=vertices_to_path(hp))$
1508 @opencatbox{Categories:}
1509 @category{Package graphs}
1510 @category{Package graphs - properties}
1515 @image{figures/graphs04,6cm}
1518 @anchor{isomorphism}
1519 @deffn {Function} isomorphism (@var{gr1}, @var{gr2})
1521 Returns a an isomorphism between graphs/digraphs @var{gr1} and
1522 @var{gr2}. If @var{gr1} and @var{gr2} are not isomorphic, it returns
1528 @c clk5:complement_graph(line_graph(complete_graph(5)))$
1529 @c isomorphism(clk5, petersen_graph());
1532 (%i1) load ("graphs")$
1533 (%i2) clk5:complement_graph(line_graph(complete_graph(5)))$
1534 (%i3) isomorphism(clk5, petersen_graph());
1535 (%o3) [9 -> 0, 2 -> 1, 6 -> 2, 5 -> 3, 0 -> 4, 1 -> 5, 3 -> 6,
1536 4 -> 7, 7 -> 8, 8 -> 9]
1539 @opencatbox{Categories:}
1540 @category{Package graphs}
1541 @category{Package graphs - properties}
1545 @anchor{in_neighbors}
1546 @deffn {Function} in_neighbors (@var{v}, @var{gr})
1547 Returns the list of in-neighbors of the vertex @var{v} in the directed
1553 @c p : path_digraph(3)$
1554 @c in_neighbors(2, p);
1555 @c out_neighbors(2, p);
1558 (%i1) load ("graphs")$
1559 (%i2) p : path_digraph(3)$
1560 (%i3) in_neighbors(2, p);
1562 (%i4) out_neighbors(2, p);
1566 @opencatbox{Categories:}
1567 @category{Package graphs}
1568 @category{Package graphs - properties}
1572 @anchor{is_biconnected}
1573 @deffn {Function} is_biconnected (@var{gr})
1574 Returns @code{true} if @var{gr} is 2-connected and @code{false} otherwise.
1579 @c is_biconnected(cycle_graph(5));
1580 @c is_biconnected(path_graph(5));
1583 (%i1) load ("graphs")$
1584 (%i2) is_biconnected(cycle_graph(5));
1586 (%i3) is_biconnected(path_graph(5));
1590 @opencatbox{Categories:}
1591 @category{Package graphs}
1592 @category{Package graphs - properties}
1596 @anchor{is_bipartite}
1597 @deffn {Function} is_bipartite (@var{gr})
1598 Returns @code{true} if @var{gr} is bipartite (2-colorable) and @code{false} otherwise.
1603 @c is_bipartite(petersen_graph());
1604 @c is_bipartite(heawood_graph());
1607 (%i1) load ("graphs")$
1608 (%i2) is_bipartite(petersen_graph());
1610 (%i3) is_bipartite(heawood_graph());
1614 @opencatbox{Categories:}
1615 @category{Package graphs}
1616 @category{Package graphs - properties}
1620 @anchor{is_connected}
1621 @deffn {Function} is_connected (@var{gr})
1622 Returns @code{true} if the graph @var{gr} is connected and @code{false} otherwise.
1627 @c is_connected(graph_union(cycle_graph(4), path_graph(3)));
1630 (%i1) load ("graphs")$
1631 (%i2) is_connected(graph_union(cycle_graph(4), path_graph(3)));
1635 @opencatbox{Categories:}
1636 @category{Package graphs}
1637 @category{Package graphs - properties}
1642 @deffn {Function} is_digraph (@var{gr})
1643 Returns @code{true} if @var{gr} is a directed graph and @code{false} otherwise.
1648 @c is_digraph(path_graph(5));
1649 @c is_digraph(path_digraph(5));
1652 (%i1) load ("graphs")$
1653 (%i2) is_digraph(path_graph(5));
1655 (%i3) is_digraph(path_digraph(5));
1659 @opencatbox{Categories:}
1660 @category{Package graphs}
1661 @category{Package graphs - properties}
1665 @anchor{is_edge_in_graph}
1666 @deffn {Function} is_edge_in_graph (@var{e}, @var{gr})
1667 Returns @code{true} if @var{e} is an edge (arc) in the (directed) graph @var{g}
1668 and @code{false} otherwise.
1673 @c c4 : cycle_graph(4)$
1674 @c is_edge_in_graph([2,3], c4);
1675 @c is_edge_in_graph([3,2], c4);
1676 @c is_edge_in_graph([2,4], c4);
1677 @c is_edge_in_graph([3,2], cycle_digraph(4));
1680 (%i1) load ("graphs")$
1681 (%i2) c4 : cycle_graph(4)$
1682 (%i3) is_edge_in_graph([2,3], c4);
1684 (%i4) is_edge_in_graph([3,2], c4);
1686 (%i5) is_edge_in_graph([2,4], c4);
1688 (%i6) is_edge_in_graph([3,2], cycle_digraph(4));
1692 @opencatbox{Categories:}
1693 @category{Package graphs}
1694 @category{Package graphs - properties}
1699 @deffn {Function} is_graph (@var{gr})
1700 Returns @code{true} if @var{gr} is a graph and @code{false} otherwise.
1705 @c is_graph(path_graph(5));
1706 @c is_graph(path_digraph(5));
1709 (%i1) load ("graphs")$
1710 (%i2) is_graph(path_graph(5));
1712 (%i3) is_graph(path_digraph(5));
1716 @opencatbox{Categories:}
1717 @category{Package graphs}
1718 @category{Package graphs - properties}
1722 @anchor{is_graph_or_digraph}
1723 @deffn {Function} is_graph_or_digraph (@var{gr})
1724 Returns @code{true} if @var{gr} is a graph or a directed graph and @code{false} otherwise.
1729 @c is_graph_or_digraph(path_graph(5));
1730 @c is_graph_or_digraph(path_digraph(5));
1733 (%i1) load ("graphs")$
1734 (%i2) is_graph_or_digraph(path_graph(5));
1736 (%i3) is_graph_or_digraph(path_digraph(5));
1740 @opencatbox{Categories:}
1741 @category{Package graphs}
1742 @category{Package graphs - properties}
1746 @anchor{is_isomorphic}
1747 @deffn {Function} is_isomorphic (@var{gr1}, @var{gr2})
1749 Returns @code{true} if graphs/digraphs @var{gr1} and @var{gr2} are isomorphic
1750 and @code{false} otherwise.
1752 See also @mrefdot{isomorphism}
1757 @c clk5:complement_graph(line_graph(complete_graph(5)))$
1758 @c is_isomorphic(clk5, petersen_graph());
1761 (%i1) load ("graphs")$
1762 (%i2) clk5:complement_graph(line_graph(complete_graph(5)))$
1763 (%i3) is_isomorphic(clk5, petersen_graph());
1767 @opencatbox{Categories:}
1768 @category{Package graphs}
1769 @category{Package graphs - properties}
1774 @deffn {Function} is_planar (@var{gr})
1776 Returns @code{true} if @var{gr} is a planar graph and @code{false} otherwise.
1778 The algorithm used is the Demoucron's algorithm, which is a quadratic time
1784 @c is_planar(dodecahedron_graph());
1785 @c is_planar(petersen_graph());
1786 @c is_planar(petersen_graph(10,2));
1789 (%i1) load ("graphs")$
1790 (%i2) is_planar(dodecahedron_graph());
1792 (%i3) is_planar(petersen_graph());
1794 (%i4) is_planar(petersen_graph(10,2));
1798 @opencatbox{Categories:}
1799 @category{Package graphs}
1800 @category{Package graphs - properties}
1804 @anchor{is_sconnected}
1805 @deffn {Function} is_sconnected (@var{gr})
1806 Returns @code{true} if the directed graph @var{gr} is strongly connected and
1807 @code{false} otherwise.
1812 @c is_sconnected(cycle_digraph(5));
1813 @c is_sconnected(path_digraph(5));
1816 (%i1) load ("graphs")$
1817 (%i2) is_sconnected(cycle_digraph(5));
1819 (%i3) is_sconnected(path_digraph(5));
1823 @opencatbox{Categories:}
1824 @category{Package graphs}
1825 @category{Package graphs - properties}
1829 @anchor{is_vertex_in_graph}
1830 @deffn {Function} is_vertex_in_graph (@var{v}, @var{gr})
1831 Returns @code{true} if @var{v} is a vertex in the graph @var{g} and @code{false} otherwise.
1836 @c c4 : cycle_graph(4)$
1837 @c is_vertex_in_graph(0, c4);
1838 @c is_vertex_in_graph(6, c4);
1841 (%i1) load ("graphs")$
1842 (%i2) c4 : cycle_graph(4)$
1843 (%i3) is_vertex_in_graph(0, c4);
1845 (%i4) is_vertex_in_graph(6, c4);
1849 @opencatbox{Categories:}
1850 @category{Package graphs}
1851 @category{Package graphs - properties}
1856 @deffn {Function} is_tree (@var{gr})
1857 Returns @code{true} if @var{gr} is a tree and @code{false} otherwise.
1862 @c is_tree(random_tree(4));
1863 @c is_tree(graph_union(random_tree(4), random_tree(5)));
1866 (%i1) load ("graphs")$
1867 (%i2) is_tree(random_tree(4));
1869 (%i3) is_tree(graph_union(random_tree(4), random_tree(5)));
1873 @opencatbox{Categories:}
1874 @category{Package graphs}
1875 @category{Package graphs - properties}
1879 @anchor{laplacian_matrix}
1880 @deffn {Function} laplacian_matrix (@var{gr})
1881 Returns the laplacian matrix of the graph @var{gr}.
1886 @c laplacian_matrix(cycle_graph(5));
1889 (%i1) load ("graphs")$
1890 (%i2) laplacian_matrix(cycle_graph(5));
1895 (%o2) [ 0 - 1 2 - 1 0 ]
1902 @opencatbox{Categories:}
1903 @category{Package graphs}
1904 @category{Package graphs - properties}
1909 @deffn {Function} max_clique (@var{gr})
1910 Returns a maximum clique of the graph @var{gr}.
1915 @c g : random_graph(100, 0.5)$
1919 (%i1) load ("graphs")$
1920 (%i2) g : random_graph(100, 0.5)$
1921 (%i3) max_clique(g);
1922 (%o3) [6, 12, 31, 36, 52, 59, 62, 63, 80]
1925 @opencatbox{Categories:}
1926 @category{Package graphs}
1927 @category{Package graphs - properties}
1932 @deffn {Function} max_degree (@var{gr})
1933 Returns the maximal degree of vertices of the graph @var{gr} and a
1934 vertex of maximal degree.
1939 @c g : random_graph(100, 0.02)$
1941 @c vertex_degree(95, g);
1944 (%i1) load ("graphs")$
1945 (%i2) g : random_graph(100, 0.02)$
1946 (%i3) max_degree(g);
1948 (%i4) vertex_degree(95, g);
1952 @opencatbox{Categories:}
1953 @category{Package graphs}
1954 @category{Package graphs - properties}
1959 @deffn {Function} max_flow (@var{net}, @var{s}, @var{t})
1960 Returns a maximum flow through the network @var{net} with the source
1961 @var{s} and the sink @var{t}.
1963 The function returns the value of the maximal flow and a list
1964 representing the weights of the arcs in the optimal flow.
1969 @c net : create_graph(
1980 @c [flow_value, flow] : max_flow(net, 1, 6);
1982 @c for u in out_neighbors(1, net)
1983 @c do fl : fl + assoc([1, u], flow)$
1987 (%i1) load ("graphs")$
1988 (%i2) net : create_graph(
1999 (%i3) [flow_value, flow] : max_flow(net, 1, 6);
2000 (%o3) [0.7, [[[1, 2], 0.5], [[1, 3], 0.2], [[2, 4], 0.2],
2001 [[2, 5], 0.3], [[3, 4], 0.1], [[3, 5], 0.1], [[4, 6], 0.3],
2004 (%i5) for u in out_neighbors(1, net)
2005 do fl : fl + assoc([1, u], flow)$
2010 @opencatbox{Categories:}
2011 @category{Package graphs}
2012 @category{Package graphs - properties}
2016 @anchor{max_independent_set}
2017 @deffn {Function} max_independent_set (@var{gr})
2018 Returns a maximum independent set of the graph @var{gr}.
2023 @c d : dodecahedron_graph()$
2024 @c mi : max_independent_set(d);
2025 @c draw_graph(d, show_vertices=mi)$
2028 (%i1) load ("graphs")$
2029 (%i2) d : dodecahedron_graph()$
2030 (%i3) mi : max_independent_set(d);
2031 (%o3) [0, 3, 5, 9, 10, 11, 18, 19]
2032 (%i4) draw_graph(d, show_vertices=mi)$
2035 @opencatbox{Categories:}
2036 @category{Package graphs}
2037 @category{Package graphs - properties}
2042 @image{figures/graphs05,6cm}
2045 @anchor{max_matching}
2046 @deffn {Function} max_matching (@var{gr})
2047 Returns a maximum matching of the graph @var{gr}.
2052 @c d : dodecahedron_graph()$
2053 @c m : max_matching(d);
2054 @c draw_graph(d, show_edges=m)$
2057 (%i1) load ("graphs")$
2058 (%i2) d : dodecahedron_graph()$
2059 (%i3) m : max_matching(d);
2060 (%o3) [[5, 7], [8, 9], [6, 10], [14, 19], [13, 18], [12, 17],
2061 [11, 16], [0, 15], [3, 4], [1, 2]]
2062 (%i4) draw_graph(d, show_edges=m)$
2065 @opencatbox{Categories:}
2066 @category{Package graphs}
2067 @category{Package graphs - properties}
2072 @image{figures/graphs06,6cm}
2076 @deffn {Function} min_degree (@var{gr})
2077 Returns the minimum degree of vertices of the graph @var{gr} and a
2078 vertex of minimum degree.
2083 @c g : random_graph(100, 0.1)$
2085 @c vertex_degree(21, g);
2088 (%i1) load ("graphs")$
2089 (%i2) g : random_graph(100, 0.1)$
2090 (%i3) min_degree(g);
2092 (%i4) vertex_degree(21, g);
2096 @opencatbox{Categories:}
2097 @category{Package graphs}
2098 @category{Package graphs - properties}
2102 @anchor{min_edge_cut}
2103 @deffn {Function} min_edge_cut (@var{gr})
2104 Returns the minimum edge cut in the graph @var{gr}.
2106 See also @mrefdot{edge_connectivity}
2108 @opencatbox{Categories:}
2109 @category{Package graphs}
2110 @category{Package graphs - properties}
2114 @anchor{min_vertex_cover}
2115 @deffn {Function} min_vertex_cover (@var{gr})
2116 Returns the minimum vertex cover of the graph @var{gr}.
2118 @opencatbox{Categories:}
2119 @category{Package graphs}
2120 @category{Package graphs - properties}
2124 @anchor{min_vertex_cut}
2125 @deffn {Function} min_vertex_cut (@var{gr})
2126 Returns the minimum vertex cut in the graph @var{gr}.
2128 See also @mrefdot{vertex_connectivity}
2130 @opencatbox{Categories:}
2131 @category{Package graphs}
2132 @category{Package graphs - properties}
2136 @anchor{minimum_spanning_tree}
2137 @deffn {Function} minimum_spanning_tree (@var{gr})
2138 Returns the minimum spanning tree of the graph @var{gr}.
2143 @c g : graph_product(path_graph(10), path_graph(10))$
2144 @c t : minimum_spanning_tree(g)$
2145 @c draw_graph(g, show_edges=edges(t))$
2148 (%i1) load ("graphs")$
2149 (%i2) g : graph_product(path_graph(10), path_graph(10))$
2150 (%i3) t : minimum_spanning_tree(g)$
2151 (%i4) draw_graph(g, show_edges=edges(t))$
2154 @opencatbox{Categories:}
2155 @category{Package graphs}
2156 @category{Package graphs - properties}
2161 @image{figures/graphs07,6cm}
2165 @deffn {Function} neighbors (@var{v}, @var{gr})
2166 Returns the list of neighbors of the vertex @var{v} in the graph @var{gr}.
2171 @c p : petersen_graph()$
2175 (%i1) load ("graphs")$
2176 (%i2) p : petersen_graph()$
2177 (%i3) neighbors(3, p);
2181 @opencatbox{Categories:}
2182 @category{Package graphs}
2183 @category{Package graphs - properties}
2188 @deffn {Function} odd_girth (@var{gr})
2189 Returns the length of the shortest odd cycle in the graph @var{gr}.
2194 @c g : graph_product(cycle_graph(4), cycle_graph(7))$
2199 (%i1) load ("graphs")$
2200 (%i2) g : graph_product(cycle_graph(4), cycle_graph(7))$
2207 @opencatbox{Categories:}
2208 @category{Package graphs}
2209 @category{Package graphs - properties}
2213 @anchor{out_neighbors}
2214 @deffn {Function} out_neighbors (@var{v}, @var{gr})
2215 Returns the list of out-neighbors of the vertex @var{v} in the directed
2221 @c p : path_digraph(3)$
2222 @c in_neighbors(2, p);
2223 @c out_neighbors(2, p);
2226 (%i1) load ("graphs")$
2227 (%i2) p : path_digraph(3)$
2228 (%i3) in_neighbors(2, p);
2230 (%i4) out_neighbors(2, p);
2234 @opencatbox{Categories:}
2235 @category{Package graphs}
2236 @category{Package graphs - properties}
2240 @anchor{planar_embedding}
2241 @deffn {Function} planar_embedding (@var{gr})
2243 Returns the list of facial walks in a planar embedding of @var{gr} and
2244 @code{false} if @var{gr} is not a planar graph.
2246 The graph @var{gr} must be biconnected.
2248 The algorithm used is the Demoucron's algorithm, which is a quadratic time
2254 @c planar_embedding(grid_graph(3,3));
2257 (%i1) load ("graphs")$
2258 (%i2) planar_embedding(grid_graph(3,3));
2259 (%o2) [[3, 6, 7, 8, 5, 2, 1, 0], [4, 3, 0, 1], [3, 4, 7, 6],
2260 [8, 7, 4, 5], [1, 2, 5, 4]]
2263 @opencatbox{Categories:}
2264 @category{Package graphs}
2265 @category{Package graphs - properties}
2269 @anchor{print_graph}
2270 @deffn {Function} print_graph (@var{gr})
2271 Prints some information about the graph @var{gr}.
2276 @c c5 : cycle_graph(5)$
2278 @c dc5 : cycle_digraph(5)$
2279 @c print_graph(dc5)$
2280 @c out_neighbors(0, dc5);
2283 (%i1) load ("graphs")$
2284 (%i2) c5 : cycle_graph(5)$
2285 (%i3) print_graph(c5)$
2286 Graph on 5 vertices with 5 edges.
2293 (%i4) dc5 : cycle_digraph(5)$
2294 (%i5) print_graph(dc5)$
2295 Digraph on 5 vertices with 5 arcs.
2302 (%i6) out_neighbors(0, dc5);
2306 @opencatbox{Categories:}
2307 @category{Package graphs}
2312 @deffn {Function} radius (@var{gr})
2313 Returns the radius of the graph @var{gr}.
2318 @c radius(dodecahedron_graph());
2321 (%i1) load ("graphs")$
2322 (%i2) radius(dodecahedron_graph());
2326 @opencatbox{Categories:}
2327 @category{Package graphs}
2328 @category{Package graphs - properties}
2332 @anchor{set_edge_weight}
2333 @deffn {Function} set_edge_weight (@var{e}, @var{w}, @var{gr})
2334 Assigns the weight @var{w} to the edge @var{e} in the graph @var{gr}.
2339 @c g : create_graph([1, 2], [[[1,2], 1.2]])$
2340 @c get_edge_weight([1,2], g);
2341 @c set_edge_weight([1,2], 2.1, g);
2342 @c get_edge_weight([1,2], g);
2345 (%i1) load ("graphs")$
2346 (%i2) g : create_graph([1, 2], [[[1,2], 1.2]])$
2347 (%i3) get_edge_weight([1,2], g);
2349 (%i4) set_edge_weight([1,2], 2.1, g);
2351 (%i5) get_edge_weight([1,2], g);
2355 @opencatbox{Categories:}
2356 @category{Package graphs}
2357 @category{Package graphs - properties}
2361 @anchor{set_vertex_label}
2362 @deffn {Function} set_vertex_label (@var{v}, @var{l}, @var{gr})
2363 Assigns the label @var{l} to the vertex @var{v} in the graph @var{gr}.
2365 A label may be any Maxima expression,
2366 and two or more vertices may have the same label.
2371 @c g : create_graph([[1, "One"], [2, "Two"]], [[1, 2]])$
2372 @c get_vertex_label(1, g);
2373 @c set_vertex_label(1, "oNE", g);
2374 @c get_vertex_label(1, g);
2375 @c h : create_graph([[11, x], [22, y], [33, x + y]], [[11, 33], [22, 33]]) $
2376 @c get_vertex_label (33, h);
2379 (%i1) load ("graphs")$
2380 (%i2) g : create_graph([[1, "One"], [2, "Two"]], [[1, 2]])$
2381 (%i3) get_vertex_label(1, g);
2383 (%i4) set_vertex_label(1, "oNE", g);
2385 (%i5) get_vertex_label(1, g);
2387 (%i6) h : create_graph([[11, x], [22, y], [33, x + y]], [[11, 33], [22, 33]]) $
2388 (%i7) get_vertex_label (33, h);
2392 @opencatbox{Categories:}
2393 @category{Package graphs}
2394 @category{Package graphs - properties}
2398 @anchor{shortest_path}
2399 @deffn {Function} shortest_path (@var{u}, @var{v}, @var{gr})
2400 Returns the shortest path from @var{u} to @var{v} in the graph @var{gr}.
2405 @c d : dodecahedron_graph()$
2406 @c path : shortest_path(0, 7, d);
2407 @c draw_graph(d, show_edges=vertices_to_path(path))$
2410 (%i1) load ("graphs")$
2411 (%i2) d : dodecahedron_graph()$
2412 (%i3) path : shortest_path(0, 7, d);
2413 (%o3) [0, 1, 19, 13, 7]
2414 (%i4) draw_graph(d, show_edges=vertices_to_path(path))$
2417 @opencatbox{Categories:}
2418 @category{Package graphs}
2419 @category{Package graphs - properties}
2424 @image{figures/graphs08,6cm}
2427 @anchor{shortest_weighted_path}
2428 @deffn {Function} shortest_weighted_path (@var{u}, @var{v}, @var{gr})
2429 Returns the length of the shortest weighted path and the shortest
2430 weighted path from @var{u} to @var{v} in the graph @var{gr}.
2432 The length of a weighted path is the sum of edge weights of edges in the
2433 path. If an edge has no weight, then it has a default weight 1.
2439 @c g: petersen_graph(20, 2)$
2440 @c for e in edges(g) do set_edge_weight(e, random(1.0), g)$
2441 @c shortest_weighted_path(0, 10, g);
2444 (%i1) load ("graphs")$
2445 (%i2) g: petersen_graph(20, 2)$
2446 (%i3) for e in edges(g) do set_edge_weight(e, random(1.0), g)$
2447 (%i4) shortest_weighted_path(0, 10, g);
2448 (%o4) [2.575143920268482, [0, 20, 38, 36, 34, 32, 30, 10]]
2451 @opencatbox{Categories:}
2452 @category{Package graphs}
2453 @category{Package graphs - properties}
2457 @anchor{strong_components}
2458 @deffn {Function} strong_components (@var{gr})
2459 Returns the strong components of a directed graph @var{gr}.
2464 @c t : random_tournament(4)$
2465 @c strong_components(t);
2466 @c vertex_out_degree(3, t);
2469 (%i1) load ("graphs")$
2470 (%i2) t : random_tournament(4)$
2471 (%i3) strong_components(t);
2472 (%o3) [[1], [0], [2], [3]]
2473 (%i4) vertex_out_degree(3, t);
2477 @opencatbox{Categories:}
2478 @category{Package graphs}
2479 @category{Package graphs - properties}
2483 @anchor{topological_sort}
2484 @deffn {Function} topological_sort (@var{dag})
2486 Returns a topological sorting of the vertices of a directed graph
2487 @var{dag} or an empty list if @var{dag} is not a directed acyclic graph.
2495 @c [1,2], [2,5], [5,3],
2496 @c [5,4], [3,4], [1,3]
2499 @c topological_sort(g);
2502 (%i1) load ("graphs")$
2503 (%i2) g:create_graph(
2506 [1,2], [2,5], [5,3],
2510 (%i3) topological_sort(g);
2511 (%o3) [1, 2, 5, 3, 4]
2514 @opencatbox{Categories:}
2515 @category{Package graphs}
2516 @category{Package graphs - properties}
2520 @anchor{vertex_connectivity}
2521 @deffn {Function} vertex_connectivity (@var{g})
2522 Returns the vertex connectivity of the graph @var{g}.
2524 See also @mrefdot{min_vertex_cut}
2526 @opencatbox{Categories:}
2527 @category{Package graphs}
2528 @category{Package graphs - properties}
2532 @anchor{vertex_degree}
2533 @deffn {Function} vertex_degree (@var{v}, @var{gr})
2534 Returns the degree of the vertex @var{v} in the graph @var{gr}.
2536 @opencatbox{Categories:}
2537 @category{Package graphs}
2538 @category{Package graphs - properties}
2542 @anchor{vertex_distance}
2543 @deffn {Function} vertex_distance (@var{u}, @var{v}, @var{gr})
2544 Returns the length of the shortest path between @var{u} and @var{v} in
2545 the (directed) graph @var{gr}.
2550 @c d : dodecahedron_graph()$
2551 @c vertex_distance(0, 7, d);
2552 @c shortest_path(0, 7, d);
2555 (%i1) load ("graphs")$
2556 (%i2) d : dodecahedron_graph()$
2557 (%i3) vertex_distance(0, 7, d);
2559 (%i4) shortest_path(0, 7, d);
2560 (%o4) [0, 1, 19, 13, 7]
2563 @opencatbox{Categories:}
2564 @category{Package graphs}
2565 @category{Package graphs - properties}
2569 @anchor{vertex_eccentricity}
2570 @deffn {Function} vertex_eccentricity (@var{v}, @var{gr})
2572 Returns the eccentricity of the vertex @var{v} in the graph @var{gr}.
2577 @c g:cycle_graph(7)$
2578 @c vertex_eccentricity(0, g);
2581 (%i1) load ("graphs")$
2582 (%i2) g:cycle_graph(7)$
2583 (%i3) vertex_eccentricity(0, g);
2587 @opencatbox{Categories:}
2588 @category{Package graphs}
2589 @category{Package graphs - properties}
2593 @anchor{vertex_in_degree}
2594 @deffn {Function} vertex_in_degree (@var{v}, @var{gr})
2595 Returns the in-degree of the vertex @var{v} in the directed graph @var{gr}.
2600 @c p5 : path_digraph(5)$
2602 @c vertex_in_degree(4, p5);
2603 @c in_neighbors(4, p5);
2606 (%i1) load ("graphs")$
2607 (%i2) p5 : path_digraph(5)$
2608 (%i3) print_graph(p5)$
2609 Digraph on 5 vertices with 4 arcs.
2616 (%i4) vertex_in_degree(4, p5);
2618 (%i5) in_neighbors(4, p5);
2622 @opencatbox{Categories:}
2623 @category{Package graphs}
2624 @category{Package graphs - properties}
2628 @anchor{vertex_out_degree}
2629 @deffn {Function} vertex_out_degree (@var{v}, @var{gr})
2630 Returns the out-degree of the vertex @var{v} in the directed graph @var{gr}.
2635 @c t : random_tournament(10)$
2636 @c vertex_out_degree(0, t);
2637 @c out_neighbors(0, t);
2640 (%i1) load ("graphs")$
2641 (%i2) t : random_tournament(10)$
2642 (%i3) vertex_out_degree(0, t);
2644 (%i4) out_neighbors(0, t);
2648 @opencatbox{Categories:}
2649 @category{Package graphs}
2650 @category{Package graphs - properties}
2655 @deffn {Function} vertices (@var{gr})
2656 Returns the list of vertices in the graph @var{gr}.
2661 @c vertices(complete_graph(4));
2664 (%i1) load ("graphs")$
2665 (%i2) vertices(complete_graph(4));
2669 @opencatbox{Categories:}
2670 @category{Package graphs}
2671 @category{Package graphs - properties}
2675 @anchor{vertex_coloring}
2676 @deffn {Function} vertex_coloring (@var{gr})
2677 Returns an optimal coloring of the vertices of the graph @var{gr}.
2679 The function returns the chromatic number and a list representing the
2680 coloring of the vertices of @var{gr}.
2685 @c p:petersen_graph()$
2686 @c vertex_coloring(p);
2689 (%i1) load ("graphs")$
2690 (%i2) p:petersen_graph()$
2691 (%i3) vertex_coloring(p);
2692 (%o3) [3, [[0, 2], [1, 3], [2, 2], [3, 3], [4, 1], [5, 3],
2693 [6, 1], [7, 1], [8, 2], [9, 2]]]
2696 @opencatbox{Categories:}
2697 @category{Package graphs}
2698 @category{Package graphs - properties}
2702 @anchor{wiener_index}
2703 @deffn {Function} wiener_index (@var{gr})
2704 Returns the Wiener index of the graph @var{gr}.
2709 @c wiener_index(dodecahedron_graph());
2712 (%i2) wiener_index(dodecahedron_graph());
2716 @opencatbox{Categories:}
2717 @category{Package graphs}
2718 @category{Package graphs - properties}
2722 @subsection Modifying graphs
2725 @deffn {Function} add_edge (@var{e}, @var{gr})
2726 Adds the edge @var{e} to the graph @var{gr}.
2731 @c p : path_graph(4)$
2733 @c add_edge([0,3], p);
2737 (%i1) load ("graphs")$
2738 (%i2) p : path_graph(4)$
2739 (%i3) neighbors(0, p);
2741 (%i4) add_edge([0,3], p);
2743 (%i5) neighbors(0, p);
2747 @opencatbox{Categories:}
2748 @category{Package graphs}
2749 @category{Package graphs - modifications}
2754 @deffn {Function} add_edges (@var{e_list}, @var{gr})
2755 Adds all edges in the list @var{e_list} to the graph @var{gr}.
2760 @c g : empty_graph(3)$
2761 @c add_edges([[0,1],[1,2]], g)$
2765 (%i1) load ("graphs")$
2766 (%i2) g : empty_graph(3)$
2767 (%i3) add_edges([[0,1],[1,2]], g)$
2768 (%i4) print_graph(g)$
2769 Graph on 3 vertices with 2 edges.
2776 @opencatbox{Categories:}
2777 @category{Package graphs}
2778 @category{Package graphs - modifications}
2783 @deffn {Function} add_vertex (@var{v}, @var{gr})
2784 Adds the vertex @var{v} to the graph @var{gr}.
2789 @c g : path_graph(2)$
2790 @c add_vertex(2, g)$
2794 (%i1) load ("graphs")$
2795 (%i2) g : path_graph(2)$
2796 (%i3) add_vertex(2, g)$
2797 (%i4) print_graph(g)$
2798 Graph on 3 vertices with 1 edges.
2805 @opencatbox{Categories:}
2806 @category{Package graphs}
2807 @category{Package graphs - modifications}
2811 @anchor{add_vertices}
2812 @deffn {Function} add_vertices (@var{v_list}, @var{gr})
2813 Adds all vertices in the list @var{v_list} to the graph @var{gr}.
2814 A vertex may be any integer,
2815 and @var{v_list} may contain vertices in any order.
2817 @opencatbox{Categories:}
2818 @category{Package graphs}
2819 @category{Package graphs - modifications}
2823 @anchor{connect_vertices}
2824 @deffn {Function} connect_vertices (@var{v_list}, @var{u_list}, @var{gr})
2825 Connects all vertices from the list @var{v_list} with the vertices in
2826 the list @var{u_list} in the graph @var{gr}.
2828 @var{v_list} and @var{u_list} can be single vertices or lists of
2834 @c g : empty_graph(4)$
2835 @c connect_vertices(0, [1,2,3], g)$
2839 (%i1) load ("graphs")$
2840 (%i2) g : empty_graph(4)$
2841 (%i3) connect_vertices(0, [1,2,3], g)$
2842 (%i4) print_graph(g)$
2843 Graph on 4 vertices with 3 edges.
2851 @opencatbox{Categories:}
2852 @category{Package graphs}
2853 @category{Package graphs - modifications}
2857 @anchor{contract_edge}
2858 @deffn {Function} contract_edge (@var{e}, @var{gr})
2859 Contracts the edge @var{e} in the graph @var{gr}.
2865 @c 8, [[0,3],[1,3],[2,3],[3,4],[4,5],[4,6],[4,7]])$
2867 @c contract_edge([3,4], g)$
2871 (%i1) load ("graphs")$
2872 (%i2) g: create_graph(
2873 8, [[0,3],[1,3],[2,3],[3,4],[4,5],[4,6],[4,7]])$
2874 (%i3) print_graph(g)$
2875 Graph on 8 vertices with 7 edges.
2885 (%i4) contract_edge([3,4], g)$
2886 (%i5) print_graph(g)$
2887 Graph on 7 vertices with 6 edges.
2898 @opencatbox{Categories:}
2899 @category{Package graphs}
2900 @category{Package graphs - modifications}
2904 @anchor{remove_edge}
2905 @deffn {Function} remove_edge (@var{e}, @var{gr})
2906 Removes the edge @var{e} from the graph @var{gr}.
2911 @c c3 : cycle_graph(3)$
2912 @c remove_edge([0,1], c3)$
2916 (%i1) load ("graphs")$
2917 (%i2) c3 : cycle_graph(3)$
2918 (%i3) remove_edge([0,1], c3)$
2919 (%i4) print_graph(c3)$
2920 Graph on 3 vertices with 2 edges.
2927 @opencatbox{Categories:}
2928 @category{Package graphs}
2929 @category{Package graphs - modifications}
2933 @anchor{remove_vertex}
2934 @deffn {Function} remove_vertex (@var{v}, @var{gr})
2935 Removes the vertex @var{v} from the graph @var{gr}.
2937 @opencatbox{Categories:}
2938 @category{Package graphs}
2942 @subsection Reading and writing to files
2944 @anchor{dimacs_export}
2945 @deffn {Function} dimacs_export @
2946 @fname{dimacs_export} (@var{gr}, @var{fl}) @
2947 @fname{dimacs_export} (@var{gr}, @var{fl}, @var{comment1}, ..., @var{commentn})
2949 Exports the graph into the file @var{fl} in the DIMACS format. Optional
2950 comments will be added to the top of the file.
2952 @opencatbox{Categories:}
2953 @category{Package graphs}
2954 @category{Package graphs - io}
2958 @anchor{dimacs_import}
2959 @deffn {Function} dimacs_import (@var{fl})
2961 Returns the graph from file @var{fl} in the DIMACS format.
2963 @opencatbox{Categories:}
2964 @category{Package graphs}
2965 @category{Package graphs - io}
2969 @anchor{graph6_decode}
2970 @deffn {Function} graph6_decode (@var{str})
2972 Returns the graph encoded in the graph6 format in the string @var{str}.
2974 @opencatbox{Categories:}
2975 @category{Package graphs}
2976 @category{Package graphs - io}
2980 @anchor{graph6_encode}
2981 @deffn {Function} graph6_encode (@var{gr})
2983 Returns a string which encodes the graph @var{gr} in the graph6 format.
2985 @opencatbox{Categories:}
2986 @category{Package graphs}
2987 @category{Package graphs - io}
2991 @anchor{graph6_export}
2992 @deffn {Function} graph6_export (@var{gr_list}, @var{fl})
2994 Exports graphs in the list @var{gr_list} to the file @var{fl} in the
2997 @opencatbox{Categories:}
2998 @category{Package graphs}
2999 @category{Package graphs - io}
3003 @anchor{graph6_import}
3004 @deffn {Function} graph6_import (@var{fl})
3006 Returns a list of graphs from the file @var{fl} in the graph6 format.
3008 @opencatbox{Categories:}
3009 @category{Package graphs}
3010 @category{Package graphs - io}
3014 @anchor{sparse6_decode}
3015 @deffn {Function} sparse6_decode (@var{str})
3017 Returns the graph encoded in the sparse6 format in the string @var{str}.
3019 @opencatbox{Categories:}
3020 @category{Package graphs}
3021 @category{Package graphs - io}
3025 @anchor{sparse6_encode}
3026 @deffn {Function} sparse6_encode (@var{gr})
3028 Returns a string which encodes the graph @var{gr} in the sparse6 format.
3030 @opencatbox{Categories:}
3031 @category{Package graphs}
3032 @category{Package graphs - io}
3036 @anchor{sparse6_export}
3037 @deffn {Function} sparse6_export (@var{gr_list}, @var{fl})
3039 Exports graphs in the list @var{gr_list} to the file @var{fl} in the
3042 @opencatbox{Categories:}
3043 @category{Package graphs}
3044 @category{Package graphs - io}
3048 @anchor{sparse6_import}
3049 @deffn {Function} sparse6_import (@var{fl})
3051 Returns a list of graphs from the file @var{fl} in the sparse6 format.
3053 @opencatbox{Categories:}
3054 @category{Package graphs}
3055 @category{Package graphs - io}
3059 @subsection Visualization
3062 @deffn {Function} draw_graph @
3063 @fname{draw_graph} (@var{graph}) @
3064 @fname{draw_graph} (@var{graph}, @var{option1}, ..., @var{optionk})
3066 Draws the graph using the @ref{Package draw} package.
3068 The algorithm used to position vertices is specified by the optional
3069 argument @var{program}. The default value is
3070 @code{program=spring_embedding}. @var{draw_graph} can also use the
3071 graphviz programs for positioning vertices, but graphviz must be
3072 installed separately.
3078 @c g:grid_graph(10,10)$
3079 @c m:max_matching(g)$
3081 @c spring_embedding_depth=100,
3082 @c show_edges=m, edge_type=dots,
3086 (%i1) load ("graphs")$
3087 (%i2) g:grid_graph(10,10)$
3088 (%i3) m:max_matching(g)$
3090 spring_embedding_depth=100,
3091 show_edges=m, edge_type=dots,
3096 @image{figures/graphs09,6cm}
3103 @c g:create_graph(16,
3105 @c [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3106 @c [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3107 @c [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3108 @c [10,14],[15,14],[13,14]
3110 @c t:minimum_spanning_tree(g)$
3113 @c show_edges=edges(t),
3114 @c show_edge_width=4,
3115 @c show_edge_color=green,
3116 @c vertex_type=filled_square,
3121 (%i1) load ("graphs")$
3122 (%i2) g:create_graph(16,
3124 [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3125 [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3126 [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3127 [10,14],[15,14],[13,14]
3129 (%i3) t:minimum_spanning_tree(g)$
3132 show_edges=edges(t),
3134 show_edge_color=green,
3135 vertex_type=filled_square,
3141 @image{figures/graphs10,6cm}
3148 @c g:create_graph(16,
3150 @c [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3151 @c [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3152 @c [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3153 @c [10,14],[15,14],[13,14]
3155 @c mi : max_independent_set(g)$
3158 @c show_vertices=mi,
3159 @c show_vertex_type=filled_up_triangle,
3160 @c show_vertex_size=2,
3168 (%i1) load ("graphs")$
3169 (%i2) g:create_graph(16,
3171 [0,1],[1,3],[2,3],[0,2],[3,4],[2,4],
3172 [5,6],[6,4],[4,7],[6,7],[7,8],[7,10],[7,11],
3173 [8,10],[11,10],[8,9],[11,12],[9,15],[12,13],
3174 [10,14],[15,14],[13,14]
3176 (%i3) mi : max_independent_set(g)$
3180 show_vertex_type=filled_up_triangle,
3190 @image{figures/graphs11,6cm}
3197 @c net : create_graph(
3200 @c [[0,1], 3], [[0,2], 2],
3201 @c [[1,3], 1], [[1,4], 3],
3202 @c [[2,3], 2], [[2,4], 2],
3203 @c [[4,5], 2], [[3,5], 2]
3209 @c show_weight=true,
3211 @c show_vertices=[0,5],
3212 @c show_vertex_type=filled_square,
3215 @c edge_color="dark-green",
3220 (%i1) load ("graphs")$
3221 (%i2) net : create_graph(
3224 [[0,1], 3], [[0,2], 2],
3225 [[1,3], 1], [[1,4], 3],
3226 [[2,3], 2], [[2,4], 2],
3227 [[4,5], 2], [[3,5], 2]
3235 show_vertices=[0,5],
3236 show_vertex_type=filled_square,
3239 edge_color="dark-green",
3245 @image{figures/graphs12,6cm}
3252 @c g: petersen_graph(20, 2);
3253 @c draw_graph(g, redraw=true, program=planar_embedding);
3256 (%i1) load("graphs")$
3257 (%i2) g: petersen_graph(20, 2);
3259 (%i3) draw_graph(g, redraw=true, program=planar_embedding);
3264 @image{figures/graphs14,6cm}
3271 @c t: tutte_graph();
3272 @c draw_graph(t, redraw=true,
3273 @c fixed_vertices=[1,2,3,4,5,6,7,8,9]);
3276 (%i1) load("graphs")$
3277 (%i2) t: tutte_graph();
3279 (%i3) draw_graph(t, redraw=true,
3280 fixed_vertices=[1,2,3,4,5,6,7,8,9]);
3285 @image{figures/graphs15,6cm}
3288 @opencatbox{Categories:}
3289 @category{Package graphs}
3293 @anchor{draw_graph_program}
3294 @defvr {Option variable} draw_graph_program
3295 Default value: @var{spring_embedding}
3297 The default value for the program used to position vertices in
3298 @code{draw_graph} program.
3300 @opencatbox{Categories:}
3301 @category{Package graphs}
3302 @category{Package graphs - draw_graphs options}
3307 @defvr {draw_graph option} show_id
3308 Default value: @var{false}
3310 If @var{true} then ids of the vertices are displayed.
3312 @opencatbox{Categories:}
3313 @category{Package graphs}
3314 @category{Package graphs - draw_graphs options}
3319 @defvr {draw_graph option} show_label
3320 Default value: @var{false}
3322 If @var{true} then labels of the vertices are displayed.
3324 @opencatbox{Categories:}
3325 @category{Package graphs}
3326 @category{Package graphs - draw_graphs options}
3330 @anchor{label_alignment_graphs}
3331 @defvr {draw_graph option} label_alignment
3332 Default value: @var{center}
3334 Determines how to align the labels/ids of the vertices. Can
3335 be @code{left}, @code{center} or @code{right}.
3337 @opencatbox{Categories:}
3338 @category{Package graphs}
3339 @category{Package graphs - draw_graphs options}
3343 @anchor{show_weight }
3344 @defvr {draw_graph option} show_weight
3345 Default value: @var{false}
3347 If @var{true} then weights of the edges are displayed.
3349 @opencatbox{Categories:}
3350 @category{Package graphs}
3351 @category{Package graphs - draw_graphs options}
3355 @anchor{vertex_type}
3356 @defvr {draw_graph option} vertex_type
3357 Default value: @var{circle}
3359 Defines how vertices are displayed. See the @var{point_type} option for
3360 the @code{draw} package for possible values.
3362 @opencatbox{Categories:}
3363 @category{Package graphs}
3364 @category{Package graphs - draw_graphs options}
3368 @anchor{vertex_size}
3369 @defvr {draw_graph option} vertex_size
3370 The size of vertices.
3372 @opencatbox{Categories:}
3373 @category{Package graphs}
3374 @category{Package graphs - draw_graphs options}
3378 @anchor{vertex_color }
3379 @defvr {draw_graph option} vertex_color
3380 The color used for displaying vertices.
3382 @opencatbox{Categories:}
3383 @category{Package graphs}
3384 @category{Package graphs - draw_graphs options}
3388 @anchor{show_vertices}
3389 @defvr {draw_graph option} show_vertices
3392 Display selected vertices in the using a different color.
3394 @opencatbox{Categories:}
3395 @category{Package graphs}
3396 @category{Package graphs - draw_graphs options}
3400 @anchor{show_vertex_type}
3401 @defvr {draw_graph option} show_vertex_type
3402 Defines how vertices specified in @var{show_vertices} are displayed.
3403 See the @var{point_type} option for the @code{draw} package for possible
3406 @opencatbox{Categories:}
3407 @category{Package graphs}
3408 @category{Package graphs - draw_graphs options}
3412 @anchor{show_vertex_size}
3413 @defvr {draw_graph option} show_vertex_size
3414 The size of vertices in @var{show_vertices}.
3416 @opencatbox{Categories:}
3417 @category{Package graphs}
3418 @category{Package graphs - draw_graphs options}
3422 @anchor{show_vertex_color }
3423 @defvr {draw_graph option} show_vertex_color
3424 The color used for displaying vertices in the @var{show_vertices} list.
3426 @opencatbox{Categories:}
3427 @category{Package graphs}
3428 @category{Package graphs - draw_graphs options}
3432 @anchor{vertex_partition}
3433 @defvr {draw_graph option} vertex_partition
3436 A partition @code{[[v1,v2,...],...,[vk,...,vn]]} of the vertices of the
3437 graph. The vertices of each list in the partition will be drawn in a
3440 @opencatbox{Categories:}
3441 @category{Package graphs}
3442 @category{Package graphs - draw_graphs options}
3446 @anchor{vertex_coloring_variable}
3447 @defvr {draw_graph option} vertex_coloring
3448 Specifies coloring of the vertices. The coloring @var{col} must be
3449 specified in the format as returned by @var{vertex_coloring}.
3451 @opencatbox{Categories:}
3452 @category{Package graphs}
3453 @category{Package graphs - draw_graphs options}
3457 @anchor{edge_color }
3458 @defvr {draw_graph option} edge_color
3459 The color used for displaying edges.
3461 @opencatbox{Categories:}
3462 @category{Package graphs}
3463 @category{Package graphs - draw_graphs options}
3468 @defvr {draw_graph option} edge_width
3471 @opencatbox{Categories:}
3472 @category{Package graphs}
3473 @category{Package graphs - draw_graphs options}
3478 @defvr {draw_graph option} edge_type
3479 Defines how edges are displayed. See the @var{line_type} option for the
3480 @code{draw} package.
3482 @opencatbox{Categories:}
3483 @category{Package graphs}
3484 @category{Package graphs - draw_graphs options}
3489 @defvr {draw_graph option} show_edges
3490 Display edges specified in the list @var{e_list} using a different
3493 @opencatbox{Categories:}
3494 @category{Package graphs}
3495 @category{Package graphs - draw_graphs options}
3499 @anchor{show_edge_color}
3500 @defvr {draw_graph option} show_edge_color
3501 The color used for displaying edges in the @var{show_edges} list.
3503 @opencatbox{Categories:}
3504 @category{Package graphs}
3505 @category{Package graphs - draw_graphs options}
3509 @anchor{show_edge_width}
3510 @defvr {draw_graph option} show_edge_width
3511 The width of edges in @var{show_edges}.
3513 @opencatbox{Categories:}
3514 @category{Package graphs}
3515 @category{Package graphs - draw_graphs options}
3519 @anchor{show_edge_type}
3520 @defvr {draw_graph option} show_edge_type
3521 Defines how edges in @var{show_edges} are displayed. See the
3522 @var{line_type} option for the @code{draw} package.
3524 @opencatbox{Categories:}
3525 @category{Package graphs}
3526 @category{Package graphs - draw_graphs options}
3530 @anchor{edge_partition}
3531 @defvr {draw_graph option} edge_partition
3532 A partition @code{[[e1,e2,...],...,[ek,...,em]]} of edges of the
3533 graph. The edges of each list in the partition will be drawn using a
3536 @opencatbox{Categories:}
3537 @category{Package graphs}
3538 @category{Package graphs - draw_graphs options}
3542 @anchor{edge_coloring_variable}
3543 @defvr {draw_graph option} edge_coloring
3544 The coloring of edges. The coloring must be specified in the
3545 format as returned by the function @var{edge_coloring}.
3547 @opencatbox{Categories:}
3548 @category{Package graphs}
3549 @category{Package graphs - draw_graphs options}
3554 @defvr {draw_graph option} redraw
3555 Default value: @var{false}
3557 If @code{true}, vertex positions are recomputed even if the positions
3558 have been saved from a previous drawing of the graph.
3560 @opencatbox{Categories:}
3561 @category{Package graphs}
3562 @category{Package graphs - draw_graphs options}
3566 @anchor{head_angle_graphs}
3567 @defvr {draw_graph option} head_angle
3570 The angle for the arrows displayed on arcs (in directed graphs).
3572 @opencatbox{Categories:}
3573 @category{Package graphs}
3574 @category{Package graphs - draw_graphs options}
3578 @anchor{head_length_graphs}
3579 @defvr {draw_graph option} head_length
3582 The length for the arrows displayed on arcs (in directed graphs).
3584 @opencatbox{Categories:}
3585 @category{Package graphs}
3586 @category{Package graphs - draw_graphs options}
3590 @anchor{spring_embedding_depth}
3591 @defvr {draw_graph option} spring_embedding_depth
3594 The number of iterations in the spring embedding graph drawing
3597 @opencatbox{Categories:}
3598 @category{Package graphs}
3599 @category{Package graphs - draw_graphs options}
3603 @anchor{terminal_graphs}
3604 @defvr {draw_graph option} terminal
3605 The terminal used for drawing (see the @var{terminal} option in the
3606 @code{draw} package).
3608 @opencatbox{Categories:}
3609 @category{Package graphs}
3610 @category{Package graphs - draw_graphs options}
3614 @anchor{file_name_graphs}
3615 @defvr {draw_graph option} file_name
3616 The filename of the drawing if terminal is not screen.
3618 @opencatbox{Categories:}
3619 @category{Package graphs}
3620 @category{Package graphs - draw_graphs options}
3625 @defvr {draw_graph option} program
3626 Defines the program used for positioning vertices of the graph. Can be
3627 one of the graphviz programs (dot, neato, twopi, circ, fdp),
3628 @var{circular}, @var{spring_embedding} or
3629 @var{planar_embedding}. @var{planar_embedding} is only available for
3630 2-connected planar graphs. When @code{program=spring_embedding}, a set
3631 of vertices with fixed position can be specified with the
3632 @var{fixed_vertices} option.
3634 @opencatbox{Categories:}
3635 @category{Package graphs}
3636 @category{Package graphs - draw_graphs options}
3640 @anchor{fixed_vertices}
3641 @defvr {draw_graph option} fixed_vertices
3642 Specifies a list of vertices which will have positions fixed along a regular polygon.
3643 Can be used when @code{program=spring_embedding}.
3645 @opencatbox{Categories:}
3646 @category{Package graphs}
3647 @category{Package graphs - draw_graphs options}
3651 @anchor{vertices_to_path}
3652 @deffn {Function} vertices_to_path (@var{v_list})
3653 Converts a list @var{v_list} of vertices to a list of edges of the path
3654 defined by @var{v_list}.
3656 @opencatbox{Categories:}
3657 @category{Package graphs}
3661 @anchor{vertices_to_cycle}
3662 @deffn {Function} vertices_to_cycle (@var{v_list})
3663 Converts a list @var{v_list} of vertices to a list of edges of the cycle
3664 defined by @var{v_list}.
3666 @opencatbox{Categories:}
3667 @category{Package graphs}