只是將演算法的主常式邊的權值改為異號的,即可完成這個演算法.我的初衷是將最小堆改成最大堆.但事實證明,這是行不通的.貼代碼.
/*9-20-12-22-11.16.c -- 第九章第二十題*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include "binary_heap_for_kruskal.h"<br />#include "disjiont_set.h"</p><p>#define SIZE (10)</p><p>int main (void) ;<br />void kruskal (const Adjacenty_List * const padj, const Hash_Table * const pht, const int start, char (* const result)[2], int * const weight) ;<br />void print_result (const char (* const result)[2], const int * const weight, const int size) ;</p><p>int main (void)<br />{<br />Adjacenty_List adj ;<br />Hash_Table ht ;<br />int capacity = 10 ;<br />char result[SIZE][2] ;<br />int weight[SIZE], size = SIZE ;</p><p>Initialize_H (&ht, capacity * 2) ;<br />Initialize_A (&adj, capacity) ;</p><p>/*Initialize the adjacenty list*/<br />InitializeALine_A (&adj, &ht, 0, 'a', 0, 6, 'd', 4, 'e', 4, 'b', 3) ;<br />InitializeALine_A (&adj, &ht, 1, 'b', 0, 8, 'a', 3, 'e', 2, 'f', 3, 'c', 10) ;<br />InitializeALine_A (&adj, &ht, 2, 'c', 0, 6, 'b', 10, 'f', 6, 'g', 1) ;<br />InitializeALine_A (&adj, &ht, 3, 'd', 0, 6, 'a', 4, 'e', 5, 'h', 6) ;<br />InitializeALine_A (&adj, &ht, 4, 'e', 0, 12, 'd', 5, 'a', 4, 'b', 2, 'f', 11, 'i', 1, 'h', 2) ;<br />InitializeALine_A (&adj, &ht, 5, 'f', 0, 12, 'e', 11, 'b', 3, 'c', 6, 'g', 2, 'j', 11, 'i', 3) ;<br />InitializeALine_A (&adj, &ht, 6, 'g', 0, 6, 'f', 2, 'c', 1, 'j', 8) ;<br />InitializeALine_A (&adj, &ht, 7, 'h', 0, 6, 'd', 6, 'e', 2, 'i', 4) ;<br />InitializeALine_A (&adj, &ht, 8, 'i', 0, 8, 'h', 4, 'e', 1, 'f', 3, 'j', 7) ;<br />InitializeALine_A (&adj, &ht, 9, 'j', 0, 6, 'i', 7, 'f', 11, 'g', 8) ;</p><p>kruskal (&adj, &ht, 0, result, weight) ;<br />print_result (result, weight, size - 1) ;</p><p>Release_H (&ht) ;<br />Release_A (&adj) ;</p><p>return 0 ;<br />}</p><p>void kruskal (const Adjacenty_List * const padj, const Hash_Table * const pht, const int start, char (* const result)[2], int * const weight)<br />{<br />Adjoin_To_Vertex * scan ;<br />Disjiont d ;<br />SetType vset, wset ;<br />Binary_Heap bh ;<br />Edge edge ;<br />int capacity = (*padj) -> capacity, vertex_sub = capacity, i ;</p><p>d = (int *) malloc (sizeof (int) * ((*pht) -> capacity + 1)) ;<br />if (NULL == d)<br />return ;<br />/*Make sure the size of Disjiont is enough large.*/<br />Initialize_D (d, (*pht) -> capacity) ;<br />/*Every edge has two vertexs and every edge will enqueueing twice.*/<br />Initialize_B (&bh, capacity * 4) ;</p><p>for (i = 0; i < capacity; i++)<br />{<br />edge.v_hash_value = (*padj) -> list[i].hash_value ;<br />scan = (*padj) -> list[i].adjoin_to ;<br />while (scan)<br />{<br />edge.w_hash_value = scan -> hash_value ;<br />/*This is the crux of the matter*/<br />edge.weight = -scan -> cvw ;<br />Insert_B (&bh, &edge) ;<br />scan = scan -> next ;<br />}<br />}<br />i = 0 ;<br />while (i < vertex_sub - 1)<br />{<br />DeleteMin_B (&bh, &edge) ;<br />vset = Find_D (d, edge.v_hash_value) ;<br />wset = Find_D (d, edge.w_hash_value) ;<br />if (vset != wset)<br />{<br />Union_D (d, vset, wset) ;<br />result[i][0] = (*pht) -> lists[edge.v_hash_value].name ;<br />result[i][1] = (*pht) -> lists[edge.w_hash_value].name ;<br />weight[i] = edge.weight ;<br />i++ ;<br />}<br />}<br />free (d) ;<br />Release_B (&bh) ;<br />}</p><p>void print_result (const char (* const result)[2], const int * const weight, const int size)<br />{<br />int i ;</p><p>for (i = 0; i < size; i++)<br />printf ("%c to %c is %d/n", result[i][0], result[i][1], weight[i]) ;<br />}