def main():
# make a new Graph called fredo
	fredo = Graph()
# add 5 vertices
	fredo.addVertex('A')
	fredo.addVertex('B')
	fredo.addVertex('C')
	fredo.addVertex('D')
	fredo.addVertex('E')
#show graph with no edges
	fredo.display()
#check edge and vertex count
	if fredo.countEdges() !=0:
		print 'Edge count incorrect'
	else:
		print 'Edge count correct'

	if fredo.countVertices() != 5:
		print 'Vertex count incorrect'
	else:
		print 'Vertex count correct'
#Set edges to values
	fredo.setEdge('A', 'A', 1.0)
	fredo.setEdge('A', 'B', 2.0)
	fredo.setEdge('A', 'C', 3.0)
	fredo.setEdge('A', 'D', 4.0)
	fredo.setEdge('B', 'A', 5.0)
	fredo.setEdge('B', 'B', 6.0)
	fredo.setEdge('B', 'C', 7.0)
	fredo.setEdge('B', 'D', 8.0)	
	fredo.setEdge('C', 'A', 9.0)
	fredo.setEdge('C', 'B', 10.0)
	fredo.setEdge('C', 'C', 11.0)
	fredo.setEdge('C', 'D', 12.0)	
	fredo.setEdge('D', 'A', 13.0)
	fredo.setEdge('D', 'B', 14.0)
	fredo.setEdge('D', 'C', 15.0)
	fredo.setEdge('D', 'D', 16.0)
	fredo.setEdge('E', 'B', 17.0)
#Show new graph
	fredo.display()
#Check new edge count
	if fredo.countEdges() !=13:
		print 'Edge count 2 incorrect'
	else:
		print 'Edge count 2 correct'
#Add duplicate vertex
	fredo.addVertex('B')
#Check if vertex count remains same
	if fredo.countVertices() != 5:
		print 'Vertex count 2 incorrect'
	else:
		print 'Vertex count 2 correct'
#Remove zero edge from graph
	fredo.removeEdge('D','D')
#Ensure edge count remains same
	if fredo.countEdges() !=13:
		print 'Edge count 3 incorrect'
	else:
		print 'Edge count 3 correct'
#Remove vertex
	fredo.removeVertex('B')
#Display graph
	fredo.display()
#Check edges and Vertices
	if fredo.countEdges() !=6:
		print 'Edge count 4 incorrect'
	else:
		print 'Edge count 4 correct'
	if fredo.countVertices() !=4:
		print 'Vertex count 4 incorrect'
	else:
		print 'Vertex count 4 correct'
#Test graph clear (THIS IS A BUG)
	fredo.clear()
#Test to see if no vertices or edges remain
	if fredo.countEdges() !=0:
		print 'Edge count 5 incorrect'
	else:
		print 'Edge count 5 correct'
	if fredo.countVertices() !=0:
		print 'Vertex count 5 incorrect'
	else:
		print 'Vertex count 5 correct'
Exemplo n.º 2
0
def main():
	# make a new Graph called fredo

	fredo = Graph()
	
	fredo.addVertex('A')	
	fredo.addVertex('B')
	fredo.addVertex('C')
	
	fredo.setEdge('A', 'A', 1.0)
	fredo.setEdge('A', 'B', 2.0)
	fredo.setEdge('A', 'C', 3.0)
	fredo.setEdge('B', 'A', 5.0)
	fredo.setEdge('B', 'B', 6.0)
	fredo.setEdge('B', 'C', 7.0)
	fredo.setEdge('C', 'A', 9.0)
	fredo.setEdge('C', 'B', 10.0)
	fredo.setEdge('C', 'C', 11.0)

	
	fredo.display()
	
	if fredo.countVertices() != 3:
		print 'Bad graph! Vertices are wrong', g.countVertices()
	else:
		print 'Vertices are correct'
	
	fredo.addVertex('D')
	fredo.addVertex('E')
	fredo.addVertex('E')
	#duplicating a vertex doesn't add another vertex to the count
	
	fredo.display()
	
	if fredo.countVertices() != 5:
		print 'Bad graph! Vertices are wrong', g.countVertices()
	else:
		print 'Vertices are correct'
	
	if fredo.countEdges() != 6:
		print 'Bad graph! Edges are wrong', fredo.countEdges()
	else:
		print 'Edges are still correct'
	
	
	
	fredo.setEdge('D', 'A', 0.0)
	fredo.setEdge('D', 'A', 9.0)
	#fredo.setEdge('D', 'A', 0.0) 
	#if you set it to zero, it doesn't 
	#count it as a edge
	fredo.setEdge('D', 'B', 2.0)
	fredo.setEdge('E', 'C', 4.0)
	#this edge below doesn't add an edge, and if you take it out, it
	#doesn't change anything as well
	fredo.setEdge('E', 'E', 4.0)
	
	
	fredo.display()
	
	if fredo.countEdges() != 9:
		print 'Bad graph! Edges are wrong', fredo.countEdges()
	else:
		print 'Edges are still correct'
