Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. int Graph::minDistance(int dist[], bool sptSet[])
  2. {
  3. // Initialize min value
  4. int min = INT_MAX, min_index;
  5.  
  6. for (int v = 0; v < V; v++)
  7. if (sptSet[v] == false && dist[v] <= min)
  8. min = dist[v], min_index = v;
  9.  
  10. return min_index;
  11. }
  12.  
  13. // A utility function to print the constructed distance array
  14. int Graph::printSolution(int dist[], int n)
  15. {
  16. cout<<endl<<endl;
  17. printf("Vertex Distance from Source\n");
  18. for (int i = 0; i < V; i++)
  19. printf("%d \t\t %d\n", i, dist[i]);
  20. }
  21.  
  22. // Funtion that implements Dijkstra's single source shortest path algorithm
  23. // for a graph represented using adjacency matrix representation
  24. void Graph::dijkstra(Graph& G, int src)
  25. {
  26.  
  27. int dist[V];
  28. bool sptSet[V];
  29. for (int i = 0; i < V; i++){
  30. dist[i] = INT_MAX, sptSet[i] = false;
  31. }
  32. dist[src] = 0;
  33. for(int i=0;i<V;i++){
  34. int u=minDistance(dist,sptSet);
  35. sptSet[u]=true;//visited
  36. for(int v=0;v<V;v++){
  37. for(auto e:edge[v]) dist[v+1]=e.second+dist[v];
  38.  
  39. }
  40. }
  41. printSolution(dist,V);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement