0% found this document useful (0 votes)
83 views24 pages

Graphs - Adjacency List

The document discusses graph algorithms including depth first search, breadth first search, Kruskal's algorithm, and Prim's algorithm. It includes C++ code for implementing graphs using an adjacency list representation and functions for traversing graphs using DFS and BFS. It also includes code for Kruskal's and Prim's minimum spanning tree algorithms on graphs represented by adjacency matrices. Sample input and output are provided showing the algorithms being run on a graph with 7 vertices and various edges.

Uploaded by

Krishna Prasad
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)
83 views24 pages

Graphs - Adjacency List

The document discusses graph algorithms including depth first search, breadth first search, Kruskal's algorithm, and Prim's algorithm. It includes C++ code for implementing graphs using an adjacency list representation and functions for traversing graphs using DFS and BFS. It also includes code for Kruskal's and Prim's minimum spanning tree algorithms on graphs represented by adjacency matrices. Sample input and output are provided showing the algorithms being run on a graph with 7 vertices and various edges.

Uploaded by

Krishna Prasad
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/ 24

104

GRAPHS ADJACENCY LIST


//Regno:2013503514
//Depth first and Breadth first traversal
#include<iostream>
using namespace std;
class vertex
{
int data;
vertex *next;
bool mark;
public:
vertex(int val=0,vertex *n=NULL)
{
data=val;
next=n;
mark=0;
}
friend int countnode(vertex *v)
{
if(v)
return (1+countnode(v->next));
else
return 0;

105

}
friend int* getvertices(vertex *v,int i,int *all)
{
if(v)
{
all[i]=v->data;
all=getvertices(v->next,i+1,all);
}
else
return all;
}
friend class graph;
};
class graph
{
vertex **v;
int nv;
int **cost;
bool *visitedDFT,*visitedBFT;
public:
graph(int n)
{
nv=n;

106

visitedDFT=new bool[n];
visitedBFT=new bool[n];
v=new vertex*[n];
cost=new int*[n];
for(int i=0;i<n;i++)
{
v[i]=new vertex[n];
cost[i]=new int[n];
visitedDFT[i]=0;
visitedBFT[i]=0;
}
}
void creategraph(int **m)
{
for(int i=0;i<nv;i++)
for(int j=0;j<nv;j++)
{
if(m[i][j]==1)
{
v[i]->next=new vertex(j,v[i]->next);
}
}
}

107

void creategraph()
{
int c;
cout<<"Enter the edges one by one with cost:\n";
while(1)
{
int head,tail;
cin>>head>>tail>>c;
if(head>=nv||tail>=nv)
break;
v[head]->next=new vertex(tail,v[head]->next);
cost[head][tail]=c;
}
}
void displaygraph()
{
for(int i=0;i<nv;i++)
{
cout<<"["<<i<<"]\t";
vertex *temp=v[i]->next;
while(temp)
{
cout<<temp->data<<"\t"<<"cost[";

108

temp=temp->next;
}
cout<<endl;
}
}
friend void DFT(graph g,int v1)
{
cout<<v1<<"\t";
g.visitedDFT[v1]=1;
int ct=countnode(g.v[v1]);
int *array=new int[ct];
array=getvertices(g.v[v1],0,array);
for(int i=0;i<ct;i++)
if(!g.visitedDFT[array[i]])
DFT(g,array[i]);
}
friend void BFT(graph g,int v1)
{
int q[100],front=0,rear=0;
q[++rear]=v1;
while(front!=rear)
{
int curr=q[++front];

109

if(!g.visitedBFT[curr])
cout<<curr<<"\t";
g.visitedBFT[curr]=1;
int ct=countnode(g.v[curr]);
int *array=new int[ct];
array=getvertices(g.v[curr],0,array);
for(int i=0;i<ct;i++)
{
if(!g.visitedBFT[array[i]])
q[++rear]=array[i];
}
}
}
// friend class node;
};
int main()
{
int n;
cout<<"Enter the number of vertices:\n";
cin>>n;
graph g(n);
g.creategraph();
cout<<"\nDFT\n";

110

DFT(g,0);
cout<<"\nBFT\n";
BFT(g,0);
cout<<"\n";
return 1;
}
Output:
Enter the number of vertices:
7
Enter the edges one by one with cost:
013
025
037
103
121
156
205
211
234
249
258
307
324

111

348
429
438
457
516
528
547
562
652
777

DFT
0

BFT
0

