Unit VI PPT
Unit VI PPT
Unit - 6
Graphs
By
Siddhasen R Patil
Course Outcomes
CO6:
• On completion of the course, learner will be able to
apply the knowledge of graph for solving the
problems of spanning tree and shortest path
algorithm.
Syllabus
• Graph: Basic Concepts & terminology.
• Representation of graphs:
– Adjacency matrix
– Adjacency list.
• Operations on graph:
– Traversing a graph.
• Spanning trees:
– Minimum Spanning tree- Kruskal’s Algorithm
– Prim’s Algorithm
– Dijkstra’s Shortest Path Algorithm
Graph: Basic Concepts & Terminology
• Undirected graph
– An undirected graph (graph) is a graph in which edges have no
orientation.
– The edge (1, 6) is identical to edge (6, 1), i.e., they are not ordered
pairs.
– Undirected edge is a bidirectional edge.
Graph: Basic Concepts & Terminology
• Directed graph
– A Directed graph (digraph) is a graph in which edges have orientations,
i.e., The edge (2, 0) is not identical to edge (0, 2).
• Two vertices are called adjacent if they are endpoints of the same edge.
• Adjacency list
– It represents a graph as an array (A) of linked lists.
– The vertices are stored as an index of the one-dimension array and edges are
stored as a list.
– It means that each element of the array Ai is a list. It contains all the vertices
adjacent to vertex i.
Operations on Graph
• Traversing a graph
– It is the process of visiting or updating each vertex in a
graph.
– The order in which they visit the vertices is used to classify
the traversals.
– There are two ways to implement a graph traversal:
• Breadth-First Search (BFS) –
• Depth-First Search (DFS):
Operations on Graph
• Traversing a graph
– It is the process of visiting or updating each vertex in a
graph.
– The order in which they visit the vertices is used to classify
the traversals.
– There are two ways to implement a graph traversal:
• Breadth-First Search (BFS)
• Depth-First Search (DFS)
Depth First Search
#include<stdio.h>
#include<conio.h>
void DFS (int v);
int n, arr[10][10];
int visited[10];
main()
{
int i,j,v;
clrscr();
printf("Enter total number of nodes in the graph:");
scanf("%d",&n);
printf("\nEnter adjacency matrix for the graph\n");
for (i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&arr[i][j]);
}
}
Depth First Search
printf("\nEnter the starting node for DFS:");
scanf("%d",&v);
for (i=0;i<n;i++)
visited[i]=0;
DFS(v);
getch();
return(0);
}
Depth First Search
void DFS(int v)
{
int v2, stack[10], top = -1, pop;
top++;
stack[top]=v;
while (top>=0) for(v2=n; v2>=1; v2--)
{ {
pop=stack[top--]; if (arr[pop][v2] == 1 && visited[v2] == 0)
if (visited[pop] == 0) {
{ stack[++top] = v2;
printf("%d",pop); }
visited[pop] = 1; }
} }
}
else
continue;
Breadth First Search
#include<stdio.h>
#include<conio.h>
void BFS (int v);
int n, arr[10][10];
int visited[10];
main()
{
int i,j,v;
clrscr();
printf("Enter total number of nodes in the graph:");
scanf("%d",&n);
printf("\nEnter adjacency matrix for the graph\n");
for (i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&arr[i][j]);
}
}
Depth First Search
printf("\nEnter the starting node for DFS:");
scanf("%d",&v);
for (i=0;i<n;i++)
visited[i]=0;
BFS(v);
getch();
return(0);
}
Breath First Search