Given an oriented weighted chart with autopiles, find a list of nodes that exactly match k given node x?

Each edge in the graph has a weight of 1, the graph can have cycles, if a node has autopilot, it can be any distance from itself from 0 to infinity, depending on not. time we take a self cycle.

I solved the problem with bfs, but the distance limit is in the order of 10 ^ 9, hence bfs is slower.

We will ask several queries on a given graph of the form (distance, source) and o / p is a list of nodes that are exactly at a given distance from the original vertex.

Limitations

1<=Nodes<=500
1<queries<=500
1<=distance<=10^9

I have a feeling, there would be a lot of repeated calculations, as not. there are few nodes, but I can’t figure out how to reduce the problem with smaller problems.

What is an effective way to do this?

: , . 1 .

+4
2

G = (V,E) - A :

A[i][j] = 1       (V[i],V[j]) is in E
          0       otherwise

k:

(A^k)[i][j] > 0 if and only if there is a path from v[i] to v[j] of length exactly k.

, , , , .

, O(M(n)^log(k)), M(n) nXn.

.

- :

: A^1 = A A A[i][j]=1 , (V[i],V[j]) E

: , l<k

A^k = A^(k-1)*A. A^(k-1)[i][j] > 0, k-1 V[i] V[j].

v1,v2 i j.
k, v1->...->u->v2. u m.
i.h. A^(k-1)[i][m] > 0, . , A[m][j] = 1, (u,v2) = (V[m],V[j]) - .

A^k[i][j] = A^(k-1)*A[i][j] = A^(k-1)[i][1]A[1][j] + ... + A^(k-1)[i][m]A[m][j] + ... + A^(k-1)[i][n]A[n][j] 

A[m][j] > 0 A^(k-1)[i][m] > 0, A^(k-1)*A[i][j] > 0

, u , (u, v2) , k-1 v u (otherweise v1->..->u->v2 - k).

, , , A^(k-1)[i][m] > 0, A[m][j] = 0, m.
, A^k[i][j], , A^k[i][j] = 0

QED

: A^k[i][j] - i j k. , .
( M(n), ), , 0/1, boolean - 0/1 - .

+2

, cycle * N + 1, , .

, !
BFS , offset + cycle*N, (K)

K.

.

A β†’ B β†’ C β†’ D β†’ B K = 1000; S = A;

A - 0
B - 1
C - 2
D - 3
B - 1 (+ 4N)
floor() k - (1+4N) = 0 > 1000 - 1 - 4N = 0 > 999 = 4N > N=249 = > B - 249*4 + 1= 997
: round(k - offset, cycle)

.
( REGEX): A(BCD){249}BCD

-1

Source: https://habr.com/ru/post/1650478/


All Articles