graph help please

hello. I need help. I got this graph code and it requires a file like a .txt i guess. I am not sure what the file should look like to make it work. I want to add weighted edges. Does anyone know?

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <iostream>
#include "strings.h"
typedef char *charptr;

strings::~strings()
{ for (int i=0; i<n; i++) delete[](p[i]);
delete[]p;
}

int strings::add(const char *s)
{	int i;
for (i=0; i<n; i++) if (strcmp(p[i], s) == 0) return i;
if (n == N)
{	char **pOld = p;
p = new charptr[N += ChunkSize];
for (i=0; i<n; i++) p[i] = pOld[i];
delete[]pOld;
}
p[n] = new char[strlen(s) + 1];
strcpy(p[n++], s);
return n - 1;
}

int strings::read(istream &f)
{	char buf[100];
f >> setw (100) >> buf;
return f.fail() ? -1 : add(buf);
}

struct edge{int num, len; edge *next;};

class graph	{
public:
	int n, N, start, finish, nInS, undirected;
	void build(strings &T, ifstream &file);
	void output(const strings &T) const;
	int length(int i, int j) const;
	void prepare();
	void ExpandSetS();
private:
	struct NodeType{ int count, D, connect, inS; edge *link;}
*list;
enum {nil=-1, inf=INT_MAX/2};
int ReadName(strings &T, istream &file, char &PrevChar);
};

void error(const char *s)
{	cout << s << endl; exit(1);
}

int graph::ReadName(strings &T, istream &file, char &PrevChar)
{	
	char s[100];
	s[0] = '\n';
	do
	{	PrevChar = s[0];
	file.get(s[0]);
	}
	while(!file.fail() && !isalpha(s[0]));
	if (file.fail()) return -1;
	file >> setw(99) >> &s[1];
	return T.add(s);
}

void graph::build(strings &T, ifstream &file)
{	int i, j, len;
	edge *p;
	char PrevChar;
	const int ChunkSize = 64;
	N = n = 0;
	list = NULL;
	for (;;)
	{ j = ReadName(T, file, PrevChar);
	if(j < 0) break;
	if (PrevChar == '\n')
	{	i=j;
	j = ReadName(T, file, PrevChar);
	}
	file >> len;
	if (file.eof()) break;
	if (file.fail())
		error("Incorrect input file.");
	int max = i > j ? i : j;
	if (max >= N)
	{ 
		NodeType *listOld = list;
		list = new NodeType[N += ChunkSize];
		for (int v=0; v<N; v++)
		if(v < n)
			list[v] = listOld[v];
		else
		{
			list[v].count = 0;
			list[v].link = NULL;
		}
		delete[]listOld;
	}
	if (max >= n) n = max + 1;
	p = new edge;
	p->num = j; p->len = len;
	p->next = list[i].link;
	list[i].link = p;
	list[j].count++;
	//Undirected graph: add <j, i> besides <i, j>
	if (undirected)
	{
		p = new edge;
		p->num = i;
		p->len = len;
		p->next = list[j].link;
		list[j].link = p;
		list[i].count++;
	}
	}
	file.close();
}
int graph::length (int i, int j) const
{
	edge *p= list[i].link;
	while (p)
	{
		if (p->num == j)
			return p->len;
		p = p->next;
	}
	return inf;
}

void graph::prepare()
{
	for(int v = 0; v<n; v++)
	{
		list[v].D = (v== start ? 0 : length(start, v));
		list[v].inS = 0;
		list[v].connect = start;
	}
	list[start].inS = 1;
	nInS = 1;
}

void graph::ExpandSetS()
{
	int v, w = inf, min = INT_MAX;
	// choose a vertex w in V such that list[w].D
	// is a minimum
	for(v=0; v<n; v++)
		if (list[v].inS == 0)
		{
			int sum = list[w].D + length(w, v);
			if (sum < list[v].D)
			{
				list[v].D = sum;
				list[v].connect = w;
			}
		}
}

void graph::output(const strings &T) const
{
	if (list[finish].D == inf)
	{
		cout << "No path from start to finish. \n";
		return;
	}
	//Replace predecessors with successors
	int j = finish, i , jnext = 0;
	for(;;)
	{
		i=list[j].connect;
		list[j].connect = jnext;
		if (j == start) break;
		jnext = j;
		j = i;
	}
	cout << "Shortest path: \n\n";
	i = start;
	cout << "			Vertex					Distance\n";
	for(;;)
	{
		cout << (i == finish ? "finish = " :
			i == start ? "start = " :
			"			")
			<<setw(20) << setiosflags(ios::left) << T[i] << "   "
			<< setw(6) << resetiosflags(ios::left) << list[i].D
			<< endl;
	if (i == finish)
		break;
	i = list[i].connect;
	}
}

int main()
{
	graph g;
	strings T;
	char fname[50], ch;
	cout << "Input file: ";
	cin >> setw(50) >> fname;
	ifstream file(fname, ios::in);
	if(file.fail())
		error("Cannot open input file.");
	cout << "Is the graph Directed or Undirectes? (D/U) ";
	cin >> ch;
	g.undirected = (ch == 'u' || ch == 'U');
	g.build(T, file);	//build adjacency lists
	cout << "Vertices:";
	int n = T.size();
	for(int i = 0; i<n; i++)
	{
		if(i % 5 == 0)
			cout << endl;
		cout << T[i] << " ";
	}
	do
	{
		cout << "\nEnter start and finish:\n";
		g.start = T.read(cin);
		g.finish = T.read(cin);
		if (g.start >= g.N || g.finish >= g.N)
			cout << "Incorrect start or finsih.\n";
			else
			{
				g.prepare();
				while (g.nInS < g.N)
					g.ExpandSetS();
				g.output(T);
			}
			cout << "Do you want another path? (Y/N) ";
			cin >> ch;
	} while (ch == 'y' || ch == 'Y');
	return 0;
}


thanks
Topic archived. No new replies allowed.