Package graphadt :: Module graphtypes :: Class WeightedGraph
[hide private]
[frames] | no frames]

Class WeightedGraph

source code

object --+    
         |    
     Graph --+
             |
            WeightedGraph
Known Subclasses:

This abstract class manages the weights of edges of a graph.

A weighted graph wraps an inner representation, and provides a few more functions for manipulating weights. WeightedGraph is only a mixin class.

One of the key invariants is that if this class returns True for self.isArc(i, j), then self.getArcWeight(i, j) must return a legitimate weight, or zero if none was set.

However, the output of self.getArcWeight(i, j), called with arbitrary arguments, is not guaranteed.

Note: a weighted graph is an abstract class, and intended to be inherited from.

Nested Classes [hide private]

Inherited from Graph: __metaclass__

Instance Methods [hide private]
 
__init__(self)
Creates a mapping dictionary of edge weights, and assigns all edges a default weight of 0
source code
 
setArcWeight(self, i, j, w)
Sets the weight of arc (i, j) to w.
source code
 
getArcWeight(self, i, j)
Returns the weight of arc (i, j), or zero if no weight was set.
source code
list
getNeighbourWeights(self, i)
Returns a list of weights of the neighbours of i.
source code
 
_removeArc(self, i, j)
Removes a given arc from this graph, and cleans up the weights.
source code
 
removeVertex(self, v)
Removes a given vertex from this graph, and cleans up the weights.
source code

Inherited from Graph: __repr__, addEdge, addVertices, defaultConnector, degree, inDegree, isEdge, neighbors, neighbours, order, removeEdge, size

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
 
copy(cls, g)
Extends Graph.copy to copy weighted graphs.
source code

Inherited from Graph: read

Class Variables [hide private]

Inherited from Graph: __abstractmethods__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self)
(Constructor)

source code 

Creates a mapping dictionary of edge weights, and assigns all edges a default weight of 0

Note: You MUST call the other Graph class's constructor _before_ calling this one.

Parameters:
  • representation - the state of the graph to be copied
Overrides: object.__init__

copy(cls, g)
Class Method

source code 

Extends Graph.copy to copy weighted graphs.

This method creates a copy graph g. The copy is of type cls. If g is a weighted graph, the weights are copied as well.

Parameters:
  • cls - a concrete subclass of graph
  • g - the graph to be copied
Overrides: Graph.copy

setArcWeight(self, i, j, w)

source code 

Sets the weight of arc (i, j) to w.

Parameters:
  • i (integer) - The label of a vertex in this graph
  • j (integer) - The label of a vertex in this graph
  • w - the weight of arc i,j
Raises:
  • ValueError - if i or j are not valid vertices in this graph
  • ValueError - if arc (i, j) does not exist

getArcWeight(self, i, j)

source code 

Returns the weight of arc (i, j), or zero if no weight was set.

Parameters:
  • i (integer) - The label of a vertex in this graph
  • j (integer) - The label of a vertex in this graph
Returns:
the weight of arc i, j
Raises:
  • ValueError - if i or j are not valid vertices in this graph
  • ValueError - if arc (i, j) does not exist

getNeighbourWeights(self, i)

source code 

Returns a list of weights of the neighbours of i.

This method returns a list of the weights of the arcs out of i, in a direct correspondence to the list returned by neighbours(i). In other words, for every element at index j in neighbours, the weight of the arc from i to j is the jth element of getNeighbourWeights,

Parameters:
  • i (integer) - The label of a vertex in this graph
Returns: list
the weights of all neighbours of i
Raises:
  • ValueError - if i is not a valid vertex in this graph

_removeArc(self, i, j)

source code 

Removes a given arc from this graph, and cleans up the weights.

Parameters:
  • i (integer) - vertex from which the arc leads
  • j (integer) - vertex from which the arc leads

removeVertex(self, v)

source code 

Removes a given vertex from this graph, and cleans up the weights.

Parameters:
  • v (integer) - label of a vertex
Raises:
  • ValueError - if v does not represent a vertex in this graph.
Overrides: Graph.removeVertex