0% found this document useful (0 votes)
16 views13 pages

Unit Iv

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)
16 views13 pages

Unit Iv

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/ 13

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.:

In the above graph a can be represents as (V, E) where

V = { , 𝐵, 𝐶, 𝐷, 𝐸 }

E = { , 𝐵𝐸, 𝐴𝐷, 𝐴𝐶, 𝐶𝐷, 𝐵𝐷, 𝐷𝐸 }

OPERATIONS ON GRAPH

1. Add Vertex: Addition of a new vertex to the graph


2. Add Edge: Addition of a new edge between 2 vertices
3. Display Vertex: Display vertex of a 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:

GRAPH TRAVERSAL METHODS

1. Depth First Search (DFS)


This algorithm traverses a graph in a depth ward direction and uses a stack
to remember the next vertex to be visited when a dead end occurs i.e. for back tracking.

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:

Initialize the stack

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.

Q. Implement depth first search algorithm

#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];
}}

cout<<"ADJACENCY MATRIX OF UNDIRECTED GRAPHis---"<<endl;


for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{ if(i==0&&j==0)
{
cout<<" ";
}
else
{
cout<<" "<<ag[i][j];
}
}
cout<<endl;
}
}
void graph::dfs()
{
int i,j,t,x,y,m,k,top=-1;
int stack[10],visited[10];
cout<<"Enter the node from which node from which traversal
start"<<endl;
cin>>x; stack[+
+top]=x;
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]==stack[top])
{
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)
{
stack[++top]=a[t];
cout<<a[t]<<" ";
visited[t]=1;
y++;
break;
}
}

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

2. Breadth First Search (BFS)


This algorithm traverses a graph in breadth wise direction and is utilising a
queue for processing vertices.

Step 1: Define a sequence of size with total number of vertices in a graph.


Step 2: Select any vertex as starting point for the traversal and visit that vertex. Insert
the newly selected vertex into the queue.
Step 3: Visit all adjacent vertex that vertex which is at the front of the queue and is not
visited, insert that vertex on to queue.
Step 4: Repeat step 3 until there is no new vertex to be visited from queue.
Step 5: When queue is empty an algorithm produces BFS sequence.
Step 1:

Initialize Queue

Step 2:

We start from visiting S (starting node), and mark it as visited. Step 3:


We then see an unvisited adjacent node from S. In this example, we have three nodes but
alphabetically we choose A, mark it as visited and enqueue it.

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.

Q. Implement breadth first search algorithm


#include<iostream>
using namespace std;
class graph
{
int n,c,s;
int ag[10][10],a[10];
public:
void matrix();
void bfs();
};
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];
}
}

cout<<"ADJACENCY MATRIX OF UNDIRECTED GRAPHis----


"<<endl;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{ if(i==0&&j==0)
{
cout<<" ";
}
else
{
cout<<" "<<ag[i][j];
}
}
cout<<endl;
}
}

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.

Pseudocode for Floyed Warshal algorithm is

Create a|V|x|V| matrix //It represents the distance between every pair of vertices as given

For each cell (I,j) in M do

If i==j

M[i][j]=0 // For all diagonal elements , value=0


If(I,j) is an edge in E

M[i][j]=weight(I,j) //If there exists a direct edge between the values, value= weight of edge

else

M[i][j]=infinity //if there is no direct edge between the vertices, value=infinity

for k from 1 to |V|

for i from 1 to |V|

for j from 1 to |V|

if M[i][j]>M[i][k]+M[k][j]

M[i][j]=M[i][k]+M[k][j]

You might also like