Question

-DFS and BFS implementations in the lecture slides ar e based on an input adjacency list of a graph. Modify each of the codes
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Below is dfs code

#include<bits/stdc++.h>

using namespace std;

// Below is dfs implementation of dfs function

void dfs(int **a,bool *vis,int n,int k){

    // Displaying the first element

    cout<<k<<" ";

    // Making that element as visited

    vis[k]=true;

    // Looping through each node and calling dfs function again if it is not visited    

    for(int i=0;i<n;i++){

        if(a[k][i] && !vis[i]){

            dfs(a,vis,n,i);

        }

    }

}

int main(){

    int n,**a;

    cout<<"Enter number of vertices\n";

    cin>>n;

    a=new int* [n];

    for(int i=0;i<n;i++)

        a[i]=new int[n];

    // Below we are taking input of the adjacency matrix

    cout<<"Enter the adjacancy matrix,1 for an edge, 0 for no edge\n";

    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

            cin>>a[i][j];

        }

    }

    cout<<"DFS\n";

    // Below we created a bool type array to keep track of visited node

    bool vis[n];

    // Below we are initializing it to 0.

    memset(vis,false,sizeof(vis));

    // Below we are calling dfs function with input adjacency matrix and other values.

    dfs(a,vis,n,0);

    cout<<endl;

    return 0;

}

output

/////////////////////////////////////////////////////////

Below is the result of bfs ...

#include<bits/stdc++.h>

using namespace std;

// Below is dfs implementation of dfs function

void bfs(int **a,int n){

    // Below we are creating an queue.

    queue<int> q;

    q.push(0);

    bool vis[n];

    memset(vis,false,sizeof(vis));

    // making first element visited

    vis[0]=1;

    while(!q.empty()){

        int f=q.front();

        // printing first element

        cout<<f<<" ";

        for(int i=0;i<n;i++){

            if(a[f][i] && !vis[i]){

                q.push(i);

                vis[i]=true;

            }

        }

        // removing that element from the queue.

        q.pop();

    }

    cout<<endl;

}

int main(){

    int n,**a;

    cout<<"Enter number of vertices\n";

    cin>>n;

    a=new int* [n];

    for(int i=0;i<n;i++)

        a[i]=new int[n];

    // Below we are taking input of the adjacency matrix

    cout<<"Enter the adjacancy matrix,1 for an edge, 0 for no edge\n";

    for(int i=0;i<n;i++){

        for(int j=0;j<n;j++){

            cin>>a[i][j];

        }

    }

    cout<<"BFS\n";

    // Below we are calling bfs function with input adjacency matrix and other values.

    bfs(a,n);

    return 0;

}

// output

Add a comment
Know the answer?
Add Answer to:
-DFS and BFS implementations in the lecture slides ar e based on an input adjacency list...
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
  • /* 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); //...

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

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include ...

    C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include <stdio.h> int main() { int fries; // A flag denoting whether they want fries or not. char bacon; // A character for storing their bacon preference. double cost = 0.0; // The total cost of their meal, initialized to start at 0.0 int choice; // A variable new to version 2, choice is an int that will store the // user's menu choice. It will also serve as our loop control...

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