Print adjacency list | Practice | GeeksforGeeks

Given the adjacency list of a bidirectional graph. Your task is to copy/clone the adjacency list for each vertex and return a new list.

Example 1:
Input:

Output: 
0-> 1-> 4 
1-> 0-> 2-> 3-> 4 
2-> 1-> 3 
3-> 1-> 2-> 4 
4-> 0-> 1-> 3
Explanation:
As 0,1 and 3 is connected to 4 so 4th row
of the list containing 4 and its connected
nodes 0,1 and 3 and we have to add those in
sorted order and same for every row.



Example 2:
Input:


Output: 
0-> 2-> 3 
1-> 2 
2-> 0-> 1 
3-> 0
Explanation:
As 2 and 3 is connected to 0 so 0th row
of the list containing 0 and its connected 
nodes 2 and 3 and we have to add those in
sorted order and same for every row. 



Your task:
You dont need to read input or print anything. Your task is to complete the function printGraph() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns the list of list  contains the node itself with its connected nodes(as same as it is given in input adjacency  list).The returned list is a copy/clone of the actual list.

Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ V, E ≤ 104
0
harshscode1 week ago
 vector<vector<int>> printGraph(int v, vector<int> adj[]) {                vector<vector<int>> ans(v);         for(int i=0;i<v;i++)         {             ans[i].push_back(i);             for(auto j:adj[i])             ans[i].push_back(j);         }                      return ans;          }   
0
databharat00071 week ago
vector<vector<int>> printGraph(int V, vector<int> adj[]) {       // Code here       for(int i=0; i<V; i++){           int sz=adj[i].size();           for(int j=0; j<sz/2; j++){               swap(adj[i][j], adj[i][sz-j-1]);           }           adj[i].push_back(i);           int newSize=adj[i].size();           for(int j=0; j<newSize/2; j++){               swap(adj[i][j], adj[i][newSize-j-1]);           }       }       vector<vector<int>> ans;       for(int i=0; i<V; i++){           ans.push_back(adj[i]);       }       return ans;   }
0
jainmuskan5651 week ago
vector<vector<int>> printGraph(int V, vector<int> adj[]) {       // Code here        vector<vector<int>>a;        for(int i=0;i<V;i++){            vector<int>v;            v.push_back(i);            for(auto x:adj[i]){                v.push_back(x);            }            a.push_back(v);        }        return a;   }
+1
aryany8711 week ago
//two ways of doing it
 
vector<vector<int>> printGraph(int n, vector<int> adj[]) {              //1. TC-O(V+E),AS-O(1)       // vector<vector<int>>v(n);              // for(int i=0;i<n;i++){       //     auto it = adj[i];       //     v[i].resize(it.size()+1);       //     int j=0;       //     v[i][j++]=i;       //     for(auto e : it){       //         v[i][j++]=e;       //     }       // }              // return v;              //2. TC-O(V+E),AS-O(1)       vector<vector<int>>v;              for(int i=0;i<n;i++){           vector<int> curr;           curr.push_back(i);                      for(auto e : adj[i]){               curr.push_back(e);           }                      v.push_back(curr);                  }              return v;   }
0
altayeblubna2 weeks ago
Optimized Python3 code
class Solution:
    def printGraph(self, V, adj):
        for i in range(V):
            adj[i] = ([i] + adj[i])
        return adj
And yes, it's as simple as that. 
0
gfgraj1 month ago
vector<vector<int>> printGraph(int V, vector<int> adj[]) {       vector<vector<int>>a;       for(int i=0;i<V;i++){           vector<int>v1;           v1.push_back(i);           for(auto x: adj[i]){               v1.push_back(x);           }           a.push_back(v1);       }       return a;          }
+2
saurabhsathe12341 month ago
vector<vector<int>> printGraph(int V, vector<int> adj[]) {
        // Code here
        vector<vector<int>> res(V);
        for(int i=0;i<V;i++)
        {
            
            res[i].push_back(i);
            for(auto v:adj[i])
            {
                res[i].push_back(v);
            }
            
                
        }
        return res;
        
        
    }
0
detroix071 month ago
vector<vector<int>> printGraph(int V, vector<int> adj[]) {       // Code here    vector<vector<int>> ans(V);  // number of row in ans is V but now column is 0 of every row         for(int i=0;i<=V-1;i++) {      ans[i].push_back(i);     for(auto itr: adj[i]) {       ans[i].push_back(itr);      }    }    return ans;   }
+2
madhukartemba2 months ago
JAVA SOLUTION:
class Solution {
    // Function to return the adjacency list for each vertex.
    public ArrayList<ArrayList<Integer>> printGraph(int V, ArrayList<ArrayList<Integer>> adj)
    {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        
        
        for(int i=0; i<V; i++)
        {
            ArrayList<Integer> temp = new ArrayList<>();
            temp.add(i);
            ans.add(temp);
        }
        
        int i = 0;
        
        for(ArrayList<Integer> temp : adj)
        {
            ans.get(i++).addAll(temp);
        }
        
        
        return ans;
        
    }
}
0
amitdeep26992 months ago
class Solution:      #Function to return the adjacency list for each vertex.   def printGraph(self, V, adj):       # code here       path=[]       for i in range(V):           path.append([i]+adj[i])       return path
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.