Why is DFS complexity O (V2) in the adjacency matrix and O (V + E) in the adjacency list view?

Why does the DFS algorithm have O (V2) complexity in adjacency matrix representation and O (V + E) in adjacency list representations.

+4
source share
1 answer

For matrix:

For each vertex, there is one row and one column. Position i, j contains 1 if the edge is from vertex i to vertex j.

The size of the entire matrix is ​​| V | ^ 2

Why complexity | V | ^ 2?

Since each position in the matrix is ​​visited once.

For a related adjacency list:

A collection of linked lists with one list for each vertex, so that the list for vertex v is a list of all vertices adjacent to vertex v.

| E | + | V |? , | V | | E | .

+5

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


All Articles