11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.6 Interfaces and Implementations • 123trueTherefore, the generic procedure vdeg works equally well with it.> vdeg( g2, a );2, 0> vdeg( g2, b );1, 1> vdeg( g2, c );0, 2Note: The full source code for these procedures is available in thesamples/graphs directory of your Maple installation.Generic Computation of Adjacency Matrices Another example of ageneric procedure over the Graph interface is the following routine forcomputing the adjacency matrix of a graph.> AdjacencyMatrix := proc( g::Graph )> local a, # the adjacency matrix; returned> n, # the order of the graph g> V, # the vertex set of the graph> E, # the edge set of the graph> row, # row index for matrix> col, # column index for matrix> e; # induction variable for loop>> n := g:-order();> a := Matrix( n, n, ’storage’ = ’sparse’ );> V := sort( convert( g:-vertices(), ’list’ ) );> E := g:-edges();> for e in E do> if not member( e:-source(), V, ’row’ )> or not member( e:-target(), V, ’col’ ) then> error "inconsistent graph structure detected"> end if;> a[ row, col ] := 1> end do;> a> end proc:> AdjacencyMatrix( g1 );

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

Saved successfully!

Ooh no, something went wrong!