Breadth-first search-minimum number of connections and breadth-first connections

Source: Internet
Author: User

Breadth-first search-minimum number of connections and breadth-first connections

When you travel to Hainan with your family, but your city has not directly arrived in Hainan, but you have collected a lot of flight information, and now you want to find a way to ride, minimize the number of connections

How can this problem be solved?

Assume that your city is in City 1 and Hainan is in city 5. The relationship is as follows:


How can we obtain the minimum number of transfers from City 1 to city 5? At this time, the content of this explanation is used,Search for breadth first!

First, we should useAdjacent matrixOrTwo-dimensional arrayTo access the relationship between vertices.

The extended search requires a queue to store the relationships of each extension.

First of all, we will join City 1. Through city 1, we can expand City 2 and City 3, and city 2 can expand City 3 and City 4. As City 3 is already in the queue, you only need to team City 4.

Next, city 3 can expand to City 4 and city 5. Because City 4 is already in the queue, you only need to team city 5. Now that city 5 has been found, the algorithm ends.



The Code is as follows:

# Include <stdio. h> struct node {int x; // city no. int s; // Number of connections} que [2501]; int main () {int e [51] [51] = {0}, book [51] = {0}; // store the relationship between the graph and the flag array int head, tail; int I, j, n, m, a, B, cur, start, end, flag = 0; scanf ("% d", & n, & m, & start, & end); // n indicates the number of cities, m indicates the relationship, start indicates the start city, and end indicates the destination city // initializes the two-dimensional matrix for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) if (I = j) e [I] [j] = 0; // This is the distance from yourself to yourself. else e [I] [j] = 0x3f3f3f3f; // hexadecimal, represents an infinite constant} // read the flight between cities for (I = 1; I <= m; I ++) {sca Nf ("% d", & a, & B); e [a] [B] = 1; // The undirected graph is a bidirectional e [B] [a] = 1;} // initialize the queue head = 1; tail = 1; // starts from the city of start, add the start city to the queue que [tail]. x = start; que [tail]. s = 0; tail ++; book [start] = 1; // The city marked with start is already in the queue. // when the queue is not empty, loop while (head <tail) {cur = que [head]. x; // number of the first city in the queue for (j = 1; j <= n; j ++) // try from 1-N in turn {// determine whether the city j has flights from the city cur to the city j and whether the city j is in the queue if (e [cur] [j]! = 0x3f3f3f3f & book [j] = 0) {// if there is a flight from cur to city j and city j is not in the queue, then j enters the queue que [tail]. x = j; que [tail]. s = que [head]. s + 1; // Number of connections + 1 tail ++; book [j] = 1; // change the flag to prevent reuse} // stop scaling when the target city is reached, exit loop if (que [tail]. x = end) {flag = 1; break ;}}if (flag = 1) break; head ++; // after the extension ends, head ++ can continue expansion} printf ("% d \ n", que [tail-1]. s); // Because tail points to the next position at the end of the queue, 1 return 0;}/* 5 7 1 51 21 32 32 43 43 54 5 */





Related Article

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.