|
| 1 | + |
| 2 | +''' |
| 3 | +
|
| 4 | + Write a function that computes the lengths of the shortest paths between start and all of the other vertices in the graph using Dijkstra's algorithm and returns them in an array. |
| 5 | +
|
| 6 | + Sample Input: |
| 7 | + Start: 0 |
| 8 | + Edges : = [ |
| 9 | + [[1, 7]], |
| 10 | + [[2, 6], [3, 20], [4, 3]], |
| 11 | + [[3, 14]], |
| 12 | + [[4, 2]], |
| 13 | + [], |
| 14 | + [], |
| 15 | + ] |
| 16 | + Output: [0, 7, 13, 27, 10, -1] |
| 17 | +
|
| 18 | + Dijkstras Algorithm |
| 19 | +
|
| 20 | + Explanation: |
| 21 | +
|
| 22 | + The code snippet is an implementation of Dijkstra's algorithm for finding the shortest path from a given starting vertex to all other vertices in a graph. Here's a breakdown of the code: |
| 23 | +
|
| 24 | + 1. The `DijkstrasAlgorithm` function takes the starting vertex (`start`) and the graph represented by the adjacency list (`edges`) as input and returns a list of minimum distances from the starting vertex to all other vertices. |
| 25 | +
|
| 26 | + 2. It initializes `numberOfVertices` as the total number of vertices in the graph. |
| 27 | +
|
| 28 | + 3. The `minDistances` slice is initialized with maximum integer values to represent infinity distance for all vertices. The length of `minDistances` is set to the number of vertices. |
| 29 | +
|
| 30 | + 4. The minimum distance from the starting vertex to itself is set to 0. |
| 31 | +
|
| 32 | + 5. The `visited` map is used to keep track of visited vertices. Initially, it is empty. |
| 33 | +
|
| 34 | + 6. The algorithm iterates until all vertices have been visited. In each iteration, it selects the vertex with the minimum distance from the `minDistances` slice using the `getVertexWithMinDistance` function. |
| 35 | +
|
| 36 | + 7. If the current minimum distance is infinity (i.e., no more vertices to visit), the loop breaks. |
| 37 | +
|
| 38 | + 8. The selected vertex is marked as visited by adding it to the `visited` map. |
| 39 | +
|
| 40 | + 9. For each neighboring vertex of the selected vertex, it calculates the new path distance and updates the `minDistances` if the new distance is smaller. |
| 41 | +
|
| 42 | + 10. After all iterations, the `finalDistances` slice is created to convert the `minDistances` into a format where unreachable vertices are represented as -1. |
| 43 | +
|
| 44 | + 11. The `getVertexWithMinDistance` function returns the vertex with the minimum distance from the `distances` slice and the current minimum distance. |
| 45 | +
|
| 46 | + Overall, the code implements Dijkstra's algorithm to find the shortest path from a starting vertex to all other vertices in a graph, using an adjacency list representation. It keeps track of minimum distances, visited vertices, and updates the distances based on the neighboring vertices. |
| 47 | +
|
| 48 | + Time Complexity: O(V^2 + e) |
| 49 | + Space complexity: O(V) |
| 50 | +
|
| 51 | +''' |
| 52 | + |
| 53 | +import "math" |
| 54 | + |
| 55 | +# DijkstrasAlgorithm finds the shortest path from a starting vertex to all other vertices in a graph. |
| 56 | +func DijkstrasAlgorithm(start int, edges [][][]int) []int { |
| 57 | + numberOfVertices := len(edges) |
| 58 | + minDistances := make([]int, 0, len(edges)) |
| 59 | + |
| 60 | + # Initialize the minDistances slice with maximum integer values |
| 61 | + for range edges { |
| 62 | + minDistances = append(minDistances, math.MaxInt32) |
| 63 | + } |
| 64 | + |
| 65 | + # Set the distance of the starting vertex to 0 |
| 66 | + minDistances[start] = 0 |
| 67 | + visited := map[int]bool{} |
| 68 | + |
| 69 | + # Iterate until all vertices have been visited |
| 70 | + for len(visited) != numberOfVertices { |
| 71 | + # Get the vertex with the minimum distance |
| 72 | + vertex, currentMinDistance := getVertexWithMinDistance(minDistances, visited) |
| 73 | + |
| 74 | + # If the current minimum distance is infinity, break the loop |
| 75 | + if currentMinDistance == math.MaxInt32 { |
| 76 | + break |
| 77 | + } |
| 78 | + |
| 79 | + # Mark the vertex as visited |
| 80 | + visited[vertex] = true |
| 81 | + |
| 82 | + # Explore neighboring vertices |
| 83 | + for _, edge := range edges[vertex] { |
| 84 | + destination, distanceToDestination := edge[0], edge[1] |
| 85 | + |
| 86 | + # Skip if the destination vertex is already visited |
| 87 | + if visited[destination] { |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + # Calculate the new path distance to the destination |
| 92 | + newPathDistance := currentMinDistance + distanceToDestination |
| 93 | + currentDestinationDistance := minDistances[destination] |
| 94 | + |
| 95 | + # Update the minimum distance if the new distance is smaller |
| 96 | + if newPathDistance < currentDestinationDistance { |
| 97 | + minDistances[destination] = newPathDistance |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + # Convert the minDistances slice to finalDistances, representing unreachable vertices as -1 |
| 103 | + finalDistances := make([]int, 0, len(minDistances)) |
| 104 | + for _, distance := range minDistances { |
| 105 | + if distance == math.MaxInt32 { |
| 106 | + finalDistances = append(finalDistances, -1) |
| 107 | + } else { |
| 108 | + finalDistances = append(finalDistances, distance) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return finalDistances |
| 113 | +} |
| 114 | + |
| 115 | +# getVertexWithMinDistance returns the vertex with the minimum distance from the distances slice. |
| 116 | +func getVertexWithMinDistance(distances []int, visited map[int]bool) (int, int) { |
| 117 | + currentMinDistance := math.MaxInt32 |
| 118 | + vertex := -1 |
| 119 | + |
| 120 | + # Find the vertex with the minimum distance among unvisited vertices |
| 121 | + for vertexIdx, distance := range distances { |
| 122 | + if visited[vertexIdx] { |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + if distance <= currentMinDistance { |
| 127 | + vertex = vertexIdx |
| 128 | + currentMinDistance = distance |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return vertex, currentMinDistance |
| 133 | +} |
0 commit comments