0% found this document useful (0 votes)
160 views4 pages

DFS, BFS, TSP

The document contains C++ code examples for implementing topological sorting, depth-first search (DFS), and breadth-first search (BFS) on graphs. The topological sorting code takes in a graph as an adjacency matrix and outputs the topological ordering. The DFS code performs DFS traversal on a graph represented by a cost matrix, outputting the visit order. The BFS code implements BFS using queues and adjacency lists to output the visit order starting from a given vertex.

Uploaded by

sathwick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views4 pages

DFS, BFS, TSP

The document contains C++ code examples for implementing topological sorting, depth-first search (DFS), and breadth-first search (BFS) on graphs. The topological sorting code takes in a graph as an adjacency matrix and outputs the topological ordering. The DFS code performs DFS traversal on a graph represented by a cost matrix, outputting the visit order. The BFS code implements BFS using queues and adjacency lists to output the visit order starting from a given vertex.

Uploaded by

sathwick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Topological sorting: -

#include<iostream>

using namespace std;

int main(){
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;

cout<<"Enter the no of vertices:\n";


cin>>n;

cout<<"Enter the adjacency matrix:\n";


for(i=0;i<n;i++){
cout<<"Enter row "<<i+1<<"\n";
for(j=0;j<n;j++)
cin>>a[i][j];
}

for(i=0;i<n;i++){
indeg[i]=0;
flag[i]=0;
}

for(i=0;i<n;i++)
for(j=0;j<n;j++)
indeg[i]=indeg[i]+a[j][i];

cout<<"\nThe topological order is:";

while(count<n){
for(k=0;k<n;k++){
if((indeg[k]==0) && (flag[k]==0)){
cout<<k+1<<" ";
flag[k]=1;
}

for(i=0;i<n;i++){
if(a[i][k]==1)
indeg[k]--;
}
}

count++;
}

return 0;
}

DFS: -
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];

main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}

cout <<"enter initial vertex";


cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}

BFS: -

#include <iostream>
#include <list>

using namespace std;

class Graph
{
int numVertices;
list *adjLists;
bool* visited;
public:
Graph(int vertices);
void addEdge(int src, int dest);
void BFS(int startVertex);
};
Graph::Graph(int vertices)
{
numVertices = vertices;
adjLists = new list[vertices];
}

void Graph::addEdge(int src, int dest)


{
adjLists[src].push_back(dest);
adjLists[dest].push_back(src);
}

void Graph::BFS(int startVertex)


{
visited = new bool[numVertices];
for(int i = 0; i < numVertices; i++)
visited[i] = false;

list queue;

visited[startVertex] = true;
queue.push_back(startVertex);

list::iterator i;

while(!queue.empty())
{
int currVertex = queue.front();
cout << "Visited " << currVertex << " ";
queue.pop_front();

for(i = adjLists[currVertex].begin(); i !=
adjLists[currVertex].end(); ++i)
{
int adjVertex = *i;
if(!visited[adjVertex])
{

visited[adjVertex] = true;
queue.push_back(adjVertex);
}
}
}
}

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);

g.BFS(2);

return 0;
}

You might also like