I have an undirected graph represented by an adjacency matrix.
I need a high efficient code,the function just need to return true or false.
And I know Dijkstra’s algorithm can work, I think I don’t need the shortest path, and my adjacency matrix is:
a[i][j] = 1 or -1
There are no other values.
Sorry, I didn’t notice your matrix only contains 0 and 1.
If only query once, maybe DFS or BFS is better, only
O(|V|+|E|)If it is bidirection path,
Disjoint-set data structure(See wikipedia) is effective!If there is only one query, all single-source shortest path algorithm is OK. Like:
O(|V||E|)O(|V|^2)Θ((|E|+|V|)log|V|)If there are many queries, consider
O(|V|^3)