Esempio n. 1
0
# This is a short test file, given to you to ensure that our grading scripts can grade your file.
# Please do not modify it.
# You should only submit "graph_adjacency_list.py", "graph_edge_list.py", and "shortest_path.py".

# This tests the simplest case: adding a single edge to the graph and checking the shortest path from one node to the other.

from graph_adjacency_list import Graph as AdjacencyGraph
from graph_edge_list import Graph as EdgeGraph
from shortest_path import shortest_path
import sys

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...")
Esempio n. 2
0
# This is a short test file, given to you to ensure that our grading scripts can grade your file.
# Please do not modify it.
# You should only submit "graph_adjacency_list.py", "graph_edge_list.py", and "shortest_path.py".

# This tests the simplest case: adding a single edge to the graph and checking the shortest path from one node to the other.

from graph_adjacency_list import Graph as AdjacencyGraph
from graph_edge_list import Graph as EdgeGraph
from shortest_path import shortest_path
import sys

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...")
Esempio n. 3
0
# This is a short test file, given to you to ensure that our grading scripts can grade your file.
# Please do not modify it.
# You should only submit "graph_adjacency_list.py", "graph_edge_list.py", and "shortest_path.py".

# This tests the simplest case: adding a single edge to the graph and checking the shortest path from one node to the other.

from graph_adjacency_list import Graph as AdjacencyGraph
from graph_edge_list import Graph as EdgeGraph
from shortest_path import shortest_path
import sys

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)
Esempio n. 4
0
from graph_adjacency_list import Graph

if __name__ == '__main__':

    graph = Graph(5)

    graph.add_edge(0, 1)
    graph.add_edge(0, 4)
    graph.add_edge(1, 2)
    graph.add_edge(1, 3)
    graph.add_edge(1, 4)
    graph.add_edge(2, 3)
    graph.add_edge(3, 4)

    graph.print_graph()
#   cost_func = lambda u, v, e, prev_e: e['cost']
#   test_function = find_path(graph, 'a', 'c', cost_func=cost_func)
#   if shortest_path(edge_graph, 's', 't') != (test_function[0], test_function[3]):
#     print("pass edge_graph")
#   else:
#     break
#   if shortest_path(adjacency_graph, 's', 't') != (test_function[0], test_function[3]):
#     print("pass adjacency_graph")
#   else:
#     break

####################### TEST 0

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)
Esempio n. 6
0
        if u == target:
            return d[u]
        S.add(u)

    return ([],float("inf"))

# YOUR CODE HERE
if __name__ == "__main__":
    # adjacency_graph is type dict
    _graph = AdjacencyGraph()
    print('AdjacencyGraph:')

    #edge_graph is type list
    # _graph = EdgeGraph()
    # print('EdgeGraph:')
    _graph.add_edge('s','2',9)
    _graph.add_edge('s','6',14)
    _graph.add_edge('s','7',15)
    _graph.add_edge('2','3',24)
    _graph.add_edge('6','3',18)
    _graph.add_edge('6','5',30)
    _graph.add_edge('6','7',5)
    _graph.add_edge('3','5',2)
    _graph.add_edge('3','t',19)
    _graph.add_edge('4','t',6)
    _graph.add_edge('4','3',6)
    _graph.add_edge('5','t',16)
    _graph.add_edge('5','4',11)
    _graph.add_edge('7','5',20)
    _graph.add_edge('7','t',44)
Esempio n. 7
0
        short_path.insert(0, curr)  # insert at beginning of list
        curr = previous_node[curr]  # get previous node
    short_path.insert(0, curr)  # insert the last, source node

    return (short_path, shortest_distance[target])


adjacency_graph = Graph()

##
# adjacency_graph.add_edge('a', 'c', 15)
# adjacency_graph.add_edge('b', 't', 1)
# adjacency_graph.add_edge('d', 't', 1)
# adjacency_graph.add_edge('a', 't', 21)
# adjacency_graph.add_edge('b', 'u', 1)
# adjacency_graph.add_edge('b', 't', 6)
# adjacency_graph.add_edge('u', 't', 2)
# adjacency_graph.add_edge('c', 't', 20)

# print shortest_path(adjacency_graph, 'a', 't')

graph1 = Graph()
graph1.add_edge('t', 'a', 5)
graph1.add_edge('a', 'c', 10)
graph1.add_edge('c', 'd', 6)
graph1.add_edge('d', 'e', 12)
graph1.add_edge('t', 'd', 2)

path = shortest_path(graph1, 't', 'e')
print path
Esempio n. 8
0
# This is a short test file, given to you to ensure that our grading scripts can grade your file.
# Please do not modify it.
# You should only submit "graph_adjacency_list.py", "graph_edge_list.py", and "shortest_path.py".

# This tests the simplest case: adding a single edge to the graph and checking the shortest path from one node to the other.

from graph_adjacency_list import Graph as AdjacencyGraph
from graph_edge_list import Graph as EdgeGraph
from shortest_path import shortest_path
import sys

try:
    print("Testing with adjacency list graph...")
    adjacency_graph = AdjacencyGraph()
    adjacency_graph.add_edge('a', 'b', 1)
    adjacency_graph.add_edge('a', 'c', 2)
    adjacency_graph.add_edge('b', 'd', 3)
    adjacency_graph.add_edge('c', 'e', 5)
    adjacency_graph.add_edge('d', 'e', 2)
    adjacency_graph.add_edge('d', 'f', 4)
    adjacency_graph.add_edge('e', 'f', 1)
    if shortest_path(adjacency_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 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: