From 9f648b5333c60d130794c8f96a8541f67daed7e7 Mon Sep 17 00:00:00 2001 From: Abhishek singh lodhi <80988197+ABHISHEK-565@users.noreply.github.com> Date: Thu, 27 Oct 2022 19:25:30 +0530 Subject: [PATCH] Create Answer55 --- Answer55 | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Answer55 diff --git a/Answer55 b/Answer55 new file mode 100644 index 0000000..64a38ae --- /dev/null +++ b/Answer55 @@ -0,0 +1,54 @@ +#include +using namespace std; + +// Function to add edges +void addEdge(vector adj[], int u, int v) +{ + adj[u].push_back(v); +} + +// Function to print adjacency list +void adjacencylist(vector adj[], int V) +{ + for (int i = 0; i < V; i++) { + cout << i << "->"; + for (int& x : adj[i]) { + cout << x << " "; + } + cout << endl; + } +} + +// Function to initialize the adjacency list of the given graph +void initGraph(int V, int edges[3][2], int noOfEdges) +{ + // To represent graph as adjacency list + vector adj[V]; + + // Traverse edges array and make edges + for (int i = 0; i < noOfEdges; i++) { + + // Function call to make an edge + addEdge(adj, edges[i][0], edges[i][1]); + } + + // Function Call to print adjacency list + adjacencylist(adj, V); +} + +// Driver Code +int main() +{ + // Given vertices + int V = 3; + + // Given edges + int edges[3][2] = { { 0, 1 }, { 1, 2 }, { 2, 0 } }; + + int noOfEdges = 3; + + // Function Call + initGraph(V, edges, noOfEdges); + + return 0; +}