Can I create a graph in c++ using STL? - c++

Im a CS student, learning graphs algorithms theory (DFS, BFS, Dijkstra, Strongly connected components). My question is what is the easiest practical way to work with a graph DS in cpp: Is there any easy way to create a graph and run DFS on it? like an STL adjacency list and ready-to-run DFS using this adjacency list?
thanks :)

Related

Question about implementing adjacency matrix in unconventional fashion

I was recently given an assignment that requires the need to construct an undirected unweighted graph in order to later on use it in a BFS algorithm. For some background, the adjacency matrix stores vertexes in string form, and its edges in string form. As an example, there would be two movie actor vertexes that are connected through a movie they worked together. The relationship between the vertexes and movies will be later used to find the shortest path between two actors.
I am still rather a novice when it comes to programming and c++, however I would appreciate any help as to how I would get started and progress through it.
Just as a preface, I am NOT asking for actual code. That would be against integrity guidelines at my school, and moreover, I am trying to learn the material rather than regurgitating algorithms into my IDE.
My main question is how do I create and store the strings representing the actors in the matrix and the strings representing the edges between the actors in the individual cells of the matrix.

Visual trace of sorting algorithms using a graph in C++?

I am in an Intro to data structures class and we are currently going over sorting algorithms. Is there any way in C++ to use a graph to trace the exchanging and sorting of sorting algorithms? There is an image illustrating shell sort if you follow this link and scroll down a bit. I also linked it down below. (Essentially what I would like to achieve). Addison Wesley, Algorithms 4th edition: http://algs4.cs.princeton.edu/21elementary/.
They spoke about using built in functions in Java to create the graph and spoke about it as if it is rather trivial. I'm assuming if its basic in Java its basic in C++.
Question being, how would I implement a function that would correspond numerically to how big the bars are and output the graph pre-sort, mid-sort and post-sort?
Note: I have intermediate level C++ knowledge and no experience with graphics.
Consider using a graphing package such as gnuplot. You would have to use it to plot, say as bar graphs, the array values your program is trying to sort.

Data structures implementation code understanding

Im taking a course in algorithms and data structers, and my instructor wants me to implement several data structers (such as BST, stack etc.), and algorithms (such as quick search, DFS, etc.).
I want to belive that I understand the basics, but everytime Im starting to plan the code I have the same difficulty:
here's my current assigment: my instructor wants me to implement a DFS (depth first search) for a directed graph (using c++).
my question is- how do I suppose to implement the graph? should I use adjacency matrix? or should I use adjacency list? neither this nor that??
so I asked my instructor, and his answare was this: "think of the graph as a black box"...
more confused than before, I rashed to stackoverflow, and here i am posting this question...
I dont look for someone to tell me how to implement DFS (or any other algorithm- I can google too!)- I just need someone to explain what should I get as input, and what should I provide as output?
I'll appreciate any comment! thanks!
What he means by a black box is just that you cannot see the nodes and how they connect before you do your DFS. You will probably just get the root node and your algorithm with have to explore from there. As for what you should output- that depends on the assignment. Are you looking for specific data? if not, perhaps a detail of which nodes were visited in which order.

Distinguish directed and undirected graph

I need to write a graph using C++ and I have a little problem. My graph should be directed or undirected, weighted or unweighted, based on matrix or list all on user's choice. And distinguishing matrix from list graph is not a big deal, since it's two different classes, I got some problem with other parameters. The most obvious way to distinguish them is to make two bool variables and check them on every adding and deleting of vertex. It is quite obvious and easy to understand, but I doubt it's efficiency, because every time I add or delete vertex I have to do additional if. I also could write subclasses for it, but I seriously doubt if it's worth it.
Every library is okay to use, if it's not representing graph itself.
For directed and undirected best case is using bool variable for your graph, however You can assume your graph is weighted and directed, but for undirected edges add one edge from a→b and one edge from b→a. Also if there isn't weight function set its weight to 1.
But if you looking for graph library it depends to your programming language, but I'd suggest graph boost library which implemented fully in c++, and too many other people implement it partially in other languages.

Shortest path program

I want to write a shortest path program. I know how the algorithm works, but I don't know where to start
Initially, I thought of using an adjacency matrix but then decided against it because of space. Now I think adjacency list would be better.
Can anyone suggest me a websites or tutorials how to start writing adjacency list to give the input to the program?
You might start with Boost::Graph, which will provide you both mechanisms for storing graph data and a structure for writing an algorithm that consumes that data.