Sure! Below are the equivalent codes in Python, Java, C#, and JavaScript, adhering to your specifications.
Python
# Function to add an edge between two vertices
def addEdge(adj, u, v):
adj[u].append(v)
# Function to display the adjacency list
def displayAdjList(adj):
for i in range(len(adj)):
print(f"{i}: ", end="")
for j in adj[i]:
print(f"{j} ", end="")
print()
# Main function
def main():
# Create a graph with 3 vertices and 3 edges
V = 3
adj = [[] for _ in range(V)]
# Now add edges one by one
addEdge(adj, 1, 0)
addEdge(adj, 1, 2)
addEdge(adj, 2, 0)
print("Adjacency List Representation:")
displayAdjList(adj)
if __name__ == "__main__":
main()
Java
import java.util.ArrayList;
import java.util.List;
class GfG {
// Function to add an edge between two vertices
static void addEdge(List<List<Integer>> adj, int u, int v) {
adj.get(u).add(v);
}
// Function to display the adjacency list
static void displayAdjList(List<List<Integer>> adj) {
for (int i = 0; i < adj.size(); i++) {
System.out.print(i + ": ");
for (int j : adj.get(i)) {
System.out.print(j + " ");
}
System.out.println();
}
}
// Main function
public static void main(String[] args) {
// Create a graph with 3 vertices and 3 edges
int V = 3;
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
// Now add edges one by one
addEdge(adj, 1, 0);
addEdge(adj, 1, 2);
addEdge(adj, 2, 0);
System.out.println("Adjacency List Representation:");
displayAdjList(adj);
}
}
C#
using System;
using System.Collections.Generic;
class GfG
{
// Function to add an edge between two vertices
static void addEdge(List<List<int>> adj, int u, int v)
{
adj[u].Add(v);
}
// Function to display the adjacency list
static void displayAdjList(List<List<int>> adj)
{
for (int i = 0; i < adj.Count; i++)
{
Console.Write(i + ": ");
foreach (int j in adj[i])
{
Console.Write(j + " ");
}
Console.WriteLine();
}
}
// Main function
static void Main(string[] args)
{
// Create a graph with 3 vertices and 3 edges
int V = 3;
List<List<int>> adj = new List<List<int>>();
for (int i = 0; i < V; i++)
{
adj.Add(new List<int>());
}
// Now add edges one by one
addEdge(adj, 1, 0);
addEdge(adj, 1, 2);
addEdge(adj, 2, 0);
Console.WriteLine("Adjacency List Representation:");
displayAdjList(adj);
}
}
JavaScript
// Function to add an edge between two vertices
function addEdge(adj, u, v) {
adj[u].push(v);
}
// Function to display the adjacency list
function displayAdjList(adj) {
for (let i = 0; i < adj.length; i++) {
let line = i + ": ";
for (let j of adj[i]) {
line += j + " ";
}
console.log(line);
}
}
// Main function
function main() {
// Create a graph with 3 vertices and 3 edges
let V = 3;
let adj = Array.from({ length: V }, () => []);
// Now add edges one by one
addEdge(adj, 1, 0);
addEdge(adj, 1, 2);
addEdge(adj, 2, 0);
console.log("Adjacency List Representation:");
displayAdjList(adj);
}
main();
Each code snippet maintains the original variable names and comments, ensuring readability and consistency across different programming languages.