Answer the question
In order to leave comments, you need to log in
Adjacency matrix in PHP, can't figure out how to implement?
Good afternoon.
The task is to calculate the adjacency matrix. Those. I need to get the distance between POIs (points on the map)
I don't understand how to build a matrix.
Are there examples of this algorithm not in a mathematical formula, but in the form of an implementation?
upd.
I'll clarify if the description is not clear, I apologize.
data as an array of coordinates
["37.7880555555556,-122.399166666667", "40.7252222222222,-73.9993888888889"]
Answer the question
In order to leave comments, you need to log in
# $citys массив городов, у которых есть poi.
# poi являются массивом в виде строки.
# calcRange
public function createdMatrix ($citys)
{
# Пробегаем грода
for ($i = 0; count($citys) > $i; $i++) {
$poi = $citys[$i]->poi;
$count_poi = count($poi);
for ($r = 0; $count_poi > $r; $r++) {
$data[$i][$r] = [];
for ($v = 0; $count_poi > $v; $v++) {
$sqrt = $this->calcRange($poi[$r]['coordinates'], $poi[$v]['coordinates']);
$data[$i][$r][] = $sqrt;
}
$data[$i][$r]['max'] = $this->maxHorizontSum($data[$i][$r]);
}
$data[$i]['max'][] = max($data[$i]);
}
return $data;
}
# Получаем разницу между точками
public function calcRange($cdr1, $cdr2)
{
$cdr1 = explode(',', $cdr1);
$cdr2 = explode(',', $cdr2);
$sqrt = sqrt(pow(( $cdr1[0] - $cdr2[0] ), 2) + pow(( $cdr1[1] - $cdr2[1] ), 2));
return round($sqrt, 6);
}
In this example, three numbers are specified: the indices of two vertices and the weight of the edge between them.
int N;
cin >> N;
int** am = new int*[N];
for(int i = 0; i != N; ++i)
am[i] = new int[N];
int i, j, l;
while(cin) {
cin >> i >> j >> l;
am[i][j] = l;
}
// N - количество точек
// points - список точек
// points[i].x - x-координата i-той точки
// points[i].y - y-координата i-той точки
int** am = new int*[N];
for(int i = 0; i != N; ++i)
am[i] = new int[N];
for(int i = 0; i != N; ++i) {
for(int j = 0; j != N; ++j) {
am[i][j] = sqrt( sqr(points[i].x - points[j].x) + sqr(points[i].y - points[j].y) );
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question