UNIT-IV
GRAPHS
SK. YAKOOB
Associate Professor
Graphs
Graph is a non-linear data structure consisting of vertices and edges
T he set of edges describes relationships among the vertices.
So , a Graph is a finite set of vertices (or nodes),and set of edges which connect a pair of vertices.
A Graph is defined as ,G(V,E) where V Set of Vertices
E Set of Edges
Example:
V(G)={ A,B,C,D,E}
E(G)={(A,B),(A,D),(B,C),(B,D),(C,E),(D,E)}
A B C
D E
Graph Terminology
Adjacent Nodes(Neighbors): Two nodes are adjacent if they are connected
with an edge.
A B
Path: A sequence of vertices that connect two nodes in a Graph.
Complete Graph: A Graph in which every vertex is directly connected to
every other vertex.
No. of Edges in complete graph, if it is Directed e= n*n-1, if it is
Undirected e=(n*n-1)/2
A
A
B C
B C
Directed Complete Graph: E= n*n-13*(3-1)= 6 UnDirected Complete Graph: E= (n*n-1)/2
3*(3-1)/2= 6/2=3
Graph Terminology
A cycle is a path, it start with vertex and ends with same vertex.
A loop is a special case of cycle in which a single arc begins and ends with the same
vertex
Two vertices are said to be connected if there is a path between them. A graph is said to
be connected if, ignoring direction, there is a path from any vertex to any other vertex.
A directed graph is strongly connected if there is a path from each vertex to every other
vertex in the digraph.
A graph is a disjoint graph if it is not connected
The degree of a vertex is the no/of lines incident to it, The out - degree of a vertex in a
digraph is the no. of arcs leaving the vertex The in - degree is the no. of arcs entering the vertex
Graph Types
According to Path or Flow:
Undirected: In a Graph if edges have no direction then it is called
Undirected Graph
Directed: In a Graph if edges have direction then it is called
Directed Graph
Undirected Graph Directed Graph
A B A B
C C
D E D E
V(G)={ A,B,C,D,E} V(G)={ A,B,C,D,E}
E(G)={(A,B),(A,D),(B,C),(B,D),(C,E),(D,E)} E(G)={(A,B),(A,D),(B,C),(D,B),(D,E),(E,C)}
Graph Types
According to Weight to Edge:
Weighted: In a Graph if edges have weights then it is called
Weighted Graph
Unweighted: In a Graph if edges have no weights then it is called
Unweighted Graph
Unweighted Graph Weighted Graph
20 25
A B A B
C C
10 30
5
15
D E D E
Graph Types
According to Loops:
Cyclic : In a Graph if Cycles are possible then it is called Cyclic Graph
Acyclic: In a Graph if Cycles are not possible then it is called Acyclic
Graph
Cyclic Graph Acyclic Graph
20 25
A B A B
C C
10 30
15
D E D E
Graph Implementation Methods
The following two are the most commonly used
representations of a graph.
Adjacency Matrix:
Adjacency List:
Graph Implementation Methods
Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V
where V is the number of vertices in a graph.
Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that
there is an edge from vertex i to vertex j.
Adjacency Matrix is also used to represent weighted graphs. If
adj[i][j] = w, then there is an edge from vertex i to vertex j
with weight w. A B C D
E
0 1 0 1 0
E
A
A B 1 0 1 1 0
B
C
0 1 0 0 1
C
Adjacency Matrix
1 1 0 0 1
D
D E
0 0 1 1 0
Graph Implementation Methods
Adjacency List: An array of lists is used. The size of the array is
equal to the number of vertices.
Let the array be an array[]. An entry array[i] represents the list
of vertices adjacent to the ith vertex.
A B D /
B A C D /
A B C
C B E /
Adjacency List
D A B E /
D E
E
D C /
Graph Traversal Methods
The following two are the most commonly used
Traversals of a graph.
Breadth First Search(BFS):
Depth First Search(DFS):
Graph Traversal Methods(BFS)
Graph traversal is a technique used for searching a vertex in a graph.
The graph traversal is also used to decide the order of vertices is visited in
the search process.
A graph traversal finds the edges to be used in the search process without
creating loops. That means using graph traversal we visit all the vertices of
the graph without getting into looping path.
BFS traversal of a graph produces a spanning tree as final
result. Spanning Tree is a graph without loops.
Graph Traversal Methods(BFS)
We use Queue data structure with maximum size of total number
of vertices in the graph to implement BFS traversal.
We use the following steps to implement BFS traversal...
Step 1: Define a Queue of size total number of vertices in the graph.
Step 2: Select any vertex as starting point for traversal. Visit that vertex
and insert it into the Queue.
Step 3: Visit all the non-visited adjacent vertices of the vertex which is at
front of the Queue and insert them into the Queue.
Step 4: When there is no new vertex to be visited from the vertex which is
at front of the Queue then delete that vertex.
Step 5: Repeat steps 3 and 4 until queue becomes empty.
Step 6: When queue becomes empty, then produce final spanning tree by
removing unused edges from the graph
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Graph Traversal Methods(BFS)
Implementation of BFS
void main()
{
#include<stdio.h>
int v;
#include<conio.h> printf("\n Enter the number of vertices:");
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1; scanf("%d",&n);
void bfs(int v) for(i=1;i<=n;i++)
{ {
q[i]=0;
for(i=1;i<=n;i++) visited[i]=0;
if(a[v][i] && !visited[i]) }
q[++r]=i; printf("\n Enter graph data in matrix form:\n");
if(f<=r) for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
visited[q[f]]=1; printf("\n Enter the starting vertex:");
bfs(q[f++]); scanf("%d",&v);
} bfs(v);
} printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
}
Graph Traversal Methods(DFS)
DFS traversal of a graph produces a spanning tree as final
result. Spanning Tree is a graph without loops.
We use Stack data structure with maximum size of total number of
vertices in the graph to implement DFS traversal.
We use the following steps to implement DFS traversal...
Step 1: Define a Stack of size 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 non-visited adjacent vertices of a vertex which is at the top of
stack and push it on to the stack.
Step 4: Repeat step 3 until there is no new vertex to be visited from the vertex which is at the
top of the stack.
Step 5: When there is no new 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 becomes Empty, then produce final spanning tree by removing unused edges from
the
graph
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Graph Traversal Methods(DFS)
Implementation of DFS
#include<stdio.h> void main()
void DFS(int); {
int i,j;
int G[10][10],visited[10],n;
printf("Enter number of vertices:");
void DFS(int i) scanf("%d",&n);
{ //read the adjecency matrix
int j; printf("\nEnter adjecency matrix of the
printf("\n%d",i); graph:");
for(i=0;i<n;i++)
visited[i]=1;
for(j=0;j<n;j++)
for(j=0;j<n;j++) scanf("%d",&G[i][j]);
if(!visited[j]&&G[i][j]==1) //visited is initialized to zero
DFS(j); for(i=0;i<n;i++)
} visited[i]=0;
DFS(0);
}
Thank you