0% found this document useful (0 votes)
4 views

Import Java

This document discusses representing graphs using adjacency lists and matrices in Java. It includes code to create an adjacency list from a list of edges, print the adjacency list, create an adjacency matrix from input, and print the matrix.

Uploaded by

jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Import Java

This document discusses representing graphs using adjacency lists and matrices in Java. It includes code to create an adjacency list from a list of edges, print the adjacency list, create an adjacency matrix from input, and print the matrix.

Uploaded by

jay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;

public class Main {

// Function to print adjacency list

static void printAdjacencyList(Map<Integer, List<Integer>> adjList) {

for (Map.Entry<Integer, List<Integer>> entry : adjList.entrySet()) {

int vertex = entry.getKey();

List<Integer> neighbors = entry.getValue();

System.out.print("vertex " + vertex + ": ");

for (int neighbor : neighbors) {

System.out.print(neighbor + " ");

System.out.println();

// Function to create adjacency list

static Map<Integer, List<Integer>> createAdjacencyList(List<int[]> edges) {

Map<Integer, List<Integer>> adjList = new HashMap<>();

for (int[] edge : edges) {

int u = edge[0];

int v = edge[1];

// Add u to v's list and v to u's list

adjList.computeIfAbsent(u, k -> new ArrayList<>()).add(v);

adjList.computeIfAbsent(v, k -> new ArrayList<>()).add(u);

return adjList;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input: Number of vertices

int numVertices = scanner.nextInt();


// Input: Edges (pairs of vertices)

List<int[]> edges = new ArrayList<>();

for (int i = 0; i < numVertices; i++) {

int u = scanner.nextInt();

int v = scanner.nextInt();

edges.add(new int[]{u, v});

// Create adjacency list

Map<Integer, List<Integer>> adjacencyList = createAdjacencyList(edges);

// Output: Display the adjacency list

printAdjacencyList(adjacencyList);

scanner.close();

GRAPH ON MATRIX

import java.util.Scanner;

public class Main {

// Function to print adjacency matrix

static void printAdjacencyMatrix(int[][] adjMatrix, int n) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

System.out.print(adjMatrix[i][j] + " ");

System.out.println();

}
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input: Number of vertices

int n = scanner.nextInt();

// Input: Adjacency matrix

int[][] adjMatrix = new int[n][n];

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

adjMatrix[i][j] = scanner.nextInt();

// Output: Display the adjacency matrix

printAdjacencyMatrix(adjMatrix, n);

scanner.close();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy