017-prim algorithm-greedy-"algorithmic design techniques and analysis" M.H.A study notes

Source: Internet
Author: User

Basic ideas:

Define node Set U, V (U = Node set that has been selected to join MST, V = not selected)

1. Choose a node to join U

2. Select an edge with the least edge, and his two nodes belong to U, V, and the node belonging to V is added to u

3. Repeat execution 2 until v null

Pseudo code:


C++Code:

int g[mnx][mnx];int N, m;int d[mnx];//naïve prim, complexity O (| V|^2) | v|: Points, | E|: The number of sides int prim () {memset (d, 0x3f, sizeof D);//Initialize int ret = d[1] = 0;  First d[1] into 0for (int i = 1; I <= n; ++i) {int u = -1;for (int j = 1; j <= N; ++j)   //Find D[u] the smallest uif ((U = =-1 | | d[u  ] > D[j]) && d[j] = 1) u = j;ret + D[u];d [u] = -1;for (int j = 1; j <= N; ++j)  //Update and U adjacency node D[j] value d[j] = Min (D[j], g[u][j]);} return ret;}

Algorithm Analysis:

The main cost is to find the edge with the least edge, the second cycle of this step consumes θ(n2), so the time complexity of the algorithm is θ(n2).

Heap Optimization Improvements:

We use the small top heap to complete the search for the minimum edge, as with the Dijkstra algorithm, the algorithm has a total of n-1 inserts,n-1 Delete, M-n+1 times siftup operation. The total time complexity is O(mlogn).

Pseudo code:


C++Code:

int FST[MNX], nxt[mxe], Cost[mxe], to[mxe], e;void init () {memset (FST,-1, sizeof FST); e = 0;} void Add (int u, int v, int c) {To[e] = V, nxt[e] = Fst[u], cost[e] = c, fst[u] = e++;} struct node {int u, dis;node (int u, int dis): U (u), dis (dis) {}bool operator < (const node &b) const {return dis ; b.dis;}};/ /heap optimization, Complexity O (| E|log| v|), dense graph with slower int primheap () {memset (d, 0x3f, sizeof D);d [1] = 0;priority_queue<node> q;q.push (node (1,0));//Select first Section First Point int ret = 0;while (!q.empty ()) {int u = q.top (). U;int dd = Q.top (). Dis;q.pop (); if (d[u]! = DD) continue;//If the value before the update is not taken , continue off ret + = Dd;d[u] = -1;for (int j = fst[u]; ~j; j = nxt[j]) {int v = to[j], c = cost[j];//Update if (D[v] > C && Amp D[V]! =-1) {D[v] = C;q.push (node (v, c));}}} return ret;}


017-prim algorithm-greed-algorithmic design techniques and analysis M.H.A learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.