Esempio n. 1
0
def get_longest_paths(graph: DiGraph, source: str) -> Dict[str, float]:
    """Get the length of the longest path to each node from a source node.

    Parameters
    ----------
    graph : networkx.classes.digraph.DiGraph
        A directed graph.
    source : str
        The name of the source node.

    Returns
    -------
    dict
        A dictionary where keys are the names of the nodes in `graph` and
        values are the lengths of the longest path from `source`.

    """

    dist = {node: -float("inf") for node in graph}
    dist[source] = 0
    visited = []
    for u in cyclic_topological_sort(graph, [source]):
        visited.append(u)
        for v in graph.neighbors(u):
            if v in visited:
                continue
            if dist[v] < dist[u] + 1:
                dist[v] = dist[u] + 1

    return dist
Esempio n. 2
0
def _visit(graph: DiGraph, node: T, order: List[T]) -> None:
    if graph.nodes[node].get("visited", False):
        return
    graph.nodes[node]["visited"] = True
    for n in graph.neighbors(node):
        _visit(graph, n, order)
    order.append(node)
Esempio n. 3
0
def containing_bags(G: DiGraph, col: str) -> int:
    return_val: int = 1

    neighbor: str
    for neighbor in G.neighbors(col):
        return_val += containing_bags(G, neighbor) * \
            G.get_edge_data(col, neighbor)["weight"]

    return return_val
Esempio n. 4
0
def create_graph_with_new_data():
    '''
    A DiGraph object holds directed edges
    - Parallel edges still aren't allowed
        - For a Digraph, two edges are parallel if they connect the same ordered pair of vertices
            - Thus, two edges that connect the same vertices but go in different directions are not parallel
    '''
    # Create with edge list
    g = DiGraph([(1, 5), (5, 1)]) 
    #g = Graph([(1, 5), (5, 1)]) 
    g.add_node(6)
    print(g.nodes()) # [1, 5, 6]
    # These are two distinct edges
    print(g.edges()) # [(1, 5), (5, 1)]
    print(g.neighbors(1)) # [5]
    print(g.neighbors(5)) # [1]
    g.edge[1][5]['foo'] = 'bar'
    print(g.edge[1][5]) # {'foo': 'bar'}
    print(g.edge[5][1]) # {}