Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- public class GraphAdjacentMatrix_TopologicalSort_BFS {
- private boolean adjMatrix[][];
- private int numVertices;
- private boolean visited[];
- public GraphAdjacentMatrix_TopologicalSort_BFS(int numVertices) {
- this.numVertices = numVertices;
- adjMatrix = new boolean[numVertices][numVertices];
- visited = new boolean[numVertices];
- }
- public void setEdge(int i, int j) {
- adjMatrix[i][j] = true;
- // adjMatrix[j][i] = true; // directed
- }
- public void delEdge(int i, int j) {
- adjMatrix[i][j] = false;
- // adjMatrix[j][i] = false; // directed
- }
- public boolean isEdge(int i, int j) {
- return adjMatrix[i][j];
- }
- public int first(int v1){
- boolean[] ary = this.adjMatrix[v1];
- int firstNeighborIndex = this.numVertices; // return n if none
- for (int i = 0; i < ary.length; i++) {
- if (ary[i] != false){
- firstNeighborIndex = i;
- break;
- }
- }
- return firstNeighborIndex;
- }
- public int next(int v1, int v2){
- boolean[] ary = this.adjMatrix[v1];
- int nextNeighborIndex = this.numVertices; // return n if none
- for (int i = v2 + 1; i < ary.length; i++) {
- if (ary[i] != false){
- nextNeighborIndex = i;
- break;
- }
- }
- return nextNeighborIndex; // return n if none
- }
- // we are processing an "directed" graph here
- public void topsort_BFS(){
- int[] prerequisiteCount = new int[this.numVertices];
- Queue<Integer> queue = new LinkedList<>();
- // add prerequisites - process every edge
- for (int v = 0; v < this.numVertices; v++) {
- for (int nextV = this.first(v); nextV < this.numVertices; nextV = this.next(v, nextV)) {
- prerequisiteCount[nextV]++;
- }
- }
- // add vertices that has no prerequisites now
- for (int v = 0; v < this.numVertices; v++) {
- if (prerequisiteCount[v] == 0) {
- queue.add(v);
- }
- }
- while(queue.peek() != null){
- Integer currV = queue.remove();
- Previsit(adjMatrix, currV);
- for (int nextV = this.first(currV); nextV < this.numVertices; nextV = this.next(currV, nextV)) {
- prerequisiteCount[nextV]--; // becuase the current vertes v is being proccessed/taken already
- if (prerequisiteCount[nextV] == 0) {
- queue.add(nextV); // it means no prerequisite left, so we are good to go with this course
- }
- }
- Postvisit(adjMatrix, currV);
- }
- }
- private void Previsit(boolean[][] graph, int v) {
- System.out.println("Previsit: " + v);
- }
- private void Postvisit(boolean[][] graph, int v) {
- System.out.println("Postvisit: " + v);
- }
- public String toString() {
- StringBuilder s = new StringBuilder();
- for (int i = 0; i < numVertices; i++) {
- s.append(i + ": ");
- for (boolean j : adjMatrix[i]) {
- s.append((j?1:0) + " ");
- }
- s.append("\n");
- }
- return s.toString();
- }
- public static void main(String[] args) {
- GraphAdjacentMatrix_TopologicalSort_BFS gam = new GraphAdjacentMatrix_TopologicalSort_BFS(8);
- // directed graph
- gam.setEdge(1, 2);
- gam.setEdge(1, 3);
- gam.setEdge(2, 6);
- gam.setEdge(2, 5);
- gam.setEdge(2, 4);
- gam.setEdge(3, 4);
- gam.setEdge(4, 5);
- gam.setEdge(5, 7);
- gam.topsort_BFS();
- System.out.println();
- }
- }
Add Comment
Please, Sign In to add comment