Help with Java Program Please
Create a simple graph class. The graph class should have the following items:
Write a driver program to test the class.
Test program with the following:
graph1.txt:
8 1 7 6 2 5 6 8 3 5 6 8 4 5 5 2 3 4 6 1 2 3 7 1 8 2 3
graph2.txt:
6 1 2 3 2 1 3 3 1 2 4 6 5 5 4 6 6 4 5
**Thank you in advance!**
Answer:
Output:
1.using graph1.txt:
2.using graph2.txt:
I was used isEmpty in while loop.
Raw code:
import java.util.*;
import java.io.*;
// Creating class
public class Connected_dfs {
// Creating variables
int adjacency_matrix[][];
int max_size;
int cur_size;
Stack<Integer> stack; // Creating stack
// Creating constructors
Connected_dfs(){
max_size = 10;
adjacency_matrix = new
int[10+1][10+1]; //I am Not using 0th position that's why i
increase it to 1
stack = new
Stack<Integer>();
}
// Overloading constructor
Connected_dfs(int size){
max_size = size;
adjacency_matrix = new
int[size+1][size+1];
stack = new
Stack<Integer>();
}
// Creating graph i.e adjacency matrix
void createGraph() throws Exception {
BufferedReader br = new
BufferedReader ( new FileReader("graph1.txt")); // Reading graph
file
int size =
Integer.parseInt(br.readLine());
max_size = size;
String s;
String list[];
cur_size = 0;
while((s = br.readLine())
!=null){
list = s.split("
"); // spliting line based on space
for (int
i=0;i<list.length;i++){
adjacency_matrix[cur_size+1][Integer.parseInt(list[i])] = 1; //
Making path
adjacency_matrix[Integer.parseInt(list[i])][cur_size+1] = 1; //
Making path
}
cur_size++;
}
}
// Checking if a graph is connected or not
void isConnected(int source){
int i, element;
int[] visited_list = new int[
max_size + 1]; // visited list
visited_list[source] = 1;
stack.push(source);
while (!stack.isEmpty())
{
element = stack.pop();
i = 1;
while (i <= max_size)
{
if (adjacency_matrix[element][i] == 1 && visited_list[i] ==
0)
{
stack.push(i);
visited_list[i] = 1;
}
i++;
}
}
int count=0;
System.out.println("visited_Nodes :"); // Printing visited
nodes
for (i = 1; i <= max_size; i++){
if (visited_list[i] == 1)
{
System.out.print(i +"\t");
count++;
}
}
// Checking whether a graph is connected or not
if(count == max_size){
System.out.println("\nGraph is Connected.");
}
else{
System.out.println("\nGraph is not Connected.");
}
}
public static void main(String[] args) throws
Exception{
Connected_dfs graph = new
Connected_dfs(); // crating object of class
// calling functions
graph.createGraph();
graph.isConnected(1); // Passing
source vertex as 1
}
}
Help with Java Program Please Create a simple graph class. The graph class should have the...
Lab 11 Adjacency Matrix Graph Objective: Create a class which constructs an adjacency matrix representation of a graph and performs a few graph operations. Write an Adjacency Matrix Graph class which has the following: Two constructors: Default which makes the matrix of a pre-defined size Parameterized which takes in a non-negative or 0 size and creates an empty matrix addEdge: this method returns nothing and takes in two string parameters and a weight. The two integer parameters correspond to the...
Write a C ++ program to: 1-Create following graph. 2-Traverse and print this graph in Depth First Search. 3-Traverse and print this graph in Breath First Search. 4-.Show the Adjacency Matrix Show the adjacency List. the graph is connected like the following 2 to 4 4 to 5 5 to 7 7 to 6 6 to 2 4 to 3 3 to 7
from collections import defaultdict # This class represents a directed graph using # adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self,u,v): self.graph[u].append(v) # Function to print a BFS of graph def BFS(self, s): # Mark all the vertices as not visited visited = [False] * (len(self.graph)) # Create a queue for BFS queue...
CSC 430 GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list. The class should have a constructor, a copy constructor, a destructor and all the methods necessary to add/delete vertices, add/delete edges… Write a menu-driven program to THOROUGHLY check your class and all the functions included in your program. You can choose the data type. Allow the user to continue with operations as long as he/she wants to. Your program should check if an operation...
Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...
Consider the following directed graph, which is given in adjacency list form and where vertexes have numerical labels: 1: 2, 4, 6 2: 4, 5 3: 1, 2, 6, 9 4: 5 5: 4, 7 6: 1, 5, 7 7: 3, 5 8: 2, 6, 7 9: 1, 7 The first line indicates that the graph contains a directed edge from vertex 1 to vertex 2, from 1 to vertex 4, and 1 to 6, and likewise for subsequent lines....
Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...
How would I traverse through this graph? Provide example code, please! class Edge { int src, dest; Edge(int src, int dest) { this.src = src; this.dest = dest; } }; // class to represent a graph object class Graph { // A list of lists to represent adjacency list List<List<Integer>> adj = new ArrayList<>(); // Constructor to construct graph public Graph(List<Edge> edges) { // allocate memory for adjacency list for (int i = 0; i < edges.size(); i++) { adj.add(i,...
Hello, I'd like someone to help me create these, thanks! 1. Type Vertex Create and document type Vertex. Each vertex v has the following pieces of information. A pointer to a linked list of edges listing all edges that are incident on v. This list is called an adjacency list. A real number indicating v's shortest distance from the start vertex. This number is −1 if the distance is not yet known. A vertex number u. The shortest path from...
Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...