2 GRAPHS - graph theory package for Maxima
3 Copyright (C) 2007-2011 Andrej Vodopivec <andrej.vodopivec@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 dimacs_export(gr, file, [comments]) := block(
23 str: apply(dimacs_export_string, cons(gr, comments)),
28 dimacs_export_string(gr, [comments]) := block(
29 [str:"", names:hash_table(), i:1],
30 if not(is_graph_or_digraph(gr)) then error("dimacs_export: first argument is not a graph:", gr),
31 for v in sort(vertices(gr)) do (
32 set_hash(v, names, i),
34 str : sconcat(str, printf(false, "c graph exported from MAXIMA~%")),
35 for c in comments do (
36 str: sconcat(str, printf(false, "c ~a~%", c))),
38 str: sconcat(str, printf(false, "p edge ~a ~a~%", graph_order(gr), graph_size(gr)))
40 str: sconcat(str, printf(false, "p arc ~a ~a~%", graph_order(gr), graph_size(gr))),
41 for v in vertices(gr) do (
42 if get_vertex_label(v, gr)#false then
43 str: sconcat(str, printf(false, "n ~a ~a~%", get_hash(v, names), get_vertex_label(v, gr)))),
44 for e in edges(gr) do block(
45 [u:get_hash(first(e), names), v:get_hash(second(e), names), u1, v1],
47 [u1,v1] : [min(u,v), max(u,v)]
50 if get_edge_weight(e, gr)=1 then
51 str: sconcat(str, printf(false, "e ~a ~a~%", u1, v1))
53 str: sconcat(str, printf(false, "e ~a ~a ~a~%", u1, v1, get_edge_weight(e, gr)))),
56 dimacs_import(file, [mv]) := block(
57 [n, fl, line, elist:[], t, e, v_labels:[], g, dir:false],
58 if length(mv)=1 and integerp(mv[1]) then mv:mv[1]
64 if length(t) > 0 then (
66 n:read_string(third(t)),
68 if dir="edges" or dir="edge" then dir:false
70 else if t[1]="n" and length(t)>=3 then
71 v_labels:cons([read_string(second(t))-mv, third(t)], v_labels)
72 else if t[1]="e" then (
73 e : map(lambda([u], read_string(u)-mv), [second(t), third(t)]),
77 elist:cons([e, read_string(fourth(t))], elist))
78 else if t[1]#"c" then error("import_dimacs: wrong file format")),
81 if not(integerp(n)) then error("import_dimacs: wrong file format"),
82 g : create_graph(n,elist,'directed=dir),
83 for v in v_labels do set_vertex_label(v[1], v[2], g),