Implementing graphs and initializing adjacency matrices

Graphs are often represented using an adjacency matrix. Various sources indicate that initialization costs can be avoided | V ^ 2 | (V is the number of vertices), but I could not figure out how to do this.

In Java, just declaring a matrix, for example. boolean adj [][]The runtime automatically initializes the array with false, and it will cost O (V ^ 2) (array sizes).
Do not I understand? Is it possible to avoid the quadratic cost of initializing the adjacency matrix, or is it just something theoretical that depends on the implementation language?

+3
source share
4 answers

, "", ( ). ,

+2

. , , , ?

: ( O (n 2)), , , . . , , , .


. O(m) , m - . , . , , , is there edge between vertices i and j. O(1) , .

(, Dijkstra, Bellman-Ford, Prim, Tarjan, BFS DFS) . , .

+2

. ( ). Java, .

, data[0..n], . , , . O (n) , - , .

"" , , . , data[i], i. , , .

, data[k], , , k. O (n) , !

, stack_check[0..n], . . , data[i], i stack_check[i], .

data[k] - , stack_check[k] , k. data[k] , stack_check[k] , k ( k ). O (1), .

, O (1) , . , O (1) , . , , . , , . , , 0 ( , !) , , .

+1

A_A. , , .

- , , , (, 20%, ).

, , . , , . , - ( , ), ( , ).

0

All Articles