Пример #1
0
def arrival_departure_dfs(graph: Graph,
                          v: str,
                          discovered: Dict[str, bool],
                          arrival: Dict[str, int],
                          departure: Dict[str, int],
                          time: int) -> int:
    """
    Method for DFS with arrival and departure times for each vertex

    O(V+E) -- E could be as big as V^2

    :param graph:
    :param v:
    :param discovered:
    :param arrival:
    :param departure:
    :param time: should be initialized to -1
    :return:
    """
    time += 1

    # when did we arrive at vertex 'v'?
    arrival[v] = time
    discovered[v] = True

    for n in graph.get_neighbors(v):
        if not discovered.get(n, False):
            time = arrival_departure_dfs(graph, n, discovered, arrival, departure, time)

    time += 1
    # increment time and then set departure
    departure[v] = time
    return time
Пример #2
0
 def mark_visited(g: Graph, v: str, v_map: Dict[str, bool],
                  t_sort_results: List[str]):
     v_map[v] = True
     for n in g.get_neighbors(v):
         if not v_map[n]:
             mark_visited(g, n, v_map, t_sort_results)
     t_sort_results.append(v)
Пример #3
0
    def test_get_neighbors(self):
        json_graph = {"label": "my graph", "graph": {"A": ["B"], "B": []}}
        graph = Graph(input_graph=json.dumps(json_graph))

        self.assertEqual(graph.get_neighbors("A"), ["B"])
        self.assertEqual(graph.get_neighbors("B"), [])