### Adjaceny matrix for dijkstra shortest path algorithm

Hi guys, I want to apply dijkstra shortest path algorithm to the following 2 files:
projet14_milestone.txt
projet14_process.txt

projet14_milestone.txt looks like :
 ``12345678910111213`` ``````12 0,5,Start 1,1,A 4,1,B 7,1,C 2,2,D 1,3,E 2,4,F 6,2,G 7,3,H 5,4,I 7,4,J 4,3,End``````

12 is the number of stages I need to consider and is stored in the variable nb.
From then on , the first number respresents the x coordinate of a stage, the second number respresents the y coordinate of a stage and then we can see the name of the stage. So for example line 2 : The x-coordinate of stage 'start' is 0 and is stored in milestone_x, the y coordinate of the stage 'start' is 5 and is stored in milestone_y. the name of the stage is stored in milestone_nom.

projet14_process.txt looks like :
 ``1234567891011121314151617`` `````` PROCESS1,-3.88,Start,A PROCESS2,-5.2,Start,B PROCESS3,-7.97,Start,C PROCESS4,-0.43,A,D PROCESS5,-2.2,B,D PROCESS6,-1.2,C,G PROCESS7,-3.18,D,G PROCESS8,-0.42,E,D PROCESS9,-0.69,H,G PROCESS10,-0.94,H,J PROCESS11,-1.64,H,I PROCESS12,-1.49,J,I PROCESS13,-2.4,I,F PROCESS14,-0.58,E,F PROCESS15,-1.22,End,I PROCESS16,-1.24,End,F``````

There are 16 different processes and each process consumes a certain amount of paint. Each process is defined by 2 stages. For example process 1 consumes -3,88 units of paint and connects stage 'start' with A. As it is and undirected graph, D->G consumes the same amount as G->D.

Thus far, I've written a code to become the adjacency matrix. The first row represents start, the second A, and so on. The first column represents start, the second A, and so on. As process 1 connects start with A, adjmatrix [0][1]= -3,88 and adjmatrix [1][0]= -3,88

But this code cannot attach the value -3,88 to this step, can someone help me?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141`` ``````#include #include #include #include using namespace std; class Algorithme { private: int i= 0; struct milestone { int milestone_x; int milestone_y; char milestone_nom[6]; }; int nb = 12; milestone node[12]; struct process { char process_nom[20]; float process_consomation; char process_node1[6]; char process_node2[6]; }; process procedure[16]; public: void lecture(); void adjmatr(); }; void Algorithme::lecture() { // lecture document 1 string ligne; ifstream infile; infile.open("/Users/gillescorswarem/documents/projet14_milestone.txt"); if (infile.fail()) { cout << "erreur de lecture: document: milestone\n"; } infile >> nb; for ( i=0; i < nb;i++) { getline(infile,ligne,','); node[i].milestone_x = std::atoi(ligne.c_str()); getline(infile,ligne,','); node[i].milestone_y = std::atoi(ligne.c_str()); getline(infile,ligne); strcpy(node[i].milestone_nom, ligne.c_str()); } infile.close(); string ligne2; ifstream infile2; infile2.open("/Users/gillescorswarem/documents/projet14_process.txt"); if (infile2.fail()) cout << "erreur de lecture: document: process\n"; for ( i=0; i < 16;i++) { getline(infile2,ligne2,','); strcpy(procedure[i].process_nom, ligne2.c_str()); getline(infile2,ligne2,','); procedure[i].process_consomation = std::atof(ligne2.c_str()); getline(infile2,ligne2,','); strcpy(procedure[i].process_node1, ligne2.c_str()); getline(infile2,ligne2); strcpy(procedure[i].process_node2, ligne2.c_str()); } infile2.close(); } void Algorithme::adjmatr() { float adjmatrix [12][12]; for (int a=0; a<16; a++){ for (int b=0; b<12; b++){ if (strcmp(procedure[a].process_node1,node[b].milestone_nom) == 0) { for (int c=0;c<12;c++){ if ( strcmp (procedure[a].process_node2,node[c].milestone_nom) == 0) { adjmatrix[b][c]= procedure[a].process_consomation; adjmatrix[c][b]= procedure[a].process_consomation; } } } } } cout << adjmatrix [0][1] << endl; cout << adjmatrix [4][7] << endl; } int main() { Algorithme reponse; reponse.lecture(); reponse.adjmatr(); return 0; } ``````

 ```##### -3.88 -5.20 -7.97 ##### ##### ##### ##### ##### ##### ##### ##### -3.88 ##### ##### ##### -0.43 ##### ##### ##### ##### ##### ##### ##### -5.20 ##### ##### ##### -2.20 ##### ##### ##### ##### ##### ##### ##### -7.97 ##### ##### ##### ##### ##### ##### -1.20 ##### ##### ##### ##### ##### -0.43 -2.20 ##### ##### -0.42 ##### -3.18 ##### ##### ##### ##### ##### ##### ##### ##### -0.42 ##### -0.58 ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### -0.58 ##### ##### ##### -2.40 ##### -1.24 ##### ##### ##### -1.20 -3.18 ##### ##### ##### -0.69 ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### -0.69 ##### -1.64 -0.94 ##### ##### ##### ##### ##### ##### ##### -2.40 ##### -1.64 ##### -1.49 -1.22 ##### ##### ##### ##### ##### ##### ##### ##### -0.94 -1.49 ##### ##### ##### ##### ##### ##### ##### ##### -1.24 ##### ##### -1.22 ##### ##### ```
(##### means no value) ¿is that correct? ¿do you get something different?

you should initialise all the elements of `adjmatrix' to an "infinite" value or you'll end up with extra edges.
This is indeed the matrix I have to become!
I want to replace all the ##### by the value 0 or infinity. 0 is also possible I believe, because I am working with negative values

But when I run the program and try to print adjmatrix [0][1] and adjmatrix [4][7] I always get the value 0.
 ``123`` ``````cout << adjmatrix [0][1] << endl; cout << adjmatrix [4][7] << endl; ``````

According to the matrix, adjmatrix [0][1] has to be equal to -3,88 and adjmatrix [4][7] to -3,18. Why does the program print 0 each time?
Last edited on
I don't see any particular wrong in your code, and it does work in my end.
initialise `adjmatrix' and print it fully

may use a debugger to see if you do reach line 116 and to inspect the `procedure' and `node' arrays after reading from the files.