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 • 119This diagram represents a graph with vertex set V = {a, b, c, d, e, f}, andedge set E = {(a, b), (a, c), (b, d), (c, f), (f, d), (b, e), (d, e), (c, b), (c, d)}.Software Models Graphs can be represented in a variety of ways. Thechoice of storage mechanism depends on the expected applications of thegraph. Three possibilities for representing graphs in software are:1. Store the set V of vertices and the set E of edges explicitly.2. Store the “adjacency matrix” of the graph.3. Store, for each vertex of the graph, the set of all its neighbours.(The adjacency matrix is a square matrix whose rows and columns areindexed by the vertices of the graph; the (i, j)-entry is equal to 1 if thereis an edge from i to j, and is equal to 0 otherwise.) You can write softwarethat manipulates a graph independent of representation.Designing a Graph Interface To demonstrate how this can be achieved,consider graphs as objects that implement the following methods:verticesedgesaddedgeordersizereturns the set of vertices of the graphreturns the set of edges of the graphallows one to add a new edge to a graphreturns the number of vertices of the graphreturns the number of edges of the graphThen, represent the abstract interface of a graph by a Maple type.> ‘type/Graph‘ := ’‘module‘( vertices, edges, addedge, order,> size )’:An object implements the graph interface if it is of type Graph.Computing Vertex Degrees Generically If an object implements thisinterface, then you can write generic code based on that interface. Forexample, you can write the following procedure to compute the in-degreeand out-degree of a vertex of a given graph.> vdeg := proc( G::Graph, v )> local vs, vt;> description "compute the in- and out-degrees "> "of a vertex in a graph";> if member( v, G:-vertices() ) then> vs := select( e -> evalb( v = e:-source() ),> G:-edges() );> vt := select( e -> evalb( v = e:-target() ),> G:-edges() );> nops( vs ), nops( vt )

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

Saved successfully!

Ooh no, something went wrong!