Used to find the shortest path from any starting node to all other nodes in a Graph - Single Source Shortest Path on a Weighted Graph that is either Directed or Non-Directed
# InitList<int[]>[] adjList = new ArrayList[n]; for (int from=0; from<n; from++) adjList[from] = new ArrayList<>();# Convert from Adjacency Matrixfor (int from=0; from<n; from++) { int[] edges = adjMaxtrix[from]; for (int to=0; to<edges.length; to++) { int edgeWeight = edges[to]; if (edgeWeight == 0 || from==to) continue; # Use this line to skip adding relationship between 2 nodes when there isn't a valid relationship present adjList[from].add(new int[]{to, edgeWeight}); }}
Debugging
Examine the relationship of adjList
for (int i=0; i<n; i++) { List<int[]> n = adjList[i]; System.out.printf("Outward edges from node %d: \n", i); for (int[] r:n) { System.out.println(Arrays.toString(r)); } System.out.println();}