Untitled

 avatar
user_0616194
plain_text
a year ago
3.2 kB
3
Indexable
Never
import java.util.LinkedList;
import java.util.Queue;

public class Graph {
    private int[][] adjacencyMatrix; // 2D array to represent the graph's adjacency matrix
    private int numVertices; // number of vertices in the graph

    public Graph(int numVertices) {
        this.numVertices = numVertices;
        adjacencyMatrix = new int[numVertices][numVertices]; // initialize the adjacency matrix with 0 values
    }

    public void addEdge(int src, int dest) {
        adjacencyMatrix[src][dest] = 1; // add directed edge from src to dest by setting the corresponding value to 1
    }

    public void BFS(int startVertex) {
        boolean[] visited = new boolean[numVertices]; // array to keep track of visited vertices
        Queue<Integer> queue = new LinkedList<Integer>(); // queue to store vertices to be visited
        visited[startVertex] = true; // mark the starting vertex as visited
        queue.add(startVertex); // add the starting vertex to the queue

        while (!queue.isEmpty()) { // loop until all vertices have been visited
            int node = queue.poll(); // get the next vertex from the queue
            System.out.print(node + " -> "); // output the vertex being visited

            for (int i = 0; i < numVertices; i++) { // loop through all vertices
                if (adjacencyMatrix[node][i] == 1 && !visited[i]) { // if there is a directed edge from node to i and i has not been visited
                    visited[i] = true; // mark i as visited
                    queue.add(i); // add i to the queue to be visited later
                }
            }
        }
        System.out.println("Done"); // output message indicating that the BFS is complete
    }

    public void printMatrix() {
        System.out.println("Adjacency matrix representation:");
        // Output the column labels
        System.out.print("   ");
        for (int i = 0; i < numVertices; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
        // Output the rows of the matrix
        for (int i = 0; i < numVertices; i++) {
            // Output the row label (the vertex number)
            System.out.print(i + ": ");
            // Output the values in the row
            for (int j = 0; j < numVertices; j++) {
                // Output the value of the adjacency matrix for the current row and column
                System.out.print(adjacencyMatrix[i][j] + " ");
            }
        // Move to the next line after outputting the entire row
        System.out.println();
        }
    }


    public static void main(String[] args) {
        Graph graph = new Graph(6); // create a graph with 6 vertices
        graph.addEdge(0, 1); // add directed edges
        graph.addEdge(0, 2);
        graph.addEdge(1, 2);
        graph.addEdge(2, 0);
        graph.addEdge(2, 3);
        graph.addEdge(3, 3);
        graph.addEdge(4, 5);

        graph.printMatrix(); // output the adjacency matrix

        System.out.print("BFS starting from vertex 2: ");
        graph.BFS(2); // run BFS starting from vertex 2
    }
}