Exemplo n.º 3
0
def main():

	print 'Testing all the things'
	# make a new variable of type Graph (so it gets made like the Graph.py class)
	g = Graph()
	# add 4 Vertices to the Graph, with the names Abilene, Boston, Chicago, Denver
	g.addVertex('Abilene')	
	g.addVertex('Boston')
	g.addVertex('Chicago')
	g.addVertex('Denver')
	# Now we have a Graph with 4 Vertices and 16 empty Edges.
	# Let's try to set all the Edges.  (We don't actually set 
	# Edges from a Vertex to itself, so these self Edges should 
	# never actually show up when we display that Graph.)
	g.setEdge('Abilene', 'Abilene', 1.0)
	g.setEdge('Abilene', 'Boston', 2.0)
	g.setEdge('Abilene', 'Chicago', 3.0)
	g.setEdge('Abilene', 'Denver', 4.0)
	g.setEdge('Boston', 'Abilene', 5.0)
	g.setEdge('Boston', 'Boston', 6.0)
	g.setEdge('Boston', 'Chicago', 7.0)
	g.setEdge('Boston', 'Denver', 8.0)
	g.setEdge('Chicago', 'Abilene', 9.0)
	g.setEdge('Chicago', 'Boston', 10.0)
	g.setEdge('Chicago', 'Chicago', 11.0)
	g.setEdge('Chicago', 'Denver', 12.0)
	g.setEdge('Denver', 'Abilene', 13.0)
	g.setEdge('Denver', 'Boston', 14.0)
	g.setEdge('Denver', 'Chicago', 15.0)
	g.setEdge('Denver', 'Denver', 16.0)
	
	# print the Vertex names and the Edge costs in the array
	g.display()
	
	# check if our vertexCount is correct
	if g.countVertices() != 4:
		print 'Bad graph! Vertices are wrong', g.countVertices()
	else:
		print 'Test 1 ok'
		
	# check if our edgeCount is correct
	if g.countEdges() != 12:
		print 'Bad graph! Edges are wrong', g.countEdges()
	else:
		print 'Test 2 ok'
		
	# make a copy of g, called h
	h = Graph()
	h.copyDeeply(g)

	# take out a Vertex
	g.removeVertex('Chicago')
	print 'Here is graph g after Chicago is removed'
	g.display()

	# make a copy of the 3-Vertex Graph g, called i
	i = Graph()
	i.copyDeeply(g)
	
	# check if our vertexCount is correct after Chicago comes out
	if g.countVertices() != 3:
		print 'Bad graph! Vertices are wrong', g.countVertices()
	else:
		print 'Test 3 ok'
	
	# check if our edgeCount is correct after Chicago's edges come out
	if g.countEdges() != 6:
		print 'Bad graph! Edges are wrong', g.countEdges()
	else:
		print 'Test 4 ok'
	
	# check if our edgeCount is correct after Abilene->Boston edge comes out
	g.setEdge('Abilene','Boston', 0.0)
	if g.countEdges() != 5:
		print 'Bad graph! Edges are wrong', g.countEdges()
	else:
		print 'Test 5 ok'
		
	g.removeEdge('Boston','Denver')
	# check if our edgeCount is correct after Boston->Denver edge comes out
	if g.countEdges() != 4:
		print 'Bad graph! Edges are wrong', g.countEdges()
	else:
		print 'Test 6 ok'
	
	g.removeEdge('Boston','Boston')
	# check if our edgeCount is correct after empty Boston->Boston edge comes out
	if g.countEdges() != 4:
		print 'Bad graph! Edges are wrong', g.countEdges()
	else:
		print 'Test 7 ok'
	
	# check if we know that the Boston->Denver edge is out
	if not g.containsEdge('Boston', 'Denver'):
		print 'Bad graph! Edges are wrong, Boston->Denver should not be in Graph'
	else:
		print 'Test 8 ok'
		
	# check if Boston is there
	if not g.containsVertex('Boston'):
		print 'Bad graph! Boston should be in Graph'
	else:
		print 'Test 9 ok'

	# check if Boston is there and Chicago is not
	if g.containsVertex('Chicago'):
		print 'Bad graph! Chicago should not be in Graph'
	else:
		print 'Test 10 ok'
	
	
	g.display()	
	
	# put Chicago back into i
	i.addVertex('Chicago')
	# put all the old Edges g used to have to and from Chicago into i
	i.setEdge('Chicago', 'Abilene', 9.0)
	i.setEdge('Chicago', 'Boston', 10.0)
	i.setEdge('Chicago', 'Chicago', 11.0)
	i.setEdge('Chicago', 'Denver', 12.0)
	i.setEdge('Abilene', 'Chicago', 3.0)
	i.setEdge('Boston', 'Chicago', 7.0)
	i.setEdge('Denver', 'Chicago', 15.0)
	
	# show us the Graph i now
	print 'Here is graph i.'
	i.display()
	
	# show us the Graph h, which we copied from g before
	# we deleted Chicago from g 
	if h.isEqual(i):
		print 'Here is graph h, which is the same as i.'
		print 'Anyone who says different is itching for a fight.'
	else:
		print 'Here is graph h, which is not the same as i.'
	h.display()
	
	j = Graph()
	j.copyDeeply(h)
	# check if deep copy works on self-copying (h to h)
	h.copyDeeply(h)
	if j.countVertices() != h.countVertices():
		print 'Bad graph! These should be the same size'
		h.display()
		j.display()
	else:
		print 'Test 11 ok'