Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java implement the missing methods in DepthFirstSearch.java in order to do a depth first search //An Iterative Java program to do DFS traversal from

In Java implement the missing methods in DepthFirstSearch.java in order to do a depth first search

//An Iterative Java program to do DFS traversal from

//a given source vertex. DFS(Graph graph, int source)

//traverses vertices reachable from source in the graph.

import java.util.*;

public class DepthFirstSearch {

// Class to represent a graph using adjacency list

// and the number of nodes

static class Graph {

int Nodes;

LinkedList adjVertices[];

int[][] adjMatrix;

@SuppressWarnings("unchecked")

public Graph(int n) {

this.Nodes = n;

adjVertices = new LinkedList[Nodes];

adjMatrix = new int[Nodes][Nodes];

for (int i = 0; i < Nodes; i++) {

adjVertices[i] = new LinkedList();

}

}

}

// Add an edge between two given vertices

static void addEdge(Graph graph, int src, int dst) {

graph.adjVertices[src].addFirst(dst);

graph.adjVertices[dst].addFirst(src);

}

// Create the adjacency matrix representation

static void generateMatrix(Graph graph) {

}

// print the adjacency matrix

static void printMatrix(Graph graph) {

}

// Print the graph as adjacency list

static void printGraph(Graph graph) {

}

static void BFS(Graph graph, int s) {

}

// prints all not yet visited vertices reachable from start

static void DFS(Graph graph, int start) {

// Initially mark all vertices as not visited

// Create a stack for DFS

// Push the current source node

// Pop a vertex from stack and print it

// Stack may contain same vertex twice. So

// we need to print the popped item only

// if it is not visited.

// Get all adjacent vertices of the popped vertex s

// If a adjacent has not been visited, then push it

// to the stack.

}

// Driver program to test methods of graph class

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("How many vertices?: ");

int nodeCount = input.nextInt();

Graph graph = null;

if (nodeCount > 0) {

graph = new Graph(nodeCount);

}

System.out

.println("Enter the edges between two nodes(counting from 0), -1 to stop");

while (true) {

int src = input.nextInt();

if (src == -1)

break;

int dest = input.nextInt();

if ((src > nodeCount - 1) || (dest > nodeCount - 1)) {

System.out.println("Invalid vertex.");

} else {

addEdge(graph, src, dest);

}

}

System.out.println("Enter your choices: "

+ "1 print adjacency list " + "2 print adjacency matrix "

+ "3 print breadth first search "

+ "4 print depth first search ");

int choice = input.nextInt();

int source = 0;

switch (choice) {

case 1:

printGraph(graph);

break;

case 2:

generateMatrix(graph);

printMatrix(graph);

break;

case 3:

System.out.println("Enter the beginning vertex:");

source = input.nextInt();

System.out.println("BFS traversal of the graph starting at "

+ source);

BFS(graph, source);

break;

case 4:

System.out.println("Enter the beginning vertex:");

source = input.nextInt();

System.out.println("DFS traversal of the graph starting at "

+ source);

DFS(graph, source);

break;

}

input.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

Students also viewed these Databases questions