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

Graphs

The document discusses the fundamentals of graphs, including their definitions, types (undirected and directed), and applications in various fields. It explains key concepts such as edges, vertices, cycles, and graph traversal techniques like Depth First Search (DFS) and Breadth First Search (BFS), along with sample code for implementing these algorithms. Additionally, it touches on properties of graphs, including self-loops, multigraphs, and complete graphs.

Uploaded by

dhanashree
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Graphs

The document discusses the fundamentals of graphs, including their definitions, types (undirected and directed), and applications in various fields. It explains key concepts such as edges, vertices, cycles, and graph traversal techniques like Depth First Search (DFS) and Breadth First Search (BFS), along with sample code for implementing these algorithms. Additionally, it touches on properties of graphs, including self-loops, multigraphs, and complete graphs.

Uploaded by

dhanashree
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

GRAPHS

• people of Koenigsberg cannot walk across each


bridge exactly once and return to their starting
location. Euler solved the problem by using a graph
Applications of Graphs
Since this first application,
Graphs have been used in a wide variety of
applications, including analysis of electrical
circuits, finding shortest routes, project
planning, and the identification of chemical
compounds. Indeed graphs may be the most
widely used of all mathematical structures.
• A graph, G, consists of two sets:
• a finite, nonempty set of vertices, and a finite,
possibly empty set of edges.
• V(G) and E(G) represent the sets of vertices
and edges of G, respectively. Alternately, we
may write G = (V, E) to represent a graph.
• An undirected graph is one in which the pair of
vertices representing any edge is unordered. For
example, the pairs (vq, vi) and (vi, vq) represent the
same edge.

• A directed graph is one in which we represent each


edge as a directed pair of vertices. For example, the
pair <vo,v1> represents an edge in which v0 is the tail
and v1 is the head. Therefore,<v0,v1> and <v1,vo>
represent two different edges in a directed graph
The set representation of each of these graphs is:
E(G1), = ((0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}
E(G2), = {{0, 1), (0, 2), (1, 3), (I, 4), (2, 5), {2, 6)}
E(G3)= {<0,1> <1,0> <1,2> )
V(G1),= {0, 1,2,3)
V(G2),= {0, 1,2,3,4,5,6 }
V(G3} = {O, 1,2}
• An edge from a vertex, i, back to itself is called
a selfloop
• A graph may have multiple occurrences of the
same edge. Such a graph is called as a
multigraph
• A complete graph is a graph that has the
maximum number of edges.
• For an undirected graph with n vertices, the
maximum number of edges is the number of
distinct, unordered pairs, (vi, vj), i!= j. This
number is: n(n - l)/2
• For a directed graph on n vertices, the
maximum number of edges is: n(n - 1)
• If (V0, V1) is an edge in an undirected graph,
then the vertices v0 and v1 are adjacent and
the edge (v0, v1) is incident on vertices V0 and
v1.

• A subgraph of G is a graph G' such that V{G') is


a subset of V (G) and E(G’ ) is subset of E( G)
A cycle is a simple path in which the first and the last
vertices are the same. For example, 0, 1, 2, 0 is a cycle in
G1
Graph traversal
• The process of visiting each node
systematically in some order is called graph
traversal
• Two traversal techniques:
• Depth First Search
• Breadth First Search
DFS : Vo, V1,V3,V7, v4,v5,v2,v6

BFS : V0,V1,V2,3,V4,V5,V6,V7
Depth First Search
#include<stdio.h>
#include<stdlib.h>
int a[50][50], n, bvisited[50],dvisited[50];
int q[20], front = -1,rear = -1;
int s[20], top = -1, count=0;

void bfs(int v)
{
int i, cur;
bvisited[v] = 1;
q[++rear] = v;
while(front!=rear){
cur = q[++front];
for(i=1;i<=n;i++)
{
if( a[cur][i]==1 && bvisited[i]==0 )
{
q[++rear] = i;
bvisited[i] = 1;
printf("%d ", i);
}}}}
void dfs(int v)
{
int i;dvisited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& dvisited[i] == 0 )
{
printf("%d ", i);
dfs(i);
}}}
int main()
{
int ch, start, i,j;
printf("\nEnter the number of vertices in graph:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1; i<=n;i++)
{ for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
for(i=1;i<=n;i++)
{
bvisited[i]=0; dvisited[i]=0;}
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
while(1){
printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);break;
case 2: printf("\nNodes reachable from starting vertex %d are:",start);
dfs(start);break;case 3: exit(0);default: printf("\nPlease enter valid choice:");}}}
Find out if graph is connected

You might also like