Unit Iv
Unit Iv
GRAPH
Graph is a pictorial representation of set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by point called vertices. And
the links that connect the vertices are called edges. Thus the graph is pair of sets (V, E)
where V is set of vertices the graph and E is set of edges.
E.g.:
V = { , 𝐵, 𝐶, 𝐷, 𝐸 }
OPERATIONS ON GRAPH
GRAPH TERMINOLOGY
1. Vertex: Each node of a graph is termed as vertex.
2. Edge: Edge represents the link between two vertices.
3. Adjacency: Two vertices are adjacent if they are connected to each other through an
edge.
4. Path: Path represents a sequence of edges between two vertices.
GRAPH REPRESENTATION
1. Adjacent Matrix
The adjacency matrix in a graph is a matrix with rows and columns, represented by
graph vertices with 1 or 0 in the position 𝑉𝑖𝑗 according to whether 𝑉𝑖 or 𝑉𝑗 has a link or
not. In such a matrix when 𝑖𝑡ℎ row and 𝑗𝑡ℎ column element is 1, then there will be an
edge between 𝑖 and 𝑗 vertex. When there is no edge, the value will be zero. The above
graph can be represented is as follows:
2. Adjacent List
The adjacent list of a graph is linked with one list per vertex. It will have a head list
which contains all the vertices in a sequential order and each element in its linked list
represents the other vertices that form an edge with the vertex. The adjacency list
representation of above graph is as follows:
Step 1: Define a stack of size with total number of vertices in the graph.
Step 2: Select any vertex as starting point for traversal visit that vertex and push it on to
the stack.
Step 3: Visit any one of the adjacent vertex of the vertex which at the top of stack and is
not visited and push it on to the stack.
Step 4: Repeat step 3 until there are no vertex to visit from vertices on the stack.
Step 5: When there is no vertex to visit then use back tracking and pop one vertex from
the stack.
Step 6: Repeat steps 3, 4 and 5 until stack becomes empty.
Step 7: When stack become empty it will give the sequence of vertices visited.
E.g.: - Consider the graph below
Step 1:
Step 2:
Mark S as visited and put it onto the stack. Explore any unvisited adjacent node from S. We have
three nodes and we can pick any of them. For this example, we shall take the node in an alphabetical
order.
Step 3:
Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both S
and D are adjacent to A but we are concerned for unvisited nodes only.
Step 4:
Visit D and mark it as visited and put onto the stack. Here, we have B and C nodes, which are
adjacent to D and both are unvisited. However, we shall again choose in an alphabetical order.
Step 5:
We choose B, mark it as visited and put onto the stack. Here B does not have any unvisited adjacent
node. So, we pop B from the stack.
Step 6:
We check the stack top for return to the previous node and check if it has any unvisited nodes. Here,
we find D to be on the top of the stack.
Step 7:
Only unvisited adjacent node is from D is C now. So we visit C, mark it as visited and put it onto the
stack.
Step 8:
As C does not have any unvisited adjacent node so we keep popping the stack until we find a
node that has an unvisited adjacent node. In this case, there's none and we keep popping until
the stack is empty.
#include<iostream>
using namespace std;
class graph
{
int n,c,s;
int ag[10][10],a[10];
public:
void matrix();
void dfs();
};
void graph::matrix()
{
int i,j;
cout<<"Enter the no. of Nodes"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the Node"<<endl;
cin>>a[i];
}
for(i=0,j=1;j<n,i<n;j++,i++)
{
ag[0][j]=a[i];
}
for(i=1,j=0;i<n,j<n;i++,j++)
{
ag[i][0]=a[j];
}
cout<<"Enter the adjacency matrix"<<endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"Enter 1 if edge is present otherwise 0
between "<<ag[i][0]<<" "<<ag[0][j]<<endl;
cin>>ag[i][j];
}}
}}}
if(y==0)
{
top=--top;
}
m=0;
for(i=0;i<n;i++)
{
if(visited[i]!=1)
{
++m;
}
}
}while(m!=0);
int main()
{
graph g;
g.matrix();
g.dfs();
return 0;
}
Initialize Queue
Step 2:
Step 4:
Next, the unvisited adjacent node from S is B. We mark it as visited and enqueue it. Step 5:
Next, the unvisited adjacent node from S is C. We mark it as visited and enqueue it. Step 6:
Now, S is left with no unvisited adjacent nodes. So, we dequeue and find A. Step 7:
From A we have D as unvisited adjacent node. We mark it as visited and enqueue it.
Step 8:
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep on
dequeuing in order to get all unvisited nodes. When the queue gets emptied, the program is over.
for(i=0,j=1;j<n,i<n;j++,i++)
{
ag[0][j]=a[i];
}
for(i=1,j=0;i<n,j<n;i++,j++)
{
ag[i][0]=a[j];
}
void graph::bfs()
{
int i,j,t,y,m,x,k,front=-1,rear=-1;
int queue[10],visited[10];
cout<<"Enter the node from which nodes are printed"<<endl;
cin>>x;
queue[++front]=x;
rear=front;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
k=i;
break;
}
}
visited[k]=1;
cout<<x<<" ";
do{
for(i=1;i<=n;i++)
{
if(ag[i][0]==queue[front])
{
y=0;
for(j=1;j<=n;j++)
if(ag[i][j]==1)
{
for(t=0;t<n;t++)
{
if(ag[0][j]==a[t])
{
break;
}
}
if(visited[t]!=1)
{
queue[++rear]=a[t];
cout<<a[t]<<" ";
visited[t]=1;
y++;
}}}}}
if(y==0)
{
front=front+1;
}
m=0;
for(i=0;i<n;i++)
{
if(visited[i]!=1)
{
++m;
}
}
}while(m!=0);
}
int main()
{
graph g;
g.matrix();
g.bfs();
return 0;
}
Floyed Warshall algorithm
The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to
find shortest distances between every pair of vertices in a given edge weighted directed Graph.
Create a|V|x|V| matrix //It represents the distance between every pair of vertices as given
If i==j
M[i][j]=weight(I,j) //If there exists a direct edge between the values, value= weight of edge
else
if M[i][j]>M[i][k]+M[k][j]
M[i][j]=M[i][k]+M[k][j]