Question

Implement Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph in Java....

Implement Depth-First Search (DFS) and Breadth-First Search
(BFS) algorithms for a graph in Java.(Can be any graph, just an example of DFS and BFS is sufficient)

If it cannot be done for a graph, then just an example of DFS and BFS are enough.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Program to print Breadth First Search and Depth First Search for a given grapgh vertex
import java.io.*;
import java.util.*;

// This class represents a graph
class Graph
{
    private int V;   // No. of vertices
    private LinkedList<Integer> adj[]; //Adjacency Lists

    // Constructor to initialize a grapgh
    Graph(int v)
    {
        V = v;
        adj = new LinkedList[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList();
    }

    // Function to add edge into the graph
    void addEdge(int v,int w)
    {
        adj[v].add(w);
    }

    // Algorith to do breath first search for a provided vertex s
    void BFS(int s)
    {
        // By default mark all vertices as false
        boolean visited[] = new boolean[V];

        // Create a queue for BFS
        LinkedList<Integer> queue = new LinkedList<Integer>();

        // Mark the current node as visited and enqueue it
        visited[s]=true;
        queue.add(s);

        while (queue.size() != 0)
        {
            // Dequeue a vertex from queue and print it
            s = queue.poll();
            System.out.print(s+" ");

            // Get all adjacent vertices of the dequeued vertex s
            // If a adjacent has not been visited, then mark it
            // visited and enqueue it
            Iterator<Integer> i = adj[s].listIterator();
            while (i.hasNext())
            {
                int n = i.next();
                if (!visited[n])
                {
                    visited[n] = true;
                    queue.add(n);
                }
            }
        }
    }
  
   // Function used by depth first search
    void DFSInner(int v,boolean visited[])
    {
        // Mark the current node as visited and print it
        visited[v] = true;
        System.out.print(v+" ");

        // Recur for all the vertices adjacent to this vertex
        Iterator<Integer> i = adj[v].listIterator();
        while (i.hasNext())
        {
            int n = i.next();
            if (!visited[n])
                DFSInner(n, visited);
        }
    }

    // Algorith to do depth first search for a provided vertex s
    void DFS(int v)
    {
          // By default mark all vertices as false
        boolean visited[] = new boolean[V];

        // Call the recursive helper function to print depth frst search traversal
        DFSInner(v, visited);
    }

    // Invoke the search algorhtms
    public static void main(String args[])
    {
        Graph g = new Graph(4);

        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(1, 2);
        g.addEdge(2, 0);
        g.addEdge(2, 3);
        g.addEdge(3, 3);

        System.out.println("Breadth First Traversal (starting from vertex 2)");
        g.BFS(2);
      
       System.out.println("------------------------------------------------------------------");
       System.out.println("Depth First Traversal (starting from vertex 2)");
        g.DFS(2);
      
    }
}


Output:
Following is Breadth First Traversal (starting from vertex 2)
2 0 3 1
------------------------------------------------------------------
Following is Depth First Traversal (starting from vertex 2)
2 0 1 3

Add a comment
Know the answer?
Add Answer to:
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph in Java....
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