try: print("Testing with adjacency list graph...") adjacency_graph = AdjacencyGraph() adjacency_graph.add_edge('a', 'b', 1) if shortest_path(adjacency_graph, 'a', 'b') != (['a', 'b'], 1): print "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." else: print "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." except: print "Your code produced this error when adding edge ('a', 'b', 1) to the adjacency list graph or getting the shortest path from 'a' to 'b'." print sys.exc_info()[0] try: print("Testing with edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('a', 'b', 1) edge_graph.add_edge('b', 'c', 2) edge_graph.add_edge('c', 'd', 3) edge_graph.add_edge('a', 'f', 1) edge_graph.add_edge('f', 'd', 1) edge_graph.add_edge('f', 'e', 3) edge_graph.add_edge('e', 'm', 4) edge_graph.add_edge('m', 'd', 6) hi = shortest_path(edge_graph, 'a', 'd') print(hi) print("Testing with adjacency list graph...") adj_graph = AdjacencyGraph() adj_graph.add_edge('a', 'b', 1) adj_graph.add_edge('b', 'c', 2)
print( "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." ) else: print( "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." ) except: print( "Your code produced this error when adding edge ('a', 'b', 1) to the adjacency list graph or getting the shortest path from 'a' to 'b'." ) print(sys.exc_info()[0]) try: print("Testing with edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('a', 'b', 1) if shortest_path(edge_graph, 'a', 'b') != (['a', 'b'], 1): print( "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your edge list graph had the edge ('a', 'b', 1) added." ) else: print( "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your edge list graph had the edge ('a', 'b', 1) added." ) except: print( "Your code produced this error when adding edge ('a', 'b', 1) to the edge list graph or getting the shortest path from 'a' to 'b'." ) print(sys.exc_info()[0])
print( "Your code ran, but did NOT output the right neighbors for 'a' when adding edge ('a', 'b', 1)." ) else: print( "Your code ran, and it correctly output the right neighbors for 'a' when adding edge ('a', 'b', 1)." ) except: print( "Your code produced this error when adding edge ('a', 'b', 1) or getting neighbors for 'a'." ) print(sys.exc_info()[0]) try: print("Testing edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('a', 'b', 1) if not edge_graph.has_edge('a', 'b'): print( "Your code ran, but did NOT give True when checking whether the graph has an edge ('a', 'b') after adding edge ('a', 'b', 1)." ) else: print( "Your code ran, and it correctly output True when checking whether the graph has an edge ('a', 'b') after adding edge ('a', 'b', 1)." ) except: print( "Your code produced this error when adding edge ('a', 'b', 1) or checking has_edge('a', 'b')." ) print(sys.exc_info()[0]) try:
try: adjacency_graph = AdjacencyGraph() adjacency_graph.add_edge('s', 'a', 4) adjacency_graph.add_edge('a', 't', 3) adjacency_graph.add_edge('s', 'b', 5) adjacency_graph.add_edge('b', 't', 5) if shortest_path(adjacency_graph, 's', 't') != (['s', 'a', 't'], 7): print("ERROR: TEST 0A, WRONG DISTANCE") else: print("pass test 0A") except: print("ERROR: TEST 0A, ADDING EDGE") print(sys.exc_info()[0]) try: edge_graph = EdgeGraph() edge_graph.add_edge('s', 'a', 4) edge_graph.add_edge('a', 't', 3) if shortest_path(edge_graph, 's', 't') != (['s', 'a', 't'], 7): print("ERROR: TEST 0B, WRONG DISTANCE") else: print("pass test 0B") except: print("ERROR: TEST 0B, ADDING EDGE") print(sys.exc_info()[0]) ####################### TEST 1 try: adjacency_graph = AdjacencyGraph() adjacency_graph.add_edge('s', 'a', 4)
try: print("Testing with adjacency list graph...") adjacency_graph = AdjacencyGraph() adjacency_graph.add_edge('a', 'b', 1) if shortest_path(adjacency_graph, 'a', 'b') != (['a','b'], 1): print "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." else: print "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." except: print "Your code produced this error when adding edge ('a', 'b', 1) to the adjacency list graph or getting the shortest path from 'a' to 'b'." print sys.exc_info()[0] try: print("Testing with edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('a', 'b', 1) if shortest_path(edge_graph, 'a', 'b') != (['a','b'], 1): print "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your edge list graph had the edge ('a', 'b', 1) added." else: print "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your edge list graph had the edge ('a', 'b', 1) added." except: print "Your code produced this error when adding edge ('a', 'b', 1) to the edge list graph or getting the shortest path from 'a' to 'b'." print sys.exc_info()[0] try: print("Testing with edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('h', 'a', 3) edge_graph.add_edge('h', 'b', 2) edge_graph.add_edge('h', 'c', 5)
print( "Your code ran, but did NOT output the shortest distance from 'a' to 'f' when your adjacency list graph had the edge ('e', 'f', 1) added." ) else: print( "Your code ran, and it correctly output the shortest distance from 'a' to 'f' when your adjacency list graph had the edge ('e', 'f', 1) added." ) except: print( "Your code produced this error when adding edge ('a', 'b', 1) to the adjacency list graph or getting the shortest path from 'a' to 'b'." ) print(sys.exc_info()[0]) try: print("Testing with edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('a', 'b', 1) edge_graph.add_edge('a', 'c', 2) edge_graph.add_edge('b', 'd', 3) edge_graph.add_edge('c', 'e', 5) edge_graph.add_edge('d', 'e', 2) edge_graph.add_edge('d', 'f', 4) edge_graph.add_edge('e', 'f', 1) if shortest_path(edge_graph, 'a', 'f') != (['a', 'b', 'd', 'e', 'f'], 7): print( "Your code ran, but did NOT output the shortest distance from 'a' to 'f' when your edge list graph had the edge ('e', 'f', 1) added." ) else: print( "Your code ran, and it correctly output the shortest distance from 'a' to 'f' when your edge list graph had the edge ('e', 'f', 1) added." )
# adjacency_graph.add_edge('1', '7', 14) # adjacency_graph.add_edge('5', '4', 4) # adjacency_graph.add_edge('5', '7', 10) # adjacency_graph.add_edge('6', '7', 1) # print(shortest_path(adjacency_graph, '1', '7')) # if shortest_path(adjacency_graph, 'a', 'b') != (['a','b'], 1): # print "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." # else: # print "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your adjacency list graph had the edge ('a', 'b', 1) added." # except: # print "Your code produced this error when adding edge ('a', 'b', 1) to the adjacency list graph or getting the shortest path from 'a' to 'b'." # print sys.exc_info()[0] try: print("Testing with edge list graph...") edge_graph = EdgeGraph() edge_graph.add_edge('s', 'a', 3) edge_graph.add_edge('s', 'b', 4) edge_graph.add_edge('a', 'c', 2) edge_graph.add_edge('a', 'f', 7) edge_graph.add_edge('a', 'b', 6) edge_graph.add_edge('b', 'f', 5) edge_graph.add_edge('c', 'f', 1) edge_graph.add_edge('c', 't', 8) edge_graph.add_edge('f', 't', 4) print(shortest_path(edge_graph, 's', 't')) if shortest_path(edge_graph, 's', 't') != (['a', 'b', 'c', 'd'], 11): print "Your code ran, but did NOT output the shortest distance from 'a' to 'b' when your edge list graph had the edge ('a', 'b', 1) added." else: print "Your code ran, and it correctly output the shortest distance from 'a' to 'b' when your edge list graph had the edge ('a', 'b', 1) added." except AttributeError:
from graph_edge_list import Graph as EdgeGraph from shortest_path import shortest_path import sys from ast import literal_eval with open("test.txt", 'r') as testfile: L = testfile.readlines() num_tests_run = 0 for l in L: (testname, g, s, t, expected_output) = l.strip().split(";") num_tests_run += 1 # Parse the graph edge_graph = EdgeGraph() edge_graph.graph = literal_eval(g) # testcase = (g,s,t) test_result = str(shortest_path(edge_graph, s, t)) if test_result == expected_output: # print("Passed test with name %s" % testname) print(test_result) continue elif test_result != expected_output: print("Failed test with name %s" % testname) print(test_result) print(expected_output) break