dijkstra.mc 2.98 KB
Newer Older
Alexander Hirsch's avatar
Alexander Hirsch committed
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

void dijkstra(int[15] path_cost)
{
	int[15] calc_cost;
	int i;
	int j;
	i = 0;

	while (i < 4)
	{
		j = 0;
		while (j < 4)
		{
			if (path_cost[i * 4 + j] == 0)
				calc_cost[i * 4 + j] = 9999;
			else
				calc_cost[i * 4 + j] = path_cost[i * 4 + j];

			j = j + 1;
		}
		i = i + 1;
	}

	int[4] distance;
	bool[4] visited;

	int node_count;
	int next_node;
	node_count = 0;

	while (node_count < 4)
	{
		distance[node_count] = calc_cost[node_count];

		if (node_count == 0)
			visited[node_count] = true;
		else
			visited[node_count] = false;

		node_count = node_count + 1;
	}

	node_count = 1;

	while (node_count < 4)
	{
		int min_distance;
		int traversel_count;

		min_distance = 9999;
		traversel_count = 0;

		while (traversel_count < 4)
		{
			if (!visited[traversel_count] &&
				distance[traversel_count] < min_distance)
			{
				min_distance = distance[traversel_count];
				next_node = traversel_count;
			}
			traversel_count = traversel_count + 1;
		}

		visited[next_node] = true;
		traversel_count = 0;

		while (traversel_count < 4)
		{
			if (!visited[traversel_count])
			{
				if (distance[traversel_count] >
					calc_cost[next_node * 4 + traversel_count] + min_distance)
				{
					distance[traversel_count] =
						calc_cost[next_node * 4 + traversel_count] + min_distance;
				}
			}
			traversel_count = traversel_count + 1;
		}
		node_count = node_count + 1;
	}

	int print_counter;
	print_counter = 1;
	while (print_counter < 4)
	{
		print("Distance to node ");
		print_int(print_counter);
		print(" = ");
		print_int(distance[print_counter]);
		print_nl();

		print_counter = print_counter + 1;
	}
}

int main()
{
	int[15] path_cost;
	int input_counter;
	int node_counter;
	input_counter = 1;
	node_counter = 1;
	path_cost[0] = 0;

	print("Enter the path costs ('0' = no path)");
	print_nl();

	while (input_counter < 15)
	{
		if (input_counter < 4)
		{
			print("Enter path cost from 0 -> ");
			print_int(node_counter);
			print(":");
			print_nl();
			path_cost[input_counter] = read_int();
		}

		if (input_counter >= 4 && input_counter < 8)
		{
			if (input_counter != 5)
			{
				print("Enter path cost from 1 -> ");
				print_int(node_counter);
				print(":");
				print_nl();
				path_cost[input_counter] = read_int();
			}
			else
			{
				path_cost[input_counter] = 0;
			}
		}

		if (input_counter >= 8 && input_counter < 12)
		{
			if (input_counter != 10)
			{
				print("Enter path cost from 2 -> ");
				print_int(node_counter);
				print(":");
				print_nl();
				path_cost[input_counter] = read_int();
			}
			else
			{
				path_cost[input_counter] = 0;
			}
		}

		if (input_counter >= 12)
		{
			if (input_counter != 15)
			{
				print("Enter path cost from 3 -> ");
				print_int(node_counter);
				print(":");
				print_nl();
				path_cost[input_counter] = read_int();
			}
			else
			{
				path_cost[input_counter] = 0;
			}
		}

		if (node_counter >= 3)
			node_counter = 0;
		else
			node_counter = node_counter + 1;

		input_counter = input_counter + 1;
	}

	dijkstra(path_cost);

	return 0;
}