DS Notes Graph
DS Notes Graph
DS Notes Graph
GRAPHS
A graph G is a set of vertices (V) and set of edges (E)
G=(V,E)
V(G)=Vertices of graph G
E(G)=Edges of graph G
V(𝐺1 )={A,B,C,D,E,F}
Types of Graph
1. Undirected Graph
A graph containing unordered pair of vertices is called an undirected
graph.
In an undirected graph, pair of vertices (1,0) and (0,1)represent the
same edge.
2. Directed Graph
A graph containing ordered pair of vertices is called a direct graph.
By Nitin Sir
2|VAISHNAVI ACADEMY
3. Complete Graph
An undirected graph, in which every vertex has an edge to all other
vertices is called a complete graph.
4. Weighted Graph
A weighted graph is a graph in which edges are assigned some value.
Most of the physical situation shown using weighted graph.
A weighted graph is a graph in which edges are assigned some value.
The weight will denote the distance between two connected cities
using highway
By Nitin Sir
3|VAISHNAVI ACADEMY
Cycle
A cycle is a simple path that begins and ends at the same vertex.
Cost : The cost of a graph is the sum of the cost of the edges in the weighted
graph.
TREE
Degree of Vertex:-
The outdegree of a node is the total number of edges going out from
that node.
By Nitin Sir
4|VAISHNAVI ACADEMY
Spanning tree
The cost of the spanning tree is the sum of the weights of all the edges in the
tree. There can be many spanning trees.
Minimum spanning tree is the spanning tree where the cost is minimum
among all the spanning trees. There also can be many minimum spanning
trees.
By Nitin Sir
5|VAISHNAVI ACADEMY
Representation of graphs
1. Adjacency matrix
A two dimensional matrix can be used to store a graph.
A graph is represented using a square matrix.
By Nitin Sir
6|VAISHNAVI ACADEMY
Adjacency List
A graph can be represented using a linked list For each vertex a list of
adjacent vertices is maintained using a linked list.
When we traverse all the adjacent nodes, we set the next pointer to
null at the end of the list.
By Nitin Sir
7|VAISHNAVI ACADEMY
Traversal of graph
By Nitin Sir
8|VAISHNAVI ACADEMY
Algorithm
By Nitin Sir
9|VAISHNAVI ACADEMY
{
scanf("%d",&G[i][j]);
}
}
//visited is initialized to zero
for(i=0;i<n;i++)
{
visited[i]=0;
}
BFS(0);
}
void BFS(int i)
{
int j;
init(&q);
enqueue(&q, i);
printf("\n %d",i);
visited[i]=1;
while(!empty(&q))
{
V=dequeue(&q);
For(j=0;j<n;i++)
{
if(!visited[i]==0 && G[i][j]==1)
{
Enqueue(&q,i);
Visited[i]=1;
printf(“ Visited %d”,i);
}
}
}
}
By Nitin Sir
10 | V A I S H N A V I A C A D E M Y
• The algorithm for BFS has to maintain a list of vertices which have
been visited but not explored for adjacent vertices.
• The vertices which have been visited but not explored for adjacent
vertices can be stored in queue.
• In every iteration, a vertex is removed from the queue and its adjacent
vertices which are not visited as yet are added to the queue.
By Nitin Sir
11 | V A I S H N A V I A C A D E M Y
Algorithm
1. Initialize all the vertex.
2. Initialize array of visited [ ] to false (0)
3. Enqueue starting vertex in queue.
4. Mark it as visited [ ] to true (1)
5. Dequeue from queue.
6. Add to rear of queue all neighbors of deleted node that are not
visited.
7. If queue is not empty go to step5.
8. End.
#include<stdio.h>
#include<conio.h>
typedef struct Queue
{
Int data[max];
Int F,R;
}Queue;
Void enqueue(Queue *q,int x);
Void Init(Queue *q);
int dequeue(Queue *q);
int empty((Queue *q);
void BFS(int);
By Nitin Sir
12 | V A I S H N A V I A C A D E M Y
int G[10][10];
int visited[10],n; //n is no of vertices and graph is sorted in array
void main()
{
int i,j,v;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&G[i][j]);
}
}
//visited is initialized to zero
for(i=0;i<n;i++)
{
visited[i]=0;
}
Printf(“Enter the starting vertex\n”);
Scanf(“%d”,&v);
BFS(v);
getch();
}
Void BFS(int v)
{
Queue q;
Init(&q);
Enqueue(&q, v);
printf(“%d”, v);
visited [v]=1;
while(!empty(&q))
{
V=dequeue(&q);
For ( i=0 ; i<n ; i++)
By Nitin Sir
13 | V A I S H N A V I A C A D E M Y
{
If(visited[i]==0 && G[V][i]==1)
{
enqueue( &q, i);
visited[i]=1;
printf(“%d”,i);
}
}
}
}
Important questions
What is graph explain methods to represent graph [May, DEC-18 5M]
Define graph? List the types of graph with example? [3M DEC-18]
What is minimal spanning tree ? Draw MST using kruskal’s and prims
algorithm and find out cost. [10M DEC-18]
By Nitin Sir