Skip to content

Commit b1683b8

Browse files
committed
Print adjacency list
1 parent 97a6598 commit b1683b8

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Graph/Print adjacency list.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
void printGraph(vector<int> adj[], int V)
6+
{
7+
for(int i=0;i<V;i++)
8+
{
9+
cout<<i;
10+
for(int j=0;j< adj[i].size();j++)
11+
{
12+
cout<<"-> "<<adj[i][j];
13+
}
14+
cout<<endl;
15+
}
16+
// Your code here
17+
}
18+
19+
20+
//Position this line where user code will be pasted.
21+
int main()
22+
{
23+
int t;
24+
cin>>t;
25+
while(t--)
26+
{ int v, e;
27+
cin>>v>>e;
28+
int a, b;
29+
vector<int> adj[v];
30+
for(int i=0;i<e;i++)
31+
{
32+
cin>>a>>b;
33+
adj[a].push_back(b);
34+
adj[b].push_back(a);
35+
}
36+
printGraph(adj, v);
37+
38+
}
39+
return 0;
40+
}
41+
42+
43+
/*This is a function problem.You only need to complete the function given below*/
44+
// The Graph structure is as folows
45+
// Function to print graph
46+
// adj: array of vectors to represent graph
47+
// V: number of vertices
48+
49+
50+
51+
/*Input:
52+
8 14
53+
0 3
54+
0 1
55+
1 2
56+
1 4
57+
1 5
58+
1 3
59+
2 6
60+
2 4
61+
4 7
62+
5 6
63+
5 2
64+
5 3
65+
5 7
66+
7 1
67+
68+
Its Correct output is:
69+
0-> 3-> 1
70+
1-> 0-> 2-> 4-> 5-> 3-> 7
71+
2-> 1-> 6-> 4-> 5
72+
3-> 0-> 1-> 5
73+
4-> 1-> 2-> 7
74+
5-> 1-> 6-> 2-> 3-> 7
75+
6-> 2-> 5
76+
7-> 4-> 5-> 1 */

Graph/Print adjacency list.exe

1.54 MB
Binary file not shown.

0 commit comments

Comments
 (0)