Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. struct node{
  2.     int index, parent;
  3.     double cost;
  4.     bool visited;
  5.     string color;
  6. };
  7.  
  8. struct graph{
  9.     vector<vector<pair<int, double> > > edges;
  10.     vector<node> nodes;
  11.     int V;
  12. };
  13.  
  14. void init(graph& g, int n){
  15.     g.V = n;
  16.     g.nodes.resize(n);
  17.     g.edges.resize(n);
  18.     for (int i = 0; i < n; ++i){
  19.         g.nodes[i].index = i;
  20.         g.nodes[i].parent = -1;
  21.         g.nodes[i].cost = 0;
  22.         g.nodes[i].visited = false;
  23.         g.nodes[i].color = "white";
  24.     }
  25. }
  26.  
  27. void dfs(graph& g, int n){
  28.     g.nodes[n].visited = true;
  29.     for (auto v : g.edges[n]){
  30.         if(g.nodes[v].visited == false){
  31.             dfs(g, v);
  32.         }
  33.     }
  34. }
  35.  
  36. int bfs(graph& g, int n){
  37.     vector<int> Q;
  38.     int num_visited = 1;
  39.     g.nodes[n].visited = true;
  40.     Q.push_back(n);
  41.     while(!Q.empty()){
  42.         int current = Q.back();
  43.         Q.pop_back();
  44.         for (auto v : g.edges[current]){
  45.             if(g.nodes[v].visited == false){
  46.                 g.nodes[v].visited = true;
  47.                 Q.push_back(v);
  48.                 num_visited++;
  49.             }
  50.         }
  51.     }
  52.     return num_visited;
  53. }
  54.  
  55. void relax(node& u, node& v, int cost){
  56.     if(v.cost > u.cost + cost){
  57.         v.cost = u.cost + cost;
  58.         v.parent = u.index;
  59.     }
  60. }
  61.  
  62. void djikstra(graph& g, int start){
  63.     priority_queue<pair<double, int>, vector<pair<double, int> >, greater<pair<double, int> > > pq;
  64.     vi dist(g.nodes.size());
  65.     vi visited(g.nodes.size());
  66.     for(int i = 0; i < g.nodes.size(); i++){
  67.         dist[i] = INF;
  68.         visited[i] = false;
  69.     }
  70.     dist[start] = 0;
  71.     pq.push(mp(dist[start], start));
  72.     while(!pq.empty()){
  73.         int u = pq.top().second;
  74.         pq.pop();
  75.         if(visited[u] == false){
  76.             visited[u] = true;
  77.             for(int i = 0; i < g.edges[u].size(); i++){
  78.                 int adj = g.edges[u][i].first;
  79.                 double w = g.edges[u][i].second;
  80.                 if(visited[adj] == false){
  81.                     if(dist[adj] > dist[u] + w){
  82.                         dist[adj] = dist[u] + w;
  83.                         g.nodes[adj].parent = u;
  84.                     }                  
  85.                     pq.push(mp(dist[adj], adj));               
  86.                 }
  87.             }
  88.         }  
  89.     }
  90. }
  91.  
  92. void prim(graph& g, int start){
  93.     priority_queue<pair<double, int>, vector<pair<double, int> >, greater<pair<double, int> > > pq;
  94.     vi dist(g.nodes.size());
  95.     vi visited(g.nodes.size());
  96.     for(int i = 0; i < g.nodes.size(); i++){
  97.         dist[i] = INF;
  98.         visited[i] = false;
  99.     }
  100.     dist[start] = 0;
  101.     pq.push(mp(dist[start], start));
  102.     while(!pq.empty()){
  103.         int u = pq.top().second;
  104.         pq.pop();
  105.         if(visited[u] == false){
  106.             visited[u] = true;
  107.             for(int i = 0; i < g.edges[u].size(); i++){
  108.                 int adj = g.edges[u][i].first;
  109.                 double w = g.edges[u][i].second;
  110.                 if(visited[adj] == false && w < dist[adj]){  // Diff from Djikstra
  111.                     dist[adj] = w;
  112.                     g.nodes[adj].parent = u;
  113.                     pq.push(mp(dist[adj], adj));               
  114.                 }      
  115.             }
  116.         }  
  117.     }
  118. }
  119.  
  120. void printMST(graph g){
  121.     for (int i = 0; i < g.nodes.size(); ++i){
  122.         if(g.nodes[i].parent != -1){
  123.             cout << min(g.nodes[i].index + 1, g.nodes[i].parent + 1) << " "
  124.                 <<  max(g.nodes[i].index + 1, g.nodes[i].parent + 1)  << endl;                 
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement