Introduction to Algorithms 19th Chapter study Questions 19-2 minimum spanning tree algorithm using two-item heap __ algorithm

Source: Internet
Author: User

Topic

train of Thought

For the nature and implementation of the two-item heap, you can refer to the definition and implementation of the two-item heap. After analyzing the algorithm given in the topic, we can get the following points:

1, 1~4 line initialization, we can use an array as VI, use and look up the set of ways, to facilitate the search for Zishugen, and so on, the program finally get the precursor of the map, corresponding to the creation of each of the two heap storage ei, where key is weighted, the value of the edge (including edge start and end);

2, 5~12 line iteration. A maximum of one vertex set is reduced for each iteration, and for the vertex set VI,

A. The minimum weight edge can be obtained by using the extractmin operation of the two-item heap;

B. Using and searching for the root of both;

C. Line 12th, using the bheapunion operation to merge two heaps, and combine vertex sets, while reducing the number of vertex sets.


Implement

After figuring out the two-item heap, it is easy to write out the program after clearing the idea of the algorithm. Before doing some conventions, refer to the basic algorithm of the graph (i). The implementation code is as follows:

#include <iostream> #include <fstream> #include <vector> #include <utility> #include
BinomialHeap.h "#define Noparent 0 #define MAX 0x7fffffff using namespace std;

Enum color{White, GRAY and Black}; struct Edgenode {//Edge node size_t adjvertex;//The associated vertex size_t weight;//edge weight Edgenode *nextedge;//the next edge edgenode (size_t adj,

size_t W): Adjvertex (adj), weight (W), Nextedge (nullptr) {}};
	Class Agraph {//non-graph private:vector<edgenode*> graph;
size_t Nodenum;
	Public:agraph (size_t n = 0) {editgraph (n);}
		void Editgraph (size_t n) {nodenum = n;
	Graph.resize (n + 1);
	size_t size () const {return nodenum}
	void Initgraph ()//Initialize edgenode* search (size_t, size_t);/Find side void Addoneedge (size_t, size_t, size_t);
	void Addtwoedge (size_t, size_t, size_t);//No to graph add edge void Deleteoneedge (size_t, size_t);
	void Deletetwoedge (size_t, size_t);//delete edge void Bhmst (agraph&) in no to graph;//Use two-item heap binomial heap compute MST void print ();
	void Destroy ();

~agraph () {Destroy ();}}; voidAgraph::initgraph () {size_t start, end;
	size_t W;
	Ifstream infile ("F:\\mst.txt");
while (infile >> start >> end >> W) Addoneedge (Start, end, W);
	} edgenode* Agraph::search (size_t start, size_t end) {Edgenode *curr = Graph[start];
	while (Curr!= nullptr && curr->adjvertex!= end) Curr = curr->nextedge;
return curr;
	} void Agraph::addoneedge (size_t start, size_t end, size_t weight) {Edgenode *curr = search (start, end);
		if (Curr = = nullptr) {Edgenode *p = new Edgenode (end, weight);
		P->nextedge = Graph[start];
	Graph[start] = p;
	} inline void Agraph::addtwoedge (size_t start, size_t end, size_t weight) {Addoneedge (Start, end, weight);
Addoneedge (end, start, weight);
	void Agraph::d Eleteoneedge (size_t start, size_t end) {Edgenode *curr = search (start, end);
			if (Curr!= nullptr) {if (Curr->adjvertex = = end) {Graph[start] = curr->nextedge;
		Delete Curr;
			else {Edgenode *pre = Graph[start]; WHile (Pre->nextedge->adjvertex!= end) Pre = pre->nextedge;
			Pre->nextedge = curr->nextedge;
		Delete Curr;
	}} inline void Agraph::d Eletetwoedge (size_t start, size_t end) {Deleteoneedge (start, end);
Deleteoneedge (end, start);
		} inline void Agraph::p rint () {for (size_t i = 1; I!= graph.size (); ++i) {Edgenode *curr = graph[i];
		cout << i;
		if (Curr = = nullptr) cout << "--> null"; else while (Curr!= nullptr) {cout << "--<" << curr->weight << ">-->" <<
				curr->adjvertex;
			Curr = curr->nextedge;
	} cout << Endl;
		} void Agraph::d Estroy () {for (size_t i = 1; I!= graph.size (); ++i) {Edgenode *curr = graph[i], *pre;
			while (Curr!= nullptr) {pre = Curr;
			Curr = curr->nextedge;
		Delete pre;
	} Graph[i] = Curr; } void Agraph::bhmst (Agraph &mst) {struct Edge {//Bureau class, Edge size_t start, finish;//contains edge start and end Edge (size_t s, size_ T f): Start (s), Finish (f) {}}; vector<binomial_heap<size_t, edge>> E (nodenum + 1);//Set up a two-item heap for each vertex's adjacency list, the key is the edge value vector<size_t> Parent (Nodenum + 1);//precursor vector<color> Isdestroy (nodenum + 1);//record vertex collection has been destroyed (merged into other sets) for (size_t i = 1; I <= nodenu M
		++i) {//initialization parent[i] = i;
		Isdestroy[i] = white;
		Edgenode *curr = Graph[i];
			while (Curr!= nullptr) {E[i].insert (Curr->weight, Edge (i, Curr->adjvertex));
		Curr = curr->nextedge; } struct Findroot:public binary_function<vector<size_t>,size_t,size_t> {//Local Function object class, find the subtree root, and search the collection size_t oper
			Ator () (const vector<size_t> &AMP;UFS, size_t v) const {while (UFS[V)!= v) v = ufs[v];
		return v;
	}
	}; struct Getvertexset:p ublic unary_function <vector<color> size_t > {//Local Function object class, search for the set of vertices not destroyed, return its number size_t ope
			Rator () (const vector<color> &isdestroy) Const {size_t set_num = 0; for (size_t i = 1; I!= isdestroy.size (); ++i) if (isdestroy[i] = white) {
					Set_num = i;
				Break
		return set_num;
	}
	};
	size_t vertex_set_total = Nodenum; while (Vertex_set_total > 1) {//iterate continuously until only one vertex set size_t Vertex_set_num = Getvertexset () (Isdestroy);//Get Vertex collection PAIR&L t;size_t, edge> min = E[vertex_set_num].extractmin (), the corresponding set of edges, that is, the minimum weight of the two-item heap and the margin size_t v_root = FindRoot () (Parent, min.se
		Cond.finish); The root of the IF (Vertex_set_num!= v_root)//start is the owning vertex set number {//If the two ends are different from the Virgin Tree Mst.addtwoedge (min.second.start,min.second.finish, min . A;/So add to MST parent[v_root] = vertex_set_num;//and merge two vertex sets Isdestroy[v_root] = black;//Set color indicates destroy e[vertex_set_ NUM].

Bheapunion (E[v_root])//merge corresponding Edge set--vertex_set_total;//vertex set number minus 1}} const int nodenum = 9;
	int main () {agraph graph (nodenum), MST (Nodenum);
	Graph.initgraph ();
	Graph.print ();
	cout << Endl; Graph.
	Bhmst (MST);
	Mst.print ();
	GetChar ();
return 0; }

Using the 22nd chapter of the diagram, such as the following, alphabetically numbered from 1, the results of the operation are as follows:




 

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.