MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / create_graph

Function create_graph

graphs/tarjans_scc.py:83–95  ·  view source on GitHub ↗

>>> n = 7 >>> source = [0, 0, 1, 2, 3, 3, 4, 4, 6] >>> target = [1, 3, 2, 0, 1, 4, 5, 6, 5] >>> edges = list(zip(source, target)) >>> create_graph(n, edges) [[1, 3], [2], [0], [1, 4], [5, 6], [], [5]]

(n: int, edges: list[tuple[int, int]])

Source from the content-addressed store, hash-verified

81
82
83def create_graph(n: int, edges: list[tuple[int, int]]) -> list[list[int]]:
84 """
85 >>> n = 7
86 >>> source = [0, 0, 1, 2, 3, 3, 4, 4, 6]
87 >>> target = [1, 3, 2, 0, 1, 4, 5, 6, 5]
88 >>> edges = list(zip(source, target))
89 >>> create_graph(n, edges)
90 [[1, 3], [2], [0], [1, 4], [5, 6], [], [5]]
91 """
92 g: list[list[int]] = [[] for _ in range(n)]
93 for u, v in edges:
94 g[u].append(v)
95 return g
96
97
98if __name__ == "__main__":

Callers 1

tarjans_scc.pyFile · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected