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.

`// A simple representation of graph using STL, ` `// for the purpose of competitive programming ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A utility function to add an edge in an ` `// undirected graph. ` `void` `addEdge(vector<` `int` `> adj[], ` `int` `u, ` `int` `v) ` `{ ` ` ` `adj[u].push_back(v); ` ` ` `adj[v].push_back(u); ` `} ` ` ` `// A utility function to do DFS of graph ` `// recursively from a given vertex u. ` `void` `DFSUtil(` `int` `u, vector<` `int` `> adj[], ` ` ` `vector<` `bool` `> &visited) ` `{ ` ` ` `visited[u] = ` `true` `; ` ` ` `cout << u << ` `" "` `; ` ` ` `for` `(` `int` `i=0; i<adj[u].size(); i++) ` ` ` `if` `(visited[adj[u][i]] == ` `false` `) ` ` ` `DFSUtil(adj[u][i], adj, visited); ` `} ` ` ` `// This function does DFSUtil() for all ` `// unvisited vertices. ` `void` `DFS(vector<` `int` `> adj[], ` `int` `V) ` `{ ` ` ` `vector<` `bool` `> visited(V, ` `false` `); ` ` ` `for` `(` `int` `u=0; u<V; u++) ` ` ` `if` `(visited[u] == ` `false` `) ` ` ` `DFSUtil(u, adj, visited); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `V = 5; ` ` ` ` ` `// The below line may not work on all ` ` ` `// compilers. If it does not work on ` ` ` `// your compiler, please replace it with ` ` ` `// following ` ` ` `// vector<int> *adj = new vector<int>[V]; ` ` ` `vector<` `int` `> adj[V]; ` ` ` ` ` `// Vertex numbers should be from 0 to 4. ` ` ` `addEdge(adj, 0, 1); ` ` ` `addEdge(adj, 0, 4); ` ` ` `addEdge(adj, 1, 2); ` ` ` `addEdge(adj, 1, 3); ` ` ` `addEdge(adj, 1, 4); ` ` ` `addEdge(adj, 2, 3); ` ` ` `addEdge(adj, 3, 4); ` ` ` `DFS(adj, V); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

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 contribute@geeksforgeeks.org. 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

## Recommended Posts:

- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Shortest cycle in an undirected unweighted graph
- Find any simple cycle in an undirected unweighted Graph
- Test Case Generation | Set 4 (Random directed / undirected weighted and unweighted Graphs)
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Number of shortest paths in an unweighted and directed graph
- Shortest path in an unweighted graph
- Multi Source Shortest Path in Unweighted Graph
- Prefix Sum Array - Implementation and Applications in Competitive Programming
- Count of unique lengths of connected components for an undirected graph using STL
- Test Case Generation | Set 3 (Unweighted and Weighted Trees)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Transitive Closure of a Graph using DFS
- Check if a given graph is Bipartite using DFS
- Minimum number of edges between two vertices of a graph using DFS
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- BFS using STL for competitive coding
- Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2