[Algorithm] Find the shortest path Dijkstra, Floyd
Update 25/05/2014: Do some of your comments, so I wrote more 1 Dijkstra's algorithm program structure and function by the way also slightly revised code for brighter and more accurate ^^.
Update 27/09/2014: additional pascal code of the algorithm here: http://ideone.com/c7J0dq
Content
Dijkstra Algorithm
Floyd algorithm
Advanced Code for 2 algorithm
Update 14/06/2014: The simulation algorithm Dijkstra
In this article only refers to the algorithm to find the shortest path Dijkstra and Floyd, some related terms I will not explain or define, you find out in the book or on the Internet.
The problem of single-source shortest path problem is to find a path between two vertices such that the sum of the weights of the edges that make up the smallest way. Or to put it mathematically is:
For single connected graph, weighted G =(The,And). Find the distance d(the,b) from a given vertex to a vertex of G and b any finding the shortest path from a to b.
As the post title, we will learn 2 algorithm to solve using graph adjacency matrix structure(note we consider the weight of the graph is not negative).
Adjacency matrix of a graph with n vertices is a square matrix G n is the number of rows of columns. G[in][j] the length of the path from i to vertex j top. Considering the undirected graph G[in][j] G =[j][in]. Length from top to itself is 0 (G[in][in] = 0). If between 2 i and j edges of the graph does not exist the way it G[in][j] = Infinity (∞). However, the performance of the computer, the value is set to ∞ 1 constant is very large or the total value of the matrix (total length of the edge).
1. Dijkstra Algorithm
About Dijkstra algorithm has 2 type is to find the shortest path from 1 sources to top 1 top destination and find the shortest path from 1 source vertex to the top left of the graph, and here I will talk about stuff 1. (the second kind you can find on the net or just change the line while (with[b] == 0) (current 43 by code 1 & current 76 by code 2) from the loop browser 0 n-1 is going to find all the vertices).
– Use 1 Mangal only[] – Only[in] is the shortest distance from a vertex to vertex i.
– Use 1 Plate S marking the special vertex i (the peak current i that time, the path from a to i is the shortest).
– Using array P[] marked paths. P[j] If i = i j is the peak ahead of the shortest path.
– Reset extremely valuable for top pair no way.
– Initialize all the way from Asia to the other peaks in extremely.
– Initialize path from a primary to a = 0.
– Browse all the vertices of the graph V
+ Find top i not in S that the path from a to i is the shortest for inclusion in S. If you can not find any means approved top of the peak can go and still have not found the top destination => could not walk.
+ If you find the vertex i j browse all vertices not in S. New only[in] + G[in][j] < Only[j] (where G[in][j] is the distance from the vertex i to vertex j) then assign Len[j] = Len[in] + G[in][j]; and marked paths P[j] = I.
Noted: Because in C, array starting 0. Therefore, when calculating the peak will from top 0 the top n-1. However, it remains to show that is from the top 1 to n and the file input.inp the top and the top end will be calculated from 1 to n. Hence the code before we need to reduce the computation and the top end of the travel tip 1 unit. After the calculation is complete, the results when the need to increase the peak of the road looking up 1 unit to display properly (For example we want to compute the path from the top 4 to top 8, the top 4 corresponding to the position 3 in the array, top 8 corresponding position 7 so we need to reduce 4 down 3, 8 down 7 to calculate. When you find a way, assuming that 3 -> 5 -> 4 -> 7 must be in place as 4 -> 6 -> 5 -> 8).
We'll go practice according to the following chart 2 code is doing right in and follow the main content:
file input.inp: The first line is 8 point, away from the point 4 to the point 8. matrix 8×8 below is the adjacency matrix of the graph.
8 4 8 0 3 5 2 0 0 0 0 3 0 1 0 7 0 0 0 5 1 0 4 0 1 0 0 2 0 4 0 0 3 6 0 0 7 0 0 0 2 0 3 0 0 1 3 2 0 4 6 0 0 0 6 0 4 0 5 0 0 0 0 3 6 5 0
* Code within the main
This Code was revised and fix some bugs from code days ago (or link redundancy).
#include <stdio.h> #include <stdlib.h> #define INP "input.inp" #define OUT "output.out" int main() { FILE *fi = fopen(INP, "r"); FILE *fo = fopen(OUT, "w"); int n, a, b, i, sum = 0; // nhap du lieu tu file input fscanf(fi, "%d%d%d", &n, &a, &b); int G[n][n]; int S[n], Len[n], P[n]; // nhap ma tran va tinh gia tri vo cung (sum) for (i = 0; i < n; i++) for (int j = 0; j < n; j++) { fscanf(fi, "%d", &G[i][j]); sum += G[i][j]; } // dat vo cung cho tat ca cap canh khong noi voi nhau for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j && G[i][j] == 0) G[i][j] = sum; } } /* Do mang tinh tu G[0][0] nen can giam vi tri di 1 don vi de tinh toan cho phu hop*/ a--; b--; for (int i = 0; i < n; i++) { Len[i] = sum; // khoi tao do dai tu a toi moi dinh la vo cung S[i] = 0; // danh sach cac diem da xet P[i] = a; // dat diem bat dau cua moi diem la a } Len[a] = 0; // dat do dai tu a -> a la 0 // tim duong di ngan nhat tu 1 dinh den moi dinh khac thi thay bang vong for: //for (int k = 0; k < n; k++) while (S[b] == 0) { // trong khi diem cuoi chua duoc xet for (i = 0; i < n; i++) // tim 1 vi tri ma khong phai la vo cung if (!S[i] && Len[i] < sum) break; // i >=n tuc la duyet het cac dinh ma khong the tim thay dinh b -> thoat if (i >= n) { printf("done dijkstra\n"); break; } for (int j = 0; j < n; j++) { // tim diem co vi tri ma do dai la min if (!S[j] && Len[i] > Len[j]) { i = j; } } S[i] = 1; // cho i vao danh sach xet roi for (int j = 0; j < n; j++) { // tinh lai do dai cua cac diem chua xet if (!S[j] && Len[i] + G[i][j] < Len[j]) { Len[j] = Len[i] + G[i][j]; // thay doi len P[j] = i; // danh dau diem truoc no } } } printf("done dijkstra\n"); /* Do ta dang tinh toan tu dinh 0 nen muon hien thi tu dinh 1 thi can dung i + 1 de phu hop */ printf("start find path\n"); if (Len[b] > 0 && Len[b] < sum) { fprintf(fo, "Length of %d to %d is %d\n", a + 1, b + 1, Len[b]); // truy vet while (i != a) { fprintf(fo, "%d <-- ", i + 1); i = P[i]; } fprintf(fo, "%d", a + 1); } else { fprintf(fo, "khong co duong di tu %d den %d\n", a + 1, b + 1); } printf("done find path\n"); fclose(fi); fclose(fo); printf("done - open file output to see result\n"); return 0; }
* The code for each function
In the code as a function containing:
– readData implementation reads the input file.
– dijkstra implementation of algorithms
– back implementation returns the string is the way to find
– outResult implementation results in the output file
#include <stdio.h> #include <stdlib.h> #include <cstring> #define INP "input.inp" #define OUT "output.out" // read data in file input int readData(int ***G, int *n, int *a, int *b) { FILE *fi = fopen(INP, "r"); if (fi == NULL) { printf("file input not found!\n"); return 0; } printf("start read file\n"); fscanf(fi, "%d %d %d", n, a, b); *G = (int **) malloc((*n) * sizeof(int)); for (int i = 0; i < *n; i++) { (*G)[i] = (int *) malloc((*n) * sizeof(int)); for (int j = 0; j < *n; j++) { int x; fscanf(fi, "%d", &x); (*G)[i][j] = x; } } fclose(fi); printf("done read file\n"); return 1; } // thuat toan dijkstra int dijkstra(int **G, int n, int a, int b, int P[]) { /* Do mang tinh tu G[0][0] nen can giam vi tri di 1 don vi de tinh toan cho phu hop*/ a--; b--; printf("start dijkstra\n"); int* Len = (int *) malloc(n * sizeof(int)); int* S = (int *) malloc(n * sizeof(int)); int sum = 0; // gia tri vo cung // tinh gia tri vo cung (sum) for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { sum += G[i][j]; } } // dat vo cung cho tat ca cap canh khong noi voi nhau for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j && G[i][j] == 0) G[i][j] = sum; } } for (int i = 0; i < n; i++) { Len[i] = sum; // khoi tao do dai tu a toi moi dinh la vo cung S[i] = 0; // danh sach cac diem da xet P[i] = a; // dat diem bat dau cua moi diem la a } Len[a] = 0; // dat do dai tu a -> a la 0 int i; // tim duong di ngan nhat tu 1 dinh den moi dinh khac thi thay bang vong for: //for (int k = 0; k < n; k++) while (S[b] == 0) { // trong khi diem cuoi chua duoc xet for (i = 0; i < n; i++) // tim 1 vi tri ma khong phai la vo cung if (!S[i] && Len[i] < sum) break; // i >=n tuc la duyet het cac dinh ma khong the tim thay dinh b -> thoat if (i >= n) { printf("done dijkstra\n"); return 0; } for (int j = 0; j < n; j++) { // tim diem co vi tri ma do dai la min if (!S[j] && Len[i] > Len[j]) i = j; } S[i] = 1; // cho i vao danh sach xet roi for (int j = 0; j < n; j++) { // tinh lai do dai cua cac diem chua xet if (!S[j] && Len[i] + G[i][j] < Len[j]) { Len[j] = Len[i] + G[i][j]; // thay doi len P[j] = i; // danh dau diem truoc no } } } printf("done dijkstra\n"); return Len[b]; } // truy vet duong di void back(int a, int b, int *P, int n, char *path) { //char *path = (char *) malloc((n * 10) * sizeof(char)); /* Do mang tinh tu G[0][0] nen can giam vi tri di 1 don vi de tinh toan cho phu hop*/ a--; b--; printf("start find path\n"); int i = b; int point[n]; // danh sach cac dinh cua duong di int count = 0; /* Do ta dang tinh toan tu dinh 0 nen muon hien thi tu dinh 1 thi can dung i + 1 de phu hop */ point[count++] = i + 1; while (i != a) { i = P[i]; point[count++] = i + 1; } strcpy(path, ""); char temp[10]; for (i = count - 1; i >= 0; i--) { sprintf(temp, "%d", point[i]); strcat(path, temp); if (i > 0) { sprintf(temp, " --> "); strcat(path, temp); } } printf("done find path\n"); } void outResult(int len, char* path) { FILE *fo = fopen(OUT, "w"); if (len > 0) { fprintf(fo, "\nLength of %c to %c is %d\n", path[0], path[strlen(path) - 1], len); } fprintf(fo, "path: %s\n", path); fclose(fo); } int main() { int **G, n, a, b, len; if (readData(&G, &n, &a, &b) == 0) { return 0; } char *path = (char *) malloc((10 * n) * sizeof(char)); int P[n]; len = dijkstra(G, n, a, b, P); if (len > 0) { back(a, b, P, n, path); outResult(len, path); } else { char *path = (char *) malloc((n * 10) * sizeof(char)); sprintf(path, "khong co duong di tu %d den %d\n", a, b); outResult(len, path); } printf("done - open file output to see result\n"); return 0; }
Looking code seems a bit long, but when reading long ball tẹo then does@@=)).
2. Floyd algorithm
This algorithm allows us to find the shortest path between every pair of vertices.
If k vertices lie on the shortest path from vertex i to vertex j, the distance from i to j and from j to j is the shortest path from i to j and from j to j corresponding. Therefore we use the matrix A to save the shortest path length between every pair of vertices.
– Initially we set A[in,j] C =[in,j], A container that is initially direct path lengths (do not go over the top at all).
– Then perform n iterations, after the first iteration k, ma trận A sẽ chứa độ dài đường đi ngắn nhất giữa mọi cặp đỉnh chỉ đi qua các đỉnh thuộc tập .comment-content 1. Such, after n iterations we obtain the matrix A contains the length of the shortest path between every pair of vertices in the graph.
– Ak is the matrix notation A later iteration k, ie Ak[in,j] là độ dài đường đi ngắn nhất từ i đến j chỉ đi qua các đỉnh thuộc .comment-content 0. If[in,j] calculated according to the following formula: If[in,j] = min .comment-body 9.
– In the iterative process we have to save the trace path, ie the shortest path passing through the vertex. Then we use sub-array P[nxn], in which P[in,j] top up if k shortest path from i to j k goes through peaks. Initially P[in,j]= 0 for all i,j, because then the shortest path is the direct path, do not go over the top at all.
Code algorithm:
void Floyd (int a, int b) { int max = tongthiethai(); for (int i=0; i<n; i++) for (int j=0; j<n; j++) { if (G[i][j]) A[i][j] = G[i][j]; else A[i][j] = max; P[i][j] = -1; } for (int k=0; k<n; k++) // lap n lan { for (int i=0; i<n; i++) // thay doi do dai duong di cua cac dinh for (int j=0; j<n; j++) if (A[i][j] > A[i][k] + A[k][j]) { A[i][j] = A[i][k] + A[k][j]; P[i][j] = k ; } } }
How to build a complete program is similar to Dijkstra's algorithm.
Advanced Code
This is the code for selection 1 in 2 algorithms and output file in accordance with the process as the results in the picture below or backup link
file input.inp:
8
A B C D E F G H
0 3 5 2 0 0 0 0
3 0 1 0 7 0 0 0
5 1 0 4 0 1 0 0
2 0 4 0 0 3 6 0
0 7 0 0 0 2 0 3
0 0 1 3 2 0 4 6
0 0 0 6 0 4 0 5
0 0 0 0 3 6 5 0
Menu console
Output Dijkstra
Output Floyd
hoi me why I run every so are discounted loans find my way. velvet stage hand, there is still.
@@You carefully review the code with input files offline. His running ok.
Oh how you copy my post code full runtime error on this
[Error] C:\UsersAdminDownloadsideone_DpaGPW.cpp:167: E2313 Constant expression required in function Dijkstra(GRAPH,int,int)
[Error] C:\UsersAdminDownloadsideone_DpaGPW.cpp:283: E2313 Constant expression required in function floyd(GRAPH,int,int)
You copy that code yet? for his return to the mail file nguyenvanquan7826@gmail.com see for yourself offline.
Intelligent send mail before it
I received then, grab your running code that is fine, no problems. Perhaps you use C-free so it imperfect or something error. You turn on your Teamview for offline viewing. Contact me via skye: nguyenvanquan7826
why not have a shortest path a @@ example 1->4=1<-2<-3<-4
Ah because in all his examples from 1-> 8 that. not going to 4 oil.
WHY I run the program it appeared it found that you have to input files .cpp files and files in general .inp 1 folder and. Thank you
You review the file extension nhé, may end your file is not a .txt .inp which is hidden.
You show up to check the file extension here with win 7, here with win 10 nhé.
I run the program it appeared it found that you have to input files .cpp files and files in general .inp 1 folder and. Thank you
You see the tail of the file correct nhé. File extension may be hidden and your files really are input.inp.txt. you see it this post.http://thuthuat.taimienphi.vn/cach-hien-duoi-file-trong-win-10-21939n.aspx
You can change the code into Pascal is not so? Know 99% answers will be “I'm very busy!” but still hope you will turn into 1 code written in Pascal to disseminate knowledge than to the newbie is not fluent in C.
Thank you for reading.
I'll try to convert pascal code into it this week. Cảm ơn bạn đã quan tâm tới các bài viết trong blog và đặc biệt là tinh thần chia sẻ của bạn 🙂
You can see right here in pascal code http://ideone.com/c7J0dq
Thank you very much, VN will forward each day more people like you.
Wishing you many more articles soon or community who love programming.
Cảm ơn bạn 🙂 Mình sẽ cố gắng viết tốt hơn 😉
Brother help me with this! I'm doing coursework essays, Post your problem as follows:
Đồ thị vô hướng G= .comment-body 8 is given by the list next to DS. Gnarled, v of V. Construction of two paths algorithm A1(in,in) and A2(in,in) so that no edge in common and the total length of the shortest.
I still faltering but do not know where to start again sir. Hope you help me dùm.
This essence is to find the shortest path that.
A1 + The shortest A2 A1 and A2 shortest shortest. You use Dijkstra find A1, A2, in the process, each time finding A2 1 to see the other side of the A1 has no offline.
thank you sir
This algorithm he did not, sir vs Heap? I'm not quite clear Dijkstra Heap. If not, I can do or just for e k sir DC?
This he has done with Heap. It is not against Heap never lun :D. Em thử search trên mạng xem có không 😀
yes sir. He asked me more. With categories 2, I want to find words 1 top to the outside instead of the loop, I have to change the j again sir, k as well as the input you must know how. Moreover, DC Kids do not run code on each part of a bye. It runs to start Dijkstra section shall crush sir. He can only e k sir DC?
When run from 1 vertex to every other vertex rotations for only browse the entire top is finished, no need to add anything else.
The code for each function, you keep building that is normal.
Gold thank you
my friend asked yourself ti..minh do on this project which uses java you can explain more about your java code section ko DC
forget,,What you really need your help her with nha..minh thank you much
You go ahead, if he would help :in
you can explain yourself Dum code section does not run sequence DC Arts :((.you give your phone number you go
Art is how you run sequence?
There is nothing you can call on their.
096.567.7826
thấy bạn trl cmt mà rơi nước mắt vì mừng 🙂
Làm gì đến mức đó 😀
Welcome Nguyen Van Quan , I can invite you to know the answer of the following problems dc ko ?
the coordinate system 0xy , have the following
A1(586,363),A2(254,137),A3(467,516),A4(798,472),A5(599,213),A6(372,344),A7(146,412),
A8(818,346),A9(850,199),A10(314,260),A11(72,268),A12(731,57),A13(429,179),A14(499,81),
A15(89,169),A16(113,82),A17(904,59),A18(926,551),A19(54,20)
Suppose that 2 any segments are connected and undirected
Length joints (type of real) between 2 any type dc per ordinary mathematics learning
The starting point : A18(926,551)
Indicates the path lengths (type of real) shortest derived from A18 and go through all the remaining ones (each vertex must go through proper 1 time )
With all this it is a different algorithm, then you. I'll try to see how its solutions.
its really too lethargic about your algorithm k out more
You do not understand the code or algorithm? Dijkstra not understand art or floyd.
I did practice baseline algorithms,,,the interface will like it?
This is your only option. You can refer to your program here:
https://cachhoc.net/2014/06/14/java-thuat-toan-mo-phong-thuat-toan-dijkstra-tim-duong-di-ngan-nhat/
My brother has no such code java? C e know nothing
Learn đk java unknown C? it is equally, What are other rolls.
My ad, e are having problems : find the longest path in the graph k cycles PERT algorithm .. DC k e ad help?
Cái này anh chưa làm mà giờ cũng chưa có thời gian làm nữa 🙂 Em thông cảm nhá.
Oh my you edit 1 bit in your code is entered 2 points from the keyboard and move the printf scanf cin and cout but not running, you help yourself at all offline
#include
#include
#include
#define INP “input.txt”
using namespace std;
int main() {
FILE * fi = fopen(“input.txt”, “r”);
int n, the, b, in, sum = 0;
cout<>the;
cout<>b;
fscanf(be, “%d% d% d”, &n);
int G[n][n];
int S[n], Only[n], P[n];
for (i = 0; in < n; i )
for (int j = 0; j < n; j ) {
fscanf(be, "% D", &G[in][j]);
sum = G[in][j];
}
for (int i = 0; in < n; i ) {
for (int j = 0; j < n; j ) {
if (in != j && G[in][j] == 0)
G[in][j] = sum;
}
}
the–;
b–;
for (int i = 0; in < n; i ) {
Only[in] = sum;
S[in] = 0;
P[in] = A;
}
Only[the] = 0;
while (S[b] == 0) {
for (i = 0; in < n; i )
if (!S[in] && Only[in] < sum)
break;
for (int j = 0; j Only[j]) {
i = j;
}
}
S[in] = 1;
for (int j = 0; j < n; j ) {
if (!S[j] && Only[in] + G[in][j] 0 && Only[b] < sum) {
cout<< " the shortest path from the top "<<the + 1<<" den palace "<<b + 1<<" the "<< Only[b];
while (in != A) {
cout<< in + 1<<"<– ";
i = P[in];
}
cout<< the + 1;
} else {
cout<<"Trackless tu"<<the + 1<<" the "<< b + 1;
}
fclose(be);
return 0;
}
You review 2 This line nhá.
cout<>the;
cout<>b;
cin is used & gt; & gt;
cout is used <<
their use cin>> and cout << but that you are not
I just ran a and b are entered only then the program will not run again it is stop working clothes
his flight, he entered a and b only done it stop working report
Above I saw that you wrote this
cout<>the;
cout<>b;
fscanf(be, "% D% d% d", &n);
cout Post <>
entering each n, only 1 the% d is not it, 3 the cheat…
my friend asked yourself in line
” for (int j = 0; j Only[j])
i = j;
then !S[j] Mean?
!S[j] ie the point at which j is not.
why you are the way out
.comment-body 7
Why not export backIn order for peak finding should do it backwards. 🙂
I can not change back
Get, you see in the above code as a function offline.
Waiter, you get your complete code in http://pastebin.com/FiZzb3UH copy DevC running on the interface, it is not like a bye, it was run as a matrix. You sir DevC 5.7.1. He just made a mistake somewhere with you!
What is your, This code will run the interface as some form towards the end of his article that? You want it to show how?
a possible transfer to java code ko sir.
C with other java anything you. If you want to view it this:
https://cachhoc.net/2014/06/14/java-thuat-toan-mo-phong-thuat-toan-dijkstra-tim-duong-di-ngan-nhat/
swim! b can help you do the dc k directed graph????
the top half to the bottom half of the matrix is 2 always right direction k b???
Do not leave your, you just need to 2 different half is a directed graph that. Example 1-> 2 but 2 do not go to 1 then A[1][2] = 2, and A[2][1] = Infinity.
a can do simulations of this algorithm?? explain the code by simulating a?? e expect a learning programming help.
You can see in here, please
https://cachhoc.net/2014/06/14/java-thuat-toan-mo-phong-thuat-toan-dijkstra-tim-duong-di-ngan-nhat/
Top ! Enjoyed your presentation.
My friends asked themselves how file input.inp then create and to Where. I used visual studio 2010 run has not DC
detective input.inp 1 file text thôi, to the same folder as the file .c or .cpp
VS, I do not know. I have not used it, you just try
ok. his thank b nhé :in
ask yourself this warshall its algorithm wrote this arrogant display it just right from w0 to p6 end without seeing the show shortest path from nowhere to nowhere . you can view and edit your household help is k.
code :
#include
#include
int main()
{
int[10][10];
int k, n, in, j;
int p[10][10];
printf(“Click sizing n:”); scanf(“%d”,&n);
//Click w[0] va p[0]
for (i=0; in<n; i ) for (j=0; jw[0]:\n”);
for (i=0; in<n; i )
{
for (j=0; jp[0]:\n”);
for (i=0; in<n; i )
{
for (j=0; j<n; j ) if (p[in][j]!=32) printf("%3c",p[in][j]+64); else printf("%3c",32);
printf("\n");
}
getch();
//printed calculating w[to],p[to] to k = 1.2,…,to
for (k=0; k n in[%d]: p[%d]:\n”,k 1, k 1);
for (i=0; in<n; i )
{
for(j=0; jw[in][to]+in[to][j])
{
in[in][j]= W[in][to]+in[to][j];
p[in][j]=p[in][to];
}
printf (“%3d”,in[in][j]);
}
printf (” || “);
for (j=0; j<n; j )
if(p[in][j]==32) printf("%3c",p[in][j]);
else printf("%3c",p[in][j]+64);
printf("||"); printf("\n");
}
}
}
//*heart the shortest path
printf("Nhap dinh xuat phat s = "); scanf("% D",&with);
printf("Nhap dinh ket thuc f = "); scanf("% D",&f);
Dijkstra(n,the,The,most,with);
if(The[f]==vc) printf("Khong co duong di");
else
{
printf("\n Duong di tu %d den %d ngan nhat %d \n",with,f,The[f]);
induong(with,f,most);
}
delete L;
delete pi;
getch();
}
Cái thuật này mình chưa tìm hiểu nên xin lỗi bạn mình chưa rõ 🙂
he asked me,Floyd wants to assess how the algorithm ạ? I just said it out 0(n^3) How do you guide me evaluate this algorithm ko?thank sir
You see in this article how to evaluate nhé:
https://www.cachhoc.net/2013/06/14/thuat-toan-p1-cach-tinh-do-phuc-tap-thuat-toan-algorithm-complexity/
themselves ask the advanced code above why I ran on what the error code block letters ran wild
But his run was okay, it's how you error?
Do you put the * .c file, it is not c libraries should not have it. The library of C that you have to put the * .cpp file
e ask why floyd used Dijkstra negative weights not?? e is not clear?? e k out
Welcome, sorry you yesterday when you called me and confirmed my bias that remains true to the key algorithm negative. To learn about this we need to rely on a demonstrated algorithm:
“We would point out, when a vertex v is added to the set S, then d[in] the value of the shortest path from s to v.
By definition label d, d[in] the value of the shortest paths in the path from source s, over the top in S, then by an edge uv connected directly to v.
Suppose there exists a path from s to v lesser value d[in]. Thus in the way, peak exists between s and v not in S. Select w is the first such summit.
My path takes the form s – … – in – … – in. But due to non-negative edge weights should segment s – … – w greater length than the whole is not the way, and therefore worth less than d[in]. On the other hand, by choosing our w, Should the length of the segment s – … – w is d[in]. Thus d[in] < d[v], trái với cách chọn đỉnh v. Đây là điều mâu thuẫn. Vậy điều giả sử của ta là sai. Ta có điều phải chứng minh." Như vậy nếu trọng số của các cạnh có thể âm thì ta không khẳng định được "đoạn s - ... - w có độ dài bé hơn đoạn s-w-v" và như vậy ta không thể chứng minh tiếp. Trong khi đó xét thuật toán floyd: "Để đi từ a --> b. You lost 1 distance is x. The algorithm will find 1 indirect way from a — to — b and if this path is shorter direct route, we always assign minimum value directly by way of indirect way.” Because finding the right way gradually indirectly without looking like Dijkstra shortest segment should be unaffected by positive or negative weights.
My friend asked yourself is to find the shortest way to find your way around with different minimum weight game. There are important regardless of the number k eh?
In essence it is different but the problem we consider it almost seriously mileage numbers are not so different.
Asked yourself looking V – not?
V - What are you?
my friend you have the algorithm code FORD & Fulkerson proving not to apply
I did not have ah.
you have c code for all on yourself would like to learn c ++ I have not vf
His other post c code that.
Star e Dijkstra run code section 1 Back faulty stop working right?? a review PES sir?
What do you check for errors nhé.
Oh my stars you it just right this error
http://www.mediafire.com/view/aowfsuq6jpz5a0c/Loi.jpg
Their code by vs ko ko c should clarify.
Apparently this Java code you collect certain authors'!
Where have your java in this article you COE? 😛 Và đảm bảo tất cả các code trong bài này và code Demo chương trình đồ họa bằng Java đều do tay mình viết. Nếu bạn thấy ở chỗ khác thì chỉ có họ copy của mình thôi. 😛 😛
Chào Quân
Chỗ này code có vấn đề :
*G = (int **) malloc((*n) * sizeof(int));
Repair: sizeof *int
*G = (int **) malloc((*n) * sizeof(*int));
Có thể bạn run trên Os 32 bit nên kết quả vẫn đúng, trên 64bit chắc là có vấn đề.
Mình chỉ đọc qua thôi chứ chưa debug :))
God.
Ah rồi, mình có thấy lỗi. 🙂 cảm ơn bạn nhé.
a có thể mô phỏng thuật toán AT giúp e dc k?
AT la thuat toan gi the?
MÌnh vừa review code thì thấy 1 quite the wonder :
This code you assign:
int n, the, b, in, sum = 0; // ==> sum = 0 ;
for (int i = 0; in Len[0] = 0 , Only[1] = 0 …. Only[A-1] =0;
}
Only[the] = 0;
// This paragraph in the middle of nowhere reassigned Len[in] where i runs from 0 to n-1 ;
//==> Only[0] = 0 , Only[1] = 0 …. Only[A-1] =0;
for (int j = 0; j < n; j ) { // the length of the hybrid crystal diem sour comment
if (!S[j] && Only[in] + G[in][j] < Only[j]) {
Only[j] = Len[in] + G[in][j]; // changes to
??? the conditions Len[in] + G[in][j] < Only[j] ever happen not? when 0 + to < 0 ?
Reviewing code nhé, Len himself assigned to initial sum in a for loop
Only[in] = sum;
a very enthusiastic.. thanks for the article
But why use code blocks mk k mk vegetarian travelers DC subroutine declared early enough .
Bạn nhìn xem nó báo lỗi gì nhé, dựa vào đó mới sửa được 🙂
There
sao mình chạy code nó báo là không tìm thấy file input là sao vậy ad, e with
You remember to file the same folder input.inp ZIP file nhé.
Oh for f a question, if building graphs from the matrix rather read directly from the file k with k a?
Get. Read from the file is read only matrix that.
My running a code can not find the way to be a
a can send via e complete code PES a
for example the way from 1->3 Can not show results a
a possible result f preview Demo PES a
And that his code runs standard. In his article also examples always before.
DC code not run the shortest path back a @ example: 1<-2<-3<-4
no place starting position enter the end position back a
Is in the first line of that input. I read all nhé.
a can for code run by a PES file.txt
This code is run by entering your export file.
my pn ! yourself asked about floyd algorithm ! not how to run their hands as well as ZIP !
Welcome!
Ask yourself in Floyd algorithm
void Floyd (int a, int b)
{
int max = tongthiethai();
}
tongthiethai function() Your lies in the code you?
ah, That function is a function of a sum[in][j] against it, in this I do not write, you can write to it. Just use 2 nested loop plus all a[in][j] again.
a có thể viết cụ thể cái hàm tongthiethai() giúp e dc ko ạ, e thank
là hàm tính tổng tất cả a[in][j] dirty.
A dear
If the input file it as a matrix k for which this is a place to read the file with the code to find the shortest path is ntn sir him. E Thanks a, a rep help e with sir.
filename contains graphs
-first line integer n represents the number of vertices
-the next line shows each edge, 1 edge cover 3 integer, 2 the first number represents the top of the charts, The next number represents the length addition, numbers separated by commas.
See:
10
0,1,10
0,5,20
0,7,50
3,5,10
5,6,15
….
Brother to brother asked the input file input and output out what it is and get somewhere with ạh
Self-generated input file, the output file generated by the program based on the input file results.
My brother's a have the input file k all ajh. k I know in the input file created for 1 the matrix ntn sir
then copy the file as input to the finish.
fails in the ham back -> int line point[n]; TV content is not network fault
This line is the only declared array. Sure you should be like VS code on. You can replace it with int point[100];
Network A is from 8 -> 1 cost is 10
But trace network-P then go 8 -> 6 -> 3 -> 2 -> 1 cost will be 11, Smart thinking is correct 8-5-6-3-2-1 but eh ?
Welcome, I have wondered vs a few problems do not understand:
– Peak began a is 4: 0
+ 4 -> 1: 2+0= 2
+ 4 -> 3: 4+0= 4
+ 4 -> 6: 3+0=3
+ 4 -> 7: 6+0=6
– Top 1: 2
+ 1 -> 2: 3+2=5
+ 1 -> 3: 5+2=7(not updated)
– Top 2: 5
+ 2 -> 5: 7+5=12
+ 2 -> 3: 1+5=6(not updated)
– Top 3: 4
+ 3 -> 6: 1+4=5(not updated)
– Top 6: 3
+ 6 -> 8: 6+3=9
To b = 8
Dijkstra read function is not understood place:
Only[in] < Len, the array contains sum [0,3,5,2] in line 0 vs. each column 0,1,2,3 while each column 5,6,7 it does not count is incredibly.
I felt confused as files enter the matrix array 2 dimensional line so I read columns vs 2 loop using array 1 its dimensions do not understand how
Read their code for days without understanding his desire to help you answer. Thanking you in advance
In the process of running Len[in] can change so i need to find certain top length < sum để đi tiếp. File đầu vào là mảng 2 chiều, mình đọc vào G rồi, làm gì có chỗ nào đọc bằng mảng 1 chiều đâu bạn.
Find the location where you comment that's not the point with extremely vs find positions that have used length is min S vs Len dimensional array, it still goes through 8 but at peak input array 2 evening. Function for i, I think retrieve each smaller peak immensely when his jaw for j not misread, Only[in] vs Ireland[j] is a peak or peak vs connection do you. His tangle…
In stating his algorithm I got you. Use 1 Mangal only[] - Len[in] is the shortest distance from a vertex to vertex i.
Thank you, mình có đọc hết nhưng vẫn chưa hiểu lắm. Để mình tìm video xem cho dễ hiểu
Please see program for building code Floyd algorithm with sir. I thank!
There above code I got you.
ko bít anh có làm video nào nói về thuật toán dijkstra ko ạ… tại em sắp thi HSG tin mà khi lên mạng tìm hỉu thì vẫn chưa hỉu lắm ạ… mog a giúp đỡ cho
Mình chỉ có bài viết này thôi.
code dau tien mình chay nhung ko ra duoc output
Bạn phải có input từ file nhé.
cho e hỏi cái hàm Tongthiethai() trong phần code của Floyd viết sao v ạ
Waiter, cho em hỏi là thuật toán floyd ở trên là dùng Phương pháp gì vậy ạ.
quy hoạch động hay quay lui hay vet cạn?