Why Adjacency List is not memory efficient

Why Adjacency List is not memory efficient

1 min read

๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต ๐—ฅ๐—ฒ๐—ฝ๐—ฟ๐—ฒ๐˜€๐—ฒ๐—ป๐˜๐—ฎ๐˜๐—ถ๐—ผ๐—ป: A ๐—–๐—ผ๐—บ๐—บ๐—ผ๐—ป ๐— ๐—ถ๐˜€๐—ฐ๐—ผ๐—ป๐—ฐ๐—ฒ๐—ฝ๐˜๐—ถ๐—ผ๐—ป๐˜€ For graph representation, we all know that there are two main way to represent a graph. (๐˜›๐˜ฉ๐˜ฆ๐˜ณ๐˜ฆ ๐˜ข๐˜ณ๐˜ฆ ๐˜ง๐˜ฆ๐˜ธ ๐˜ฎ๐˜ฐ๐˜ณ๐˜ฆ ๐˜ธ๐˜ข๐˜บ๐˜ด ๐˜ต๐˜ฐ ๐˜ณ๐˜ฆ๐˜ฑ๐˜ณ๐˜ฆ๐˜ด๐˜ฆ๐˜ฏ๐˜ต ๐˜จ๐˜ณ๐˜ข๐˜ฑ๐˜ฉ)   

  • ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐— ๐—ฎ๐˜๐—ฟ๐—ถ๐˜…  

  • ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐—Ÿ๐—ถ๐˜€๐˜

We were taught that ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐—Ÿ๐—ถ๐˜€๐˜ is memory efficient from space complexity perspective than ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐— ๐—ฎ๐˜๐—ฟ๐—ถ๐˜…. (Not true in worst case)

But how? let's see,

We have a graph G of ๐™ฃ=๐Ÿฐ vertices and we have edges ๐™ฃ(๐™ฃ-๐Ÿญ)/๐Ÿฎ = ๐Ÿฒ (maximum possible edge).  If we store this graph using ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐— ๐—ฎ๐˜๐—ฟ๐—ถ๐˜…, we will have simply a matrix representation like below.     

  ๐Ÿญ  ๐Ÿฎ  ๐Ÿฏ  ๐Ÿฐ 
-------------- 
๐Ÿญ| ๐Ÿฌ  ๐Ÿญ   ๐Ÿญ   ๐Ÿญ 
๐Ÿฎ| ๐Ÿญ  ๐Ÿฌ   ๐Ÿญ   ๐Ÿญ 
๐Ÿฏ| ๐Ÿญ  ๐Ÿญ   ๐Ÿฌ   ๐Ÿญ 
๐Ÿฐ| ๐Ÿญ  ๐Ÿญ   ๐Ÿญ   ๐Ÿฌ 

which takes at most ๐™ฃ^๐Ÿฎ (๐Ÿญ๐Ÿฒ๐—ฏ๐—ถ๐˜๐˜€ ๐—ณ๐—ผ๐—ฟ ๐—ป=๐Ÿฐ)

_____________________________________________

But what if we represent the same graph using ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐—Ÿ๐—ถ๐˜€๐˜?

How it will looks like?

Showing below

๐Ÿญ: ๐Ÿฎ, ๐Ÿฏ, ๐Ÿฐ 
๐Ÿฎ: ๐Ÿญ, ๐Ÿฏ, ๐Ÿฐ 
๐Ÿฏ: ๐Ÿญ, ๐Ÿฎ, ๐Ÿฐ 
๐Ÿฐ: ๐Ÿญ, ๐Ÿฎ, ๐Ÿฏ

Here we can see each vertex has (๐™ฃ-๐Ÿญ) ๐—ป๐—ฒ๐—ถ๐—ด๐—ต๐—ฏ๐—ผ๐˜‚๐—ฟ๐˜€ (๐—ณ๐—ผ๐—ฟ ๐™ฃ=๐Ÿฐ, ๐—ป๐—ฒ๐—ถ๐—ด๐—ต๐—ฏ๐—ผ๐˜‚๐—ฟ๐˜€ ๐—ถ๐˜€ ๐Ÿฏ), so for ๐™ฃ neighbors its ๐™ฃ(๐™ฃ-๐Ÿญ) bits of space (๐—ณ๐—ผ๐—ฟ ๐™ฃ=๐Ÿฐ, ๐Ÿฐ๐™ญ(๐Ÿฐ-๐Ÿญ)=๐Ÿญ๐Ÿฎ ๐—ฏ๐—ถ๐˜๐˜€  and for each vertex requires ๐™ก๐™ค๐™œ๐Ÿฎ๐™ฃ bits to represent itself.

So the total memory required is ๐™ฃ(๐™ฃ-๐Ÿญ)๐™ก๐™ค๐™œ_๐Ÿฎ๐™ฃ

๐Ÿญ๐Ÿฎ+๐™ก๐™ค๐™œ_๐Ÿฎ(๐Ÿฐ) = ๐Ÿฎ๐Ÿฐ๐™—๐™ž๐™ฉ๐™จ  ๐™›๐™ค๐™ง ๐™ฃ=๐Ÿฐ

_____________________________________________

From the above example, we can say ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐—Ÿ๐—ถ๐˜€๐˜ takes more space than ๐—”๐—ฑ๐—ท๐—ฎ๐—ฐ๐—ฒ๐—ป๐—ฐ๐˜† ๐— ๐—ฎ๐˜๐—ฟ๐—ถ๐˜…, which is ๐™ฃ^๐Ÿฎ๐™ก๐™ค๐™œ_๐Ÿฎ๐™ฃ > ๐™ฃ^๐Ÿฎ.

1755115193549.jpeg