Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- public class GraphAdjacentMatrix_DFS_BFS {
- private boolean adjMatrix[][];
- private int numVertices;
- private boolean visited[];
- public GraphAdjacentMatrix_DFS_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;
- }
- public void delEdge(int i, int j) {
- adjMatrix[i][j] = false;
- adjMatrix[j][i] = false;
- }
- 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
- }
- public void DFS(int v){
- DFS(this.adjMatrix, v);
- }
- public void DFS(boolean[][] graph, int v){
- Previsit(graph, v);
- // mark the current node
- visited[v] = true;
- // find neighbor
- for (int nextV = this.first(v); nextV < this.numVertices; nextV = this.next(v, nextV)) {
- if (!visited[nextV]){
- DFS(graph, nextV);
- }
- }
- Postvisit(graph, v);
- }
- public void BFS(int v){
- BFS(this.adjMatrix, v);
- }
- public void BFS(boolean[][] graph, int v){
- Queue<Integer> queue = new LinkedList<>();
- visited[v] = true;
- queue.add(v);
- while(queue.peek() != null){
- Integer currV = queue.remove();
- Previsit(graph, currV);
- for (int nextV = this.first(currV); nextV < this.numVertices; nextV = this.next(currV, nextV)) {
- if (!visited[nextV]) {
- visited[nextV] = true;
- queue.add(nextV);
- }
- }
- Postvisit(graph, 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_DFS_BFS gam = new GraphAdjacentMatrix_DFS_BFS(7);
- gam.setEdge(1, 3);
- gam.setEdge(3, 1);
- gam.setEdge(1, 5);
- gam.setEdge(5, 1);
- gam.setEdge(2, 3);
- gam.setEdge(3, 2);
- gam.setEdge(3, 4);
- gam.setEdge(4, 3);
- gam.setEdge(3, 6);
- gam.setEdge(6, 3);
- gam.setEdge(2, 6);
- gam.setEdge(6, 2);
- gam.setEdge(4, 6);
- gam.setEdge(6, 4);
- gam.setEdge(5, 6);
- gam.setEdge(6, 5);
- // System.out.println("DFS: ");
- // gam.DFS( 1);
- System.out.println("BFS: ");
- gam.BFS(1);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement