omplete the method to perform breadth first traversal (search) here is the method: private static void breadthFirst() { //Todo System.out.println("test"); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Complete the method to perform breadth first traversal (search) 

here is the method:


 private static void breadthFirst() {
        //Todo 
        System.out.println("test");
    }


here is the complete source code:

 

 

Graph.java
---------------------------------
package finlab;

import java.util.*;

public class Graph {
    List<Edge>[] list;
    public Graph(int n){
        list = new LinkedList[n];
        for(int i=0; i<list.length;i++){
            list[i] = new LinkedList<>();
        }
    }

    void addEdge(int u, int v, int w){
        list[u].add(0,new Edge(v,w));
    }

    boolean isConnected(int u, int v){
        for(Edge e: list[u]) {
            if (e.v == v)
                return true;
        }
        return false;
    }

    @Override
    public String toString(){
        String result= "";
        for(int i=0; i<list.length; i++){
            result+= i+": "+list[i]+"\n";
        }
        return result;
    }
}


Edge.java
--------------------------------------------------------
package finlab;

class Edge{
    int v;
    int w;
    public Edge(int v,int w){
        this.v = v;
        this.w = w;
    }

    public int getVertex(){
        return v;
    }

    public int getWeight(){
        return w;
    }

    @Override
    public String toString(){
        return "("+v+", "+w+")";
    }
}
 

Main.java
----------------------------------

package finlab;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("1. Load file containing the graph's data");
        System.out.println("2. Perform Depth First Traversal of the graph");
        System.out.println("3. Perform Breadth First Traversal of the graph");
        System.out.println("4. Show to shortest path from one vertex to another");
        System.out.println("5. Exit");

        System.out.print("Enter your choice: ");
        int c = scan.nextInt();

        try {
            while (c != 5) {
                performChoice(c);
                System.out.println("\n----------");
                System.out.println("1. Load file containing the graph's data");
                System.out.println("2. Perform Depth First Traversal of the graph");
                System.out.println("3. Perform Breadth First Traversal of the graph");
                System.out.println("4. Show to shortest path from one vertex to another");
                System.out.println("5. Exit");
                System.out.print("Enter your choice: ");
                c = scan.nextInt();
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    static void performChoice(int c) throws Exception {
        switch (c){
            case 1 -> loadFile(); 
            case 2 -> depthFirst(); 
            case 3 -> breadthFirst();
            case 4 -> dijkstra();
            case 5 -> System.exit(0);
            default -> System.out.println("Invalid choice, try again.");
        }
    }

    private static void loadFile() throws Exception {
        File file = new File("src/finlab/data.txt");
        Scanner scan = new Scanner(file);

        System.out.println("==========");
        if(file.canRead()){
            System.out.println("File is readable");
            System.out.println("File contents: \n");
        }else{
            System.out.println("File is not readable");
        }
        while (scan.hasNextLine()){
            System.out.println(scan.nextLine());
        }
    }

    private static void depthFirst() {
        ///Todo 
        System.out.println("test");
    }

    private static void breadthFirst() {
        //Todo 
        System.out.println("test");
    }

    private static void dijkstra() {
        //Todo 
        System.out.println("test");
    } 
}

 

Test.java

------------------------------

 

package finlab;

public class Test {
    public static void main(String[] args) {
        Graph g = new Graph(4);
        g.addEdge(0,1,2);
        g.addEdge(0,2,3);
        System.out.println(g.toString());
    }
}


data.txt

--------------------------
 

destination, vertex, weight
0,1,2
0,2,1
0,3,6
1,0,2
1,4,4
1,5,8
2,0,1
2,6,2
3,0,6
3,7,7
4,1,4
4,7,5
5,1,8
5,7,3
6,2,2
6,7,4
7,3,7
7,6,4
7,4,5
7,5,3

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education