Question
Write a java program to do BFS topological sort . your program must be able to catch the loop.

m у 2.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;

class grph { 
        int vrt;
        List<Integer> adj[]; 
        public grph(int vrt) 
        { 
                this.vrt = vrt; 
                adj = new ArrayList[vrt]; 
                for (int p= 0; p < vrt; p++) 
                        adj[p] = new ArrayList<Integer>(); 
        } 
        public void add(int u, int v) 
        { 
                adj[u].add(v); 
        } 
        public void BfsTopologicalSort() 
        { 
                int Indegree[] = new int[vrt]; 
                for (int p = 0; p < vrt; p++) { 
                        ArrayList<Integer> temp 
                                = (ArrayList<Integer>)adj[p]; 
                        for (int node : temp) { 
                                Indegree[node]++; 
                        } 
                } 
                Queue<Integer> q 
                        = new LinkedList<Integer>(); 
                for (int p = 0; p < vrt; p++) { 
                        if (Indegree[p] == 0) 
                                q.add(p); 
                } 
                int c = 0; 
                Vector<Integer> TopOrder = new Vector<Integer>(); 
                while (!q.isEmpty()) { 
                        int u = q.poll(); 
                        TopOrder.add(u); 

                        for (int node : adj[u]) { 
                                if (--Indegree[node] == 0) 
                                        q.add(node); 
                        } 
                        c++; 
                } 
                if (c != vrt) { 
                        System.out.println( 
                                "cycle iS PRESENT IN the graph"); 
                        return; 
                } 
                for (int p : TopOrder) { 
                        System.out.print(p + " "); 
                } 
        } 
} 
class Main { 
        public static void main(String args[]) 
        { 
                grph gp = new grph(14); 
                gp.add(12,9); 
                gp.add(10,13); 
                gp.add(9,11); 
                gp.add(9,10); 
                gp.add(8,7); 
                gp.add(6,5); 
                gp.add(5,12);
                gp.add(5,8);
                gp.add(4,7);
                gp.add(3,13);
                gp.add(3,6);
                gp.add(3,2);
                gp.add(2,6);
                gp.add(2,9);
                gp.add(2,5);
                gp.add(1,8);
                gp.add(1,4);
                gp.add(1,2);
                gp.add(0,11);
                gp.add(0,4);
                gp.add(0,5);
               
                System.out.println( 
                        "BFS _Topological Sort"); 
                gp.BfsTopologicalSort(); 
        } 
}

As per the program i assigned

edge m is taken 0

edge n is taken 1

edge o is taken 2

edge p is taken 3

edge q is taken 4

edge r is taken 5

edge s is taken 6

edge t is taken 7

edge u is taken 8

edge v is taken 9

edge w is taken 10

edge x is taken 11

edge y is taken 12

edge z is taken 13

please take accordingly bro and give me thumbs up

Add a comment
Know the answer?
Add Answer to:
Write a java program to do BFS topological sort . your program must be able to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the following graph, write a Java program using Topological Sort. please write java code and...

    In the following graph, write a Java program using Topological Sort. please write java code and show me the output. Thank you :) b.

  • 1) A Java program that implements an insertion sort algorithm. (InsertionSort.java): Must include the proper comments...

    1) A Java program that implements an insertion sort algorithm. (InsertionSort.java): Must include the proper comments in the code. 2) A report in pdf. It contains: a) the pseudocode of the insertion sort algorithm and assertions between lines of the algorithm. b) As insertion sort has a nested-loop of two layers, you need to put one assertion in each loop. c) a proof of the partial correctness of the algorithm using mathematical induction c.1) prove the correctness of the two...

  • Write a Java program to implement Counting Sort and write a driver to test it. Note:...

    Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n).

  • Write a java program that makes use of the ‘DO WHILE ’ loop. The program asks...

    Write a java program that makes use of the ‘DO WHILE ’ loop. The program asks a user to enter information about several of their friends. The information is their last name, the state they live in, their age, and their expected graduation year. After you enter one friend, write out each individually. You must use JOptionPane to input the 4 fields, and to output the 4 fields. Refer to the textbook Code Listing 4-6 to see how to use...

  • Write a program in Java that converts the following do-while loop block to: for loop Write,...

    Write a program in Java that converts the following do-while loop block to: for loop Write, compile and run below program segment. Make sure to click on the Output Window to input the number. Evaluate the program. Take notes and comment of below program segments of your observations. int x; Scanner input = new Scanner(System.in);       x = input.nextInt();       do{             System.out.printf(“%d\n”, x * 2);             x++;       } while(x < 100);

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • JAVA question: Write a program to sort the following US States in their natural order :...

    JAVA question: Write a program to sort the following US States in their natural order : Missouri, Illinois, California, Washington, Maine. HINT : Create a List of the States and user the sort method of the Collections interface. Please copy and paste your code below.

  • Please do in Java with the code available for copy :) Write a computer program that...

    Please do in Java with the code available for copy :) Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of...

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order...

    Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order (from smallest to largest). You can either get the array as input or hardcode it inside your program.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT