Files
agent_compositor_test/references/programiz/adj_list.cpp
T
Abdelrahman Said a11edf0c53 Add graph references
2026-06-28 13:49:01 +01:00

40 lines
671 B
C++

// vim:fileencoding=utf-8:foldmethod=marker
// Adjascency List representation in C++
#include <vector>
#include <iostream>
using namespace std;
// Add edge
void addEdge(vector<int> adj[], int s, int d) {
adj[s].push_back(d);
adj[d].push_back(s);
}
// Print the graph
void printGraph(vector<int> adj[], int V) {
for (int d = 0; d < V; ++d) {
cout << "\n Vertex "
<< d << ":";
for (auto x : adj[d])
cout << "-> " << x;
printf("\n");
}
}
int main() {
int V = 5;
// Create a graph
vector<int> adj[V];
// Add edges
addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 0, 3);
addEdge(adj, 1, 2);
printGraph(adj, V);
}