Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
Last active July 6, 2020 07:08
Show Gist options
  • Select an option

  • Save jonathanagustin/e47359472bb95ae373004c114cc1257b to your computer and use it in GitHub Desktop.

Select an option

Save jonathanagustin/e47359472bb95ae373004c114cc1257b to your computer and use it in GitHub Desktop.
Python - build graph with list of edges
def buildGraph(edges):
from collections import defaultdict
graph = defaultdict(list)
for u, v in edges:
graph[u].append(v)
graph[v].append(u) # delete this line if directed graph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment