0% found this document useful (0 votes)
11 views

Unit VI PPT

Uploaded by

s21570028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit VI PPT

Uploaded by

s21570028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Department of Electronics and Telecommunication Engineering

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

• A graph is an ordered pair G = (V, E)


– Where V is set of vertices or nodes
– E is collection of pairs of vertices from V, known as edges of a graph.

• For example, for the graph below.


– V = { 1, 2, 3, 4, 5, 6 }
– E = { (1, 4), (1, 6), (2, 6), (4, 5), (5, 6)
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).

– Directed edge is a unidirectional edge


Graph: Basic Concepts & Terminology

• Directed Acyclic Graph (DAG)


– A Directed Acyclic Graph (DAG) is a directed graph that contains no
cycles.
Graph: Basic Concepts & Terminology

• Weighted and Unweighted graph


– A weighted graph associates a value (weight) with every edge in the graph.
– An unweighted graph does not have any value (weight) associated with every
edge in the graph.
– Weighted edge is a edge with value (cost) on it..
Graph: Basic Concepts & Terminology

• Two vertices are called adjacent if they are endpoints of the same edge.

• The degree of a vertex in a graph is the total number of edges connected to


it.

• In-degree of a vertex in a graph is the total number of incoming edges


connected to a vertex.

• Out-degree of a vertex in a graph is the total number of edges outgoing


from the vertex.

• In a directed graph, the out-degree of a vertex is the total number of


outgoing edges, and the in-degree is the total number of incoming edges.
Representation of graphs
• Adjacency matrix
– An Adjacency Matrix is used to represent a graph.
– It is a 2D array of vertices with each row and column representing a vertex.
– The matrix consists of “0” or “1”.
• 0 depicts that there is no path
• 1 represents that there is a path.
Representation of graphs

• 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

void BFS(int v1)


{
int v2, front, rear, Q[10];
visited[v1] = 1;
front = rear = -1;
for (v2=1;v2<=n;v2++)
Q[++rear] = v1;
{
while (front != rear) if (arr[v1][v2]==1 && visited[v2]==0)
{ {
v1 = Q[++front]; Q[++rear]=v2;
printf("%d ", v1); visited[v2]=1;
}
}
}
}

You might also like