-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab12_Q3.cpp
More file actions
36 lines (30 loc) · 811 Bytes
/
Lab12_Q3.cpp
File metadata and controls
36 lines (30 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
const int maxV = 100;
int adjList[maxV][maxV];
int adjSize[maxV];
void createAdjacencyList(int V, int E, int edges[][2]) {
for (int i = 0; i < V; ++i) {
adjSize[i] = 0;
}
for (int i = 0; i < E; ++i) {
int u = edges[i][0];
int v = edges[i][1];
adjList[u][adjSize[u]++] = v;
adjList[v][adjSize[v]++] = u;
}
}
int main() {
int V = 5, E = 7;
int edges[7][2] = {{0, 1}, {0, 4}, {4, 1}, {4, 3}, {1, 3}, {1, 2}, {3, 2}};
createAdjacencyList(V, E, edges);
cout<<"Adjacency List"<<endl;
for (int i = 0; i < V; ++i) {
cout << "Vertex " << i << ": ";
for (int j = 0; j < adjSize[i]; ++j) {
cout << adjList[i][j] << " ";
}
cout << endl;
}
return 0;
}