Last active
September 11, 2015 21:09
-
-
Save psaitu/5c6adceb7d623251d2ba to your computer and use it in GitHub Desktop.
Basic Graph Representation in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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