Question

Create an unweighted undirected Graph Class using an Adjacency Matrix with the following functions 1. Graph(int numofV) 3. in

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CSC 430              GRAPH PROJECT Implement a graph class using an adjacency matrix or an adjacency list....

    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...

    #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...

    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...

    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...

  • You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a...

    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, ...

    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...

    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...

    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(),...

    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 =...

  • Creating a simple graph in C++; need solution ASAP. EDIT: Pls comment letting me know what...

    Creating a simple graph in C++; need solution ASAP. EDIT: Pls comment letting me know what other information you need rather than just "..." Thank you. Here is the assignment: In this final practice problem, you’ll: read a set of data representing a directed, unweighted graph build an in-memory graph structure using the data display the graph using depth-first traversal display the graph using breadth-first traversal Input data - The data consists of records like this: 16 3 15 4...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT