
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
CODE
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX 20
typedef struct edge {
int source;
int destination;
} edge;
/*
* Adjacency Matrix Class
*/
class Graph
{
private:
int n;
int **adj;
bool *visited;
public:
Graph(int n)
{
this->n = n;
visited = new bool [n];
adj = new int* [n];
for (int i = 0; i < n; i++)
{
adj[i] = new int [n];
for(int j = 0; j < n; j++)
{
adj[i][j] = 0;
}
}
}
/*
* Adding Edge to Graph
*/
void addedge(edge e)
{
if( e.source > n || e.destination > n || e.source < 0 || e.destination < 0)
{
cout<<"Invalid edge!\n";
}
else
{
adj[e.source - 1][e.destination - 1] = 1;
}
}
/*
* Print the graph
*/
void display()
{
int i,j;
for(i = 0;i < n;i++)
{
for(j = 0; j < n; j++)
cout<<adj[i][j]<<" ";
cout<<endl;
}
}
int numOfOutgoingEdges(int vertex) {
int count = 0;
for (int i=0; i<n; i++) {
if (adj[vertex][i] != 0) {
count ++;
}
}
return count;
}
int numOfIncomingEdges(int vertex) {
int count = 0;
for (int i=0; i<n; i++) {
if (adj[i][vertex] != 0) {
count ++;
}
}
return count;
}
};
/*
* Main
*/
int main()
{
int nodes, max_edges, origin, destin;
cout<<"Enter number of nodes: ";
cin>>nodes;
Graph graph(nodes);
max_edges = nodes * (nodes - 1);
for (int i = 0; i < max_edges; i++)
{
cout<<"Enter edge (-1 -1 to exit): ";
cin>>origin>>destin;
if((origin == -1) && (destin == -1))
break;
edge e;
e.source = origin;
e.destination = destin;
graph.addedge(e);
}
graph.display();
return 0;
}
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 usin...
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...
#include <iostream>
#include <queue>
using namespace std;
class Graph {
public:
Graph(int n);
~Graph();
void addEdge(int src, int tar);
void BFTraversal();
void DFTraversal();
void printVertices();
void printEdges();
private:
int vertexCount;
int edgeCount;
bool** adjMat;
void BFS(int n, bool marked[]);
void DFS(int n, bool marked[]);
};
Graph::Graph(int n=0) {
vertexCount = n;
edgeCount = 0;
if(n == 0)
adjMat = 0;
else {
adjMat = new bool* [n];
for(int i=0; i < n; i++)
adjMat[i] = new bool [n];
for(int i=0;...
3. Graph Connected Components (25 pts) You are given an undirected, unweighted graph that may be disconnected i.e. some vertices may not be reachable from other vertices. Every group of mutually reachable vertices forms an island, called a con- nected component. There is no path between vertices in different connected components. If a graph is not disconnected, then its vertices are in a single connected component, which is the entire graph itself. Implement a method using depth-first search that will...
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...
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...
You will be implementing a Breadth-First Search (BFS) and a
Depth-First Search (DFS) algorithm on a graph stored as an
adjacency list. The AdjacencyList class inherits from the Graph
class shown below. class Graph { private: vector _distances; vector
_previous; public: Graph() { } virtual int vertices() const = 0;
virtual int edges() const = 0; virtual int distance(int) const = 0;
virtual void bfs(int) const = 0; virtual void dfs(int) const = 0;
virtual void display() const = 0;...
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...
In this problem, you are expected to implement Prim's Algorithm on an undirected simple graph. Write a method that is part of a class that implements Graph as an adjacency matrix. This method should generate a minimum spanning tree using Prim's Algorithm, and print out the edge added by the algorithm on each iteration. 3 10 4 8 Output: 1 2 1 3 34 35 5 6 17 3 12 34 5 1 6 8 20 4 Output: 26 65...
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...
I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...