Bellman-Ford, read nodeFrom nodeTo weight

Hi, I saw a code of Bellman-Ford where I have to manually input everything.
So I decided to try and make it so that I can read the number of nodes and edges with it weight from the txt files as a practice of fstream.

Can someone tell me with the solution? Thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <fstream>
#include <iostream>

using namespace std;
 
struct Edge
{
    // This structure is equal to an edge. Edge contains two end points. These edges are directed edges so they
    //contain source and destination and some weight. These 3 are elements in this structure
    int source, destination, weight;
};
 
// a structure to represent a connected, directed and weighted graph
struct Graph
{
    int V, E;
    // V is number of vertices and E is number of edges
 
    struct Edge* edge;
    // This structure contain another structure which we already created edge.
};
 
struct Graph* createGraph(int V, int E)
{
    struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph));
    //Allocating space to structure graph
 
    graph->V = V;   //assigning values to structure elements that taken form user.
 
    graph->E = E;
 
    graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) );
    //Creating "Edge" type structures inside "Graph" structure, the number of edge type structures are equal to number of edges
 
    return graph;
}
 
void FinalSolution(int dist[], int n)
{
    // This function prints the final solution
    printf("\nVertex\tDistance from Source Vertex\n");
    int i;
 
    for (i = 0; i < n; ++i){
        printf("%d \t\t %d\n", i, dist[i]);
    }
}
 
void BellmanFord(struct Graph* graph, int source)
{
    int V = graph->V;
 
    int E = graph->E;
 
    int StoreDistance[V];
 
    int i,j;
 
    // This is initial step that we know , we initialize all distance to infinity except source.
    // We assign source distance as 0(zero)
 
    for (i = 0; i < V; i++)
        StoreDistance[i] = INT_MAX;
 
    StoreDistance[source] = 0;
 
    //The shortest path of graph that contain V vertices, never contain "V-1" edges. So we do here "V-1" relaxations
    for (i = 1; i <= V-1; i++)
    {
        for (j = 0; j < E; j++)
        {
            int u = graph->edge[j].source;
 
            int v = graph->edge[j].destination;
 
            int weight = graph->edge[j].weight;
 
            if (StoreDistance[u] + weight < StoreDistance[v])
                StoreDistance[v] = StoreDistance[u] + weight;
        }
    }
 
    // Actually upto now shortest path found. But BellmanFord checks for negative edge cycle. In this step we check for that
    // shortest distances if graph doesn't contain negative weight cycle.
 
    // If we get a shorter path, then there is a negative edge cycle.
    for (i = 0; i < E; i++)
    {
        int u = graph->edge[i].source;
 
        int v = graph->edge[i].destination;
 
        int weight = graph->edge[i].weight;
 
        if (StoreDistance[u] + weight < StoreDistance[v])
            printf("This graph contains negative edge cycle\n");
    }
 
    FinalSolution(StoreDistance, V);
 
    return;
}
 
int main()
{
	ifstream file;
	file.open("G:\\BINUS\\ADA\\Thedy\\FinalProject\\Bellman\\graph.txt"); //ORIGINAL PATH FROM ASSIGNMENT
	
	if(!file.is_open()){
		cout << "File Error";
		return -1;
	}
	
    int V,E,S;  //V = no.of Vertices, E = no.of Edges, S is source vertex
    
	file >> V >> E;
	cout << V <<" "<< E << endl;
	
//    printf("Enter number of vertices in graph\n");
//    scanf("%d",&V);
 
//    printf("Enter number of edges in graph\n");
//    scanf("%d",&E);
 
    printf("Enter your source vertex number\n");
    scanf("%d",&S);
    
    struct Graph* graph = createGraph(V, E);    //calling the function to allocate space to these many vertices and edges
	
	ifstream file2;
	file2.open("G:\\BINUS\\ADA\\Thedy\\FinalProject\\Bellman\\graphDetails.txt"); //ORIGINAL PATH FROM ASSIGNMENT
	
	if(!file.is_open()){
		cout << "File Error";
		return -1;
	}
	
	int a, b, w;
	
	file2 >> a >> b >> w; // debugging
	cout << a << "\t" << b << "\t" << w << endl; //just for debugging
	
	while( file2 >> a >> b >> w){ // I don't know how I'm supposed to write this
		for (int i = 0; i < E; i++){
			a >> graph->edge[i].source;
			b >> graph->edge[i].destination;
			w >> graph->edge[i].weight;
		}
		cout << a << "\t" << b << "\t" << w << endl;
	}
	
//    for(int i=0; i<E; i++){
//        printf("\nEnter edge %d properties Source, destination, weight respectively\n",i+1);
//        scanf("%d",&graph->edge[i].source);
//        scanf("%d",&graph->edge[i].destination);
//        scanf("%d",&graph->edge[i].weight);
//    }
 
    BellmanFord(graph, S);
    //passing created graph and source vertex to BellmanFord Algorithm function
 
    return 0;
}



This is Txt file for the source destination weight (graphDetails.txt):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
0 0 8
0 1 3
0 2 0
0 4 6
0 7 1
0 12 6
0 13 4
0 14 2
0 15 4
1 3 9
1 6 4
1 9 0
1 12 5
1 14 9
2 2 6
2 4 5
2 6 9
2 13 6
3 0 6
3 5 5
3 9 1
3 11 6
3 13 6
3 15 5
4 8 1
4 9 4
4 10 1
4 11 1
4 13 5
5 4 3
5 5 3
5 8 5
5 9 8
5 14 6
6 2 7
6 6 6
6 7 6
6 8 5
6 9 2
6 15 1
7 0 4
7 2 8
7 3 8
7 4 7
7 5 9
7 6 2
7 8 2
7 9 3
7 10 7
7 13 2
7 15 6
8 5 6
8 7 0
8 14 0
9 2 0
9 5 1
9 6 3
9 13 1
9 14 7
10 4 6
10 6 0
10 7 7
10 10 0
10 12 5
10 14 7
10 15 8
11 1 7
11 2 4
11 4 0
11 6 1
11 9 1
11 10 0
11 11 8
11 13 7
11 14 9
11 15 1
12 1 6
12 5 5
12 7 3
12 8 7
12 10 0
12 13 0
12 15 0
13 1 7
13 5 9
13 7 6
13 8 2
13 9 9
13 13 2
14 2 7
14 5 9
14 13 3
15 0 0
15 1 0
15 3 7
15 6 3
15 7 2
15 8 8
15 9 8
15 11 0
15 13 8
15 15 8


and this is graph.txt :
 
16 101
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//	int a, b, w;
	
//	file2 >> a >> b >> w; // debugging
//	cout << a << "\t" << b << "\t" << w << endl; //just for debugging
	
//	while( file2 >> a >> b >> w){ // I don't know how I'm supposed to write this
//		for (int i = 0; i < E; i++){
//			a >> graph->edge[i].source;
//			b >> graph->edge[i].destination;
//			w >> graph->edge[i].weight;
//		}
//		cout << a << "\t" << b << "\t" << w << endl;
//	}
	
    for(int i=0; i<E; i++){
//        printf("\nEnter edge %d properties Source, destination, weight respectively\n",i+1);
//        scanf("%d",&graph->edge[i].source);
//        scanf("%d",&graph->edge[i].destination);
//        scanf("%d",&graph->edge[i].weight);
	file2 >> graph->edge[i].source;
	file2 >> graph->edge[i].destination;
	file2 >> graph->edge[i].weight;	
    }
Topic archived. No new replies allowed.