Question

1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using stack (define it using clas
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

template <class T>
class queue{
   private:
       T *arr;
       int front,rear,capacity;
      
   public:
       queue(int size){
           capacity=size;
           front=-1;
           rear =-1;
           arr = new T[capacity];
          
       }
       bool empty(){
           return front==-1;
       }
       bool full(){
           return (rear+1)%capacity==front;
       }
       void enqueue(T data){
           rear++;
       rear%=capacity;
       arr[rear]=data;
       if(front==-1)front=rear;
       }
       T frontElement(){
           return arr[front];
       }
       void dequeue(){
       if(front==rear)
           front=rear=-1;
       else{
       front++;
       front%=capacity;}
       }
};

class graph{
   private:
   int **arr;
   int v;
  
public:
   graph(int v){
       this->v=v;
      
       arr=new int* [this->v];
       for(int i=0;i<this->v;i++)
       this->arr[i]=new int[this->v];
       for(int i=0;i<this->v;i++){
           for(int j=0;j<this->v;j++)
           this->arr[i][j]=0;
       }
   }
   bool addEdge(int fromVertex , int toVertex ){
       if(fromVertex<0||fromVertex>=v||toVertex<0||toVertex>=v)return false;
       this->arr[fromVertex][toVertex]=1;
       this->arr[toVertex][fromVertex]=1;
       return true;
   }
  
   void RecDFS(int s, bool visited[])
   {
  
visited[s] = true;
cout << s << " ";

for (int i = 0; i <v; ++i)
if (!visited[i]&&arr[s][i]==1)
RecDFS(i, visited);
           }

void DFS(int source)
{
  
bool *visited = new bool[v];
for (int i = 0; i < v; i++)
visited[i] = false;

RecDFS(source, visited);
   }
  
   void BFS(int source)
   {
  
bool *visited = new bool[v];
for(int i = 0; i < v; i++)
visited[i] = false;

queue<int> q(v);
visited[source] = true;
q.enqueue(source) ;
  
while(!q.empty())
{
source = q.frontElement();
cout << source << " ";
q.dequeue();
for (int i = 0; i <v; ++i)
{
if (!visited[i]&&arr[source][i]==1)
{
visited[i] = true;
q.enqueue(i);
}
}
}
}
  
};

int main(){
   graph g(10);
   g.addEdge(0,1);
   g.addEdge(0,2);
   g.addEdge(0,3);
   g.addEdge(1,5);
   g.addEdge(2,4);
   g.addEdge(3,5);
   g.addEdge(3,4);
   g.addEdge(3,8);
   g.addEdge(4,5);
   g.addEdge(6,7);
   g.addEdge(6,8);
   g.addEdge(7,9);
   g.addEdge(8,9);
  
   cout<<"DFS\n";
   g.DFS(0);
   cout<<"\n\nBFS\n";
   g.BFS(0);
}

//sample output

C:Users IshuManish\Documents dfsbfs.exe DFS 1 5 3 4286 ? 9 FS 01 23 5 4 8 69 ? Process exited after 0.07008 seconds with retu

Add a comment
Know the answer?
Add Answer to:
1. a. Using C++, represent the following graph using adjacency matrix, and implement DFS by using...
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
  • 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...

  • LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents th...

    JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...

  • /* Graph read from file, and represnted as adjacency list. To implement DFS and BFS on...

    /* 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); //...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

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

  • USING JAVA: Create a class: AdjListGraph.java, and just submit AdjListGraph.java. where -Step 1(1 credit): Please implement...

    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)

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

  • 4&5 0 1 2 3 1. Draw the undirected graph that corresponds to this adjacency matrix...

    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. Use the following graph for the questions. Show all the steps (a) Draw the adjacency...

    1. Use the following graph for the questions. Show all the steps (a) Draw the adjacency matrix and the adjacency list (b) Using the Depth First Search algorithm learned in class, topologically sort the graph. 4 t5 64 (c) Use Dijstra's algorithm to determine the shortest path from node s to all other nodes. (d) Use Bellman-Ford's algorithm to determine the shortest path from node s to all other nodes.

  • Give the adjacency matrix representation and the adjacency lists representation for the graph G_1. Assume that...

    Give the adjacency matrix representation and the adjacency lists representation for the graph G_1. Assume that vertices (e.g., in adjacency lists) are ordered alphabetically. For the following problems, assume that vertices are ordered alphabetically in the adjacency lists (thus you will visit adjacent vertices in alphabetical order). Execute a Breadth-First Search on the graph G_1, starting on vertex a. Specifiy the visit times for each node of the graph. Execute a Depth-First Search on the graph G_1 starting on vertex...

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