#include<iostream>
#include <list>
#include <limits.h>
using namespace std;
// Defines class Graph for undirected graph
class Graph
{
// To store number of vertices
int VER;
// Declares a pointer for an array to store adjacency lists
list<int> *adjacent;
bool DFSSearch(int v, bool visited[], int parent);
public:
// Prototype of member functions
Graph(int);
void addingEdges(int, int);
bool DFS();
};// End of class
// Parameterized constructor definition
Graph::Graph(int V)
{
// Assigns number of vertices
VER = V;
// Dynamically creates a list of size V
adjacent = new list<int>[VER];
}// End of parameterized constructor
// Function to add edge
void Graph::addingEdges(int ver, int wait)
{
// Add wait to ver’s list
adjacent[ver].push_back(wait);
// Add ver to waits’s list.
adjacent[wait].push_back(ver);
}// End of function
// Recursive function that uses visited[] and parent to detect
cycle in subgraph reachable from vertex ver.
bool Graph::DFSSearch(int ver, bool visited[], int parent)
{
// Mark the current node as visited
visited[ver] = true;
// Creates an iterator for the list
list<int>::iterator it;
for (it = adjacent[ver].begin(); it != adjacent[ver].end();
++it)
{
// Checks if an adjacent is not visited, then recur for that
adjacent
if (!visited[*it])
{
// Recur for all the vertices adjacent to this vertex
// Checks if the return value of the function is true then return
true
if (DFSSearch(*it, visited, ver))
return true;
}// End of if condition
// Otherwise checks if an adjacent is visited and not parent of
current vertex, then there is a cycle.
else if (*it != parent)
return true;
}// End of for loop
// Otherwise returns false
return false;
}// End of function
// Function to return true if the graph contains a cycle,
otherwise returns false
bool Graph::DFS()
{
// Mark all the vertices as not visited and not part of recursion
stack
bool *visited = new bool[VER];
// Initializes the visited array to false initially
// Loops till number of vertices
for (int c = 0; c < VER; c++)
// Assigns false to c index position of array visited
visited[c] = false;
// Call the recursive helper function to detect cycle in
different DFS trees
for (int t = 0; t < VER; t++)
// Checks if not visited
// Don't recur for t if it is already visited
if (!visited[t])
// Recursively calls the function
// If return value of the function is true then return true
if (DFSSearch(t, visited, -1))
return true;
// Otherwise returns false
return false;
}// End of function
// main function definition
int main()
{
// Creates a graph with 6 vertices
Graph graph(6);
// Calls the function to add edges
graph.addingEdges(0, 1);
graph.addingEdges(0, 2);
graph.addingEdges(0, 3);
graph.addingEdges(1, 4);
graph.addingEdges(1, 2);
graph.addingEdges(1, 0);
graph.addingEdges(2, 0);
graph.addingEdges(2, 1);
graph.addingEdges(2, 3);
graph.addingEdges(2, 5);
graph.addingEdges(3, 2);
graph.addingEdges(4, 1);
graph.addingEdges(4, 5);
graph.addingEdges(4, 4);
graph.addingEdges(4, 2);
// Calls the function to check cycle is present or not
graph.DFS()? cout << "Graph contains cycle\n" : cout <<
"Graph does not contain cycle\n";
return 0;
}// End of main function
Sample Output:
Graph contains cycle
6) Explain how a cycle is found using Depth First Search in the followidg graph. Must...
answer all and asap please Solve the following recurrence relation in terms of m f(n) 5f(n-1) - 6f(n-2); n> 1 f(1) 1 5) f0)0 6) Explain how the two cycle are found in the graph illustrated below, using Depth First Search (DFS (int v, the graph as a linked-list. int p) using the where p is the parent node of the vertex v. Represent Solve the following recurrence relation in terms of m f(n) 5f(n-1) - 6f(n-2); n> 1 f(1)...
Show how depth-first search works on the graph of Figure 22.6. Assume that the for loop of lines 5–7 of the DFS procedure considers the vertices in reverse alphabetical order, and assume that each adjacency list is ordered alphabetically. Show the discovery and finishing times for each vertex, and show the classification of each edge. DIJKSTRA(G,w,s) 1INITIALIZE-SINGLE-SOURCE(G,s) 2 S ?? 3 Q ? V[G] 4 while Q =? 5 do u ? EXTRACT-MIN(Q) 6 S ? S?{u} 7 for each...
Show the operation of depth-first search (DFS) on the graph of Figure 1 starting from vertex q. Always process vertices in alphabetical order. Show the discovery and finish times for each vertex, and the classification of each edge. (b) A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first search (BFS) tree can also be used to classify the edges reachable from the source of the search into the same four categories....
7.[6] Consider the graph G below: a.[3] Find a Depth-First Search tree T for the above graph starting with the vertex 0. Show all the vertices as they are discovered in sequence starting from 1 to the last vertex included in T. b.[3] Find a Breadth-First Search tree T for the above graph starting with the vertex 0. Show all the vertices as they are discovered in sequence starting from 1 to the last vertex included in T.
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...
From the given graph discover the structure of the graph using 1. breadth first search(BFS) a. depth first search(DFS) b. Show the steps and techniques used for each method (20 points) From the given graph discover the structure of the graph using 1. breadth first search(BFS) a. depth first search(DFS) b. Show the steps and techniques used for each method (20 points)
Find the spanning tree using Breath and Depth first search. Show work Using breath-first search, find a spanning tree for the graph below Using depth-first search, find a spanning tree for the graph below.
please help I will upvote. pts) Show how depth-first search works on the following graph. Assume hat the that the DES procedure considers vertices in alphabetical order. Assume also that eachi adjateu ordered alphabetically. Show the discovery and inishing times tor the classification of each edge for each vertex, and show pts) Show how depth-first search works on the following graph. Assume hat the that the DES procedure considers vertices in alphabetical order. Assume also that eachi adjateu ordered alphabetically....
Programming Traversal Methods in C++ (depth first & breadth first) Need solution ASAP any help is much appreciated. 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 -1 This represents a vertex 16 with neighbors 3, 15, and 4. The -1 is the indicator that...