Minimum Spanning Tree prim algorithm C Language

Source: Internet
Author: User

I was writing the Kruskal algorithm and thought that I should not be too overdrawn. After all, the young capital would easily be squandered. Yesterday I did not take off my clothes and slept all night.

It will take up to 5.30 days, and it will be a day. It will be much better to write code when you are busy recently. Otherwise, it will be annoying to get stuck in your head.

I am patient and have been patient. It is for the sake that my patience can be exchanged for the silent "support" of the surrounding environment, that is, no one stops me.

Maybe I am not patient enough, maybe I am frivolous, in short, the situation is not good. in any case, I have to take care of my loved ones and myself. I still choose to give up some code. this is my future.

Okay, let's get out and go to bed. Come back from work and finish Kruskal. It's almost done.

Come on. What else will happen.

We look forward to giving ourselves a holiday on our birthday.

/* 9-18 (a)-12-18-02-42.c -- Chapter 9, question 1 */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include "binary_heap.h" </P> <p> int main (void); <br/> int prim (const adjacenty_list * padj, const hash_table * const PHT, const int start); </P> <p> int main (void) <br/>{< br/> adjacenty_list adj; <br/> hash_table HT; <br/> int capacity = 10; </P> <p> initialize_h (& HT, capacity * 2 ); <br/> initialize_a (& adj, capacity); <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); <br/> prim (& adj, & HT, 0); <br/> printadjacenty_list_a (& adj, & HT ); </P> <p> release_h (& HT); <br/> release_a (& adj); </P> <p> return 0; <br/>}</P> <p> int prim (const adjacenty_list * padj, const hash_table * const PHT, const int start) <br/>{< br/> binary_heap BH; <br/> vertex * V; <br/> adjoin_to_vertex * w; <br/> int capacity = (* padj) -> capacity, I; </P> <p> initialize_ B (& BH, capacity * 2); <br/> (* padj)-> list [start]. dist = 0; <br/> for (I = 0; I <capacity; I ++) <br/> insert_ B (& BH, (* padj) -> List + I); <br/> while (1) <br/>{< br/> DO <br/> {<br/> V = deletemin_ B (& BH ); <br/> If (negativeinfinity = V-> Dist) <br/> break; <br/>}< br/> while (true = (* Pht) -> lists [V-> hash_value]. be_deleted) <br/>; <br/> If (negativeinfinity = V-> Dist) <br/> break; <br/> (* Pht) -> lists [V-> hash_value]. be_deleted = true; <br/> V-> known = true; <br/> W = V-> adjoin_to; <br/> while (W) <br/>{< br/> If (false = (* padj)-> list [(* Pht)-> lists [w-> hash_value]. index_in_adjacenty_list]. known) <br/>{< br/> If (W-> cvw <(* padj)-> list [(* Pht)-> lists [w-> hash_value]. index_in_adjacenty_list]. dist) <br/>{< br/> (* padj)-> list [(* Pht)-> lists [w-> hash_value]. index_in_adjacenty_list]. dist = W-> cvw; <br/> (* padj)-> list [(* Pht)-> lists [w-> hash_value]. index_in_adjacenty_list]. path = (* Pht)-> lists [V-> hash_value]. name; <br/>}< br/> insert_ B (& BH, (* padj)-> List + (* Pht)-> lists [w-> hash_value]. index_in_adjacenty_list); <br/>}< br/> W = W-> next; <br/>}< br/> release_ B (& BH ); </P> <p> return 1; <br/>}

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.