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
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
ADVERTISEMENT