 Open in App
Not now

# Print adjacency list of a Bidirectional Graph

• Last Updated : 07 Dec, 2022

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

An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of all the other vertices to which the particular vertex has an edge.

Examples:

Input: N = 5

Output: Node 0 is connected to 1 and 2
Node 1 is connected to 0 and 3
Node 2 is connected to 0,3 and 4
Node 3 is connected to 1 and 2
Node 4 is connected to 2 The example graph

Approach:

The basic Idea to clone adjacency list for each vertex is to create a cloned vector and a visited array and check whether the node is visited or not.

• First Create a vector (say clone) of size N and an array visited[] of size N to check whether a particular is visited or not.
• Check if a node is visited or not:
• If not, then call a function to mark a non-visited node as present
•  Create two vectors adj[] and clone[] where adj[] has a list stored in it and clone[] will store the unmarked nodes.
• Mark every node as visited by visited[node] = 1.
• Then, traverse the adjacency list and check for non-visited nodes to add edges in the bidirectional graph.
• Push nodes and edges in the clone list

Below is the implementation of the above idea:

## C++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to add the edges we have
// in the adjacency list to the clone list
vector<int> clone[], vector<int>& visited)
{
// Marking node as visited
visited[node] = 1;

for (int it : adj[node]) {
if (!visited[it]) {

// Nodes are bidirectionally connected
clone[node].push_back(it);
clone[it].push_back(node);
}
}
}

// Function to print the adjacency list
void printList(vector<int> clone[], int n)
{
for (int node = 0; node < n; node++) {
cout << "Node " << node << " is connected to ";
for (int it : clone[node]) {
cout << it << " ";
}
cout << endl;
}
}

// Function to clone the given adjacency list
{
vector<int> clone[N];

// Visited array to check whether a particular
// node is visited or not
vector<int> visited(N, 0);

for (int node = 0; node < N; node++) {
if (!visited[node]) {
}
}
printList(clone, N);
}

// Driver code
int main()
{
// Adjacency List of a bidirectional graph
int N = 5;
adj = { 1, 2 };
adj = { 0, 3 };
adj = { 0, 3, 4 };
adj = { 1, 2 };

// Function call

return 0;
}```

## Java

```// Java code to implement the approach

import java.util.*;

class GFG {

// Function to add the edges we have
// in the adjacency list to the clone list
ArrayList<Integer>[] clone,
boolean[] visited)
{
visited[node] = true;

for (int it : adj[node]) {
if (!visited[it]) {
}
}
}

// Function to clone the given adjacency list
static void cloneList(ArrayList<Integer> adj[], int n)
{
ArrayList<Integer>[] clone = new ArrayList[n];
for (int i = 0; i < n; i++)
clone[i] = new ArrayList<>();

// Visited array to check
// whether a particular node is
// visited or not
boolean[] visited = new boolean[n];

for (int node = 0; node < n; node++) {
if (!visited[node]) {
}
}

// Printing cloned the adjacency list
for (int node = 0; node < n; node++) {
System.out.print("Node " + node
+ " is connected to ");
for (int it : clone[node]) {
System.out.print(it + " ");
}
System.out.println();
}
}

// Driver code
public static void main(String[] args)
{
// Adjacency List of a bidirectional graph
int N = 5;

adj = new ArrayList<>(List.of(0, 3, 4));

}
}```

## Python3

```# Python code to implement the approach

# Function to add the edges we have
# in the adjacency list to the clone list
# Marking node as visited
visited[node] = 1

if(not visited[it]):
# Nodes are bidirectionally connected
clone[node].append(it)
clone[it].append(node)

# Function to print the adjacency list
def printList(clone,n):
for node in range(n):
print("Node ",end="")
print(node,end="")
print(" is connected to ",end="")
for it in clone[node]:
print(it,end=" ")
print()

# Function to clone the given adjacency list
clone=[[] for j in range(N)]

# Visited array to check whether a particular
# node is visited or not
visited=*N

for node in range(N):
if(not visited[node]):

printList(clone, N)

# Driver code

# Adjacency List of a bidirectional graph
N = 5
adj = [[] for j in range(N)]

# Function call

# This code is contributed by Pushpesh Raj.

```

## C#

```using System;

public class GFG{

static public void Main (){

// Code
}
```

## Javascript

```// JS code to implement the approach

// Function to add the edges we have
// in the adjacency list to the clone list
{
// Marking node as visited
visited[node] = 1;

if (!visited[it]) {

// Nodes are bidirectionally connected
clone[node].push(it);
clone[it].push(node);
}
}
}

// Function to print the adjacency list
function printList( clone, n)
{
for (let node = 0; node < n; node++) {
console.log( "Node " , node , " is connected to ", clone[node]);
}
}

// Function to clone the given adjacency list
{
let clone  = [];
for(let i = 0; i < N; i++)
{
clone.push([]);
}

// Visited array to check whether a particular
// node is visited or not
let visited = [];
for(let i = 0; i < N; i++)
{
visited.push(0);
}

for (let node = 0; node < N; node++) {
if (!visited[node]) {
}
}
printList(clone, N);
}

// Driver code
// Adjacency List of a bidirectional graph
let N = 5;
for(let i =0;i<N;i++)
{
}
adj = [ 1, 2 ];
adj = [ 0, 3 ];
adj = [ 0, 3, 4 ];
adj = [ 1, 2 ];

// Function call

// This code is contributed by ksam24000```
Output

```Node 0 is connected to 1 2
Node 1 is connected to 0 3
Node 2 is connected to 0 3 4
Node 3 is connected to 1 2
Node 4 is connected to 2 ```

Time Complexity: O ( V + E ) where V is the number of vertices and E is the number of edges of the graph
Auxiliary Space: O ( V + E )

Related Articles:

My Personal Notes arrow_drop_up
Related Articles