src/com/michaelho/DynamicProgramming/DP3.java
/**
         * The Dijkstra's method used to calculate shortest path among vertices in a graph. This method does not
         * consider negative cycles in the graph.
         * Reference: https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-in-java-using-priorityqueue/
         *
         * @param graph The graph data structure to find shortest path algorithm.
         * @param src   The source (start point) of the graph.
         * */
The Dijkstra's method used to calculate shortest path among vertices in a graph. This method does not
consider negative cycles in the graph.

@param graph The graph data structure to find shortest path algorithm.
@param src   The source (start point) of the graph.
The Dijkstra ' s method used to calculate shortest path among vertices in a graph . This method does not consider negative cycles in the graph . @param graph The graph data structure to find shortest path algorithm . @param src The source ( start point ) of the graph .
int[] shortestPath(Graph graph, int src) {
            int V = graph.V;
            int[] dist = new int[V];
            boolean[] sptSet = new boolean[V];
            for (int i = 0; i < V; i++) {
                dist[i] = Integer.MAX_VALUE;
                sptSet[i] = false;
            }
            dist[src] = 0;
            for (int count = 0; count < V-1; count++) {
                int u = minDistance(dist, sptSet, V);
                sptSet[u] = true;
                List<Edge> edges = graph.getEdgesList();
                for (int v = 0; v < V; v++)
                    for (Edge edge : edges) {
                        if (!sptSet[v] && edge.src.val == u && edge.dest.val == v
                                && dist[u] != Integer.MAX_VALUE && dist[u] + edge.weight < dist[v])
                            dist[v] = dist[u] + edge.weight;
                    }
            }
            return dist;
        }
int [ ] shortestPath ( Graph graph , int src ) { int V = graph . V ; int [ ] dist = new int [ V ] ; boolean [ ] sptSet = new boolean [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { dist [ i ] = Integer . MAX_VALUE ; sptSet [ i ] = false ; } dist [ src ] = 0 ; for ( int count = 0 ; count < V - 1 ; count ++ ) { int u = minDistance ( dist , sptSet , V ) ; sptSet [ u ] = true ; List < Edge > edges = graph . getEdgesList ( ) ; for ( int v = 0 ; v < V ; v ++ ) for ( Edge edge : edges ) { if ( ! sptSet [ v ] && edge . src . val == u && edge . dest . val == v && dist [ u ] != Integer . MAX_VALUE && dist [ u ] + edge . weight < dist [ v ] ) dist [ v ] = dist [ u ] + edge . weight ; } } return dist ; }
The Dijkstra's method used to calculate shortest path among vertices in a graph.
The Dijkstra ' s method used to calculate shortest path among vertices in a graph .
// sptSet[i] is true if vertex i is included in shortest path tree // or shortest distance from src to i is finalized // Initialize all distances as INFINITE and stpSet[] as false // Distance of source vertex from itself is always 0 // Find shortest path for all vertices // Pick the minimum distance vertex from the set of vertices // not yet processed. u is always equal to src in first // iteration. // Mark the picked vertex as processed // Update dist value of the adjacent vertices of the picked vertex. // Update dist[v] only if it is not in sptSet, there is an // edge from u to v, and total weight of path from src to // v through u is smaller than current value of dist[v]