0% found this document useful (0 votes)
38 views3 pages

Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 13 Date: 14 July, 2020

The document contains source code for representing a graph using vectors in C++. It defines functions to add edges to the graph, represented as an adjacency list, and to print the adjacency list. It then provides a main function that creates a sample graph with 5 vertices and prints the adjacency list representation.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Source Code:: Atif Jalal 02-235191-027 BS (IT) - 3A Lab 13 Date: 14 July, 2020

The document contains source code for representing a graph using vectors in C++. It defines functions to add edges to the graph, represented as an adjacency list, and to print the adjacency list. It then provides a main function that creates a sample graph with 5 vertices and prints the adjacency list representation.

Uploaded by

Atif Jalal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Atif Jalal

02-235191-027
BS (IT)-3A Lab 13 Date: 14 July, 2020

Source Code:
// A simple representation of graph using STL
#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 print the adjacency list


Atif Jalal
02-235191-027
BS (IT)-3A Lab 13 Date: 14 July, 2020
// representation of graph
void printGraph(vector<int> adj[], int V)
{
for (int v = 0; v < V; ++v)
{
cout << "\n Adjacency list of vertex "
<< v << "\n head ";
for (auto x : adj[v])
cout << "-> " << x;
printf("\n");
}
}

// Driver code
int main()
{
int V = 5;
vector<int> adj[V];
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);
printGraph(adj, V);
return 0;
}
Atif Jalal
02-235191-027
BS (IT)-3A Lab 13 Date: 14 July, 2020

Output:

You might also like