Skip to content

Instantly share code, notes, and snippets.

@theptrk
Last active January 27, 2022 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/2d9d3378e7726529aeabf3b1f5b43a76 to your computer and use it in GitHub Desktop.
Save theptrk/2d9d3378e7726529aeabf3b1f5b43a76 to your computer and use it in GitHub Desktop.
# Q: what are all the shortest paths from a single node to all other nodes?
"""
Example: All shortest paths from id:0 to all other nodes
{0: [[]],
1: [[1]],
2: [[2]],
3: [[1, 3], [2, 3]],
4: [[1, 3, 4], [2, 3, 4]],
5: [[1, 3, 4, 5], [2, 3, 4, 5]],
6: [[1, 3, 4, 5, 6], [2, 3, 4, 5, 6]],
7: [[1, 3, 4, 5, 7], [2, 3, 4, 5, 7]],
8: [[1, 3, 4, 5, 6, 8],
[2, 3, 4, 5, 6, 8],
[1, 3, 4, 5, 7, 8],
[2, 3, 4, 5, 7, 8]],
9: [[1, 3, 4, 5, 6, 8, 9],
[2, 3, 4, 5, 6, 8, 9],
[1, 3, 4, 5, 7, 8, 9],
[2, 3, 4, 5, 7, 8, 9]]}
"""
users = [
{"id": 0, "name": "Hero"},
{"id": 1, "name": "Dunn"},
{"id": 2, "name": "Sue"},
{"id": 3, "name": "Chi"},
{"id": 4, "name": "Thor"},
{"id": 5, "name": "Clive"},
{"id": 6, "name": "Hicks"},
{"id": 7, "name": "Devin"},
{"id": 8, "name": "Kate"},
{"id": 9, "name": "Klein"},
]
friendships = [
(0,1),
(0,2),
(1,2),
(1,3),
(2,3),
(3,4),
(4,5),
(5,6),
(5,7),
(6,8),
(7,8),
(8,9),
]
for user in users:
user["friends"] = []
for i,j in friendships:
users[i]["friends"].append(users[j])
users[j]["friends"].append(users[i])
@theptrk
Copy link
Author

theptrk commented Jan 27, 2022

This is the network diagram by ids

Screen Shot 2022-01-26 at 6 07 24 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment