Skip to content

Commit b6c0973

Browse files
author
AshuG11
authoredOct 12, 2022
Adjancancy list in graph.cpp
1 parent f31edd7 commit b6c0973

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
 

‎Adjancancy list in graph.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Print Adjancancy list in graph
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
// } Driver Code Ends
7+
8+
9+
10+
class Solution
11+
{
12+
public:
13+
//Function to return the adjacency list for each vertex.
14+
vector<vector<int>>printGraph(int V, vector<int> adj[])
15+
{
16+
// Code here
17+
vector<vector<int>>v(V);
18+
for(int i=0;i<V;i++)
19+
{
20+
v[i].push_back(i);
21+
for(auto x:adj[i])
22+
{
23+
v[i].push_back(x);
24+
}
25+
}
26+
return v;
27+
}
28+
};
29+
30+
// { Driver Code Starts.
31+
int main(){
32+
int tc;
33+
cin >> tc;
34+
while(tc--){
35+
int V, E;
36+
cin >> V >> E;
37+
vector<int>adj[V];
38+
for(int i = 0; i < E; i++){
39+
int u, v;
40+
cin >> u >> v;
41+
adj[u].push_back(v);
42+
adj[v].push_back(u);
43+
}
44+
Solution obj;
45+
vector<vector<int>>ans=obj.printGraph(V, adj);
46+
for(int i=0;i<ans.size();i++){
47+
for(int j=0;j<ans[i].size()-1;j++){
48+
cout<<ans[i][j]<<"-> ";
49+
}
50+
cout<<ans[i][ans[i].size()-1];
51+
cout<<endl;
52+
}
53+
}
54+
return 0;
55+
} // } Driver Code Ends

0 commit comments

Comments
 (0)
Please sign in to comment.