0% found this document useful (0 votes)
37 views2 pages

hw2 3

This program takes in a directed graph from a user and converts it to an adjacency list format by creating arrays to store the vertices and edges, reading in the graph details, creating adjacency lists for each vertex, and outputting the resulting adjacency lists.

Uploaded by

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

hw2 3

This program takes in a directed graph from a user and converts it to an adjacency list format by creating arrays to store the vertices and edges, reading in the graph details, creating adjacency lists for each vertex, and outputting the resulting adjacency lists.

Uploaded by

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

/* Title: hw2_3.

java
* Abstract: This program converts a directed graph from a user into a
* corresponding adjanceny list format.
* Author: Justin Dinkelbach
* ID:
* Date: 03/6/2021
*/

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class hw2_3


{
public static void main(String[] args)
{
ArrayList<Integer> vertices = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);

// Get num Vertices/Edges from user


int numVertices = input.nextInt();
input.nextLine();
int edges = input.nextInt();
input.nextLine();

// Case: no edges
if (edges == 0){
for (int i = 0; i < numVertices; i++){
System.out.println(i);
}
return;
}

// Create ArrayList for each vertex


ArrayList<ArrayList<Integer>> adj = new
ArrayList<ArrayList<Integer>>(numVertices);
for (int i = 0; i < numVertices; i++){
adj.add(new ArrayList<Integer>());
}

// Determine adjancency lists


for(int i = 0; i < edges; i++){
String line = input.nextLine();
Pattern pattern = Pattern.compile(" ");
List<Integer> directedGraph = pattern.splitAsStream(line)
.map(Integer::valueOf)
.collect(Collectors.toList());

if (!vertices.contains(directedGraph.get(0))){
vertices.add(directedGraph.get(0));
}
if (!vertices.contains(directedGraph.get(1))){
vertices.add(directedGraph.get(1));
}

adj.get(directedGraph.get(0)).add(directedGraph.get(1));
Collections.sort(adj.get(directedGraph.get(0)));
Collections.sort(vertices);
}
// Print resulting adjancency list
for (int i = 0; i < numVertices; i++){
String result = String.valueOf(vertices.get(i));
for (int j = 0; j < adj.get(i).size(); j++){
result += "->" + adj.get(i).get(j);
}
System.out.println(result);
}
}
}

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