Beispiel #1
0
 def test_getEdge(self): # Tests error alert when getting non-existent edge
     g = LinkedDirectedGraph()
     g.addVertex("A")
     g.addVertex("B")
     g.addVertex("C")
     g.addEdge("A", "B", 2.5)
     with self.assertRaises(AttributeError):
         g.getEdge("Q", "P")
Beispiel #2
0
# Test some edge methods
print("Expect False False:", g.containsEdge("a", "b"),
      g.containsEdge("a", "a"))
g.addEdge("a","b", 1)
print("Expect True False False :", g.containsEdge("a", "b"),
      g.containsEdge("b", "a"), g.containsEdge("a", "a"))
g.addEdge("a","c", 2)
g.addEdge("e","c", 4)
g.addEdge("e","d", 5)
g.addEdge("e","f", 6)
g.addEdge("d","a", 7)
      
print("Expect True False True :", g.removeEdge("a","c"),
      g.containsEdge("a","c"), g.containsEdge ("d","a"))

edge = g.getEdge("e", "d")
edge.setWeight(55)
print("Expect 55 :", edge.getWeight())
edge.setWeight(5)

# Test incidentEdges
result = ""
for edge in g.getVertex("a").incidentEdges():
    result += str(edge) + " "  
print("Expect edges ab1 ad7 :", result)

# Test neighboringVertices 
result = ""
for vertex in g.neighboringVertices("a"):
    result += str(vertex) + " "    
print("Expect vertices bd :" + result)
Beispiel #3
0
# Add vertices labeled A, B, and C to the graph and print it
g.addVertex("A")
g.addVertex("B")
g.addVertex("C")
print("Expect vertices ABC and no edges: \n" + str(g))

# Insert edges with weight 2.5 and print the graph
g.addEdge("A", "B", 2.5)
g.addEdge("B", "C", 2.5)
g.addEdge("C", "B", 2.5)
print("Expect same vertices and edges AB BC CB all with weight 2.5: \n" +
      str(g))

#g.addVertex("A")

#g.addEdge("A", "D", 2.5)

#g.addEdge("A", "B", 2.5)

print("Expect A: ", g.getVertex("A"))
#print(g.getVertex("D"))

print("Edge from A to B:", g.getEdge("A", "B"))
print("Edge from B to A:", g.getEdge("B", "A"))
#print(g.getEdge("D", "A"))

print("Incident edges of A:", list(map(str, g.incidentEdges("A"))))
#print(list(map(str, g.incidentEdges("D"))))
#print(list(map(str, g.neighboringVertices("D"))))