Data structure/graph theory: minimum spanning tree problem

Data structure/graph theory: minimum spanning tree problem

1. Problem description

The spanning tree of a graph is a subgraph containing all vertices and is a tree. A graph may have many spanning trees. Spanning tree problem is one of the typical problems solved by network theory, and there are many application fields.
Application: Minimize the welding circuit required to connect a series of connectors on the circuit board; Minimize the circuit required to establish a telephone network between several cities.

Two, algorithm implementation

1. Prim algorithm

Algorithm idea:
Prim's algorithm is a greedy algorithm. Take any vertex v in the graph as the root of the spanning tree, and then add a new vertex w to the spanning tree. There must be an edge between the added vertex w and the vertex v already on the spanning tree, and the weight of this edge takes the smallest value among the edges between all connected vertices v and w. Then continue to add vertices to the spanning tree until there are n vertices in the spanning tree.

Pseudo code implementation:

void Prim(Graph<string>* G, int* D, int s)
{
    
    
	// Prim's MSTalgorithm构造最小生成树算法
	int V[G->n()];// Store closest vertex 存储最近节点
	int i, w;
	for (i=0;i<G->n(); i++)
	{
    
    // Process the vertices 处理节点
	int v =minVertex(G, D);
	G->setMark(v, VISITED);
	if (v != s)
		AddEdgetoMST(G,V[v], v);// Add edge to MST 向MST中添加边
	if (D[v] ==INFINITY) return;// Unreachable vertices 不可达节点
	for(w=G->first(v); w<G->n(); w = G->next(v,w))
	if (D[w]> G->weight(v,w))
		{
    
    D[w] =G->weight(v,w);// Update distance 更新距离
		V[w] =v;// Where it came from记录节点
		}
	}
}

minVertex function:

// Find min cost vertex寻找未访问的最短距离顶点
int minVertex(Graph* G,int* D)
{
    
    
	int i,V;
	// Set v to an unvisited vertex
	for(i=0;i<G->n();i++)
	if(G->getMark(i)==UNVISITED)
	{
    
    v=i;break;}
	// Now find smallest D va lue
	for(i++;i<G->n0;i++)
	if((G->getMark(i)==UNVISITED)&&(i<D[v1]))
	v=i;
	return V;
}

Prim algorithm implemented by priority queue:

void Prim(Graph* G, int* D, int s)
{
    
    
	int i, v,w; // The current vertex
	int V[G-n()]; // Who's closest
	DijkElem temp;
	DijkElem E[G->e()]; // Heap array with lots of space 
	temp.distance = 0;
	temp.vertex = s;
	E[0] = temp; // Initialize heap array
	minheap< DijkElem,DDComp> H(E, 1, G->e()); // Create the heap
	for (i=0; i<G->n(); i++)
	{
    
    // Now build MST
		do
		{
    
    if (!H.removemin(temp)) return;
		v = temp.vertex;
		} while (G.getMark(v) == VISITED);
	G->setMark(v, VISITED);
	if (v != s) AddEdgetoMST(V[v], v);
	if (D[v] == INFINITY) return; // Remaining vertices
	for (w = G->first(v); w<G->n(); w = G->next(v,w))
	if (D[w] > G->weight(v,w))
		{
    
    D[w] = G->weight(v,w);
		V[w] = v; temp.distance = D[w];
		temp.vertex = w;
		H.insert(temp); // Insert new distance in heap
		}
	}
}

Note: Suitable for dense graphs.

2. Kruskal algorithm

Algorithm idea: Kruskal algorithm is also a simple greedy calculation. First, divide the vertex set into |V| equivalence classes, and each equivalence class includes a vertex. Then, the edges are processed in order of the size of the weight. If an edge connects two vertices of different equivalence classes, this edge is added to the MST, and the two equivalence classes are also merged into one. Repeat this process until only one equivalence class remains.

Pseudo code implementation:

Class KruskElem
{
    
    
Public:
	int from,to,distance;
	KruskElem()
	{
    
    from=to=distance=-1;}
	KreskElem(int f,int t,int d)
	{
    
    from=f;to=t;distance=d;}
};
Kruskel(Graph* G)
{
    
    
	Gentree A(G->n());
	KruskElem E[G->e()];
	int I;
	int edgecnt=0;
	for (i=0;i<G->n();i++)
	for (int w=G->first(I);w<G->weight(I,w))
	{
    
    E[edgecnt].distance=G->weight(i,w);
	E[edgecnt].from=i;
	E[edgecnt++].to=w;
	}
	Minheap<KruskElem,KKComp>H(E,edgecnt,edgecnt);
	int numMST=G->n();
	for (i=0;numMST>1;i++)
	{
    
    KruskElem temp;
	H.removemin(temp);
	int v=temp.from;
	int u=temp.to;
	if (A.differ(v,u))
		{
    
    A.UNION(v,u);
		AddEdgetoMST(temp.from,temp.to);
		numMST--;
		}
	}
}

Note: Applies to sparse graphs.

Guess you like

Origin blog.csdn.net/hyl1181/article/details/107783692