# Creating a City Graph with Nodes & Edges

• Comp Sci
• fishturtle1

#### fishturtle1

Homework Statement
This is code from a lecture but it won't work for me. My question is about the buildCityGraph function. It seems like the for loop doesn't work and I don't know why. Also i'm confused why we are adding edges the way we do. Why?
Relevant Equations
.
Edit: Trying to fix the formatting...

Python:
class Node(object):
def __init__(self, name):
self.name = str(name)

def get_name(self):
return self.name

def __str__(self):
return self.name

class Edge(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest

def getSource(self):
return self.src

def getDestination(self):
return self.dest

def __str__(self):
return self.src.get_name() + "->" + self.dest.get_name()

class Digraph(object):
def __init__(self):
self.edges = {}

if node in self.edges:
raise ValueError("Duplicate node found.")
else:
self.edges[node] = []

src = edge.getSource()
dest = edge.getDestination()
if not (src in self.edges and dest in self.edges):
raise ValueError("At least one node not in graph.")
else:
self.edges[src].append(dest)

def childrenOf(self, node):
return self.edges[node]

def hasNode(self, node):
return node in self.edges

def getNode(self, name):
for n in self.edges:
if n.get_name == name:
return n
else:
raise NameError(name)

def __str__(self):
result = ''
for src in self.edges:
for dest in self.edges[src]:
result += src.get_name() + "->" + dest.get_name() + "\n"
return result[:-1] #omit final new line

class Graph(Digraph):
rev = Edge(edge.getDestination(), edge.getSource())

def buildCityGraph(graphType):
g = graphType()
for name in ('Boston', 'Providence', 'New York', 'Chicago',
'Denver', 'Phoenix', 'Los Angeles'): #Create 7 nodes

print(str(buildCityGraph(Digraph)))

When i run this i get the error: NameError: 'Boston'. Why do we use g.addEdge(Edge(g.getNode('Boston'), g.getNode('Providence')))? It seems like g.addEdge(Edge(Node('Boston'), Node('Providence'))) is what we want? When I try this

Python:
class Node(object):
def __init__(self, name):
self.name = str(name)

def get_name(self):
return self.name

def __str__(self):
return self.name

class Edge(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest

def getSource(self):
return self.src

def getDestination(self):
return self.dest

def __str__(self):
return self.src.get_name() + "->" + self.dest.get_name()

class Digraph(object):
def __init__(self):
self.edges = {}

if node in self.edges:
raise ValueError("Duplicate node found.")
else:
self.edges[node] = []

src = edge.getSource()
dest = edge.getDestination()
if not (src in self.edges and dest in self.edges):
raise ValueError("At least one node not in graph.")
else:
self.edges[src].append(dest)

def childrenOf(self, node):
return self.edges[node]

def hasNode(self, node):
return node in self.edges

def getNode(self, name):
for n in self.edges:
if n.get_name == name:
return n
else:
raise NameError(name)

def __str__(self):
result = ''
for src in self.edges:
for dest in self.edges[src]:
result += src.get_name() + "->" + dest.get_name() + "\n"
return result[:-1] #omit final new line

class Graph(Digraph):
rev = Edge(edge.getDestination(), edge.getSource())

def buildCityGraph(graphType):
g = graphType()
for name in ('Boston', 'Providence', 'New York', 'Chicago',
'Denver', 'Phoenix', 'Los Angeles'): #Create 7 nodes

print(str(buildCityGraph(Digraph)))

I get the error: ValueError: At least one node not in graph. Why is the for loop in buildCityGraph function not working?

I fixed the getNode method but now it when i run, the code returns None.

Python:
class Node(object):
def __init__(self, name):
self.name = str(name)

def get_name(self):
return self.name

def __str__(self):
return self.name

class Edge(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest

def getSource(self):
return self.src

def getDestination(self):
return self.dest

def __str__(self):
return self.src.get_name() + "->" + self.dest.get_name()

class Digraph(object):
def __init__(self):
self.edges = {}

if node in self.edges:
raise ValueError("Duplicate node found.")
else:
self.edges[node] = []

src = edge.getSource()
dest = edge.getDestination()
if not (src in self.edges and dest in self.edges):
raise ValueError("At least one node not in graph.")
self.edges[src].append(dest)

def childrenOf(self, node):
return self.edges[node]

def hasNode(self, node):
return node in self.edges

def getNode(self, name):
for n in self.edges:
if n.get_name() == name:
return n
raise NameError(name)

def __str__(self):
result = ''
print(self.edges)
for src in self.edges:
print('hi')
for dest in self.edges[src]:
print('here')
result += src.get_name() + "->" + dest.get_name() + "\n"
return result #omit final new line

class Graph(Digraph):
rev = Edge(edge.getDestination(), edge.getSource())

def buildCityGraph(graphType):
g = graphType()
for name in ('Boston', 'Providence', 'New York', 'Chicago',
'Denver', 'Phoenix', 'Los Angeles'): #Create 7 nodes

print(str(buildCityGraph(Digraph)))

Now it works, I forgot to return g in the buildCityGraph.

Python:
class Node(object):
def __init__(self, name):
self.name = str(name)

def get_name(self):
return self.name

def __str__(self):
return self.name

class Edge(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest

def getSource(self):
return self.src

def getDestination(self):
return self.dest

def __str__(self):
return self.src.get_name() + "->" + self.dest.get_name()

class Digraph(object):
def __init__(self):
self.edges = {}

if node in self.edges:
raise ValueError("Duplicate node found.")
else:
self.edges[node] = []

src = edge.getSource()
dest = edge.getDestination()
if not (src in self.edges and dest in self.edges):
raise ValueError("At least one node not in graph.")
self.edges[src].append(dest)

def childrenOf(self, node):
return self.edges[node]

def hasNode(self, node):
return node in self.edges

def getNode(self, name):
for n in self.edges:
if n.get_name() == name:
return n
raise NameError(name)

def __str__(self):
result = ''
print(self.edges)
for src in self.edges:
for dest in self.edges[src]:
result += src.get_name() + "->" + dest.get_name() + "\n"
return result #omit final new line

class Graph(Digraph):
rev = Edge(edge.getDestination(), edge.getSource())

def buildCityGraph(graphType):
g = graphType()
for name in ('Boston', 'Providence', 'New York', 'Chicago',
'Denver', 'Phoenix', 'Los Angeles'): #Create 7 nodes

print(str(buildCityGraph(Digraph)))