//Kruskal algorithm:
#include<iostream>
using namespace std;
class vertex
{
public:
int data,mark;
vertex *next;

112

vertex(int val=0,vertex *n=NULL)


{
data=val;
next=n;
mark=0;
}
friend int countnode(vertex *v)
{
if(v->next)
return (1+countnode(v->next));
else
return 1;
}
friend int* getvertices(vertex *v,int i,int *all)
{
if(v)
{
all[i]=v->data;
all=getvertices(v->next,i+1,all);
}
else
return all;
}

113

};
class graph
{
vertex **v;
int nv;
bool *visitedDFT,*visitedBFT;
public:
graph(int n)
{
nv=n;
visitedDFT=new bool[n];
visitedBFT=new bool[n];
v=new vertex*[n];
for(int i=0;i<n;i++)
{
v[i]=new vertex[n];
visitedDFT[i]=0;
visitedBFT[i]=0;
}
}
void creategraph()
{
cout<<"Enter the edges one by one:\n";

114

while(1)
{
int head,tail;
cin>>head>>tail;
if(head>=nv||tail>=nv)
break;
v[head]->next=new vertex(tail,v[head]->next);
}
}
void displaygraph()
{
for(int i=0;i<nv;i++)
{
cout<<"["<<i<<"]\t";
vertex *temp=v[i]->next;
while(temp)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<endl;
}
}

115

friend void kruskal(graph &g,int **mygraph,int n)


{
int minhead=0,mintail=0,min=10;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if((mygraph[i][j]>0)&&(mygraph[i][j]<min))
{
minhead=i;
mintail=j;
min=mygraph[i][j];
}
}
if(min==10)
return;
int fl=g.closed(minhead,mintail,mygraph,n);
for(int j=0;j<n;j++)
g.v[j]->mark=0;
if(!fl)
{
vertex *temp=g.v[minhead];
while(temp->next)
temp=temp->next;

116

temp->next=new vertex(mintail,temp->next);
mygraph[minhead][mintail]=10;
mygraph[mintail][minhead]=10;
return kruskal(g,mygraph,n);
}
else
{
mygraph[minhead][mintail]=20;
mygraph[mintail][minhead]=20;
return kruskal(g,mygraph,n);
}
}
int closed(int minhead,int mintail,int **mygraph,int n)
{
v[mintail]->mark=1;
if(minhead==mintail) return 1;//{cout<<"Loop formed\n";
return 1;}
int arr[n],ii=0;
for(int i=0;i<n;i++)
if(mygraph[mintail][i]==10)
{
arr[ii]=i;//cout<<"\narray "<<ii<<"th value "<<arr[ii]<<'\n';
ii++;

117

}
arr[ii]=-1;ii=0;
/* cout<<"connected with "<<mintail<<"\n";
for(int i=0;arr[i]>=0;i++)
cout<<arr[i]<<"\t";
cout<<"\n";*/
for(int i=0;arr[i]>=0;i++)
{
if(!v[arr[i]]->mark)
ii=closed(minhead,arr[i],mygraph,n);
if(ii==1)
return ii;
}
return 0;//cout<<"No loop\n"; return 0;
}
};
int main()
{
int n;
cout<<"Enter the number of vertices:\n";
cin>>n;
int **mygraph;
mygraph=new int*[n];

118

for(int i=0;i<n;i++)
mygraph[i]=new int[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mygraph[i][j]=0;
cout<<"Enter the edge and weight one by one:\n";
while(1)
{
int tail,head,weight;
cin>>head>>tail>>weight;
if(tail>=n||head>=n)
break;
if(head==tail)
cout<<"Invalid edge\nEnter again\n";
else
mygraph[head][tail]=weight;
}
cout<<"The Adjacency matrix is:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<mygraph[i][j]<<"\t";
cout<<"\n";

119

}
graph g(n);
kruskal(g,mygraph,n);
g.displaygraph();
return 1;
}
Output:
Enter the number of vertices:
7
Enter the edge and weight one by one:
013
025
037
103
121
156
205
211
234
249
258
307
324

120

348
429
438
457
516
528
547
562
652
777
The Adjacency matrix is:
0

[0]

[1]

[2]

[3]
[4]

121

[5]

[6]
//Prims algorithm:
#include<iostream>
using namespace std;
class vertex
{
public:
int data,mark;
vertex *next;
vertex(int val=0,vertex *n=NULL)
{
data=val;
next=n;
mark=0;
}
};
class graph
{
vertex **v;
int nv;
bool *visitedDFT,*visitedBFT;
public:

122

graph(int n)
{
nv=n;
visitedDFT=new bool[n];
visitedBFT=new bool[n];
v=new vertex*[n];
for(int i=0;i<n;i++)
{
v[i]=new vertex[n];
visitedDFT[i]=0;
visitedBFT[i]=0;
}
}
void displaygraph()
{
for(int i=0;i<nv;i++)
{
cout<<"["<<i<<"]\t";
vertex *temp=v[i]->next;
while(temp)
{
cout<<temp->data<<"\t";
temp=temp->next;

123

}
cout<<endl;
}
}
friend void prim(graph &g,int **mygraph,int v1,int n,int ct)
{
if(ct==n-1)
return;
int head=0,tail=0,min=10;
g.v[v1]->mark=1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(g.v[i]->mark && mygraph[i][j] && mygraph[i][j]<min)
{
head=i;
tail=j;
min=mygraph[i][j];
}
}
mygraph[head][tail]=10;
mygraph[tail][head]=10;
if(g.v[tail]->mark)

124

return prim(g,mygraph,head,n,ct);
else
{
g.v[head]->next=new vertex(tail,g.v[head]->next);
return prim(g,mygraph,tail,n,ct+1);
}
}
};
int main()
{
int n;
cout<<"Enter the number of vertices:\n";
cin>>n;
int **mygraph;
mygraph=new int*[n];
for(int i=0;i<n;i++)
mygraph[i]=new int[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mygraph[i][j]=0;
cout<<"Enter the edge and weight one by one:\n";
while(1)
{

125

int tail,head,weight;
cin>>head>>tail>>weight;
if(tail>=n||head>=n)
break;
if(head==tail)
cout<<"Invalid edge\nEnter again\n";
else
mygraph[head][tail]=weight;
}
cout<<"The Adjacency matrix is:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<mygraph[i][j]<<"\t";
cout<<"\n";
}
graph g(n);
prim(g,mygraph,0,n,0);
g.displaygraph();
return 1;
}
Output:
Enter the number of vertices:

126

7
Enter the edge and weight one by one:
013
025
037
103
121
156
205
211
234
249
258
307
324
348
429
438
457
516
528
547
562

127

652
777
The Adjacency matrix is:
0

[0]

[1]

[2]

[3]
[4]
[5]
[6]

You might also like