We have introduced Graph basics in Graph and its representations. In this post, a different STL based representation is used that can be helpful to quickly implement graph using vectors. The implementation is for adjacency list representation of graph.
Following is an example undirected and unweighted graph with 5 vertices.
Below is adjacency list representation of the graph.
We use vector in STL to implement graph using adjacency list representation.
- vector : A sequence container. Here we use it to store adjacency lists of all vertices. We use vertex number as index in this vector.
The idea is to represent graph as an array of vectors such that every vector represents adjacency list of a vertex. Below is complete STL based C++ program for DFS Traversal.
0 1 2 3 4
Below are related articles:
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
Dijkstra’s Shortest Path Algorithm using priority_queue of STL
Dijkstra’s shortest path algorithm using set in STL
Kruskal’s Minimum Spanning Tree using STL in C++
Prim’s algorithm using priority_queue in STL
This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.