Question

C++ //FInd shortest path of any given vertices using bfs graph algorithm.you can assume between any...

C++ //FInd shortest path of any given vertices using bfs graph algorithm.you can assume between any two vertix and make code from there

int adjmat[N][N] = { {0,1,0,0,1,0,0,0},
{1,0,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0},
{0,1,0,0,0,1,0,1},
{1,0,1,0,0,0,1,0},
{0,0,0,1,0,0,0,0},
{0,0,1,0,1,0,0,0},
{0,0,0,1,0,0,0,0},

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

        #include <iostream>

    #include <list>

    using namespace std;

    class Graph

    {

            int V; // No. of vertices

            list<int> *adj;

        public:

            Graph(int V);

            void addEdge(int v, int w);

            bool isReachable(int s, int d);

    };

   

    Graph::Graph(int V)

    {

        this->V = V;

        adj = new list<int> [V];

    }

   

    void Graph::addEdge(int v, int w)

    {

        adj[v].push_back(w);

    }

    bool Graph::isReachable(int s, int d)

    {

        if (s == d)

            return true;

       bool *visited = new bool[V];

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

            visited[i] = false;

        list<int> queue;

        visited[s] = true;

        queue.push_back(s);

        list<int>::iterator i;

   while (!queue.empty())

        {

            s = queue.front();

            queue.pop_front()

            for (i = adj[s].begin(); i != adj[s].end(); ++i)

            {

                if (*i == d)

                    return true;

                if (!visited[*i])

                {

                    visited[*i] = true;

                    queue.push_back(*i);

                }

            }

        }

   

        return false;

}

    int main()

    {

       

        Graph g(4);

        g.addEdge(0, 1);

        g.addEdge(0, 2);

        g.addEdge(1, 2);

        g.addEdge(2, 0);

        g.addEdge(2, 3);

        g.addEdge(3, 3);

   

        cout << "Enter the source and destination vertices: (0-3)";

        int u, v;

        cin >> u >> v;

        if (g.isReachable(u, v))

            cout << "\nThere is a path from " << u << " to " << v;

        else

            cout << "\nThere is no path from " << u << " to " << v;

   

        int temp;

        temp = u;

        u = v;

        v = temp;

        if (g.isReachable(u, v))

            cout << "\nThere is a path from " << u << " to " << v;

        else

            cout << "\nThere is no path from " << u << " to " << v;

   

        return 0;

    }

Add a comment
Know the answer?
Add Answer to:
C++ //FInd shortest path of any given vertices using bfs graph algorithm.you can assume between any...
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
  • BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source ver...

    Solve (a) and (b) using BFS and DFS diagram BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source vertex H and predecessor tree on the graph that result from running breadth-finst search (BFS).Choose adjacen vertices in al phabetical order b) Show the start and finsh time for each vertex, starting from source vertex H, that result from running depth-first search (DFS)Choose aljacent vertices in alphabet- ical order DFS BFS Given an undirected graph...

  • Find the shortest path P: s P by heavier lines. t and its length by Moore's BFS algorithm: sketch the graph with the labels and indicate Find the shortest path P: s P by heavier lines. t and...

    Find the shortest path P: s P by heavier lines. t and its length by Moore's BFS algorithm: sketch the graph with the labels and indicate Find the shortest path P: s P by heavier lines. t and its length by Moore's BFS algorithm: sketch the graph with the labels and indicate

  • BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source ver...

    BFS Given an undirected graph below (a) Show the shortest distance to each vertex from source vertex H and predecessor tree on the graph that result from running breadth-finst search (BFS).Choose adjacen vertices in al phabetical order b) Show the start and finsh time for each vertex, starting from source vertex H, that result from running depth-first search (DFS)Choose aljacent vertices in alphabet- ical order DFS BFS Given an undirected graph below (a) Show the shortest distance to each vertex...

  • IN C++ Write a BFS path or DFS when it is given a string array of...

    IN C++ Write a BFS path or DFS when it is given a string array of vertices "towns": string towns[]={"Seattle","Lynnwood","Edmonds","Shoreline","Everet","Kenmore","Kirkland"}; and int array of edges: int roads [][2]= {0,1},{0,3}, {1,0},{1,5},   {2,4}, {3,0},{3,5},   {4,2}, {5,1},{5,3},{5,6},   {6,5}; Do not create any graph struct,class or egde struct. You only need to create a BFS or DFS path function that accepts parameters the array of strings, the number of verteces,the array of edges and the nuber of edges. for example you call in main...

  • write a program that will find the shortest path from a given vertex to a given...

    write a program that will find the shortest path from a given vertex to a given vertex. Your program must allow the user to enter any start vertex and any destination vertex and output the shortest path. A B F E D Using the attached graph, write a program that will find the shortest path from a given vertex to a given vertex. Your program must allow the user to enter any start vertex and any destination vertex and output...

  • Using Java Code. Thankyou. The file has 1 edge per line in the format (origin,destination) - Ass...

    Using Java Code. Thankyou. The file has 1 edge per line in the format (origin,destination) - Assume the graph is not directional - (2,5) means there is a path from 2 to 5 and from 5-2 For the Lab write the following code. 1) Write a function to find all adjacent vertices to a node N ( find_adjacent(n)) 2) Write a function that will return true or false that an edge exists ( edge_exists(origin,destination)) 3) Print out all Vertices using...

  • Is Dijkstra’salgorithm guaranteed to find the shortest path between any two nodes in any connected, weighted...

    Is Dijkstra’salgorithm guaranteed to find the shortest path between any two nodes in any connected, weighted and undirected graph? Explain why/why not.

  • Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An...

    Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An adjacency matrix using a one-dimensional array for storing only the elements of the lower triangle in the adjacency matrix.[Program in C language] The input to program must be connected, undirected, and weighted graphs. The programs must be able to find shortest paths on two types of connected, undirected, and weighted graphs: complete graph (a graph with a link between every pair of nodes) and...

  • Bipartite graph is a graph, which vertices can be partitioned into 2 parts - so that...

    Bipartite graph is a graph, which vertices can be partitioned into 2 parts - so that all edges connect only vertices from different parts. For example, this is a bipartite graph where one part has 3 vertices (a,b,c), and the other part - 4 vertices (d.e.f.g). Note there are NO edges in-between vertices coming from the same part. a b d f e g Give the order in which nodes are traversed with BFS. After listing a node, add its...

  • Implement Dijkstra's algorithm to find the shortest path from vertex O to all other vertices in...

    Implement Dijkstra's algorithm to find the shortest path from vertex O to all other vertices in the graph below. Use the adjacency list representation to store and use the graph in memory. Do not use any other representation Use vertex 'A' as your source vertex (begin the algorithm from A). Your output should be of the following format with the second column filled out. The distance from the source vertex (second column) is the sum of weights on the shortest...

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