Dijkstra's Weighted Graph Shortest Path in c++

#include <limits.h> 
#include <stdio.h> 
  

#define V 9 
  

int minDistance(int dist[], bool sptSet[]) 
{ 

    int min = INT_MAX, min_index; 
  
    for (int v = 0; v < V; v++) 
        if (sptSet[v] == false && dist[v] <= min) 
            min = dist[v], min_index = v; 
  
    return min_index; 
} 
  

void printSolution(int dist[]) 
{ 
    printf("Vertex \t\t Distance from Source\n"); 
    for (int i = 0; i < V; i++) 
        printf("%d \t\t %d\n", i, dist[i]); 
} 
  

void dijkstra(int graph[V][V], int src) 
{ 
    int dist[V]; 
    
  
    bool sptSet[V];
 
    for (int i = 0; i < V; i++) 
        dist[i] = INT_MAX, sptSet[i] = false; 
  
  
    dist[src] = 0; 
  
  
    for (int count = 0; count < V - 1; count++) { 
       
        int u = minDistance(dist, sptSet); 
  
       
        sptSet[u] = true; 
  
       
        for (int v = 0; v < V; v++) 
  
           
            if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX 
                && dist[u] + graph[u][v] < dist[v]) 
                dist[v] = dist[u] + graph[u][v]; 
    } 
  
 
    printSolution(dist); 
} 
  

int main() 
{ 
    
    int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, 
                        { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, 
                        { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, 
                        { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, 
                        { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, 
                        { 0, 0, 4, 14, 10, 0, 2, 0, 0 }, 
                        { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, 
                        { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, 
                        { 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; 
  
    dijkstra(graph, 0); 
  
    return 0; 
} 

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
C program to find the shortest path using Dijkstra&rsquo;s algorithm for a weighted connected graph How can I modify Dijkstra's algorithm to find the shortest path in a graph with weighted edges and weighted vertices? shortest path dijkstra c++ dijkstra algorithm for weighted graph dijkstra's algorithm weighted graph java Dijktsra's Shortest Weighted Path Algorithm weighted directed graph Dijkstra algorithm to find shortest path using adj list weighted directed graph Dijkstra algorithm to find shortest path implement Dijkstra's algorithm for computing shortest paths in a directed graph how to find shortest path using dijkstra c++ Dijkstra Algorithm to find shortest path to a leaf in the given graph procedure for shortest path of a graph using dijkstra algorithm in c++ dijkstra shortest path froma weighted graph turn base dijkstra algorithm for finding shortest path in c++ stl in directed graph dijkstra's algorithm weighted graph dijkstra's algorithm in c++ shortest path algorithm weighted graph dijkstra algorith c++ what will be the running time of dijkstra's single source dijkstra algorithm for finding shortest path dijkstra on directed graph dijkstra's dijkstra algorithm c++ code dijakstra algorithm when to use dijijkstra in graph shortest path algorithm for directed graph dijkstras algorithm cpp find shortest path in graph using dijkstra algorithm dijkstra dijkstra's algorithm c++ dijkstra weigthed graph single source shortest path algorithm geeksforgeeks Dijkashtra Algorithm dijkstras algorithm djikstra algorithm dijkstra algorithm steps which among the following be used to dijkstra shortest path algorithm dijkstra algorithm in c print shortest path dijkstra Use Dijkstra&rsquo;s algorithm to find the shortest path from (A &amp; C) to all nodes. single source shortest path problem dijkstra algorithm code dijkstra algorithm c++ how to solve shortest path problem in c++ dijkstra algorithm single source shortest path dijkstra algorithm, steps dykstras algorithm shortest path algorithm dijkstra's algorithm we discussed solving the single-source shortest-path problem with dijkstra's algorithm Dijkstra&rsquo;s algorithm we discussed solving the single-source shortest-path problem with dijkstra's algorith Dijkstra's Weighted Graph Shortest Path in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source