Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

EXPERIMENT -20

Aim-Program to traverse graphs using DFS.


Hardware and Software Requirement- CPU , C++ , Dev C++ , Screen, Mouse,
Theory
A graph is a pair (V, E), where V is a set of nodes, called vertices and E is a collection of pairs of
vertices, called edges. Vertices and edges are positions and store elements. Removal of an edge
from a circuit or connected graph creates a sub-graph that is a tree.
The graphs are classified into various categories such as directed, non-directed, connected,
nonconnected, simple and multi-graph. Properties of a graph are as follows:

• A vertex in a graph can be connected to any number of other vertices using edges.

• An edge can be bi-directed or directed.

• An edge can be weighted.


Graph Traversal: Graph traversals are also known as graph search algorithms. Like trees traversal
algorithms, graph search algorithms can be thought of as starting at some source vertex in a graph
and “search” the graph by going through the edges and marking the vertices. There are two such
algorithms for traversing the graph: -
1. Breadth First Search (BFS)
2. Depth First Search (DFS)
1. Breadth First Search: BFS algorithms work similar to level-order traversal of the trees. Like
level-order traversal BFS also uses queues. BFS works level by level. Initially, BFS starts at a
given vertex, which is at level 0. In the first stage, it visits all vertices at level 1. In the second
stage, it visits all vertices at second level. BFS continues this process until all the levels of the
graph are completed. Generally, queue data structure is used for sorting the vertices of a level.
2. Depth First Search: DFS algorithms work similar to pre-order traversal of the trees. Like
preorder traversal, internally this algorithm also uses stack. The process of returning from the
“dead end” is called Backtracking. We are trying to go away from starting vertex into the graph as
deep as possible, until we have to backtrack to the preceding vertex

CODE
#include<iostream>
#include<list>
#include<memory> using
namespace std; class
Graph{ int V;//No. of
vertices
list<int>* adj;//Pointer to an array containing adjacency lists
void DFSUtil(int v, bool visited[]);//A function used by DFS
public:
Graph(int V);//Constructor
void addEdge(int v, int w);//Function to add an edge to graph
void DFS();//Prints DFS traversal of the complete graph
};
Graph::Graph(int V){
this->V = V; adj =
new list<int>[V];
}
void Graph::addEdge(int v, int w){ adj[v].push_back(w);//Add

w to v’s list.

}
void Graph::DFSUtil(int v, bool visited[]){
visited[v]=true;//Mark the current node as visited and print it cout<<v<<"
";
list<int>::iterator i;//Recur for all the vertices adjacent to this vertex
for(i=adj[v].begin(); i!=adj[v].end(); ++i) if (!visited[*i])
DFSUtil(*i, visited);
}
void Graph::DFS(){//The function to do DFS traversal. It uses recursive DFSUtil()
bool *visited=new bool[V];//Mark all the vertices as not visited for(int i=0; i<V;
i++) visited[i]=false;

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


if(visited[i]==false)
DFSUtil(i, visited);
} int
main(){
Graph g(7);//Creation of a graph g.addEdge(0,
1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(1, 0);
g.addEdge(1, 5);
g.addEdge(2, 5);
g.addEdge(3, 0);
g.addEdge(3, 4);
g.addEdge(4, 6);
g.addEdge(5, 1);
g.addEdge(6, 5);
cout<<"Following is Depth First Traversal"<<endl;
cout<<"\t"; g.DFS(); cout<<endl; return 0; }

OUTPUT

EXPERIMENT -19
Aim-Program to traverse graphs using BFS.
Hardware and Software Requirement- CPU , C++ , Dev C++ , Screen, Mouse,
Theory
A graph is a pair (V, E), where V is a set of nodes, called vertices and E is a collection of pairs of
vertices, called edges. Vertices and edges are positions and store elements. Removal of an edge
from a circuit or connected graph creates a sub-graph that is a tree.
The graphs are classified into various categories such as directed, non-directed, connected,
nonconnected, simple and multi-graph. Properties of a graph are as follows:

• A vertex in a graph can be connected to any number of other vertices using edges.

• An edge can be bi-directed or directed.

• An edge can be weighted.


Graph Traversal: Graph traversals are also known as graph search algorithms. Like trees traversal
algorithms, graph search algorithms can be thought of as starting at some source vertex in a graph
and “search” the graph by going through the edges and marking the vertices. There are two such
algorithms for traversing the graph: -
1. Breadth First Search (BFS)
2. Depth First Search (DFS)
1. Breadth First Search: BFS algorithms work similar to level-order traversal of the trees. Like
level-order traversal BFS also uses queues. BFS works level by level. Initially, BFS starts at a
given vertex, which is at level 0. In the first stage, it visits all vertices at level 1. In the second
stage, it visits all vertices at second level. BFS continues this process until all the levels of the
graph are completed. Generally, queue data structure is used for sorting the vertices of a level.
2. Depth First Search: DFS algorithms work similar to pre-order traversal of the trees. Like
preorder traversal, internally this algorithm also uses stack. The process of returning from the
“dead end” is called Backtracking. We are trying to go away from starting vertex into the graph as
deep as possible, until we have to backtrack to the preceding vertex

CODE
#include<iostream>
#include<list> using
namespace std; class
Graph{ int V;//No. of
vertices list<int>
*adj;//Pointer to an
array containing
adjacency lists
public:
Graph(int V);//Constructor
void addEdge(int v, int w); //Function to add an edge to graph
void BFS(int s);//Print BFS traversal from a given source s
};
Graph::Graph(int V){
this->V=V; adj=new
list<int>[V];
}
void Graph::addEdge(int v, int w){ adj[v].push_back(w);//Add

w to v’s list.

}
void Graph::BFS(int s)
bool *visited=new bool[V];//Mark all the vertices as not visited
for(int i=0; i<V; i++) visited[i]=false;
list<int>queue;//Create a queue for BFS
visited[s]=true;//Mark the current node as visited and enqueue it queue.push_back(s);
list<int>::iterator i;//'i' will be used to get all adjacent vertices of a vertex
cout<<"\t"; while(!queue.empty()){
s=queue.front();//Dequeue a vertex from queue and print it
cout<<s<<" "; queue.pop_front();
for(i=adj[s].begin(); i!=adj[s].end(); ++i){ if(!visited[*i]){

visited[*i] = true; queue.push_back(*i);


}}}}
int main(){// Driver program to test methods of graph class
Graph g(7);//Creation of a graph g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(1, 0);
g.addEdge(1, 5);
g.addEdge(2, 5);
g.addEdge(3, 0);
g.addEdge(3, 4);
g.addEdge(4, 6);
g.addEdge(5, 1);
g.addEdge(6, 5);
cout<<"Following is Breadth First Traversal for given graph"<<endl;
cout<<"\t(starting from vertex 2)"<<endl<<endl; g.BFS(2);
cout<<endl; return 0; }

OUTPUT

You might also like