예제 #1
0
    if vertex.isMarked():
        count += 1
print("Expect 4: " + str(count))
g.clearVertexMarks()
count = 0
for vertex in g.vertices():
    if vertex.isMarked():
        count += 1
print("Expect 0: " + str(count))
      
# Insert some edges
g.addEdge("a", "b", 1)
g.addEdge("c", "d", 2)
      
# Mark edges, count marks, clear marks, count marks
for edge in g.edges():
    edge.setMark();
count = 0;
for edge in g.edges():
    if edge.isMarked():
        count += 1
print("Expect 2: " + str(count))
g.clearEdgeMarks()
count = 0;
for edge in g.edges():
    if edge.isMarked():
        count += 1
print("Expect 0: " + str(count))
      
# Clear graph
g.clear()
예제 #2
0
파일: useGraph.py 프로젝트: mark124/CIS-214
def topologicalSort(graph):
    # <your code>
    graph = LinkedDirectedGraph()

    # The graph represents the following course prerequisites:
    # A requires nothing
    # B requires nothing
    # C requires A
    # D requires A, B, and C
    # E requires C
    # F requires B and D
    # G requires E and F
    # H requires C, F, and G

    # Part 2:
    # Add the vertices:
    # <your code>

    graph.addVertex("A")
    graph.addVertex("B")
    graph.addVertex("C")
    graph.addVertex("D")
    graph.addVertex("E")
    graph.addVertex("F")
    graph.addVertex("G")
    graph.addVertex("H")

    # Part 3:
    # Add the edges:
    # <your code>

    graph.addEdge("A", "C", 0)

    graph.addEdge("A", "D", 0)
    graph.addEdge("B", "D", 0)
    graph.addEdge("C", "D", 0)

    graph.addEdge("C", "E", 0)

    graph.addEdge("B", "F", 0)
    graph.addEdge("D", "F", 0)

    graph.addEdge("E", "G", 0)
    graph.addEdge("F", "G", 0)

    graph.addEdge("C", "H", 0)
    graph.addEdge("F", "H", 0)
    graph.addEdge("G", "H", 0)

    print("Graph:")
    print(graph)

    print("Courses:")
    # Part 4:
    # Display each vertex on a separate line:
    # <your code>

    for vertex in graph.vertices():
        print(str(vertex))

    print()
    print("Prerequisites:")
    # Part 5:
    # Display each edge on a separate line:
    # <your code>

    for edge in graph.edges():
        print(str(edge))

    print("One possible order to take the courses:")

    # Part 6:
    # Display the courses in prerequisite (topological) order:
    # <your code>

    stack = []

    for i in graph.vertices():
        if not i.isMarked():
            topologicalSortStack(i, stack)

    print(" ".join(str(x) for x in stack))

    print()