Advertisement
shabbyheart

Prims Algorithm using Link list

Jan 5th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int adj[1000][1000];
  5.  
  6. class Node{
  7. public:
  8.     int destination;
  9.     int weight;
  10.     Node *next;
  11. };
  12. class Integer{
  13. public:
  14.     Node *head = NULL;
  15. };
  16.  
  17.  
  18. void addEdge(Integer arr[],int sorc,int dest,int w)
  19. {
  20.     Node *newNode = new Node();
  21.     newNode->destination = dest;
  22.     newNode->weight = w;
  23.     newNode->next = NULL;
  24.     if( arr[sorc].head == NULL)
  25.         arr[sorc].head = newNode;
  26.     else
  27.     {
  28.         Node *temp = arr[sorc].head;
  29.         while(temp->next != NULL)
  30.             temp = temp->next;
  31.         temp->next = newNode;
  32.     }
  33. }
  34.  
  35. void display(Integer listt[], int v)
  36. {
  37.     for(int i =0; i<=v; i++)
  38.     {
  39.         cout<<"\n Adjacency list of vertex "<<i<<" -->";
  40.         while(listt[i].head != NULL)
  41.         {
  42.             cout<<" "<<listt[i].head->destination;
  43.             listt[i].head = listt[i].head->next;
  44.         }
  45.     }
  46.  
  47. }
  48.  
  49.  
  50.  
  51. class node{
  52. public:
  53.     int v,cost;
  54.     node()
  55.     {
  56.  
  57.     }
  58.     node(int vertex,int costt)
  59.     {
  60.         v= vertex;
  61.         cost = costt;
  62.     }
  63.  
  64. };
  65. bool operator<(node a,node b)
  66. {
  67.     return a.cost>b.cost;
  68. }
  69. class Prims{
  70.         int vertex,edge,u,v,weight,i,j,ans = 0;
  71.         bool visited[100] = {false};
  72. public:
  73.     void solution(Integer adj[],int v)
  74.     {
  75.         priority_queue<node>pq;
  76.         pq.push(node(0,0));
  77.         //int parent[100]={0};
  78.         while(!pq.empty())
  79.         {
  80.             //cout<<"hi";
  81.             node temp = pq.top();
  82.             pq.pop();
  83.             if(visited[temp.v] == 1) continue;
  84.             cout<<temp.v<<" "<<temp.cost<<endl;
  85.             //parent = temp.v;
  86.             ans +=temp.cost;
  87.             visited[temp.v] =1;
  88.             Node *tmp = adj[temp.v].head;
  89.             while(tmp != NULL)
  90.             {
  91.                 //cout<<"hello"<<endl;
  92.                 if(visited[tmp->destination] == 1)
  93.                 {
  94.                     tmp = tmp->next;
  95.                     continue;
  96.                 }
  97.                 pq.push(node(tmp->destination,tmp->weight));
  98.                 tmp = tmp->next;
  99.  
  100.             }
  101.         }
  102.         cout<<endl<<"Minimum cost is "<<ans<<endl;
  103.     }
  104. };
  105. int main()
  106. {
  107.     freopen("input.txt","r",stdin);
  108.  
  109.     int v,E;
  110.     cin>>v>>E;
  111.  
  112.     Integer arr[v];
  113.  
  114.     /// eita na korle arr er vitore v+1 dite hobe
  115.      for (int i = 0; i <= v; i++)
  116.         arr[i].head = NULL;
  117.  
  118.     /// getting input
  119.     int m,n,w;
  120.     while(scanf("%d%d%d",&m,&n,&w) == 3)
  121.     {
  122.         addEdge(arr,m,n,w);
  123.         addEdge(arr,n,m,w);
  124.     }
  125.     //display(arr,v);
  126.  
  127.     Prims p;
  128.     p.solution(arr,v);
  129. }
  130. /*
  131. 5 7
  132.  
  133. 0 1 6
  134. 0 2 1
  135. 1 2 2
  136. 2 3 8
  137. 1 3 3
  138. 1 4 1
  139. 3 4 6
  140.  
  141. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement