Created
March 19, 2017 03:19
-
-
Save rupalbarman/c08fc00a676002f24d50c56d1204d8fb to your computer and use it in GitHub Desktop.
Utility to convert adjacency matrix into adjacency list in python, for efficient BFS and DFS maybe
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
| # get adjancy matrix | |
| n= int(input()) #number of vertices | |
| am=[] | |
| for i in range(n): | |
| am.append([int(x) for x in input().split()]) | |
| print(am) #show matrix | |
| print() | |
| #conver to adjancy list | |
| al= {} #doesnt contain path from a node to itself. | |
| for x, row in enumerate(am): | |
| al[x+1]=[] | |
| for i, v in enumerate(row): | |
| if v== 1 and i!=x: | |
| al[x+1].append(i+1) | |
| print(al) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works well!