-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertex.py
More file actions
95 lines (77 loc) · 2.63 KB
/
Copy pathvertex.py
File metadata and controls
95 lines (77 loc) · 2.63 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
CSCI-603: Graphs
Author: Sean Strout @ RIT CS
An implementation of a vertex as part of a graph.
Code taken from the online textbook and modified:
http://interactivepython.org/runestone/static/pythonds/Graphs/Implementation.html
"""
class Vertex:
"""
An individual vertex in the graph.
:slots: id: The identifier for this vertex (user defined, typically
a string)
:slots: connectedTo: A dictionary of adjacent neighbors, where the key is
the neighbor (Vertex), and the value is the edge cost (int)
"""
__slots__ = 'id', 'connectedTo'
def __init__(self, key):
"""
Initialize a vertex
:param key: The identifier for this vertex
:return: None
"""
self.id = key
self.connectedTo = {}
def addNeighbor(self, nbr, weight=0):
"""
Connect this vertex to a neighbor with a given weight (default is 0).
:param nbr (Vertex): The neighbor vertex
:param weight (int): The edge cost
:return: None
"""
self.connectedTo[nbr] = weight
def __str__(self):
"""
Return a string representation of the vertex and its direct neighbors:
vertex-id connectedTo [neighbor-1-id, neighbor-2-id, ...]
:return: The string
"""
return str(self.id) + ' connectedTo: ' + str([str(x.id) for x in self.connectedTo])
def getConnections(self):
"""
Get the neighbor vertices.
:return: A list of Vertex neighbors
"""
return self.connectedTo.keys()
def getWeight(self, nbr):
"""
Get the edge cost to a neighbor.
:param nbr (Vertex): The neighbor vertex
:return: The weight (int)
"""
return self.connectedTo[nbr]
def testVertex():
"""
A test function for the Vertex class.
:return: None
"""
vertexA = Vertex('A')
vertexB = Vertex('B')
vertexC = Vertex('C')
vertexD = Vertex('D')
vertexA.addNeighbor(vertexB, 3)
vertexA.addNeighbor(vertexC, 1)
vertexB.addNeighbor(vertexA, 4)
vertexB.addNeighbor(vertexC, 2)
vertexC.addNeighbor(vertexD, 5)
# test __str__()
print(vertexA)
print(vertexD)
# test getWeight()
print('A -> B weight (3):', vertexA.getWeight(vertexB))
print('C -> D weight (5):', vertexC.getWeight(vertexD))
# test getConnections():
print("B's neighbors ():", [vertex.id for vertex in vertexB.getConnections()])
print("D's neighbors ():", list(vertexD.getConnections()))
if __name__ == '__main__':
testVertex()