answersLogoWhite

0


Best Answer

Breadth First Search is the technique to find the shortest distance between some starting node and the remaining nodes of the graph. This shortest distance is the minimum number of edges traversed in order to travel from the start node to the specific node being examined. It is called BFS because the distances are given breadth wise. It is the faster search technique as the representation of the nodes and the edges are in the form of adjacency list representation. We can also use this technique for searching.

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

/******************************************************************

-> This Program is to implement Depth First search.

-> Data Structers:

Graph:Adjacency List

Stack:Array

-> This program works in Microsoft vc++ 6.0 environment.

*******************************************************************/

#include

int visit[100];

class graph

{

private:

int n;

graph*next;

public:

graph* read_graph(graph*);

void dfs(int); //dfs for a single node

void dfs(); //dfs of the entire graph

void ftraverse(graph*);

}*g[100];

int dfs_span_tree[100][100];

graph* graph::read_graph(graph*head)

{

int x;

graph*last;

head=last=NULL;

cout<<"Enter adjecent node ,-1 to stop:\n";

cin>>x;

while(x!=-1)

{

graph*NEW;

NEW=new graph;

NEW->n=x;

NEW->next=NULL;

if(head==NULL)

head=NEW;

else

last->next=NEW;

last=NEW;

cout<<"Enter adjecent node ,-1 to stop:\n";

cin>>x;

}

return head;

}

void graph::ftraverse(graph*h)

{

while(h!=NULL)

{

cout<n<<"->";

h=h->next;

}

cout<<"NULL"<

}

void graph::dfs(int x)

{

cout<<"node "<

visit[x]=1;

graph *p;

p=g[x];

while(p!=NULL)

{

int x1=p->n;

if(visit[x1]==0)

{

cout<<"from node "<

//Add the edge to the dfs spanning tree

dfs_span_tree[x][x1]=1;

dfs(x1);

}

p=p->next;

}

}

void graph::dfs()

{

int i;

cout<<"**********************************************************\n";

cout<<"This program is to implement dfs for an unweighted graph \n";

cout<<"**********************************************************\n";

cout<<"Enter the no of nodes ::";

cin>>n;

for(i=1;i<=n;i++)

g[i]=NULL;

for(i=1;i<=n;i++)

{

cout<<"Enter the adjacent nodes to node no. "<

cout<<"***************************************\n";

g[i]=read_graph(g[i]);

}

//display the graph

cout<<"\n\nThe entered graph is ::\n";

for(i=1;i<=n;i++)

{

cout<<" < "< ::";

ftraverse(g[i]);

}

for(i=1;i<=n;i++)

visit[i]=0; //mark all nodes as unvisited

cout<<"\nEnter the start vertex ::";

int start;

cin>>start;

for(i=1;i<=n;i++)

for(int j=1;j<=n;j++)

dfs_span_tree[i][j]=0;

cout<<"\nThe dfs for the above graph is ::\n";

dfs(start);

cout<<"\n\nThe required dfs spanning tree is ::\n";

for(i=1;i<=n;i++)

{

for(int j=1;j<=n;j++)

cout<

cout<

}

}

int main()

{

graph obj;

obj.dfs();

return 0;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the C program for DFS and BFS?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Who is faster between dfs and bfs?

dfs better then from bfs..


Difference between DFS and BFS?

1. bfs uses queue implementation ie.FIFO dfs uses stack implementation ie. LIFO 2. dfs is faster than bfs 3. dfs requires less memory than bfs 4. dfs are used to perform recursive procedures.


Can apply bfs and dfs on weighted graph?

nop


Prove that if a DFS and BFS trees in graph G are the same than?

ok here we go...Proof:If the some graph G has the same DFS and BFS then that means that G should not have any cycle(work out for any G with a cycle u will never get the same BFS and DFS .... and for a graph without any cycle u will get the same BFS/DFS).We will prove it by contradiction:So say if T is the tree obtained by BFS/DFS, and let us assume that G has atleast one edge more than T. So one more edge to T(T is a tree) would result in a cycle in G, but according to the above established principle no graph which has a cycle would result the same DFS and BFS, so out assumption is a contradiction.Hence G should have more edges than T, which implies that if the BFS and DFS for a graph G are the same then the G = T.Hope this helps u......................


Why you use DFS and BFS in graphs?

DFS and BFS are both searching algorithms. DFS, or depth first search, is a simple to implement algorithm, especially when written recursively. BFS, or breadth first search, is only slightly more complicated. Both search methods can be used to obtain a spanning tree of the graph, though if I recall correctly, BFS can also be used in a weighted graph to generate a minimum cost spanning tree.


Why BFS is implemented using queue and DFS is implemented using stack?

yes pagal


Why is dfs better then from bfs?

It isn't necessarily better... They each have their own pros and cons


What is the algorithm DFS and BFS in graph traversal?

DFS and BFS stands for Depth First Search and Breadth First Search respectively. In DFS algorithm every node is explored in depth; tracking back upon hitting an already visited node and starts visiting from a node which has any adjacent nodes unvisited. In BFS, the nodes are visited level wise. These algorithms are used to traverse the nodes on a connected digraph. Primal


Why if dfs equal to bfs tree say call it T than G equal to T?

Prove_that_if_a_DFS_and_BFS_trees_in_graph_G_are_the_same_than


Am trying to find the solution to this question give a linear-time algorithm for a directed acyclic graph?

DFS, BFS


How can you design an algorithm to check if a given graph is connected?

Use a simple DFS/BFS traversal. If you have gone through all nodes, the graph is connected.


What is the population of BFS Group Ltd?

The population of BFS Group Ltd is 4,200.