-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from LeeBergstrand/develop
Develop
- Loading branch information
Showing
16 changed files
with
386 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
#!/usr/bin/env python | ||
#!/usr/bin/env python | ||
|
||
# ---------------------------------------------------------------------------------------- | ||
# From: http://interactivepython.org/courselib/static/pythonds/Graphs/graphintro.html#graphs | ||
# Description: Lite Weight Script For Creating Graph Objects. | ||
# Description: Light Weight Script For Creating Graph Objects. | ||
# ---------------------------------------------------------------------------------------- | ||
|
||
|
||
class Vertex: | ||
def __init__(self, key): | ||
self.id = key | ||
self.connectedTo = {} | ||
def __init__(self, key): | ||
self.id = key | ||
self.connectedTo = {} | ||
|
||
def addNeighbor(self, nbr, weight=0): | ||
self.connectedTo[nbr] = weight | ||
def addNeighbor(self, nbr, weight=0): | ||
self.connectedTo[nbr] = weight | ||
|
||
def __str__(self): | ||
return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo]) | ||
def __str__(self): | ||
return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo]) | ||
|
||
def getConnections(self): | ||
return self.connectedTo.keys() | ||
def getConnections(self): | ||
return self.connectedTo.keys() | ||
|
||
def getId(self): | ||
return self.id | ||
def getId(self): | ||
return self.id | ||
|
||
def getWeight(self, nbr): | ||
return self.connectedTo[nbr] | ||
def getWeight(self, nbr): | ||
return self.connectedTo[nbr] | ||
|
||
|
||
class Graph: | ||
def __init__(self): | ||
self.vertList = {} | ||
self.numVertices = 0 | ||
|
||
def addVertex(self, key): | ||
self.numVertices = self.numVertices + 1 | ||
newVertex = Vertex(key) | ||
self.vertList[key] = newVertex | ||
return newVertex | ||
|
||
def getVertex(self, n): | ||
if n in self.vertList: | ||
return self.vertList[n] | ||
else: | ||
return None | ||
|
||
def __contains__(self, n): | ||
return n in self.vertList | ||
|
||
def addEdge(self, f, t, cost=0): | ||
if f not in self.vertList: | ||
nv = self.addVertex(f) | ||
if t not in self.vertList: | ||
nv = self.addVertex(t) | ||
self.vertList[f].addNeighbor(self.vertList[t], cost) | ||
|
||
def getVertices(self): | ||
return self.vertList.keys() | ||
|
||
def __iter__(self): | ||
return iter(self.vertList.values()) | ||
def __init__(self): | ||
self.vertList = {} | ||
self.numVertices = 0 | ||
|
||
def addVertex(self, key): | ||
self.numVertices = self.numVertices + 1 | ||
newVertex = Vertex(key) | ||
self.vertList[key] = newVertex | ||
return newVertex | ||
|
||
def getVertex(self, n): | ||
if n in self.vertList: | ||
return self.vertList[n] | ||
else: | ||
return None | ||
|
||
def __contains__(self, n): | ||
return n in self.vertList | ||
|
||
def addEdge(self, f, t, cost=0): | ||
if f not in self.vertList: | ||
self.addVertex(f) | ||
if t not in self.vertList: | ||
self.addVertex(t) | ||
self.vertList[f].addNeighbor(self.vertList[t], cost) | ||
|
||
def getVertices(self): | ||
return self.vertList.keys() | ||
|
||
def __iter__(self): | ||
return iter(self.vertList.values()) |
Oops, something went wrong.