c++ - Dynamically adding to a graph data structure -
let me first state want direction, not actual code, unless small snippet way point across.
i need create directed graph data structure using adjacency list or matrix in c++, , add vertices/edges standard input, means dynamically.
i think i'd able create graph fine if able instantiate set of vertices first, create edges , add them graph, don't understand how possible add edge contains vertex hasn't been instantiated yet.
for example, first line standard input reads:
miami -> new york/1100 -> washington/1000 -> albuquerque/1700
how supposed add edge miami new york if new york vertex hasn't been added graph yet?
thanks direction everyone!
how possible add edge contains vertex hasn't been instantiated yet.
simple: instantiate it..
i not see issue this. assume v
vertex set seen far. v
empty. read input x->y
, end points (x
, y
). if 1 of them not instantiated (i.e., not in v
), instantiate , add vertex set.
another way it: imagine defining graph edge set e
. definition edge pair of vertices in turn defines vertex set of graph.
Comments
Post a Comment