Give the input edges in the file graph input.txt
- The file has 1 edge per line in the format (origin, destination)
- Assume the graph is not directional
- (2,5) means there is a path from 2 to 5 and from 5-2

For the Lab write the following code.
1) Write a function to find all adjacent vertices to a node N (find_adjacent(n))
2) Write a function that will return true or false that an edge exists (edge_exists(origin,destination))
3) Print out all Vertices using BFS

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Logger;

* @author
public class Lab {

    public static void main(String[] args) {
       System.out.print("find aout all adjaenct vertices of 5: ");
       for (Integer integer : find_adjancent(5)) {
            System.out.print(integer + " ");
       if (edge_exits(5, 3)) {
            System.out.println("There is an edge between 5 and 3");
       if (edge_exits(10, 5) == false) {
            System.out.println("There isn't any edge between 10 and 5");
       System.out.print("Prinit out all vertices using BFS: ");

    public static void printAllUsingBFS() {
       /// get set of all vertices in the grap
       Set<Integer> vertices = new HashSet<>();
       try {
            Scanner scanner = new Scanner(new File("graph_input.txt"));
            while (scanner.hasNext()) {
                // read a line
                String line = scanner.nextLine().trim();
                // split it by comma
                String[] sp = line.split(",");
                // turn its tokens to integer
                int n0 = Integer.parseInt(sp[0]);
                int n1 = Integer.parseInt(sp[1]);
                // push them to set
       } catch (FileNotFoundException | NumberFormatException ex) {

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats