def jsonToGraph(filepath): """ takes a JSON file as input, and generates a graph """ json_data = readin_data(filepath) for index, course in enumerate(json_data): # Create all the vertices vertex = Vertex(index, course['name']) graph.add_vertex(vertex) for course in json_data: if len(course['prerequisites']) != 0: vertex = graph.find_vertex(course['name']) for prerequisite in course['prerequisites']: neighbor = graph.find_vertex(prerequisite) vertex.add_neighbor(neighbor)
def build_test_graph(): """ A --> B <-> ^ | | D <-> C -> E """ v1 = Vertex("A") v2 = Vertex("B") v3 = Vertex("C") v4 = Vertex("D") v5 = Vertex("E") v1.add_neighbor(v2) v2.add_neighbors([v3,v5]) v3.add_neighbors([v2,v4,v5]) v4.add_neighbors([v1,v3]) v5.add_neighbors([v2]) return Graph({"A":v1,"B":v2,"C":v3,"D":v4,"E":v5})