Parsing an input file to create a directed graph C++ -


so i'm starting project on directed graphs , topological sorting. i'm looking efficient way parse input file of courses in form:

course_1    course_2     

where course_1 prerequisite course_2

none    course_3 

where course_3 has no prerequisites.

all vertex labels strings, obviously. have created graph class data members store vertices, edges, , vertex labels. later, adding methods topologically sort , find shortest path 1 vertex another. given these future plans, questions here better use adjacency list? or matrix? efficient way populate graph input files? initial thought using adjacency list. since size of graph isn't known @ compile time idea

std::vector<std::list<std::string>> adjacencylist; 

also, thought of creating helper function parse input file. along lines of

void populategraph(std::string filename, graph* graph) 

am totally going in wrong direction here?

you're on right track lot of things.

if can assume inputs encounter none , course_x , can assume xes form continuous interval of ints starting 1 (or 0) , span number of vertices, treat vertices numbers internally. if not case, can assign each vertex label number (using std::unordered_map example) , have abstract structure anyway.

now, if choose stick model, convenient use, because whole graph can represented std::vector<std::list<int>>. substitute int struct type if want store more info edge, labels, weights, etc. whenever want access specific node's adjacency list, access vector's cell under node's id.

obviously adjacency list based solution. it's sparse graphs speaking. think of way: if you're using matrix sparse graph, big part of allocated memory not going used. using adjacency graph eliminates this, but takes away constant access time arbitrary edge. means can take linear time check if given edge exists. being said, wouldn't expect use check in topological sort. if chose use matrix however, still need map vertices numbers, part stays.

last not least, can use pointers instead of integer ids. wouldn't vertices in vector using id, you'd directly access vertice through stored pointer. you'd still need have string -> pointer mapping, @ least graph creation.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -