-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph.py
70 lines (52 loc) · 2.36 KB
/
Graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
### Matt Caldwell
### 2004
class Graph:
""" Class representing a generic directed graph data structure. """
def __init__(self):
# Define a dictionary with vertices as the keys and adjacent vertices
# as the values.
self._adjacentVertices = {}
def __repr__(self):
# Allows you to specify "print <some_graph>" and prints the graph
# as a string of vertices.
return str(self._adjacentVertices)
def __str__(self):
# Allows you to specify "str(<some_graph>)" and returns a string
# of vertices.
return str(self._adjacentVertices)
def add(self, vertex, adjacent=None):
# adds transition from vertex to adjacent
# we should only omit the adjacent parameter if the vertex has not yet
# been added. It is no use adding the same vertex again, without specifying
# any adjacent vertices. Check for this:
if not adjacent:
if self._adjacentVertices.get(vertex, None) != None:
exc = "Ambiguous vertex. Adjacent to 'None', yet has already been added\n"
raise exc
# if 'vertex' has already been added, simply add the adjacent vertex
# to the adjacency list for 'vertex'.
if self._adjacentVertices.get(vertex, False):
self._adjacentVertices[vertex] = self._adjacentVertices[vertex] + [adjacent]
# if 'vertex' has not yet been added, and an adjacent vertex was specified,
# then create a new key (vertex) in the dictionary, and initiailize it with
# a list containing the adjacent vertex only. If an adjacent vertex was not
# specified, then initialize the new key with an empty list.
else:
if adjacent:
self._adjacentVertices[vertex] = [adjacent]
else:
self._adjacentVertices[vertex] = []
# For testing purposes only
if __name__ == "__main__":
# Test graph implementation
graph = Graph()
graph.add('a','b')
graph.add('a','c')
graph.add('c')
graph.add('d','e')
# Demonstrate support for weighted graphs as well
weightedGraph = Graph()
weightedGraph.add('a',('b',0.2))
weightedGraph.add('a',('c',0.4))
print graph
print weightedGraph