C++ Program to Find Single Source Shortest Path in DAG

«
»
This C++ program displays the shortest path traversal from a particular node to every other node present inside the graph relative to the former node.

Here is the source code of the C++ program of the Djikstra’s Algoritm of finding shortest paths from the first node in graph to every other node with the shortest path length displayed beside each pair of vertices.This C++ program is successfully compiled and run on DevCpp,a C++ compiler.The program output is given below.

  1. /*
  2.  * C++ Program to find SSSP(Single Source Shortest Path) 
  3.  * in DAG(Directed Acyclic Graphs)
  4.  */
  5. #include <iostream>
  6. #include <conio.h>
  7. using namespace std;
  8. #define INFINITY 999
  9. struct node
  10. {
  11.    int from;
  12. }p[7];
  13. int c = 0;
  14. void djikstras(int *a,int b[][7],int *dv)
  15. {
  16.     int i = 0,j,min,temp;
  17.     a[i] = 1;
  18.     dv[i] = 0;
  19.     p[i].from = 0;
  20.     for (i = 0; i < 7;i++)
  21.     {
  22.         if (b[0][i] == 0)
  23.         {
  24.             continue;
  25.         }
  26.         else
  27.         {
  28.             dv[i] = b[0][i];
  29.             p[i].from = 0;
  30.         }
  31.     }
  32.     while (c < 6)
  33.     {
  34.         min = INFINITY;
  35.         for (i = 0; i < 7; i++)
  36.         {
  37.             if (min <= dv[i] || dv[i] == 0 || a[i] == 1)
  38.             {
  39.                 continue;
  40.             }
  41.             else if (min > dv[i])
  42.             {
  43.                 min = dv[i];
  44.             }
  45.         }
  46.         for (int k = 0; k < 7; k++)
  47.         {
  48.             if (min == dv[k])
  49.             {
  50.                 temp = k;
  51.                 break;
  52.             }
  53.             else
  54.             {
  55.                 continue;
  56.     	    }
  57.         }
  58.         a[temp] = 1;
  59.         for (j = 0; j < 7; j++)
  60.         {
  61.             if (a[j] == 1 || b[temp][j] == 0)
  62.             {
  63.                 continue;
  64.             }
  65.             else if (a[j] != 1)
  66.             {
  67.                 if (dv[j] > (dv[temp] + b[temp][j]))
  68.                 {
  69.                     dv[j] = dv[temp] + b[temp][j];
  70.                     p[i].from = temp;
  71.                 }
  72.             }
  73.         }
  74.         c++;
  75.     }  
  76.     for (int i = 0; i < 7; i++)
  77.     {
  78.         cout<<"from node "<<p[i].from<<" cost is:"<<dv[i]<<endl;
  79.     } 
  80. }       
  81. int main()
  82. {
  83.     int a[7];
  84.     int dv[7];
  85.     for(int k = 0; k < 7; k++)
  86.     {
  87.         dv[k] = INFINITY;
  88.     }
  89.     for (int i = 0; i < 7; i++)
  90.     {
  91.         a[i] = 0;
  92.     }
  93.     int b[7][7];
  94.     for (int i = 0;i < 7;i++)
  95.     {
  96.         cout<<"enter values for "<<(i+1)<<" row"<<endl;
  97.         for(int j = 0;j < 7;j++)
  98.         {
  99.             cin>>b[i][j];
  100.         }
  101.     }
  102.     djikstras(a,b,dv);
  103.     getch();
  104. }

advertisement
Output
enter values for 1 row
0
3
6
0
0
0
0
enter values for 2 row
3
0
2
4
0
0
0
enter values for 3 row
6
2
0
1
4
2
0
enter values for 4 row
0
4
1
0
2
0
4
enter values for 5 row
0
0
4
2
0
2
1
enter values for 6 row
0
0
2
0
2
0
1
enter values for 7 row
0
0
0
4
1
1
0
from node 0 to node 0 minimum cost is:0
from node 0 to node 1 minimum cost is:3
from node 0 to node 2 minimum cost is:5
from node 0 to node 3 minimum cost is:6
from node 0 to node 4 minimum cost is:8
from node 0 to node 5 minimum cost is:7
from node 0 to node 6 minimum cost is:8

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Linked List, go to Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.