W
W
Wonddez2015-05-07 17:00:50
PHP
Wonddez, 2015-05-07 17:00:50

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"]

65e54fd4a2ab4494b2551e64b4f7c17e.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergej, 2015-05-09
@Wonddez

# $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);
    }

As a result, you will get something like the following. Unless, of course, you will output in HTML.
11 field, horizontal addition
285fe0eab5d1499388b743ed5846c69f.png

A
Alexander Movchan, 2015-05-07
@Alexander1705

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;
}

Okay, if the vertices are points, and the edges are the distances between them, we do this as follows:
// 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) );
    }
}

You can also optimize by calculating not the entire matrix, but only half of it (a triangle). And fill the second half with a mirror image of the main diagonal.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question