-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_tsp.c
143 lines (114 loc) · 5.95 KB
/
naive_tsp.c
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
#include <stdio.h>
#include <stdlib.h>
#define N 5
// Traveling Salesman Problem
struct Graph
{
int** adjList;
};
struct Graph* createGraph()
{
// allocating graph and adjList
struct Graph* graph = malloc(sizeof(struct Graph));
graph->adjList = malloc(N * sizeof(int*));
int i, j;
// allocating every array in adjList
for(i = 0; i < N; i++)
graph->adjList[i] = malloc(N * sizeof(int));
// filling adjList with 0s
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
graph->adjList[i][j] = 0;
return graph;
}
// adding edges to a non-oriented graph
void addEdge(struct Graph* graph, int src, int dest, int weight)
{
graph->adjList[src][dest] = weight;
graph->adjList[dest][src] = weight;
}
void printGraph(struct Graph* graph)
{
int i, j;
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%d ", graph->adjList[i][j]);
printf("\n");
}
}
// finding if array contains given value
int arrayContains(int* array, int value)
{
int i;
for(i = 0; i < N; i++)
if(array[i] == value)
return 1;
return 0;
}
int arrayMinIndex(int* array, int* visited)
{
// setting minimum to 1000
int i, min = 1000, minInd = -1;
int* cpArray = malloc(N * sizeof(int*));
// copying argument array
for(i = 0; i < N; i++) cpArray[i] = array[i];
// setting values of visited nodes to 1000
for(i = 0; i < N; i++)
if(arrayContains(visited, i))
cpArray[i] = 1000;
// finding index of minimal value in array
for(i = 0; i < N; i++)
if(cpArray[i] > 0 && cpArray[i] < min)
{
minInd = i;
min = cpArray[i];
}
free(cpArray);
return minInd;
}
void tsp(struct Graph* graph, int start)
{
printf("Path starting from %d: ", start);
int i, curr, tmp, length = 0;
int* visited = malloc(N * sizeof(int));
for(i = 0; i < N; i++) visited[i] = -1;
curr = start;
for(i = 0; i < N; i++)
{
// adding current node to visited (path)
visited[i] = curr;
// setting tmp to index of minimal value of edge from current node neighbours (closest neighbour)
tmp = arrayMinIndex(graph->adjList[curr], visited);
if(i < N - 1)
{
// increasing length by length of the edge between current node and tmp node
length += graph->adjList[curr][tmp];
// changing current node to tmp (closest neighbour)
curr = tmp;
}
}
// increasing length by length of edge between last node and starting node
length += graph->adjList[curr][start];
for(i = 0; i < N; i++) printf("%d ", visited[i]);
printf("%d ; length = %d\n", start, length);
free(visited);
}
int main()
{
struct Graph* graph = createGraph();
addEdge(graph,0,1,15);
addEdge(graph,0,2,12);
addEdge(graph,0,3,6);
addEdge(graph,0,4,21);
addEdge(graph,1,2,3);
addEdge(graph,1,3,10);
addEdge(graph,1,4,4);
addEdge(graph,2,3,11);
addEdge(graph,2,4,26);
addEdge(graph,3,4,17);
printf("Graph:\n");
printGraph(graph);
int i;
for(i = 0; i < N; i++) tsp(graph, i);
}