Skip to content

Instantly share code, notes, and snippets.

@psaitu
Last active September 11, 2015 21:09
Show Gist options
  • Select an option

  • Save psaitu/5c6adceb7d623251d2ba to your computer and use it in GitHub Desktop.

Select an option

Save psaitu/5c6adceb7d623251d2ba to your computer and use it in GitHub Desktop.
Basic Graph Representation in Python
a, b, c, d, e, f, g, h = range(8)
N = [
{b:2, c:1, d:3, e:9, f:4},
{c:4, e:3},
{d:8},
{e:7},
{f:5},
{c:2, g:2, h:2},
{f:1, h:6},
{f:9, g:8}
]
b in N[a] # N[a] is the neighbours of a
len(N[f]) # Degree
N[a][b] #Edge weight for (a, b)
# Time is O(n)
a, b, c, d, e, f, g, h = range(8)
N = [
[b, c, d, e, f],
[c, e],
[d],
[e],
[f],
[c, g, h],
[f, h],
[f, g]
]
b in N[a] # N[a] is the neighbours of a
a, b, c, d, e, f, g, h = range(8)
N = [
{b, c, d, e, f},
{c, e},
{d},
{e},
{f},
{c, g, h},
{f, h},
{f, g}
]
b in N[a] # N[a] is the neighbours of a
a, b, c, d, e, f, g, h = range(8)
N = {
'a': set('bcdef'),
'b': set('ce'),
'c': set('d'),
'd': set('e'),
'e': set('f'),
'f': set('cgh'),
'g': set('fh'),
'h': set('fg')
}
b in N[a] # N[a] is the neighbours of a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment