#Some question about graph

68 messages · Page 1 of 1 (latest)

primal jay
#

is there other way to create adjacency list without creating 3 classes?

proud steppeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

steep sapphire
primal jay
steep sapphire
primal jay
#

first time i learnt adjacency list was when i was using js

#

you couldve just wrote:

steep sapphire
primal jay
#
const graph = {
a : ['b', 'c'],
b : ['d', 'e'],
c : ['w', etc]}
upbeat gorge
#

just curious what would be the problem of using 3 classes?

primal jay
steep sapphire
#

it's the same thing

#

but it works faster

upbeat gorge
primal jay
steep sapphire
# primal jay you sure?

in javascript you are accessing dynamic fields from an object, a.k.a. a hashmap
which takes some time

with vectors you are accessing elements at index i, always instantly

primal jay
#

its not about languages

#

its about implementation

#

i just wanna find efficient way of creating adjacency list

steep sapphire
#

why are you not believing me

steep sapphire
primal jay
steep sapphire
# primal jay

you are wrong, the time complexity for these operations when vector<vector<int>> is used is the same as the column "Adjacency list"

#

from the screenshot here

#

let me demonstrate

#

so, the graph from the link

#

a.k.a.

#

this thing

#

in C++ can be hardcoded like this

#
vector<vector<int>> g = 
{
  {},//vertex 0 does not connect to any other vertexes
  {2,3}, //vertex 1 connects one-directional edges to vertex 2 and 3
  {1,5}, //vertex 2 connects one-directional edges to vertex 5 and 1
  {1,4}, //vertex 3 connects one-directional edges to vertex 1 and 4
  {5,3}, //vertex 4 connects one-directional edges to vertex 5 and 3
  {2,4}, //vertex 5 connects one-dimensional edges to vertex 2 and 4
}
#

@primal jay

primal jay
#

what if you have vertex 10^8

steep sapphire
primal jay
#

thats tone of memory

primal jay
steep sapphire
#

what is this?

primal jay
steep sapphire
#

when you vertexes are defined with something more compilcated, like random ints, or strings

#

you must make a one to one mapping of your vertexes and numbers from 0 to n-1

#

and construct your adjacency list

#

@primal jay anything else?

primal jay
#

tbh i wanted smth like this:

#

implemented with less used memory

steep sapphire
#
vector<forward_list<int>>
primal jay
#

oh, btw

#

why do adjacency lists use list?

#

why dont they use vector?

steep sapphire
#

but C++ calls it vector

#

it's a dynamically resizable array under the hood

primal jay
#

why do c++ implementation use lists?

steep sapphire
#

by knowing exactly when vectors and linked lists are fast and when they are slow, you can create your graph to suit your time complexity requirements

steep sapphire
#

you gotta know their internals to decide when to use what

#

99% of the time vector<vector<int>> is used

#

because the graph's vertexes are read from the standard input

#

and the graph never resizes again

primal jay
#

okie, thanks!

#

!solved