Advertisement
Ridwanul_Haque

Dijkstra + Bellman_Ford

Mar 6th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. ifstream file;
  4. ofstream file2;
  5.  
  6. int findMin(int* distance,bool* visited,int n)
  7. {
  8.     int minVertex = -1;
  9.     for(int i = 0 ; i<n; i++)
  10.     {
  11.         if(!visited[i] && (minVertex==-1 || distance[i]<distance[minVertex]))
  12.         {
  13.             minVertex = i;
  14.         }
  15.     }
  16.  
  17.     return minVertex;
  18.  
  19. }
  20.  
  21. void printPath(int *parent, int destination)
  22. {
  23.  
  24.     if (parent[destination] == - 1)
  25.         return;
  26.  
  27.     printPath(parent, parent[destination]);
  28.  
  29.     file2<<"->"<<destination;
  30. }
  31.  
  32. void dijkstra(int** graph,int n,int source,int destination)
  33. {
  34.     bool* visited = new bool[n];
  35.     int *distance = new int[n];
  36.     int *parent = new int[n];
  37.  
  38.     for(int i =0; i<n; i++)
  39.     {
  40.         distance[i] = INT_MAX;
  41.         visited[i] = false;
  42.         parent[i] = -1;
  43.     }
  44.  
  45.     distance[source] = 0;
  46.  
  47.  
  48.     for(int i =0; i<n; i++)
  49.     {
  50.         int minVertex = findMin(distance,visited,n);
  51.         visited[minVertex] = true;
  52.         for(int j = 0; j<n; j++)
  53.         {
  54.             if(graph[minVertex][j]!=0 && !visited[j])
  55.             {
  56.                 int dist = distance[minVertex] + graph[minVertex][j];
  57.                 if(dist<distance[j])
  58.                 {
  59.                     distance[j] = dist;
  60.                     parent[j] = minVertex;
  61.  
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     file2<<distance[destination]<<endl;
  67.     file2<<source;
  68.     printPath(parent,destination);
  69.  
  70. }
  71.  
  72. void bellman(int** graph,int n,int m,int source,int destination)
  73. {
  74.     int *distance = new int[n];
  75.     int *parent = new int[n];
  76.     for(int i =0; i<n; i++)
  77.     {
  78.         distance[i] = INT_MAX;
  79.         parent[i] = -1;
  80.     }
  81.     distance[source] = 0;
  82.  
  83.     for(int i  = 0; i<n-1; i++)
  84.     {
  85.         for(int j = 0; j<m; j++)
  86.         {
  87.             int u = graph[j][0];
  88.             int v = graph[j][1];
  89.             int w = graph[j][2];
  90.             if (distance[u]!=INT_MAX && distance[u] + w < distance[v])
  91.             {
  92.                 distance[v] =distance[u] + w;
  93.                 parent[v] = u;
  94.             }
  95.  
  96.         }
  97.     }
  98.  
  99.     for(int i =0; i<m; i++)
  100.     {
  101.         int u = graph[i][0];
  102.         int v = graph[i][1];
  103.         int w = graph[i][2];
  104.         if(distance[u]!=INT_MAX && distance[u]+w <distance[v])
  105.         {
  106.             file2<<"Graph contains Negative weight cycle"<<endl;
  107.         }
  108.     }
  109.     file2<<distance[destination]<<endl;
  110.     file2<<source;
  111.     printPath(parent,destination);
  112.  
  113. }
  114.  
  115. int main()
  116. {
  117.  
  118.     file.open("test.txt");
  119.     int n,m,s,d;
  120.     file>>n>>m;
  121.     int **matrix = new int*[n];
  122.     int **graph = new int*[n];
  123.     for(int i = 0; i < n; i++)
  124.     {
  125.         matrix[i] = new int[n];
  126.         for(int j = 0; j < n; j++)
  127.         {
  128.             matrix[i][j] = 0;
  129.         }
  130.     }
  131.  
  132.     for(int i = 0; i<m; i++)
  133.     {
  134.         graph[i] = new int[3];
  135.         int u,v,w;
  136.         file>>u>>v>>w;
  137.         matrix[u][v] = abs(w);
  138.         graph[i][0] = u;
  139.         graph[i][1] = v;
  140.         graph[i][2] = w;
  141.     }
  142.  
  143.     file>>s>>d;
  144.     file.close();
  145.  
  146.     file2.open("1705111.txt");
  147.     file2<<"Bellman Ford Algorithm:"<<endl;
  148.     bellman(graph,n,m,s,d);
  149.  
  150.     file2<<"\n\n";
  151.  
  152.     file2<<"Dijkstra Algorithm:"<<endl;
  153.     dijkstra(matrix,n,s,d);
  154.  
  155.     file2.close();
  156.  
  157.  
  158.  
  159.  
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement