Dijkstra Algorithm Based on the Minimum Spanning Tree Algorithm

Source: Internet
Author: User

Dijkstra Algorithm Based on the Minimum Spanning Tree Algorithm

Dijkstra Algorithm

Dijkstra is a typical Shortest Path used to calculate the shortest path from one node to all other nodes. The main feature is to expand horizontally at the center of the starting point until the end point is reached. Dijkstra algorithm can obtain the optimal solution of the shortest path, but it is inefficient because it traverses Many computing nodes.

 

Dijkstra algorithm is used to find the shortest path between any two vertices. In this algorithm, we use the adjacent matrix to store graphs. Set a two-dimensional array in this program to store the edge weights between any two vertices. You can enter the information of any graph on the keyboard, and then enter the two vertices to be searched. The program can automatically find the shortest path between the two vertices.

Algorithm IDEA

S is the source, Map [u, v] is the length of the edge between Point u and v, and the result is stored in dis [];

Initialization: the source distance dis [s] is set to 0, the distance from other points is set to infinite, and the status of all points is set to not extended.

Loop n-1 times:

1. Use the flag variable array to take a point u with the smallest distance from the point that has not been extended, and set its status to extended.

2. for each vertex v adjacent to u, if dis [u] + Map [u, v] <dis [v], update dist [v] to a shorter distance from dis [u] + Map [u, v]. In this case, the first node is u in the shortest path of point v.

End: For any u, dist [u] is the distance from s to u.

Attached Hdu 2544 Short Circuit

Question-making ideas

Change the initial value in the two-dimensional array of A Map to an INF (large number) to store the path length from A to B.

Create an array of Dis to store the data loop N-1 in Map [s] [0 → n] and mark the smallest label in dis each time

If (! Flag [I] & dis [I]> dis [t] + Map [t] [I]) this label updates the Dis Array

After the update, dis [x] is the shortest distance from S to x.


Code

# Include <stdio. h> // Dijkstra algorithm # include <iostream> # include <string. h> # define INF 10000000 using namespace std; int Map [101] [101], dis [110]; int n, m; void Dijkstra () {int I, j, D = 0; bool flag [110]; memset (flag, 0, sizeof (flag); // tag array for (I = 1; I <= n; I ++) dis [I] = Map [1] [I]; // The distance from 1. Each I represents the distance between itself and 1. for (I = 1; I <n; I ++) // mark a point in each loop to mark n-1 points in total {int temp = INF; for (j = 1; j <= n; j ++) // find the smallest value position in the dis array and use D to record if (! Flag [j] & dis [j] <temp) {temp = dis [j]; D = j;} flag [D] = 1; // mark this point as for (j = 1; j <= n; j ++) // update the dis array to D {if (! Flag [j] & dis [j]> Map [D] [j] + dis [D]) dis [j] = Map [D] [j] + dis [D] ;}} int main () {int I, j, x, y, z; while (scanf ("% d", & n, & m )! = EOF & (m | n) {for (I = 0; I <101; I ++) {// Map clearing is initialized to a large number for (j = 0; j <101; j ++) Map [I] [j] = INF; map [I] [I] = 0;} for (I = 1; I <= m; I ++) {cin> x> y> z; // input if (Map [x] [y]> z) // if the input is smaller than the previous one, overwrite Map [x] [y] = Map [y] [x] = z;} Dijkstra (); printf ("% d \ n", dis [n]); // after all cycles, the value of dis [n] is the distance from 1 to n;} return 0 ;}

Appendix:

ACM holiday training sentiment:

In a twinkling of an eye, half a month has passed. In the morning, I listened to my teacher's brother to talk about algorithms and read books. In the afternoon, I reviewed questions and summarized them;

Others say that our programmers are bitter and tired, and there is also a nickname "diaosi programmers ";

But I don't think so. I learn algorithms every day to train my thinking, and I need to brush questions to improve my code and logic thinking.

During ACM training, the happiest time was to take a long time to correct the error. WA tried again and finally had the last BUG, AC!

The mood is very comfortable, watching the leaves outside the window is so green, the sky is so blue.

How happy it is to be able to live in college with such enrichment, motivation, goals, and step by step towards goals.

Here, I will send a sentence:

The whole world cannot fill up the ambitions and dreams of 18-year-olds.


What are the differences between the two algorithms?

The Prim algorithm looks for the next vertex closest to any vertex in the MST;
The Dijkstra algorithm looks for the next vertex closest to the given vertex (single source.
In addition, when two edges with the same minimum weight are available, you can select one of them. Therefore, the MST constructed is not unique.
The Prim algorithm and Dijkstra algorithm are very similar. The only difference is:
The Prim algorithm is used to find the closest vertex to the added vertex;
Dijkstra algorithm is used to find the closest vertex to a fixed vertex.
Therefore, the time complexity analysis of the Prim algorithm is the same as that of the Dijkstra algorithm, which is O (| V ^ 2 |)

How to optimize the MATLAB program of the minimal spanning tree of Dijkstra Algorithm

I have done this algorithm, but what do you mean by optimization?
 

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.