Problem StatementImplement a Graph using an Adjacency List. Given the number of vertices (V) and the edges, create an undirected graph using an adjacency list. Print the adjacency list of each vertex.Input format :The first line of input consists of an integer V, representing the number of vertices.The second line consists of an integer E, representing the number of edges.The following E lines consist of the edge information.Output format :The output prints the adjacency list of each vertex.Refer to the sample output for formatting specifications.Code constraints :1 ≤ V ≤ 1001 ≤ E ≤ 100Sample test cases :Input 1 :570 10 41 21 31 42 33 4Output 1 :Adjacency list of vertex 0head -> 1-> 4Adjacency list of vertex 1head -> 0-> 2-> 3-> 4Adjacency list of vertex 2head -> 1-> 3Adjacency list of vertex 3head -> 1-> 2-> 4Adjacency list of vertex 4head -> 0-> 1-> 3Input 2 :671 31 41 52 32 42 54 6Output 2 :Adjacency list of vertex 0head Adjacency list of vertex 1head -> 3-> 4-> 5Adjacency list of vertex 2head -> 3-> 4-> 5Adjacency list of vertex 3head -> 1-> 2Adjacency list of vertex 4head -> 1-> 2-> 6Adjacency list of vertex 5head -> 1-> 2Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Question
Problem StatementImplement a Graph using an Adjacency List. Given the number of vertices (V) and the edges, create an undirected graph using an adjacency list. Print the adjacency list of each vertex.Input format :The first line of input consists of an integer V, representing the number of vertices.The second line consists of an integer E, representing the number of edges.The following E lines consist of the edge information.Output format :The output prints the adjacency list of each vertex.Refer to the sample output for formatting specifications.Code constraints :1 ≤ V ≤ 1001 ≤ E ≤ 100Sample test cases :Input 1 :570 10 41 21 31 42 33 4Output 1 :Adjacency list of vertex 0head -> 1-> 4Adjacency list of vertex 1head -> 0-> 2-> 3-> 4Adjacency list of vertex 2head -> 1-> 3Adjacency list of vertex 3head -> 1-> 2-> 4Adjacency list of vertex 4head -> 0-> 1-> 3Input 2 :671 31 41 52 32 42 54 6Output 2 :Adjacency list of vertex 0head Adjacency list of vertex 1head -> 3-> 4-> 5Adjacency list of vertex 2head -> 3-> 4-> 5Adjacency list of vertex 3head -> 1-> 2Adjacency list of vertex 4head -> 1-> 2-> 6Adjacency list of vertex 5head -> 1-> 2Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Solution
To implement a graph using an adjacency list, we need to follow the given steps:
- Read the input values for the number of vertices (V) and the number of edges (E).
- Create an empty adjacency list to store the graph.
- Iterate E times to read the edge information.
- For each edge, read the source vertex (u) and the destination vertex (v).
- Add v to the adjacency list of u, and add u to the adjacency list of v.
- Repeat steps 4 and 5 for all E edges.
- Print the adjacency list of each vertex.
- For each vertex from 0 to V-1, print "Adjacency list of vertex i" followed by "->" and the list of adjacent vertices.
- Repeat step 8 for all vertices.
Here is the code to implement the above steps:
# Step 1: Read the input values
V = int(input())
E = int(input())
# Step 2: Create an empty adjacency list
adj_list = [[] for _ in range(V)]
# Step 3: Iterate E times to read the edge information
for _ in range(E):
# Step 4: Read the source vertex (u) and the destination vertex (v)
u, v = map(int, input().split())
# Step 5: Add v to the adjacency list of u, and add u to the adjacency list of v
adj_list[u].append(v)
adj_list[v].append(u)
# Step 7: Print the adjacency list of each vertex
for i in range(V):
# Step 8: Print "Adjacency list of vertex i" followed by "->" and the list of adjacent vertices
print("Adjacency list of vertex", i)
print("head", end=" ")
for vertex in adj_list[i]:
print("->", vertex, end=" ")
print()
This code will create an undirected graph using an adjacency list and print the adjacency list of each vertex as per the given input format and constraints.
Similar Questions
vector < vector < int >> printAdjacency(int n, int m, vector < vector < int >> & edges) { // Write your code here. vector < vector < int>> adj_list[n+1]; for (int i=0; i<m; i++){ adj_list[edges[i][0]].push_back(edges[i][1]); adj_list[edges[i][1]].push_back(edges[i][0]); } return adj_list;}
Consider the undirected graph: A / \ E D \ / C | B Which of the following is a valid adjacency list representation of the above graph?
Adjacency list requires memory of the order of2 pointsθ(V-E)θ(VE)θ(V/E)θ(V+E)
Create an adjacency list based on the given graph.
Adjacency list requires memory of the order of
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.