// Function to add an edge between two vertices
void addEdge(vector<vector<pair<int,int>>>& adj, int u, int v, int w) {
adj[u].push_back({v,w});
}
// Function to display the adjacency list
void displayAdjList(const vector<vector<pair<int,int>>>& adj) {
for (int i = 0; i < adj.size(); i++) {
cout << i << ": "; // Print the vertex
for (auto &j : adj[i]) {
cout << "{"<<j.first << ", "<<j.second<<"}"; // Print its adjacent
}
cout << endl;
}
}
// Main function
int main() {
// Create a graph with 3 vertices and 3 edges
int V = 3;
vector<vector<pair<int,int>>> adj(V);
code
// Now add edges one by one
addEdge(adj, 1, 0, 4);
addEdge(adj, 1, 2, 3);
addEdge(adj, 2, 0, 1);
cout << "Adjacency List Representation:" << endl;
displayAdjList(adj);
return 0;
}
Give me code in Python, Java, C#, JavaScript
without changing variable name and also do not change comments
have one line space before every comment.
don’t use class if needed then name it GfG
don’t use public if not needed.
Assistant
Python
python
# Function to add an edge between two vertices
def addEdge(adj, u, v, w):
adj[u].append((v, w))
# 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[0]}, {j[1]}}}", 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, 4)
addEdge(adj, 1, 2, 3)
addEdge(adj, 2, 0, 1)
print("Adjacency List Representation:")
displayAdjList(adj)
if __name__ == "__main__":
main()
Java
java
import java.util.ArrayList;
import java.util.List;
import java.util.AbstractMap.SimpleEntry;
// Function to add an edge between two vertices
class GfG {
static void addEdge(List<List<SimpleEntry<Integer, Integer>>> adj, int u, int v, int w) {
adj.get(u).add(new SimpleEntry<>(v, w));
}
// Function to display the adjacency list
static void displayAdjList(List<List<SimpleEntry<Integer, Integer>>> adj) {
for (int i = 0; i < adj.size(); i++) {
System.out.print(i + ": "); // Print the vertex
for (SimpleEntry<Integer, Integer> j : adj.get(i)) {
System.out.print("{" + j.getKey() + ", " + j.getValue() + "}"); // Print its adjacent
}
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<SimpleEntry<Integer, 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, 4);
addEdge(adj, 1, 2, 3);
addEdge(adj, 2, 0, 1);
System.out.println("Adjacency List Representation:");
displayAdjList(adj);
}
}
C#
csharp
using System;
using System.Collections.Generic;
// Function to add an edge between two vertices
class GfG
{
static void addEdge(List<List<KeyValuePair<int, int>>> adj, int u, int v, int w)
{
adj[u].Add(new KeyValuePair<int, int>(v, w));
}
// Function to display the adjacency list
static void displayAdjList(List<List<KeyValuePair<int, int>>> adj)
{
for (int i = 0; i < adj.Count; i++)
{
Console.Write(i + ": "); // Print the vertex
foreach (var j in adj[i])
{
Console.Write("{" + j.Key + ", " + j.Value + "}"); // Print its adjacent
}
Console.WriteLine();
}
}
// Main function
static void Main(string[] args)
{
// Create a graph with 3 vertices and 3 edges
int V = 3;
List<List<KeyValuePair<int, int>>> adj = new List<List<KeyValuePair<int, int>>>();
for (int i = 0; i < V; i++)
{
adj.Add(new List<KeyValuePair<int, int>>());
}
// Now add edges one by one
addEdge(adj, 1, 0, 4);
addEdge(adj, 1, 2, 3);
addEdge(adj, 2, 0, 1);
Console.WriteLine("Adjacency List Representation:");
displayAdjList(adj);
}
}
JavaScript
javascript
// Function to add an edge between two vertices
function addEdge(adj, u, v, w) {
adj[u].push([v, w]);
}
// Function to display the adjacency list
function displayAdjList(adj) {
for (let i = 0; i < adj.length; i++) {
let line = i + ": "; // Print the vertex
for (let j of adj[i]) {
line += `{${j[0]}, ${j[1]}}`; // Print its adjacent
}
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, 4);
addEdge(adj, 1, 2, 3);
addEdge(adj, 2, 0, 1);
console.log("Adjacency List Representation:");
displayAdjList(adj);
}
main();