11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

120 • Chapter 3: <strong>Programming</strong> with Modules> else> 0, 0> end if> end proc:You can write this procedure even though, at this point, you do not knowthe graph implementation. This capability is very important when youare designing a large software system.Edge Object Representation Assume that edges are represented as objectsthat implement the interface ‘module‘( source, target ). Theinterface provides methods for extracting the source and target verticesfrom an edge. Writing a constructor Edge for edges is easy.> Edge := proc( src, targ )> module()> local the_source, the_target;> export source, target, setsource, settarget;> the_source := src;> the_target := targ;> source := () -> the_source;> target := () -> the_target;> setsource := proc( v )> the_source := v> end proc;> settarget := proc( v )> the_target := v> end proc;> end module> end proc:First Graph Constructor At first, you might choose to adopt a graphrepresentation that is simple to implement. Here is a graph constructorthat produces graphs represented by storing the vertex and edge setsexplicitly as part of the state of a module.> Graph1 := proc()> local vertex_set, edge_set;> description "graph constructor";>> edge_set := { args };> if not andmap( type, edge_set, ’[ anything, anything ]’ )> then> error "graph must be specified by a sequence of edges"> end if;> if not andmap( edge -> evalb( nops ( edge )= 2), edge_set )> then> error "each edge must be specified "> "as a [ source, target ] pair"> end if;> vertex_set := map( op, edge_set );

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!