Add the vertices and/or edges. Parameters can be single vertex or list of vertices. If no second parameter given, assume vertex addition only. >>> g = Graph(VertexType=WVertex) >>> g.add(1) #single vertex addition >>> g.add(1) #adding ex
(self, head, tail=[], edge_value=VertexCommon.EDGEVALUE)
| 393 | super(Graph, self).update(other, default, collision) |
| 394 | |
| 395 | def add(self, head, tail=[], edge_value=VertexCommon.EDGEVALUE): |
| 396 | """Add the vertices and/or edges. |
| 397 | Parameters can be single vertex or list of vertices. |
| 398 | If no second parameter given, assume vertex addition only. |
| 399 | |
| 400 | >>> g = Graph(VertexType=WVertex) |
| 401 | >>> g.add(1) #single vertex addition |
| 402 | >>> g.add(1) #adding existing vertex is ignored |
| 403 | >>> g.add([2, 3, 4]) #multiple vertex addition |
| 404 | >>> g.add([2]) #list containing only one vertex is allowed |
| 405 | >>> print g |
| 406 | {1: {}, 2: {}, 3: {}, 4: {}} |
| 407 | |
| 408 | If second parameter given, then edge addition is performed. |
| 409 | Vertices are added as necessary. An optional edge value |
| 410 | is accepted as a third parameter. |
| 411 | |
| 412 | >>> g.add(2, 1) #edge from vertex 2 to vertex 1 |
| 413 | >>> g.add(1, 5, 100) #edge from 1 to new vertex 5 with weight 100 |
| 414 | >>> g.add(1, 5, 90) #adding existing edge, edge value overwritten |
| 415 | >>> g.add(3, 3, 2) #loops are allowed |
| 416 | >>> g.add(3, 3) #edge weight overwritten by default if not specified |
| 417 | >>> print g |
| 418 | {1: {5: 90}, 2: {1: 1}, 3: {3: 1}, 4: {}, 5: {}} |
| 419 | |
| 420 | Vertex lists allowed on either parameter for multiple edge addition. |
| 421 | |
| 422 | >>> g.clear() #remove all vertices (and edges) |
| 423 | >>> g.add(1, [0, 2]) #add edges (1, 0) and (1, 2) |
| 424 | >>> g.add(1, [1]) |
| 425 | >>> print g |
| 426 | {0: {}, 1: {0: 1, 1: 1, 2: 1}, 2: {}} |
| 427 | >>> g.add(range(3), range(3)) #fully-connected 3-vertex graph |
| 428 | >>> print g |
| 429 | {0: {0: 1, 1: 1, 2: 1}, 1: {0: 1, 1: 1, 2: 1}, 2: {0: 1, 1: 1, 2: 1}} |
| 430 | """ |
| 431 | #XXX if no edge_value given, then value should not be overwritten |
| 432 | if not isinstance(tail, list): tail = [tail] |
| 433 | try: #single head addition |
| 434 | self[head].add(tail, edge_value) |
| 435 | except TypeError, error: #multiple head addition |
| 436 | if not isinstance(head, list): raise TypeError(error) |
| 437 | for h in head: #XXX will add same tails multiple times |
| 438 | self[h].add(tail, edge_value) |
| 439 | |
| 440 | def discard(self, head, tail=[]): |
| 441 | """Remove vertices and/or edges. Parameters can be single vertex or list of vertices. |
no outgoing calls
no test coverage detected