Tuesday, 19 Oct 2021

# Print Adjacency List for a Directed Graph

An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of all the other vertices which the particular vertex has an edge to.Problem: Given the adjacency list and number of vertices and edges of a graph, the task is to represent the adjacency list for a directed graph.Examples:Input: V = 3, edges[][]= {{0, 1}, {1, 2} {2, 0}}  Output:   0 -> 1                1 -> 2                2 -> 0Explanation: The output represents the adjacency list for the given graph. Input: V = 4, edges[][] = {{0, 1}, {1, 2}, {1, 3}, {2, 3}, {3, 0}}  Output:   0 -> 1                1 -> 2 3                2 -> 3                3 -> 0Explanation: The output represents the adjacency list for the given graph. Approach (using STL): The main idea is to represent the graph as an array of vectors such that every vector represents the adjacency list of a single vertex. Using STL, the code becomes simpler and easier to understand. Below is the implementation of the above approach:C++#include using namespace std;void addEdge(vector adj[], int u, int v){    adj[u].push_back(v);}void adjacencylist(vector adj[], int V){    for (int i = 0; i < V; i++) {        cout