// Java program to represent a graph as adjacency matrix and traverse the graph using DFS and BFS using start node as 0
import java.util.LinkedList;
public class Graph {
private int n;
private int graph[][];
// constructor
public Graph(int n)
{
this.n = n;
graph = new int[n][];
for(int i=0;i<n;i++)
graph[i] = new int[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
graph[i][j] = 0;
}
// method to add an edge from ith node to jth node
public void addEgde(int i, int j)
{
graph[i][j] =1;
}
// method to display the adjacency matrix
public void display()
{
for(int i=0;i<graph.length;i++)
{
for(int j=0;j<graph[i].length;j++)
System.out.print(graph[i][j]+" ");
System.out.println();
}
}
// method to traverse the graph using DFS from starting from the vertex
public void DFS(int vertex)
{
boolean visited[] = new boolean[n];
DFS(vertex,visited);
}
//helper method to implement DFS traversal
private void DFS(int src,boolean visited[])
{
visited[src]=true;
System.out.print(src+" ");
for(int i=0;i<graph[src].length;i++)
{
if((graph[src][i] == 1) && (!visited[i]))
DFS(i,visited);
}
}
// method to traverse the graph using BFS from starting from the vertex
public void BFS(int vertex)
{
boolean visited[] = new boolean[n];
LinkedList<Integer> queue = new LinkedList<Integer>();
visited[vertex]=true;
queue.addLast(vertex);
while(!queue.isEmpty())
{
int node = queue.poll();
System.out.print(node+" ");
for(int i=0;i<graph[node].length;i++)
{
if((graph[node][i] == 1) && (!visited[i]))
{
visited[i] = true;
queue.addLast(i);
}
}
}
}
public static void main(String[] args) {
Graph g = new Graph(10); // create the graph object
// add edges to graph
g.addEgde(0, 1);
g.addEgde(1, 0);
g.addEgde(0, 6);
g.addEgde(6, 0);
g.addEgde(1, 4);
g.addEgde(4, 1);
g.addEgde(1, 7);
g.addEgde(7, 1);
g.addEgde(2, 5);
g.addEgde(5, 2);
g.addEgde(3, 7);
g.addEgde(7, 3);
g.addEgde(5, 9);
g.addEgde(9, 5);
g.addEgde(6, 7);
g.addEgde(7, 6);
g.addEgde(7, 9);
g.addEgde(9, 7);
g.addEgde(7, 8);
g.addEgde(8, 7);
// print the adjacency matrix
System.out.println("Adjacency matrix : ");
g.display();
// dfs traversal from vertex 0
System.out.println("\nDFS from vertex 0: ");
g.DFS(0);
// bfs traversal from vertex 0
System.out.println("\nBFS from vertex 0 : ");
g.BFS(0);
}
}
//end of program
Output:
LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents th...
1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using class) to traverse the graph. b. Similarly, implement BFS (define queue using class) to traverse the graph c.When node 6 is printed, what numbers are in the stack (for DFS) and queue (for BFS) respectively? Draw pictures to show them. 1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using...
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...
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...
The following is an adjacency matrix of a directed graph. Start from vertex D, write down the order of node visited in Breadth-First- Search (BFS) traversal. (Enter the nodes in order in the following format: [A B C D E F G]) Adjacenc y Matrix ABCDEFG A 1111 000 BO00 0101 C0111010 DO 0 1 0 0 1 1 E 0 1 0 1 000 F 100 1 100 G0000100
refer to the question using c++. if you could not do the bonus part no problem you don't have too , but if you can so please do it and let me know Create an unweighted undirected Graph Class using an Adjacency Matrix with the following functions 1. Graph(int numofV) 3. int noOfOutgoingEdges(int vertex); 4. int noOflncomingEdges (int vertex) 5. void print) You may use vectors/2D dynamic arrays to implement the matrix. Bonus (20) 6. void DFS(); Depth First Search...
4&5 0 1 2 3 1. Draw the undirected graph that corresponds to this adjacency matrix 0 0 1 1 0 1 1 1 1 0 1 1 1 2 1 1 1 0 1 3 1 0 1 1 0 1 2. Given the following directed graph, how would you represent it with an adjacency list? 3. We've seen two ways to store graphs - adjacency matrices, and adjacency lists. For a directed graph like the one shown above,...
1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...
/* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on the graph */ #include <iostream> #include <sstream> #include <fstream> #include <vector> #include <utility> #include <unordered_map> #include <set> #include <queue> using namespace std; // Each vertex has an integer id. typedef vector<vector<pair<int,int>>> adjlist; // Pair: (head vertex, edge weight) adjlist makeGraph(ifstream& ifs); void printGraph(const adjlist& alist); vector<int> BFS(const adjlist& alist, int source); // Return vertices in BFS order vector<int> DFS(const adjlist& alist, int source); //...
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...
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...