use DFS to check if a graph is connected. in java and add comment please
java program to check if a graph is connected using the DFS
Source code:
import java.util.Scanner;
import java.util.Stack;
public class Connectivity_DFS
{
private final int vertices;
private int[][] adjacency_matrix;
private Stack<Integer> stack;
public Connectivity_DFS(int v)
{
vertices = v;
adjacency_matrix = new int[vertices + 1][vertices + 1];
stack = new Stack<Integer>();
}
public void makeEdge(int to, int from, int edge)
{
try
{
adjacency_matrix[to][from] = edge;
adjacency_matrix[from][to] = edge;
} catch (ArrayIndexOutOfBoundsException index)
{
System.out.println("The vertices does not exists");
}
}
public int getEdge(int to, int from)
{
try
{
return adjacency_matrix[to][from];
} catch (ArrayIndexOutOfBoundsException index)
{
System.out.println("The vertices does not exists");
}
return -1;
}
public void dfs(int source)
{
int number_of_nodes = adjacency_matrix[source].length - 1;
int[] visited = new int[number_of_nodes + 1];
int i, element;
visited[source] = 1;
stack.push(source);
while (!stack.isEmpty())
{
element = stack.pop();
i = 1;// element;
while (i <= number_of_nodes)
{
if (adjacency_matrix[element][i] == 1 && visited[i] ==
0)
{
stack.push(i);
visited[i] = 1;
}
i++;
}
}
System.out.print("The source node " + source + " is connected to:
");
int count = 0;
for (int v = 1; v <= number_of_nodes; v++)
if (visited[v] == 1)
{
System.out.print(v + " ");
count++;
}
if (count == number_of_nodes)
System.out.print("\nThe Graph is Connected ");
else
System.out.print("\nThe Graph is Disconnected ");
}
public static void main(String args[])
{
int v, e, count = 1, to = 0, from = 0;
Scanner sc = new Scanner(System.in);
Connectivity_DFS graph;
System.out.println("The Undirected Graph Connectivity Test");
try
{
System.out.println("Enter the number of vertices: ");
v = sc.nextInt();
System.out.println("Enter the number of edges: ");
e = sc.nextInt();
graph = new Connectivity_DFS(v);
System.out.println("Enter the edges: <to>
<from>");
while (count <= e)
{
to = sc.nextInt();
from = sc.nextInt();
graph.makeEdge(to, from, 1);
count++;
}
System.out.println("The adjacency matrix for the given graph is:
");
System.out.print(" ");
for (int i = 1; i <= v; i++)
System.out.print(i + " ");
System.out.println();
for (int i = 1; i <= v; i++)
{
System.out.print(i + " ");
for (int j = 1; j <= v; j++)
System.out.print(graph.getEdge(i, j) + " ");
System.out.println();
}
System.out.println("Enter the Source Node: ");
int sourceNode = sc.nextInt();
graph.dfs(sourceNode);
} catch (Exception E)
{
System.out.println("Somthing went wrong");
}
sc.close();
}
}
in this above program we need to check connectivity of nodes in
graph by using DFS . in the DFS nodes that are traversed last which
come first serve.
in this code we create a visited array to avoid the revisiting node.if the destination node appear in visited array source and destination node are connected in the graph.
Output:
use DFS to check if a graph is connected. in java and add comment please
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.
Help with Java Program Please Create a simple graph class. The graph class should have the following items: an adjacency list or matrix to hold the graph data variables to hold the current size and max size of the graph default constructor create empty adjacency list/matrix max size = 10 overloaded constructor create empty adjacency list/matrix max size = int parameter isEmpty check if current size is zero createGraph read a formatted file and fill adjacency list/matrix The first line...
ignore red marks. Thanks
10. (16) You will compute the strongly connected components of this graph in three steps. a. STRONGLY-CONNECTED-COMPONENTS (G) (7) Perform a depth-first search on call DFS(G) to compute finishing times w/ for each vertex the following graph. (To make 2 compute GT this easier to grade, everyone call DFS(GT), but in the main loop of DFS, consider the vertices in order of decreasing wf (as computed in line 1) please start with vertex "a" and 4...
Java programming. Please, could you add the comment so I can follow and understand. Thanks Create a program with JavaFX that displays a bouncing graphics (an image of a ball or the head of Darth Vader or anything else). The object should bounce around in the window. There should also be a button in the win- dow that releases another object (unlimited number should be possible to have in the window, but only one new object should be released for...
Problem 3 (15 points) Consider the graph shown on the right. Find the strongly connected components of the graph. For full credit, a) (6 points) Run DFS on the reverse graph, showing the discovery and finish times of each 10 vertex. b) (6 points) Run DFS again, to discover the strongly connected components. What is the 15 order the components are discovered? 12 c) (3 points) Draw the DAG of the components. What is the minimum number of edges that...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.
USING JAVA: Create a class: AdjListGraph.java, and just submit AdjListGraph.java. where -Step 1(1 credit): Please implement a graph by adjacency list. -Step 2(1 credit): Write a method: void dfs(){\\ TO-DO}; which can traverse a graph by DFS(stack based or recursive)
During the course, we have seen a recursive implementation for the DFS graph visit. There is also a simple equivalent iterative implementation that uses a stack as an auxiliary data structure (it runs just like the BFS algorithm, except that we use the stack in place of the queue). However, there is an algorithm that iteratively implements the DFS visit and does not use any auxiliary data structure. Design this algorithm and implement it.
In Java: We say that a graph G is strongly-connected if, for every pair of vertices i and j in G, there is a path from i to j. Showhowtotest if G is strongly-connected in O(n + m) time. . Write a method and test it in Main. Explain why it is O(n+m). Graph is directed
Use Java if possible please:
Write an algorithm using pseudo code to determine if an undirected graph has any cycle. Analyze the complexity of your algorithm. Write an algorithm using pseudo code to determine if an undirected graph is connected or not. Analyze the complexity of your algorithm. (i) (ii)